Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read note from MIDI file using AudioKit

I am trying to build a sequencer that render the note from midi file.

Currently I am using AudioKit for the music data processing. Would like to know how can I get the note data / event from the midi file with AudioKit.

I have tried to use AKSequencer and output to AKMIDINode to listen the MIDI event, but seems cannot get anything from it.

class CustomMIDINode: AKMIDINode {

    override init(node: AKPolyphonicNode) {
        print("Node create") // OK
        super.init(node: node)
    }

    func receivedMIDINoteOff(noteNumber: MIDINoteNumber, velocity: MIDIVelocity, channel: MIDIChannel) {
        print("midi note off") // Not printed
    }

    func receivedMIDISetupChange() {
        print("midi setup changed") // Not printed
    }

    override func receivedMIDINoteOn(_ noteNumber: MIDINoteNumber, velocity: MIDIVelocity, channel: MIDIChannel) {
        print("receivedMIDINoteOn") // Not printed
    }
}

func setupSynth() {
    oscBank.attackDuration = 0.05
    oscBank.decayDuration = 0.1
    oscBank.sustainLevel = 0.1
    oscBank.releaseDuration = 0.1
}


let seq = AKSequencer(filename: "music")
let oscBank = AKOscillatorBank()
var midi = AKMIDI()
let midiNode = CustomMIDINode(node: oscBank)

setupSynth()

midi.openInput()
midi.addListener(midiNode)
seq.tracks.forEach { (track) in
    track.setMIDIOutput(midiNode.midiIn)
}
AudioKit.output = midiNode
AudioKit.start()
seq.play()
like image 275
Tony Fung Choi Fung Avatar asked Dec 27 '17 07:12

Tony Fung Choi Fung


2 Answers

Have you looked at any of the example Audio Kit projects available for download? they are very useful for troubleshooting AK. I actually find the examples better than the documentation (as implementation isn't explained very well).

As for your question you can add a midi listener to an event, there is an example of this code in the Analog Synth X Project available here.

let midi = AKMIDI()
        midi.createVirtualPorts()
        midi.openInput("Session 1")
        midi.addListener(self)

For a more worked bit of code you can refer to this although the code is likely out of date in parts.

like image 81
Axemasta Avatar answered Nov 04 '22 05:11

Axemasta


Tony, is it that you aren’t receiving any MIDI events, or just the print statements?

I agree with Axemasta’s response about adding AKMidiListener to the class, along with checking out the MIDI code examples that come with AudioKit. This ROM Player example shows how to play external MIDI files with the AKMidiSsmpler node:

https://github.com/AudioKit/ROMPlayer

In order for the print to display, try wrapping it in a DispatchQueue.main.async so that it’s on the main thread. Here’s an AudioKit MIDI implementation question with a code example that I posted here:

AudioKit iOS - receivedMIDINoteOn function

I hope this helps.

like image 36
Mark Jeschke Avatar answered Nov 04 '22 07:11

Mark Jeschke