- Joined
- Jun 22, 2012
- Messages
- 234
- Trophies
- 0
- Age
- 28
- Location
- New York, USA
- Website
- www.youtube.com
- XP
- 544
- Country
I'm trying to fix some audio glitches in my program. Basically, every couple of times it plays a sound, it glitches a bit before playing another sound. I can tell it's data leftover in the buffer because it's a glitchy version of what played before, so I'm guessing this has something to do with the way I implemented it, I'm only using 1 data buffer and 1 waveBuf. The function itself flushes buffers though and appears to handle that stuff. I know for a fact that's probably wrong, but I can't find good example code.
I'm not very familar with the ndsp system so I did find an example and copied it (I know, that's probably the reason why it's buggy), can someone tell me what I'm doing wrong?
I'm not very familar with the ndsp system so I did find an example and copied it (I know, that's probably the reason why it's buggy), can someone tell me what I'm doing wrong?
Code:
// Global wave buffer
ndspWaveBuf waveBuf;
// Data buffer
u8* data = NULL;
...
int playWav(string path, int channel = 1, bool toloop = true) {
u32 sampleRate;
u32 dataSize;
u16 channels;
u16 bitsPerSample;
ndspSetOutputMode(NDSP_OUTPUT_STEREO);
ndspSetOutputCount(2); // Num of buffers
// Reading wav file
FILE* fp = fopen(path.c_str(), "rb");
if(!fp)
{
printf("Could not open the example.wav file.\n");
return -1;
}
char signature[4];
fread(signature, 1, 4, fp);
if( signature[0] != 'R' &&
signature[1] != 'I' &&
signature[2] != 'F' &&
signature[3] != 'F')
{
printf("Wrong file format.\n");
fclose(fp);
return -1;
}
fseek(fp, 40, SEEK_SET);
fread(&dataSize, 4, 1, fp);
fseek(fp, 22, SEEK_SET);
fread(&channels, 2, 1, fp);
fseek(fp, 24, SEEK_SET);
fread(&sampleRate, 4, 1, fp);
fseek(fp, 34, SEEK_SET);
fread(&bitsPerSample, 2, 1, fp);
if(dataSize == 0 || (channels != 1 && channels != 2) ||
(bitsPerSample != 8 && bitsPerSample != 16))
{
printf("Corrupted wav file.\n");
fclose(fp);
return -1;
}
// Allocating and reading samples
data = static_cast<u8*>(linearAlloc(dataSize));
fseek(fp, 44, SEEK_SET);
fread(data, 1, dataSize, fp);
fclose(fp);
fseek(fp, 44, SEEK_SET);
fread(data, 1, dataSize, fp);
fclose(fp);
// Find the right format
u16 ndspFormat;
if(bitsPerSample == 8)
{
ndspFormat = (channels == 1) ?
NDSP_FORMAT_MONO_PCM8 :
NDSP_FORMAT_STEREO_PCM8;
}
else
{
ndspFormat = (channels == 1) ?
NDSP_FORMAT_MONO_PCM16 :
NDSP_FORMAT_STEREO_PCM16;
}
ndspChnReset(channel);
ndspChnSetInterp(channel, NDSP_INTERP_NONE);
ndspChnSetRate(channel, float(sampleRate));
ndspChnSetFormat(channel, ndspFormat);
// Create and play a wav buffer
std::memset(&waveBuf, 0, sizeof(waveBuf));
waveBuf.data_vaddr = reinterpret_cast<u32*>(data);
waveBuf.nsamples = dataSize / (bitsPerSample >> 3);
waveBuf.looping = toloop;
waveBuf.status = NDSP_WBUF_FREE;
DSP_FlushDataCache(data, dataSize);
ndspChnWaveBufAdd(channel, &waveBuf);
return ((dataSize / (bitsPerSample >> 3)) / sampleRate); // Return duration in seconds, for debugging purposes
}
...
// Somewhere in main...
if (condition1) {
ndspChnWaveBufClear(1);
playWav("sdmc:/3ds/appname/data/song.wav",1,false); // This plays often
}
if(keys & KEY_Y) playWav("sdmc:/3ds/appname/data/song.wav",1,false); // This plays occasionally
Last edited by LeifEricson,