1

I am using the MIKMIDI framework and this is using the AudioToolbox type MusicTimeStamp

How can i convert this timestamp to milliseconds?

Martin Mlostek
  • 2,755
  • 1
  • 28
  • 57

2 Answers2

1

The MusicTimeStamp is a raw beat count, you need to know the tempo (and tempo map, tempo isn't an invariant) of the music you're working with in order to convert this into milliseconds.

Outside of a MusicSequence a MTS can't be mapped to a wall time.

Edit: A CoreMedia CMTime can be converted to wall times if that helps.

iluvcapra
  • 9,436
  • 2
  • 30
  • 32
1

There's new API for this in MIKMIDI. It's in a branch (1.8) as I write this, but should be merged soon, and released in the 1.8 release. It makes it much easier to do the conversion you're asking about.

In the context of a sequence, do:

let seconds = sequence.timeInSeconds(forMusicTimeStamp: musicTimeStamp)

There's also a method to convert in the opposite direction. MIKMIDISequencer has very similar, but more sophisticated (to account for looping, tempo override, etc.) methods to do the same kinds of conversions.

Without this new API in MIKMIDI, you can still use MusicSequenceGetSecondsForBeats(). You can get the underlying MusicSequence for an MIKMIDISequence using its musicSequence property:

var timeInSeconds = Float64(0)
MusicSequenceGetSecondsForBeats(sequence, musicTimeStamp, &timeInSeconds)

As far as I know this doesn't take into account looping even if you're doing it with the MusicPlayer API, and certainly not an overridden tempo if one is set on MIKMIDISequencer, so you should prefer MIKMIDI's API above if possible.

Andrew Madsen
  • 21,309
  • 5
  • 56
  • 97