Search code examples
audiokit

AKMIDICallbackInstrument - callback NOT called


How to Reproduce

  • I copied the "Callback Instrument" playground (which works), into a new project. installed AudioKit via pod (version 4.8)
  • I removed the implementation of the callback and put there only a print() statement.
  • Open workspace and run the project.
import UIKit
import AudioKit

class ViewController: UIViewController {

    var sequencer = AKAppleSequencer()
    var tempo = 120.0
    var division = 1

    var callbacker = AKMIDICallbackInstrument { statusByte, note, _ in
        print("Callback called")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        let clickTrack = sequencer.newTrack()
        for i in 0 ..< division {
            clickTrack?.add(noteNumber: 80,
                            velocity: 100,
                            position: AKDuration(beats: Double(i) / Double(division)),
                            duration: AKDuration(beats: Double(0.1 / Double(division))))
            clickTrack?.add(noteNumber: 60,
                            velocity: 100,
                            position: AKDuration(beats: (Double(i) + 0.5) / Double(division)),
                            duration: AKDuration(beats: Double(0.1 / Double(division))))
        }

        clickTrack?.setMIDIOutput(callbacker.midiIn)
        clickTrack?.setLoopInfo(AKDuration(beats: 1.0), numberOfLoops: 10)
        sequencer.setTempo(tempo)

        sequencer.play()
    }
}

What happens

  • The callback is not called (print log is not printed)
  • I can hear a sound of the added notes.
  • This code works in the example playground.

Solution

  • If you are hearing sounds, but haven't connected your tracks to an audio generating output, then you are probably hearing the default sampler. This will happen if you do not have audio enabled, in 'Background Modes'. If you look at the console output, you should see a message to instructing you to make sure that it is enabled - it is necessary with MusicSequence/AKAppleSequencer.