views:

1431

answers:

3

How would one go about creating a 'beat box' style sound engine, where a series of sounds can be queued up ahead of time and during playback. These sounds need to play back without any gaps or hiccups though.

I've looked into OpenAL and have attempted to use the alSourceQueueBuffers() to create a source with a series of pre-buffered clips, but did not achieve what I was looking for.

I load my audio files with AudioFileOpenURL, and then load it into a char array with AudioFileReadBytes, creating a buffer with alGenBuffers and then buffering with alBufferData.

I then create a source with alGenSources, and hold onto a reference to that source. I then call alSourceQueueBuffers(sourceId, 1, &bufferId) a handful of times where bufferId is a parameter passed into my 'queueClip' method and references a handful of different clips.

After doing this, and calling alSourcePlay I hear what appears to be two of my clips playing, back-to-back ... but then nothing (I loaded it up with 3 audio files, and randomly adding them to the source with alSourceQueueBuffers a couple of times).

I also need to know the best way to update my source, to add new sounds to it and remove already played sounds from it to clean up memory, etc.

+1  A: 

Are you polling the source to see when it has used up a buffer?

ALuint freeBuffer;
ALint processed;
alGetSourcei (myALSource, AL_BUFFERS_PROCESSED, &processed);
while (processed > 0) {
  // remove spent buffer
  alSourceUnqueueBuffers(myALSource, 1, &freeBuffer);
  // refill buffer with samples, if they're going to be different this time
  // ...
  // re-queue buffer on source
  alSourceQueueBuffers(myALSource, 1, &freeBuffer);
  // check again for more processed buffers
  alGetSourcei (myALSource, AL_BUFFERS_PROCESSED, &processed);
}

You'll need to keep doing this polling to check for spent buffers... I used a simple NSTimer.

invalidname
A: 

I overlooked an easier option. Set the AL_LOOPING property on your source:

alSourcei (myALSource, AL_LOOPING, AL_TRUE);
invalidname
that would just loop a single sound, wouldn't it?
David Higgins
A: 

Macaroni and Cheese

whazzup