This is a bit of a longshot, but I have been wrestling with this problem
for days now, so want a quick sanity check.
If I call SDL_LoadWAV(), then SDL_OpenAudio() and then SDL_PauseAudio(0),
my WAV file plays perfectly. However, if I call SDL_LoadWAV() after
SDL_OpenAudio(), then garbage gets played back (and very occasionally I
hear snippets of the expected tune beneath the garbage).
Is there a requirement that SDL_LoadWAV() is called before/after
SDL_OpenAudio()? I would have thought that as long as there is valid data
for my SDL callback function before SDL_PauseAudio(0) is called, it
shouldn’t matter. Am I mistaken?
Now, here is the weird part. In order to rule out SDL_LoadWAV()
corrupting the data stream, I changed my callback function to just set
silence (regardless of what was loaded from the file). In the case where
it was working previously, it now just plays silence. Good! However, it
still plays garbage if SDL_LoadWAV() is after SDL_OpenAudio(). So, I can
only presume that something in SDL_LoadWAV() is stomping on memory
somewhere else.
A possible candidate is my implementation of malloc, but it is virtually
the standard malloc that comes with newlib (libc replacement).
Anyway, I just wanted to see if there was anything specifically weird/dumb
that I am doing. Any help or advice would really be appreciated!! Thanks
a lot.
typedef struct
{
unsigned char *data;
unsigned int currentPos;
unsigned int wavLength;
} WaveData;
// a global to hang on to the WAV data
WaveData waveData;
void mixaudio(void *userdata, Uint8 *stream, int len)
{
if (waveData.data != NULL && waveData.currentPos < waveData.wavLength)
{
memcpy(stream, waveData.data, len);
// memset(stream, 0, len); // set silence
waveData.data += len;
waveData.currentPos += len;
}
}
void XBoxStartup()
{
SDL_AudioSpec *desired, *obtained;
SDL_AudioCVT wav_cvt;
desired = malloc(sizeof(SDL_AudioSpec));
obtained = malloc(sizeof(SDL_AudioSpec));
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
waveData.data = NULL;
/* If I move these two lines, things start going haywire */
SDL_LoadWAV("44_16_Stereo.wav", desired, &waveData.data,
&waveData.wavLength);
waveData.currentPos = 0;
desired->freq = 44100;
desired->format = AUDIO_S16;
desired->channels = 2;
desired->samples = 1024;
desired->callback = mixaudio;
desired->userdata = NULL;
/* Open the audio device */
SDL_OpenAudio(desired, NULL;
/* this is where I move the SDL_LoadWAV() to */
free(desired);
SDL_PauseAudio(0);
XSleep(10000);
XReboot();
}–
Craig Edwards