0

I'm trying to apply a custom volume level for an audio file. My following code runs successfully, but the final .m4a audio does not play.

#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;

int main(int argc, char* argv[]) {
    if (argc != 4) {
        cerr << "Usage: " << argv[0] << " <input file> <volume level> <output file>" << endl;
        return 1;
    }

    // Parse arguments
    string input_path = argv[1];
    float volume_level = stof(argv[2]);
    string output_path = argv[3];
    cout << "Input file: " << input_path << endl;
    cout << "Volume level: " << volume_level << endl;
    cout << "Output file: " << output_path << endl;

    // Open input file
    FILE* input_file = fopen(input_path.c_str(), "rb");
    if (input_file == NULL) {
        cerr << "Could not open input file" << endl;
        return 1;
    }

    // Open output file
    FILE* output_file = fopen(output_path.c_str(), "wb");
    if (output_file == NULL) {
        cerr << "Could not open output file" << endl;
        return 1;
    }

    // Read input file and write to output file with volume level
    const int BUFFER_SIZE = 4096;
    char buffer[BUFFER_SIZE];
    while (true) {
        // Read from input file
        size_t bytes_read = fread(buffer, 1, BUFFER_SIZE, input_file);
        if (bytes_read == 0) {
            break;
        }

        // Apply volume level
        for (int i = 0; i < bytes_read; i++) {
            buffer[i] = (char) (buffer[i] * volume_level);
        }

        // Write to output file
        fwrite(buffer, 1, bytes_read, output_file);
    }

    // Close files
    fclose(input_file);
    fclose(output_file);

    cout << "Done" << endl;
    return 0;
}
// to compile this use : g++ -o transcode transcode.cpp
Rojo
  • 2,749
  • 1
  • 13
  • 34
  • Is your computer giving you some sort of error when you try to play the file? Or can you just not hear it? – Rojo Apr 07 '23 at 22:31
  • 3
    Audio files generally have a header at the beginning. You need to keep that intact. You also need to parse that header to know the format of the file such as the size of each sample. If the file is compressed, like m4a would be, then you need to decompress the data, modify it, then compress it again before writing. There's a good reason to use a library where someone else has already figured all these things out for you. – Retired Ninja Apr 07 '23 at 22:31
  • no, it's not even playing like the file has a size but when I open it won't start! see the video below. https://www.loom.com/share/1a9eb1b8b55349f6a96e52a2a849de5c?focus_title=1&muted=1&from_recorder=1 – Mohamed HAMDI Apr 07 '23 at 22:39
  • @RetiredNinja thanks for the feedback. i m new to this stuff can you provide some docs or something to solve it? – Mohamed HAMDI Apr 07 '23 at 22:40
  • 1
    100% what @RetiredNinja said. Try to find a tool that can do that for you. Or at least a library if you still want to write the code. Simply multiplying a value is not a great procedure either. This will still not give you equally loud files. You probably want to google for the term *normalization*. This will get a statistics of the whole file first and then figure out the multiplier needed. – Thomas Weller Apr 07 '23 at 22:42
  • You could also use an existing metadata solution such as ReplayGain or SoundCheck. – Aaron Liu Apr 07 '23 at 22:57

0 Answers0