c++audiomp3

playing mp3 downloaded via curllib gets cut short


I am trying to make a c/c++ program that downloads an mp3 file from a server, then plays the audio over a speaker. The problem is that the audio is cut short when I try to play it. The amount of time it cuts of is different every audio, but is is consistant in that the same file gets cut off at the same time every time, even on different downloads. I am doing this using curl to download the file and currently I am trying with sdl2_mixer to play it. I am working on a raspi 4 and a speaker connected via the jack. i am connnected via vscode remote ssh to my pc, Where i can see the file and when i play it from vscode it is the complete audio, but when i try to download it from pi to pc it does not show up in my files. i am fairly new to all this so excuse me if anything is wrong or missing.

I have tried playing the files using system(mpg123...) and 1 or 2 other things, but they all cut short when playing. i now have SDL2_mixer doing it, but same results. when i go to the server with my pc via google, i get a proper mp3 so i think its not that i get the wrong data from the server in any way.

dt (datetime) is for logging and naming purpose.

 // temporary file to store the response
    string rf = program_id + "-" + dt + ".mp3";
    FILE *rfptr = fopen(rf.c_str(), "wb");


    // setup curl
    curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, rfptr);

    // perform request
    res = curl_easy_perform(curl);
    if (res != CURLE_OK)
    {
        cerr << "Error: " << dt << " Curl failed" << endl;
        return -1;
    }

    // play mp3 file using sdl2-mixer
    if (PlaySound(rf, dt))
    {
        cerr << "Error: " << dt << " Could not play mp3 file" << endl;
        return -1;
    }

    // cleanup
    curl_easy_cleanup(curl);
    // remove(rf.c_str());
    fclose(rfptr);

int PlaySound(string rf, string dt)
{
    if (SDL_Init(SDL_INIT_AUDIO) < 0)
    {
        cerr << "Error: " << dt << " Could not initialize SDL" << endl;
        return -1;
    }
    if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0)
    {
        cerr << "Error: " << dt << " Could not open audio" << endl;
        return -1;
    }
    Mix_Music *music = Mix_LoadMUS(rf.c_str());
    if (music == NULL)
    {
        cerr << "Error: " << dt << " Could not load music" << endl;
        return -1;
    }
    Mix_PlayMusic(music, 1);
    while (Mix_PlayingMusic())
    {
        SDL_Delay(100);
    }
    Mix_FreeMusic(music);
    Mix_CloseAudio();
    SDL_Quit();
    return 0;

Solution

  • You need to flush the open file stream (or close it) because the last bit is hanging around in a memory buffer. One of the following should suffice:

    fflush(rfptr);
    

    or

    fclose(rfptr);