The basic question would be: "How do I seek to a point in the middle of an audio file and schedule that region to play after a 2.5 second delay using the AUFilePlayer as an input node in a running AUGraph?"
- (void) seekTime:(Float64)timeOffset throughTime:(Float64)stopOffset
{
OSStatus result;
result = AudioUnitReset(fileAU, kAudioUnitScope_Global, 0);
firstFrameInSegment = timeOffset *fileFormat.mSampleRate*fileFormat.mFramesPerPacket;
lastFrameInSegment = stopOffset *fileFormat.mSampleRate*fileFormat.mFramesPerPacket;
SInt64 maxFrame = totalPackets*fileFormat.mFramesPerPacket;
if (lastFrameInSegment > maxFrame) { // in case stopOffset time is beyond EOF
lastFrameInSegment = maxFrame;
}
if (lastFrameInSegment > firstFrameInSegment) { // only start this AUFP if there is more file to play
ScheduledAudioFileRegion rgn;
memset (&rgn.mTimeStamp, 0, sizeof(rgn.mTimeStamp));
rgn.mTimeStamp.mFlags = kAudioTimeStampSampleTimeValid;
/********** WHAT SHOULD THIS REALLY BE? ***********/
rgn.mTimeStamp.mSampleTime = firstFrameInSegment/fileFormat.mFramesPerPacket;
rgn.mAudioFile = auFileID;
rgn.mLoopCount = 0;
rgn.mStartFrame = firstFrameInSegment;
rgn.mFramesToPlay = lastFrameInSegment - firstFrameInSegment;
// Then hook up the completion callback and reinstall it
rgn.mCompletionProc = &myFileRegionCompletionProc;
rgn.mCompletionProcUserData = (__bridge void*)self;
currentFrame = firstFrameInSegment;
result = AudioUnitSetProperty(fileAU, kAudioUnitProperty_ScheduledFileRegion, kAudioUnitScope_Global, 0, &rgn, sizeof(rgn));
if (noErr != result) {
[self printErrorMessage: @"AUScheduled File Region" withStatus: result]; return;}
// prime the fp AU with default values
UInt32 defaultVal = 0;
result = AudioUnitSetProperty(fileAU, kAudioUnitProperty_ScheduledFilePrime, kAudioUnitScope_Global, 0, &defaultVal, sizeof(defaultVal));
if (noErr != result) {
[self printErrorMessage: @"AUScheduled File Prime" withStatus: result]; return;}
// tell the fp AU when to start playing (this ts is in the AU's render time stamps; -1 means next render cycle)
AudioTimeStamp startTime;
memset (&startTime, 0, sizeof(startTime));
startTime.mFlags = kAudioTimeStampSampleTimeValid;
if (!self.isPlaying) {
startTime.mSampleTime = -1;
} else {
/********** AND WHAT SHOULD THIS REALLY BE? ***********/
Float64 tail = self.timeRemainingInSegment; // computed seconds from mCompletedProc callback moment until we his lastFrameInSegment
if (tail < 0.001) {
tail = -1;
}
startTime.mSampleTime = tail;
NSLog(@"TailTime: %.4f",tail);
}
result = AudioUnitSetProperty(fileAU, kAudioUnitProperty_ScheduleStartTimeStamp, kAudioUnitScope_Global, 0, &startTime, sizeof(startTime));
if (noErr != result) {
[self printErrorMessage: @"AUScheduled File start timestamp" withStatus: result]; return;}
} else {
NSLog(@"Bus %ld not scheduled from %lld - %lld", busNumber, firstFrameInSegment, lastFrameInSegment);
}
}