• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Scheduling sub-sections of audio files to play with AUFilePlayer
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Scheduling sub-sections of audio files to play with AUFilePlayer


  • Subject: Scheduling sub-sections of audio files to play with AUFilePlayer
  • From: Chris Watson <email@hidden>
  • Date: Tue, 21 Aug 2012 11:50:37 -0500

I'm trying to use AUFilePlayer to "stitch" sections of a file together at playback. When I schedule the first segment, I use typical setting found in all sample code from Apple and here on the web: mStartFrame = 0 and the start time of -1 in the AudioTimeStamp set in the kAudioUnitProperty_ScheduleStartTimeStamp setting call. My problem is finding the proper combination of scheduled region.mTimeStamp.mSampleTime and scheduled startTimeStamp.mSampleTime to cause the AUFilePlayer to begin the next selected section at the moment the first section finishes playing. I use a mCompletionProc which is called when the AUFilePlayer has read the last batch of frames (since it's not really a "completion" callback). At the callback time call to get the current play timeStamp and do the math to find the number of frames left to play when the mCompletionProc is called and set that into the scheduled start time timeStamp so that the following section will start at the moment the current buffer runs out. I'm trying to jump to another section without a break. Sort of looping "somewhere else".

I see a behavior I can't get around. The first time I try to "chain" these segments, the second region starts immediately when scheduled (as if I had set the typical "-1" on the start timeStamp) even though I have computed about a 2.5 second delay. Now, the same code that overlaps the finishing 1st section and the prematurely starting 2nd section, works correctly at every seam after.

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?"

Simple version of my code:

- (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);
    }
        
}

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Coreaudio-api mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Scheduling sub-sections of audio files to play with AUFilePlayer
      • From: Orestis Markou <email@hidden>
  • Prev by Date: Re: Using vDSP in audio USB Driver
  • Next by Date: Re: Using vDSP in audio USB Driver
  • Previous by thread: Re: Using vDSP in audio USB Driver
  • Next by thread: Re: Scheduling sub-sections of audio files to play with AUFilePlayer
  • Index(es):
    • Date
    • Thread