iOS Development

ios – Listening MIDI Occasions from MIDI file (Audiokit)

Spread the love


I’m attempting to construct a rhythm sport with SwiftUI and SpriteKit for iOS units and I am caught with an issue relating to midi. I’m attempting to set off an occasion based mostly on the observe at present enjoying once I play a MIDI file. I play the MIDI File with AVMIDIPlayer and attempt to pay attention occasions with AudioKit MIDIListener. Nevertheless the perform receivedMIDINoteOn within the listener by no means triggers once I play the file.

Here’s a take a look at code to exhibit my drawback. So on the contact of the display screen, a midi file is performed however the MIDIReceiver does not detect it, it does nonetheless detect the road “self.midi.sendNoteOnMessage(noteNumber: 60, velocity: 127)” which I put there for take a look at functions.

class MidiPlayer: ObservableObject {

    let midiFile: URL = Bundle.foremost.url(forResource: "no_name" , withExtension: "mid")!
    var midi = MIDI()
    
    var midiPlayer: AVMIDIPlayer?
    func playFile() {
        do {
            self.midi.openOutput()
            strive midiPlayer = AVMIDIPlayer(contentsOf: midiFile as URL, soundBankURL: nil)
            midiPlayer?.prepareToPlay()
            
        } catch {
            print("couldn't create MIDI participant")
        }
        self.midi.sendNoteOnMessage(noteNumber: 60, velocity: 127)
        midiPlayer?.play {
            
            print("completed enjoying")
        }
    }
}


class MIDIReceiver: MIDIListener, ObservableObject {
    static let occasion = MIDIReceiver()

    var midi = MIDI()

    
    func connectInputs() {
        print("connecting units")

        midi.openInput()

        midi.addListener(self)


    }

    func receivedMIDINoteOn(noteNumber: AudioKit.MIDINoteNumber, velocity: AudioKit.MIDIVelocity, channel: AudioKit.MIDIChannel, portID: MIDIUniqueID?, timeStamp: MIDITimeStamp?) {
        print("okay3")
        print(String(noteNumber))

    }
    
    func receivedMIDINoteOff(noteNumber: AudioKit.MIDINoteNumber, velocity: AudioKit.MIDIVelocity, channel: AudioKit.MIDIChannel, portID: MIDIUniqueID?, timeStamp: MIDITimeStamp?) {
        print("okay4")

    }
    
    
    
}

struct GameView: View {
    @StateObject var midi = MIDIReceiver()
    
    var scene: SKScene {
        let scene = GameScene()
        scene.measurement = CGSize(width: 800, peak: 600)
        scene.scaleMode = .fill
        return scene
    }

    var physique: some View {
        VStack {
            SpriteView(scene: scene)
                .body(width: 800, peak: 600)
                .ignoresSafeArea()
        }.onAppear(carry out: {
            midi.connectInputs()

        })
    }
        
    

}


class GameScene: SKScene {
    var participant = MidiPlayer()


    override func didMove(to view: SKView) {
        physicsBody = SKPhysicsBody(edgeLoopFrom: body)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with occasion: UIEvent?) {
        participant.playFile()

    }
    
}

I am sorry if this can be a very primary query, I’m a newbie dev. Thanks for studying!

Leave a Reply

Your email address will not be published. Required fields are marked *