SDL_LoadWAV weirdness on XBOX

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

Well, I replaced the call to SDL_LoadWAV() with code that manually
malloc’s a buffer and reads the WAV data and it works perfectly. So, I
think (hope!) I have ruled out any malloc issues.

So, to recap, the following call to SDL_LoadWAV() doesn’t work:

SDL_LoadWAV(“44_16_Stereo.wav”, desired, &waveData.data,
&waveData.wavLength);

but the following code that manually seeks to offset 0x3A (the start of
WAV data) does work:

// I dunno how long it is, so I will make I have enough room for 10
seconds
waveData.wavLength = 10 * 44100 * 4;
waveData.data = (char *)malloc(waveData.wavLength);
readFromFile(“44_16_Stereo.wav”, waveData.data, waveData.wavLength);

Any thoughts would be much appreciated. Thanks a lot.On Wed, 17 Nov 2004 15:17:21 +1100, Craig Edwards <@Craig_Edwards> wrote:

A possible candidate is my implementation of malloc, but it is virtually
the standard malloc that comes with newlib (libc replacement).


Craig Edwards

Hello !

Is there example code + wav online,
that you have problems with ?

CU

The source code that exhibits the problem is in my original post but I
have reposted below. However, there are a few small XBOX features that
will need to be removed before compiling on Win32 or Linux (eg. the entry
point being XBoxStarup() and the XReboot()/XSleep() statements). The wave
file is located at http://www.openxdk.org/44_16_Stereo.wav (2.8MB)

You make a good point though… in the meantime, I might try to compile
and run it on my Win32 pc and see what happens.

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <hal/xbox.h>
#include <openxdk/debug.h>
#include <SDL.h>
#include <hal/audio.h>
#include <hal/fileio.h>

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;

/* this is where I have to move the SDL_LoadWAV() to make it work */

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;

/* If I move these two lines, things start going haywire */
SDL_LoadWAV("44_16_Stereo.wav", desired, &waveData.data,  

&waveData.wavLength);
waveData.currentPos = 0;

free(desired);

SDL_PauseAudio(0);

XSleep(10000);
XReboot();

}On Wed, 17 Nov 2004 13:17:41 +0100 (CET), Torsten Giebl wrote:

Is there example code + wav online,
that you have problems with ?


Craig Edwards

Craig Edwards wrote:

/* Open the audio device */
SDL_OpenAudio(desired, NULL;

/* If I move these two lines, things start going haywire */
SDL_LoadWAV("44_16_Stereo.wav", desired, &waveData.data,  

&waveData.wavLength);

you missed parenthsis on the SDL_OpenAudio function call.
nowonder then next function doesn’t work…
heck, does it even compile?

In an attempt to limit the post length, I removed the error checking 'if’
statements. In doing that, I have obviously snipped one too many
parenthesis. Sorry for the confusion.On Wed, 17 Nov 2004 19:54:49 -0500, Jonathan Atkins wrote:

Craig Edwards wrote:

/* Open the audio device */
SDL_OpenAudio(desired, NULL;
 /* If I move these two lines, things start going haywire */
SDL_LoadWAV("44_16_Stereo.wav", desired, &waveData.data,   

&waveData.wavLength);

you missed parenthsis on the SDL_OpenAudio function call.
nowonder then next function doesn’t work…
heck, does it even compile?


Craig Edwards

Honestly… I shouldn’t be allowed near a computer. It turns out that I
had a bug in my SDL XBOX audio driver that was not doing conversions
correctly, which is why I was getting spurious output. My apologies to
everyone for wasting your time!–
Craig Edwards