AudioKit: Using the new AKSequencer with any variety of the callback instruments

前端 未结 2 479
梦谈多话
梦谈多话 2021-01-16 04:32

This topic has been covered Numerous Times, and I have successfully used a AKMIDICallbackInstrument with the old AKAppleSequencer in my previous ap

2条回答
  •  花落未央
    2021-01-16 04:45

    To get AKCallbackInstrument working with the new AKSequencer, try connecting your callback instrument to your output, e.g.,

    metroCallback >>> mixer
    

    Not obvious, but has worked for me.

    Edit: including a minimal working version of the new AKSequencer with AKCallbackInstrument

    class SequencerWrapper {
        var seq: AKSequencer!
        var cbInst: AKCallbackInstrument!
        var mixer: AKMixer!
    
        init() {
            mixer = AKMixer()
            AudioKit.output = mixer
            seq = AKSequencer()
            cbInst = AKCallbackInstrument()
    
            // set up a track
            let track = seq.addTrack(for: cbInst)
            for i in 0 ..< 4 {
                track.add(noteNumber: 60, position: Double(i), duration: 0.5)
            }
            track.length = 4.0
            track.loopEnabled = true
            track >>> mixer  // must send track to mixer
    
            // set up the callback instrument
            cbInst.callback = { status, note, vel in
                guard let status = AKMIDIStatus(byte: status),
                    let type = status.type,
                    type == .noteOn else { return }
                print("note on: \(note)")
                // trigger sampler etc from here
            }
            cbInst >>> mixer // must send callbackInst to mixer
        }
    
        func play() {
            seq.playFromStart()
        }
    }
    

提交回复
热议问题