jimparis wrote:It's in malloc, are you running out of RAM by any chance? Bus errors can also be caused by alignment problems. It's also possible (and more likely) that you are just corrupting memory and that's messing with the malloc data structures. If you can, try compiling the same code on the PC and run it under valgrind.
I thought
malloc just returned NULL if there wasn't enough RAM? Anyway, I printed the available RAM as close to the crash as possible, and got ~17MB. The OGG I played is only a few hundred K, so that's probably not it.
The actual player code is sort of stripped-down from Sakya's LightMP3 OGG player, with some changes and additions to accommodate the way I'm using it:
Code: Select all
#include "common.h"
#include <pspaudiolib.h>
#include <tremor/ivorbiscodec.h> //libtremor
#include <tremor/ivorbisfile.h> //libtremor
struct oggFile_t
{
struct OggVorbis_File vf;
int loop, autounload;
int lvol, rvol;
};
size_t ogg_read(void *ptr, size_t size, size_t nmemb, void *datasource)
{
return pgeFileRead((pgeFile *)datasource, ptr, size * nmemb) / size;
}
int ogg_seek(void *datasource, ogg_int64_t offset, int whence)
{
return pgeFileSeek((pgeFile *)datasource, (long)offset, whence);
}
long ogg_tell(void *datasource)
{
return pgeFileSeek((pgeFile *)datasource, 0, PGE_FILE_CUR);
}
int ogg_close(void *datasource)
{
return pgeFileClose((pgeFile *)datasource);
}
static ov_callbacks pspio_callbacks = { ogg_read, ogg_seek, ogg_close, ogg_tell };
static oggFile *ogg_info[PSP_NUM_AUDIO_CHANNELS] = { 0 };
static void oggDecodeCallback(void *buffer, u32 nsamples, void *pdata)
{
if (!(buffer && nsamples)) return;
oggFile *ogg = (oggFile *)pdata;
int sect = 0;
int nbytes = nsamples * 4;
int res = 0;
while (nbytes > 0)
{
res = ov_read(&(ogg->vf), buffer, nbytes, §);
if (res == OV_HOLE) continue;
if (res <= 0) break;
nbytes -= res;
}
if (res <= 0 && ogg->autounload)
{
oggFileUnload(ogg);
}
}
int oggInit()
{
return pspAudioInit() < 0 ? 0 : 1;
}
void oggShutdown()
{
pspAudioEnd();
}
void oggFileUnload(oggFile *ogg)
{
ov_clear(&(ogg->vf));
}
oggFile *oggFileLoad(const char *filename)
{
pgeFile *file = pgeFileOpen((char *)filename, PGE_FILE_RDONLY);
if (!file) return 0;
DEBUG("Before: %8dkB RAM free\n", pgeSystemGetFreeRam() >> 10);
oggFile *ogg = (oggFile *)pgeMalloc(sizeof(oggFile));
if (!ogg) return 0;
int res = ov_open_callbacks(file, &(ogg->vf), 0, 0, pspio_callbacks);
DEBUG("After: %8dkB RAM free\n", pgeSystemGetFreeRam() >> 10);
if (res < 0)
{
pgeFileClose(file);
return 0;
}
vorbis_info *vi = ov_info(&(ogg->vf), -1);
DEBUG("samplerate == %ld, channels == %d\n", vi->rate, vi->channels);
if (vi->channels > 2)
{
oggFileUnload(ogg);
return 0;
}
ogg->loop = ogg->autounload = 0;
ogg->lvol = ogg->rvol = 0x8000;
return ogg;
}
int oggFileLoop(oggFile *ogg, int loop)
{
if (!ogg) return 0;
int old = ogg->loop;
ogg->loop = loop;
return old;
}
void oggFileSetVolume(oggFile *ogg, int lvol, int rvol)
{
if (!ogg) return;
ogg->lvol = lvol;
ogg->rvol = rvol;
}
int oggFilePlay(oggFile *ogg)
{
int slot;
for (slot = 0; slot < PSP_NUM_AUDIO_CHANNELS; ++slot)
{
if (!ogg_info[slot]) break;
}
if (slot < PSP_NUM_AUDIO_CHANNELS)
{
ogg_info[slot] = ogg;
pspAudioSetVolume(slot, ogg->lvol, ogg->rvol);
pspAudioSetChannelCallback(slot, oggDecodeCallback, ogg);
return 1;
}
else if (ogg->autounload)
{
oggFileUnload(ogg);
}
return 0;
}
int oggPlay(const char *filename)
{
oggFile *ogg = oggFileLoad(filename);
if (!ogg) return 0;
ogg->autounload = 1;
return oggFilePlay(ogg);
}
AFAIK I'm not messing with anything dangerous, system structures and whatnot...