.ogg file doesn't play back correctly/how do you play .ogg files?

I’m having an issue playing .ogg files. When I play one there is weird noise included.
Any idea?
Here is a full reproduction. Everything else it needs to run is stb/stb_vorbis.c at master · nothings/stb · GitHub (you can simply copy-paste this) and any .ogg file.

#define SDL_MAIN_USE_CALLBACKS 1  
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#include "stb_vorbis.c"

static SDL_Window *window = NULL;
static SDL_Renderer *renderer = NULL;
static SDL_AudioStream *stream = NULL;

stb_vorbis *decoder;
stb_vorbis_info info;

static void SDLCALL FeedTheAudioStreamMore(void *userdata, SDL_AudioStream *astream, int additional_amount, int total_amount)
{
    additional_amount /= sizeof (float);
    while (additional_amount > 0) {
        float samples[8096];
        const int total = SDL_min(additional_amount, SDL_arraysize(samples));
        stb_vorbis_get_samples_float_interleaved(
            decoder,
            info.channels,
            samples,
            total
        );
        SDL_PutAudioStreamData(astream, samples, total * sizeof (float));
        additional_amount -= total;
    }
}

SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
{
    // load the entire .ogg file into memory
    FILE* fp = fopen("any_ogg_file.ogg", "r");
    fseek(fp, 0, SEEK_END);
    int size = ftell(fp);
    fseek(fp, 0, SEEK_SET);
    void* data = malloc(size);
    fread(data, 1, size, fp);

    int err;
    decoder = stb_vorbis_open_memory(data, size, &err, NULL);
    info = stb_vorbis_get_info(decoder);

    if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)) {
        SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
        return SDL_APP_FAILURE;
    }
    if (!SDL_CreateWindowAndRenderer("examples/audio/simple-playback-callback", 640, 480, 0, &window, &renderer)) {
        SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
        return SDL_APP_FAILURE;
    }

    SDL_AudioSpec spec;
    spec.channels = info.channels;
    spec.format = SDL_AUDIO_F32;
    spec.freq = info.sample_rate;
    stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec, FeedTheAudioStreamMore, NULL);
    if (!stream) {
        SDL_Log("Couldn't create audio stream: %s", SDL_GetError());
        return SDL_APP_FAILURE;
    }

    SDL_ResumeAudioStreamDevice(stream);

    return SDL_APP_CONTINUE;  
}

SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
{
    if (event->type == SDL_EVENT_QUIT) {
        return SDL_APP_SUCCESS;  
    }
    return SDL_APP_CONTINUE;  
}

SDL_AppResult SDL_AppIterate(void *appstate)
{
    SDL_RenderClear(renderer);
    SDL_RenderPresent(renderer);
    return SDL_APP_CONTINUE;  
}

void SDL_AppQuit(void *appstate, SDL_AppResult result)
{
}

Basically the question is: how do I play .ogg files correctly?

There seems to be an issue with mismatching units when calculating total.

const int total = SDL_min(additional_amount, SDL_arraysize(samples));

SDL_arraysize(samples) returns the number of bytes while additional_amount is number of samples (not in bytes).

Are you sure?
If I do

         printf("%d\n", SDL_arraysize(samples));

it prints 8096, which matches the amount of floats (samples).
If it was bytes wouldn’t it be 8096 * 4?

Either way changing it to

         const int total = SDL_min(additional_amount , SDL_arraysize(samples) / sizeof(float));

Does not resolve the issue if that was what you were suggesting.

Would be nice if someone could tell me if they’re also not hearing .ogg files correctly with this code

I’m sorry. I was confused. Please, ignore me.

I’m not sure but maybe you want to check the return value of stb_vorbis_get_samples_float_interleaved to see how many samples you actually got to avoid passing uninitialized samples to SDL_PutAudioStreamData.

I didn’t realize it was C code and changed line 37 to use an unsigned char*, which was the only line needed to change for C++. My headphones aren’t the greatest quality but if there’s a “weird noise” I don’t hear it. I tried it on linux, the OS known for sound working :rofl:

I see that the latest release of SDL3 (3.2.12) there was a crackling sound that occurred on Android devices that they fixed. “Fixed Android audio crackling introduced in 3.2.10”
What version of SDL3 and what Operating System are you using?
Is the sound only at launch and end of the music/file, or is it occurring throughout the recording?

Does updating to the latest release of SDL3 fix your issue?