简体   繁体   中英

SDL2 pcm audio playing too fast

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM