Sdl2 audio silent

//focus on sdl

//random noise

//fosdl5_1.cpp
//16MAY2002
//ernest pazera

//requires static linkage to:  
//sdl.lib, sdlmain.lib

//requires dynamic linkage to: 
//sdl.dll

//include SDL stuff
#include "sdl.h"

//include ability to exit program
#include <stdlib.h>
#include <stdio.h>
//screen dimensions
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

//display surface
SDL_Surface* g_pDisplaySurface = NULL;

//event structure
SDL_Event g_Event;

//audio specs
SDL_AudioSpec* g_SpecDesired;
SDL_AudioSpec* g_SpecObtained;

//prototype for audio callback function
void FOSDLAudioCallback(void* userdata, Uint8* buffer, int len);

//main function
int main(int argc, char* argv[])
{
	//initialize SDL
	if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) == -1)
	{
		//error initializing SDL

		//report the error
		fprintf(stderr, "Could not initialize SDL!\n");

		//end the program
		exit(1);
	}
	else
	{
		//SDL initialized

		//report success
		fprintf(stdout, "SDL initialized properly!\n");

		//set up to uninitialize SDL at exit
		atexit(SDL_Quit);
	}

	//create windowed environment
	SDL_Window* screen = SDL_CreateWindow("test", 0, 0, 480, 640, NULL);

	//error check
	if (screen == NULL)
	{
		//report error
		fprintf(stderr, "Could not set up display surface!\n");

		//exit the program
		exit(1);
	}

	//allocate desired and obtained audio specs
	g_SpecDesired = new SDL_AudioSpec;
	g_SpecObtained = new SDL_AudioSpec;

	//fill in desired audio spec
	g_SpecDesired->freq = 22050;
	g_SpecDesired->format = AUDIO_S16LSB;
	g_SpecDesired->channels = 1;
	g_SpecDesired->samples = 8192;
	g_SpecDesired->callback = FOSDLAudioCallback;
	g_SpecDesired->userdata = NULL;

	//open the audio device
	if (SDL_OpenAudio(g_SpecDesired, g_SpecObtained) < 0)
	{
		//error
		fprintf(stderr, "Could not open audio device!\n");

		delete g_SpecDesired;
		delete g_SpecObtained;

		//exit the program
		exit(1);
	}

	//free the desired spec
	delete g_SpecDesired;

	//turn off the paused state of the sound buffer
	SDL_PauseAudio(0);

	//repeat forever
	for (;;)
	{
		//wait for an event
		if (SDL_PollEvent(&g_Event) == 0)
		{
			//update the screen
			//SDL_UpdateRect(g_pDisplaySurface, 0, 0, 0, 0);
		}
		else
		{
			//event occurred, check for quit
			if (g_Event.type == SDL_QUIT) break;
		}
	}

	//normal termination
	fprintf(stdout, "Terminating normally.\n");

	//close audio
	SDL_CloseAudio();

	//free the obtained spec
	delete g_SpecObtained;

	//return 0
	return(0);
}

//audio callback
void FOSDLAudioCallback(void* userdata, Uint8* buffer, int len)
{
	//loop variable
	int index;
	for (index = 0; index < len; index++)
	{
		//fill buffer with random bytes
		buffer[index] = rand() % 256;
	}
}

unable to play sound plz help

as far as I can tell, since you’re not specifying a audio device, you may be playing to wrong place. I’m not well aroused in SDL1, in SDL2 you can specify which audio device you’re handling.

Here your code runs fine. I’m rewriting it to SDL2 (also handling multiple audio devices). I will send a copy soon.

noise.c (2.7 KB)

Compile with:

gcc -o noise noise.c $(pkg-config --cflags sdl2) $(pkg-config --libs sdl2) -lm

Thank you for your response. I tried compiling with SDL2 and get no sound still but i’m using visual studio 2019. For visual studio 2019 I added x86 lib and include directories into visual studio 2019 properties (I followed lazy foo to setup visual studio).Do I need to update my sound drivers? https://imgur.com/a/mvbvn7c <-- is what the output looks like

your volume seems pretty low. Also I know nothing about SDL2 on windows.

Weird, because the code attached should print the buffer length to terminal. If you can’t see this seems you even haven’t started the AudioCallback.

The file I downloaded didn’t have output now the new one I downloaded has output: https://imgur.com/a/85vmqpQ Am I supposed to adjust something?

The solution was change
audiodev = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(audio_dev_id, 0), 0, &spec_desired, &spec_received, SDL_AUDIO_ALLOW_FORMAT_CHANGE);

to

audiodev = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(audio_dev_id, 0), 0, &spec_desired, &spec_received, 0);

Thank you for your help

Another solution and able to keep SDL_AUDIO_ALLOW_FORMAT_CHANGE is to change format to AUDIO_F32 and change the buffer as follows:
void FOSDLAudioCallback(void* userdata, Uint8* buffer, int len)
{
fprintf(stdout, “%d\n”, len);
fflush(stdout);
for (size_t index = 0; index < len/sizeof(float); index++)
{
((float*)buffer)[index] = (rand() % 256)/256.f;

}

}

Glad you had working. Yes, AUDIO_F32 is more common, I had kept since you specified that in first hand.

SDL_AUDIO_ALLOW_FORMAT_CHANGE isn’t the issue for sure, since if it changes the if condition will warn you.

Also if possible, tag your code as “CODE” using the editor. It makes things easier to read.

But if SDL_AUDIO_ALLOW_FORMAT_CHANGE is not specified SDL2 should convert the format automatically. Why was that not working in this case?

Don’t know exactly. I don’t have enough experience in SDL_Audio, and I would like to know too.