admsyn / ofxAudioUnit

An openFrameworks addon which makes it easy to work with Audio Units on OSX and iOS
MIT License
120 stars 24 forks source link

getCurrentTimestamp().mSampleTime #31

Open stephanschulz opened 6 years ago

stephanschulz commented 6 years ago

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;
}
admsyn commented 6 years ago

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

stephanschulz commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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