Hello,
I saw such kind of question on the net but mainly when using SDL_mixer and in fact I didn’t really found answer ever.
In my case, I do not use it and when I play a sound, I have almost 1 second of delay.
My code, running on Linux with SDL2.0.9 is:
void audioCallback(void *userdata, Uint8 *stream, int len) {
pthread_mutex_lock (&condvar_audio_lock);
if (audioLen == -1) {
audioLen = 0;
pthread_cond_signal (&cond_var_audio);
}
if (audioLen ==0) {
SDL_PauseAudioDevice(deviceId, 1);
pthread_mutex_unlock (&condvar_audio_lock);
return;
}
len = ( len > audioLen ? audioLen : len );
SDL_memcpy (stream, audioPos, len); // simply copy from one buffer into the other
audioPos += len;
audioLen -= len;
pthread_mutex_unlock (&condvar_audio_lock);
}
void WID_playAudio(WID_sound_t *sound) {
if (canPlayAudio && (sound->buffer != NULL)) {
pthread_mutex_lock (&condvar_audio_lock);
if (audioLen > 0) {
audioLen = -1;
while (audioLen == -1) {
pthread_cond_wait(&cond_var_audio, &condvar_audio_lock);
}
}
audioPos = sound->buffer; // copy sound buffer
audioLen = sound->bufferLen; // copy file length
pthread_mutex_unlock (&condvar_audio_lock);
SDL_PauseAudioDevice(deviceId, 0);
}
}
void main(void) {
SDL_memset(&audioSpec, 0, sizeof(audioSpec));
audioSpec.freq = 22050;
audioSpec.channels = 1;
audioSpec.format = AUDIO_S16;
audioSpec.callback = audioCallback;
if ((deviceId = SDL_OpenAudioDevice(NULL, 0, &audioSpec , NULL, 0)) == 0) {
canPlayAudio = true;
}
WID_startupSound = WID_calloc(1, sizeof(WID_sound_t));
// Fill up the sound sample
if (WID_setSoundResources(WID_startupSound, "sound_file/startup", "sounds/sound_resources.json") == WID_OK) {
WID_playAudio(WID_startupSound);
}
}
Any idea?
That would be a pity to use system(‘aplay mysound.wav’) which works great !
Kind regards,
Alain