sp193
Developer
Code:struct audsrv_adpcm_t sfx[6]; u8* snd_buffer[6]; int snd_size[6]; char snd_path[6][256]; char device_path[128];
If you declare these outside of any function, they will be considered globals. This would cause the path array to be placed on the stack instead, so that it'll take up memory, only during the runtime of sfxInit():
Code:
int sfxInit(void)
{
char snd_path[6][256];
FILE* adpcm;
int ret, i;
You are trying to assign the (uninitialized) value of the elements of the sfx_size[] array to sfx.size. At that point, snd_buffer[]'s elements would also be uninitialized (until you use malloc).
Code:
//sfx0
sfx[0].buffer = snd_buffer[0];
sfx[0].size = snd_size[0];
sprintf(snd_path[0], "%s/scrollV.adp", device_path);
There's also no need to have 6 different paths, if you divide and conquer:
Code:
#define NUM_SFX_FILES 7
static const char *sfx_files[NUM_SFX_FILES] = {
"scrollV.adp",
"scrollH.adp",
"page.adp",
"cancel.adp",
"transition.adp",
"alert.adp",
"confirm.adp",
};
static audsrv_adpcm_t sfx[NUM_SFX_FILES];
//Returns 0 (AUDSRV_ERR_NOERROR) if the sound was loaded successfully.
static int sfxLoad(const char *snd_path, audsrv_adpcm_t *sfx)
{
FILE* adpcm;
void *buffer;
int ret, size;
adpcm = fopen(snd_path, "rb");
if (adpcm == NULL)
{
printf("failed to open adpcm file %s\n", snd_path);
return -ENOENT;
}
fseek(adpcm, 0, SEEK_END);
size = ftell(adpcm);
rewind(adpcm);
buffer = malloc(size);
if (buffer == NULL)
{
printf("Failed to allocate memory for SFX\n");
return -ENOMEM;
}
fread(buffer, 1, size, adpcm);
fclose(adpcm);
ret = audsrv_load_adpcm(sfx, buffer, size);
free(buffer);
return ret;
}
static void getAudioDevice(char *device_path)
{
if ((gAudioPath == 0))
sprintf(device_path, "mc0:Sound/");
if ((gAudioPath == 1))
sprintf(device_path, "pfs0:Sound/");
if ((gAudioPath == 2))
sprintf(device_path, "mass0:Sound/");
}
//Returns number of audio files successfully loaded, < 0 if an unrecoverable error occurred.
int sfxInit(void)
{
char snd_path[256];
char device_path[128];
int ret, i, loaded;
ret = audsrv_init();
if (ret != 0)
{
printf("sfx: failed to initialize audsrv\n");
printf("audsrv returned error string: %s\n", audsrv_get_error_string());
return -1;
}
audsrv_adpcm_init();
audsrv_set_volume(MAX_VOLUME);
getAudioDevice(device_path);
loaded = 0;
for (i = 0; i < NUM_SFX_FILES; i++)
{
sprintf(snd_path, "%s/%s", device_path, sfx_files[i]);
ret = sfxLoad(snd_path, &sfx[i]);
if (ret == 0)
{
loaded++;
}
else
{
printf("sfx: failed to load %s, error %d\n", snd_path, ret);
}
}
return loaded;
}
You were using the elements of the audsrv_adpcm_t structure to handle the loading of data, like the buffer and size attributes:
Code:
//sfx0
sfx[0].buffer = snd_buffer[0];
sfx[0].size = snd_size[0];
I don't think it is wrong, but this is a structure that belongs to AUDSRV and is filled in by audsrv_load_adpcm().
So by using its fields, you are creating some coupling with AUDSRV, limiting the reusability of your code, should there be some need to move to a different audio RPC in the future.
So what I did, was to use local variables instead.
Regarding the return value of malloc(), it returns a pointer. So you should compare it against NULL.
Comparing it against 0 will work, since NULL is defined as 0. But it's less readable.
I don't know why it's not working. Do you know when it hangs?Can only get 3 sfx loaded, attempting more freezes the console but hey....its 1 more than before
The SPU2 has 2MB, but it's not 2MB for just storing ADPCM.


