ofxAudioUnit icon indicating copy to clipboard operation
ofxAudioUnit copied to clipboard

getCurrentTimestamp().mSampleTime

Open stephanschulz opened this issue 7 years ago • 4 comments

I am trying to make an old OF project work again. It used your great add-on.

I used to play files and compared their current play head position with their actual duration.

I use getLength() for the duration. I see you have getCurrentTimestamp(). back in then I had to make my own function to get the current play head time. But something changed because the returned number does not match make any sense in relation to the duration time.

Here is my old function which I can now replace with getCurrentTimestamp().mSampleTime. But only on my OSX 10.8.5 system are the returned numbers useful. Foe example right after the first update() is done getCurrentTimestamp().mSampleTime returns 78848.

Would you know why? What changed between os 10.8.5 and 10.12 ?

// ----------------------------------------------------------
int ofxAudioUnitFilePlayer::getPlayheadPos()
// ----------------------------------------------------------
{
    AudioTimeStamp timestamp;
    UInt32 size = sizeof(timestamp);
    AudioUnitGetProperty(*_unit, kAudioUnitProperty_CurrentPlayTime, kAudioUnitScope_Global, 0, &timestamp, &size);
    
    return timestamp.mSampleTime;
}

stephanschulz avatar Feb 27 '18 15:02 stephanschulz

Sorry, I don't know and a quick googling isn't turning up much.

Maybe check if AudioUnitGetProperty(...) is returning an error, you can use the ofxAudioUnit util for automating that here https://github.com/admsyn/ofxAudioUnit/blob/f649c84e61cc254cdf095111ce453af49d26e710/src/ofxAudioUnitUtils.h#L42-L45

admsyn avatar Feb 28 '18 12:02 admsyn

calling getCurrentTimestamp() does not print out any error messages. I guess it would print something in case of error, since OFXAU_PRINT seems to be used. Right? https://github.com/admsyn/ofxAudioUnit/blob/f649c84e61cc254cdf095111ce453af49d26e710/src/ofxAudioUnitFilePlayer.cpp#L79

stephanschulz avatar Feb 28 '18 14:02 stephanschulz

I think this fixes it

int ofxAudioUnitFilePlayer::getPlayheadPos()
// ----------------------------------------------------------
{
    /*
    AudioTimeStamp timestamp;
    UInt32 size = sizeof(timestamp);
    AudioUnitGetProperty(*_unit, kAudioUnitProperty_CurrentPlayTime, kAudioUnitScope_Global, 0, &timestamp, &size);
    
     //       progress += timestamp/canonicalFormat.mSampleRate;
     
     return progress; //timestamp.mSampleTime;
     */
    
   // https://github.com/ronaldmannak/YBAudioFramework/blob/master/YBAudioUnit/YBAudioFilePlayer.m#L32
    Float64 _sampleRateRatio = 1;
    
    AudioTimeStamp currentPlayTime;
    UInt32 dataSize = sizeof(currentPlayTime);
    AudioUnitGetProperty(*_unit, kAudioUnitProperty_CurrentPlayTime, kAudioUnitScope_Global, 0, &currentPlayTime, &dataSize);
    if (currentPlayTime.mSampleTime == -1.) {
        currentPlayTime.mSampleTime = 0;
    }
    currentPlayTime.mFlags = kAudioTimeStampSampleTimeValid;
    currentPlayTime.mSampleTime += _sampleRateRatio * (Float64)_region.mStartFrame;
    return currentPlayTime.mSampleTime;
    
}

go it from here: https://github.com/ronaldmannak/YBAudioFramework/blob/master/YBAudioUnit/YBAudioFilePlayer.m#L32

stephanschulz avatar Mar 08 '18 00:03 stephanschulz

added the changes to my fork https://github.com/antimodular/ofxAudioUnit/blob/b09d380ce3396c38aa700e4d5ca2ef7c112bc15b/src/ofxAudioUnitFilePlayer.cpp#L237-L265

stephanschulz avatar Mar 10 '18 01:03 stephanschulz