SDL_QueueAudio not working

#define MUS_PATH "../MAZIOB/Pliki/11.wav"

int main(int argc, char* argv[]){

	// Initialize SDL.
	if (SDL_Init(SDL_INIT_AUDIO) < 0)
			return 1;

	// local variables
	static Uint32 wav_length; // length of our sample
	static Uint8 *wav_buffer; // buffer containing our audio file
	static SDL_AudioSpec wav_spec; // the specs of our piece of music
	SDL_AudioDeviceID dev;
	
	/* Load the WAV */
	// the specs, length and buffer of our wav are filled
	if( SDL_LoadWAV(MUS_PATH, &wav_spec, &wav_buffer, &wav_length) == NULL ){
	  return 1;
	}
wav_spec.callback = NULL;
	wav_spec.userdata = NULL;
	
	/* Open the audio device */
	dev = SDL_OpenAudioDevice(NULL, 0, &wav_spec, NULL, SDL_AUDIO_ALLOW_FORMAT_CHANGE);
	/* Start playing */
Print SDL_QueueAudio(dev, wav_buffer, wav_length) NL
	SDL_PauseAudio(0);

	Pause(1000)
	
	// shut everything down
	SDL_CloseAudio();
	SDL_FreeWAV(wav_buffer);

}

I hear nothing…
It works using callback

Would it be possible instead of mixing the sounds, e.g. triggering the sounds to play in separate threads, and the mixing would be done by the operating system?

Try replacing

SDL_PauseAudio(0);

with

SDL_PauseAudioDevice(dev, 0);

Now it works in Linux, still not in Windows.

You need to open the audio device before you load the WAV.

Still silent in Windows… There are many other examples of sound not working in Windows…

I use very similar code, and it works in Windows for me. The main difference is that I don’t pass the SDL_AUDIO_ALLOW_FORMAT_CHANGE flag, instead I convert the audio to AUDIO_S16LSB format using SDL_ConvertAudio().

Hello,

Can you please try to add the following lines:

(in the includes list)

#ifdef _WIN32
#include <windows.h>
#include <codecvt>
#endif

And before to init SDL_Audio:

#ifdef _WIN32
CoInitialize(NULL);
#endif

And tell us if it works or not ?

Réference : #6891 (FFplay: WASAPI can't initialize audio client (zeranoe's FFmpeg 3.4 MS Windows binaries)) – FFmpeg

tstsound.c:15:19: fatal error: codecvt: No such file or directory
#include

Sorry, maybe I wrongly added an useless line … what does the compiler say if you remove

#include <codecvt>

I fixed such issue long time ago, and when it works on Windows, change nothing is the rule :slight_smile:

Last but not least, I’m cross-compiling all my Windows applications on Linux, more easy to maintain.

C:\Users\User\AppData\Local\Temp\ccCDfTJH.o:tstsound.c:(.text+0xde51): undefined reference to `CoInitialize@4’

Try adding this, before SDL_Init():

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

Still nothing…

In this example, it’s the other way around…:

works after changing to SDL_OpenAudio