0

I'm trying to record MIDI tracks with one sampler, but in the same time. For every new recording, it's creating new track on sequencer, callback instrument using different channel.

When tracks don't intersect, everything works fine. But when I add notes for new track, which intersect with already recorded track, just recordered track on playback will not call callback function for sound and it will be standard 'beep'.

Here's the code of setting track and callback function:

func startRecord() {
        guard let newTrack = sequencer.newTrack() else { return }
        recordingTrack = newTrack
        recordingChannel = MIDIChannel(sequencer.tracks.count)

        let midiInstrument = AKMIDICallbackInstrument()
        let channel = recordingChannel
        let sampler = currentSampler
        midiInstrument.callback = { (status, note, velocity) in
            let status = AKMIDIStatus(byte: status)!.type!
            switch status {
            case .noteOn:
                try! sampler.play(noteNumber: note, velocity: velocity, channel: channel!)
            case .noteOff:
                try! sampler.stop(noteNumber: note, channel: channel!)
            default:
                break
            }
        }
        newTrack.setMIDIOutput(midiInstrument.midiIn)

        sequencer.rewind()
        sequencer.preroll()
        sequencer.play()
    }
Evgenii Shishko
  • 142
  • 2
  • 8

1 Answers1

0

You are creating a new instance of AKMIDICallbackInstrument each time you call this function. You don't seem to be storing these references outside of this code block. Try keeping a class level array of AKMIDICallbackIntruments to which you can add the new instances that you create.

c_booth
  • 2,185
  • 1
  • 13
  • 22
  • I tried what you said, but it doesn't solve the problem. In the moment of playing, AKMIDICallbackInstrument for each track is retained by class and every AKMIDICallbackInstrument has **midiIn** property. – Evgenii Shishko Apr 17 '19 at 17:55