SDL Audio sent only 466 samples on callback

Hello,

I made same tests, I see, SDL sent only 466 samples on callback though I request 1024 or more in SDL_AudioSpec.

I also tested other audio libraries and they all send 1024 and even more. Why this limitation on SDL?

Are you running Windows, by any chance? If so, then I think the problem is that SDL2 is defaulting to WASAPI for audio (which is unfortunate, because it seems to have some issues with SDL2). If I recall correctly, the buffer size you give the audio device on open is not respected when WASAPI is used.
In all my SDL2 programs, I disable WASAPI by selecting directsound (or winmm if directsound is not available). Not only do you get the correct audio buffer sizes, but it also fixes random buffering issues on some machines (clicks/pops in sound).

Here’s some C code to show how it’s done, I call disableWasapi() before SDL_Init(): https://pastebin.com/1UKkmGWM

Me too, but since I’m not concerned about situations in which directsound is not available (I don’t believe I’ve yet encountered any!) I simply set the environment variable without any fallback:

#ifdef __WINDOWS__
SDL_setenv ("SDL_AUDIODRIVER", "directsound", 1) ;
#endif
1 Like

Thanks, directsound provides good results. Winmm cuts the wave, about 600 samples are null with this code:

typedef float MY_TYPE;
#define FORMAT AUDIO_F32SYS
const int BITS_PER_SAMPLE = 4;
MY_TYPE SCALE = 0.1f;

float add_saw=6.f/1024;
float sawL=0, sawR=1;

void audio_callback(void* /*user_data*/, Uint8 *raw_buffer, int bytes) {
	MY_TYPE *buffer = (MY_TYPE *)raw_buffer;
  int length = bytes / BITS_PER_SAMPLE; 

  for(int i = 0; i < length; i++) {
  
    sawL+=add_saw;
    sawR+=add_saw*2;
    if (sawL>1) sawL=-1;
    if (sawR>1) sawR=-1;
    buffer[i]=(MY_TYPE) SCALE * sawL;
    buffer[i+1]=(MY_TYPE) SCALE * sawR;

  i++;
  }
}