Here is my sound callback that I use:
Err PlaySoundCallBack(void *userDataPtr, SndStreamRef stream, void
*frameBuffer, UInt32 *inCount)
{
PlaySoundStruct *playSoundPtr;
UInt32 byteCount = *inCount;
byteCount = MIN(byteCount, SOUND_PLAYBACK_SIZE);
if (frameBuffer && *inCount) {
playSoundPtr = (PlaySoundStruct *) userDataPtr;
if (playSoundPtr->complete || playSoundPtr->stopped) {
*inCount = 0;
return errNone;
}
if (playSoundPtr->sndAmtPlayed >= playSoundPtr->sndSize) {
if (playSoundPtr->loop)
playSoundPtr->sndAmtPlayed = 0;
else {
*inCount = 0;
SendStopSound(playSoundPtr);
return errNone;
}
}
byteCount = MIN(byteCount, playSoundPtr->sndSize -
playSoundPtr->sndAmtPlayed);
MemMove(frameBuffer, &playSoundPtr->memPtr[playSoundPtr->sndAmtPlayed],
byteCount);
playSoundPtr->sndAmtPlayed += byteCount;
(*inCount) = byteCount;
}
return errNone;
}
Things to note about the PlaySoundStruct is:
sndSize is the total size of my raw sound initiated at creation time.
sndAmtPlayed is initiated to 0 and is used to build up the accumulated amount
of data played.
memPtr is the pointer to my raw/locked data.
loop is a flag to indicate if I want to continue to loop or not.
SOUND_PLAYBACK_SIZE is defined at a resonable 1024.
The SendStopSound() just sends a message to stop the sound and sets the stopped
flag.
I've tried to minimize the chance of having both main program and callback from
touching any shared memory at the same time.
David
--
For information on using the PalmSource Developer Forums, or to unsubscribe,
please see http://www.palmos.com/dev/support/forums/