0
votes

I wrote a library to read unsigned 8 bit pcm audio from a old game and I stuffed the raw data into a Mix_Chunk using this code:

Mix_Chunk sfx;
sfx.allocated = 1;
sfx.abuf = (Uint8*)data;
sfx.alen = length;
sfx.volume = 32;

I know the samplerate is 10989HZ but when I set the sample rate using Mix_OpenAudio(10989, AUDIO_U8, 2, 2048 it plays the sound way too fast, am I setting the sample rate wrong or is it just SDL2 not liking PCM?

if I dump it into a file and open it with Audacity it plays just fine

1
So, what is your question?akk0rd87
@akk0rd87 sorry fixed the postuser12586940
Wild guess: you have mono data, but declare two channels. This would typically cause the data to be interpreted as interleaved channels, and thus played twice as fast.Quentin
@Quentin declaring only one channel doesn't fix the problemuser12586940
If you call Mix_QuerySpec does it say the correct frequently?user253751

1 Answers

1
votes

fixed the problem, before you call Mix_OpenAudio run this:

SDL_AudioSpec wavSpec;
SDL_memset(&wavSpec, 0, sizeof(wavSpec)); /* or SDL_zero(want) */

wavSpec.callback = audioCallback;
wavSpec.userdata = nullptr;
wavSpec.format = AUDIO_S16;
wavSpec.channels = 2;
wavSpec.samples = 2048;

if (SDL_OpenAudio(&wavSpec, NULL) < 0)
{
    fprintf(stderr, "Could not open audio: %s\n", SDL_GetError());
}

pcm audio plays correctly now