SDL1 to SDL2 audio

I am having trouble converting this code to work on SDL2.

//focus on sdl

//wav loading

//fosdl5_2.cpp
//17MAY2002
//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>

//memory management functions
#include <memory.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;
SDL_AudioSpec* g_SpecWAV;

//type for streaming wav data to sound buffer
typedef struct
{
	//pointer to current sound's data
	Uint8* m_CurrentSound;
	//length of current sound
	int	m_CurrentSoundLength;
	//pointer to next sound's data
	Uint8* m_NextSound;
	//length of next sound
	int m_NextSoundLength;
} FOSDL_AudioStream;

//audio stream variable
FOSDL_AudioStream g_AudioStream;

//wave data
Uint8* g_WAVBuffer;
Uint32 g_WAVLength;

//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
	g_pDisplaySurface = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,0,SDL_ANYFORMAT);

	//error check
	if (g_pDisplaySurface == 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;
	g_SpecWAV=new SDL_AudioSpec;

	//set up the audio stream to point to nothing
	g_AudioStream.m_CurrentSound=NULL;
	g_AudioStream.m_CurrentSoundLength=0;
	g_AudioStream.m_NextSound=NULL;
	g_AudioStream.m_NextSoundLength=0;

	//fill in desired audio spec
	g_SpecDesired->freq=11025;
	g_SpecDesired->format=AUDIO_U8;
	g_SpecDesired->channels=1;
	g_SpecDesired->samples=8192;
	g_SpecDesired->callback=FOSDLAudioCallback;
	g_SpecDesired->userdata=(void*)&g_AudioStream;
	
	//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;

	//load wav file
	g_SpecWAV=SDL_LoadWAV("song.wav",g_SpecWAV,&g_WAVBuffer,&g_WAVLength);

	//lock the audio buffer
	SDL_LockAudio();

	//set current sound to WAV data
	g_AudioStream.m_CurrentSound=g_WAVBuffer;
	g_AudioStream.m_CurrentSoundLength=g_WAVLength;

	//unlock the audio buffer
	SDL_UnlockAudio();

	//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)
		{
			//lock audio
			SDL_LockAudio();
			//check if next sound is NULL
			if(g_AudioStream.m_NextSound==NULL)
			{
				//set next sound
				g_AudioStream.m_NextSound=g_WAVBuffer;
				g_AudioStream.m_NextSoundLength=g_WAVLength;
			}
			//unlock audio
			SDL_UnlockAudio();
			//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;

	//free the wave spec
	delete g_SpecWAV;

	//free wav data
	SDL_FreeWAV(g_WAVBuffer);

	//return 0
	return(0);
}

//audio callback
void FOSDLAudioCallback(void* userdata,Uint8* buffer,int len)
{
	//cast user data to stream data
	FOSDL_AudioStream* pstrm;
	pstrm=(FOSDL_AudioStream*)userdata;

	//continue while len > 0 and at least one sound is non-empty
	while(len>0 && (pstrm->m_CurrentSoundLength>0 || pstrm->m_NextSoundLength>0))
	{
		//check for current sound being NULL
		if(pstrm->m_CurrentSoundLength==0)
		{
			//copy next sound to current sound
			pstrm->m_CurrentSound=pstrm->m_NextSound;
			pstrm->m_CurrentSoundLength=pstrm->m_NextSoundLength;
			//clear next sound
			pstrm->m_NextSound=NULL;
			pstrm->m_NextSoundLength=0;
		}
		//while len>0 and length of current sound>0, stream to buffer
		while(len>0 && pstrm->m_CurrentSoundLength>0)
		{
			//stream a byte to audio buffer
			*(buffer++)=*(pstrm->m_CurrentSound++);
			//decrease lengths
			len--;
			pstrm->m_CurrentSoundLength--;
		}
	}
}

bump because i want to move on to the next section in my book

please, do not bump posts here.

Your post was probably ignored because shows no sign of dedication. Posts like “do my job” are often ignored.

To start you should:

  1. Post your own code (no matter how bad it is), but it should shows that you have tried to do.

  2. Display the error messages you’re receiving.

  3. Explain what you have tried.

  4. Describe your environment OS, Compiler, SDL version, how do you install it.

  5. Try to reduce your code to a minimum example that shows exactly your problem, posting 3000 lines of code with a tag FIX IT FOR ME will get no attention.

Hope it helps.

Some indication of the problem would be helpful. The 1.2 audio API is (mostly) intact in SDL2 (the biggest change is that the callback doesn’t initialize the stream buffer for you, since generally you are going to completely fill it anyhow…but if for some reason you aren’t going to, you need to memset it to silence or you’ll get audio artifacts or static noise or repeating sounds, etc).

Yay problem solved. Thanks

1 Like