1

Right now I have a loop that loops through an array of file player audio units and tells them what position in the audio file to start playing. (this works) In this same loop I have the following code to tell the units when to start playing (-1 makes them play in the next render cycle). The problem is that they are not starting at the same time because the first track starts playing before i have had a chance to tell the third track to play. What I want to say is "track one, you play in exactly 5 cycles, Track 2 you play in exactly 4 cycles, Track 3 you play in exactly 3 cycles... etc. that way they play at the same time. Is this the right approach? If so, what value do you set for startTime.mSampleTime ? I have not found any documentation that tells me how to do this. Thanks

    // tell the file player AU when to start playing (-1 sample time means next render cycle)
    AudioTimeStamp startTime;
    memset (&startTime, 0, sizeof(startTime));
    startTime.mFlags = kAudioTimeStampSampleTimeValid;
    startTime.mSampleTime = -1;
    AudioUnitSetProperty(fileUnitArray[mycount], kAudioUnitProperty_ScheduleStartTimeStamp, kAudioUnitScope_Global, 0, &startTime, sizeof(startTime));
Beleg
  • 362
  • 2
  • 23

2 Answers2

0

I was not able to track down any information on setting mSampleTime to any other value then -1 (i.e start on the next cycle) but I was able to work around the problem. Instead of keeping the AUGraph running, using AudioUnitReset to reset the file player audio unit and then using the code above to restart the file players, I store the current file player play-head position, stop the AUGraph completely, reinitialize the AUGraph with the current play-head position instead of telling it to start at position zero, and then restart the AUGRAPH.

Beleg
  • 362
  • 2
  • 23
0

What you need to do is schedule the playback of the audio files from the render thread. You load the files on a different thread but you tell the files to play in the next render cycle from the render thread. This way all of your files wil start at the same time.

Aran Mulholland
  • 23,555
  • 29
  • 141
  • 228
  • Could you please explain a little bit more by what you mean when you say "You load the files on a different thread", I'm new into CoreAudio and I wonder how we can do that. – DEADBEEF Aug 09 '17 at 12:34
  • Read this first http://www.rossbencina.com/code/real-time-audio-programming-101-time-waits-for-nothing – Aran Mulholland Aug 09 '17 at 18:35
  • This stuff is quite complicated. If you want to play short files then use the AUSampler and make a custom instrument which has all of your sound files loaded. That way you can just trigger it with midi notes. Basically I use the pthread library to start a new thread, then I make the thread sleep. Then when I need to load a new file I use non blocking message sending to tell the thread to wake up and load the file. Then I use a non blocking operation to tell the AUFilePlayer's to start playing on the next render cycle. It is really tricky to get it working and in sync but it is possible. – Aran Mulholland Aug 09 '17 at 18:38
  • @DEADBEEF follow me on twitter (I have followed you) and we can have a conversation about this, maybe we could help each other out further. I have some new ideas I would love to get someone with DSP experience to help me with and I can help with CoreAudio. – Aran Mulholland Aug 09 '17 at 18:40