Search code examples
audiokit

the proper way to detect the current bar?


I am working on a sequencer app based on the SequencerDemo app, using AKAppleSequencer. It's been great so far, but now I need to add a new function to highlight the current bar.

Is there any delegate I can use to keep track of the bar that's currently being played? What is the proper way to handle this? Any help is much appreciated.


Solution

  • I ended up using AKMIDICallbackInstrument as per suggestion by @c_booth. And I added midi notes (2 beats per bar) to a separate (dedicated) track just to track the transport.

    I put the following in init:

    callbackInstr = AKMIDICallbackInstrument(
            midiInputName: "midiCallBack",
            callback: { status, noteNumber, velocity in
                let statusType = AKMIDIStatus(byte: status)?.type
                if(statusType == AKMIDIStatusType.noteOn){
                    self.sequencerCallback()
                }
        })
    

    and I'm tracking the sequencer.currentPosition.musicTimeStamp in sequencerCallback()

    it seems to be working.