Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't get V5 MIDI to work at all. #2261

Closed
brentbrinkley opened this issue Sep 12, 2020 · 13 comments
Closed

Can't get V5 MIDI to work at all. #2261

brentbrinkley opened this issue Sep 12, 2020 · 13 comments

Comments

@brentbrinkley
Copy link

I know you guy are hard at work trying to update everything for the new release but just wanted to make you aware I can't get MIDI to work at all in a SwiftUI app on Xcode 12. I've followed some examples online and I can't so much as get a signal to register in a super simple project. So I wasn't sure if it's not working at all at the moment or if it's something on my end.

Here's the model I'm using it's completely barebones

 //
//  DataModel.swift
//  AKTestSetup
//
//  Created by Brent Brinkley on 9/10/20.
//

import SwiftUI
import CoreMIDI
import AudioKit

class Conductor: ObservableObject, AKMIDIListener {
    
    //     Single shared instance of this class
    static let sharedInstance = Conductor()
    
    var midi = AKMIDI()
    
    @Published var noteNumber: UInt8 = 0
    
    
    init() {
        midi.openInput(name: "Session 1")
        
        midi.addListener(self)
    }
    
    func receivedMIDINoteOn(noteNumber: MIDINoteNumber, velocity: MIDIVelocity, channel: MIDIChannel, portID: MIDIUniqueID? = nil, offset: MIDITimeStamp = 0) {
        print("MidiNote received: \(self.noteNumber) on channel: \(channel) and port ID: \(String(describing: portID)) ")
        //        DispatchQueue.main.async {
        //            self.noteNumber = noteNumber
        //        }
    }
    
    func receivedMIDINoteOff(noteNumber: MIDINoteNumber, velocity: MIDIVelocity, channel: MIDIChannel, portID: MIDIUniqueID?, offset: MIDITimeStamp) {
        print("MidiNote off: \(noteNumber) on channel: \(channel) and port ID: \(String(describing: portID)) ")
        //        DispatchQueue.main.async {
        //            self.noteNumber = 0
        //        }
    }
    
    func receivedMIDIController(_ controller: MIDIByte, value: MIDIByte, channel: MIDIChannel, portID: MIDIUniqueID?, offset: MIDITimeStamp) {
        print("received midi controller")
    }
    
    func receivedMIDIAftertouch(noteNumber: MIDINoteNumber, pressure: MIDIByte, channel: MIDIChannel, portID: MIDIUniqueID?, offset: MIDITimeStamp) {
        print("received midi aftertouch")
    }
    
   .......... other protocol stubs
    
}

I'm then registering this to the environment object in my main app file. So I'm not sure if it's something I'm doing wrong. Or if you guys are just in the weeds right now getting everything ready.

@aure
Copy link
Member

aure commented Sep 12, 2020

I have written a midi monitor in swiftUI that works and is basically set up just like how you have it except that instead of opening up inputs on init, I have a start function that I call when the SwiftUI appears:

        .onAppear {
            self.conductor.start()
        }
        .onDisappear {
            self.conductor.stop()
        }

and in the conductor:

    func start() {
        midi.openInput(name: "Bluetooth")
        midi.openInput()
        midi.addListener(self)
    }

    func stop() {
        midi.closeAllInputs()
    }

See if that change helps.

@brentbrinkley
Copy link
Author

I updated with your code. Unfortunately I'm still not getting any output. Here's the other parts of the code.

app entry point:

import SwiftUI

@main
struct AKTestSetupApp: App {
    let midiData = Conductor()
   
    var body: some Scene {
        WindowGroup {
            ContentView()
                .environmentObject(midiData)
        }
    }
}

View for MIDI

import SwiftUI

struct ContentView: View {
    @EnvironmentObject var midi: Conductor
    
    var body: some View {
        Text("Test midi inputs \(midi.noteNumber)")
            .onAppear {
                midi.start()
            }
            .onDisappear {
                midi.stop()
            }
        
    }
}

model

class Conductor:  AKMIDIListener, ObservableObject {
    
    //     Single shared instance of this class
    static let sharedInstance = Conductor()
    
    var midi = AKMIDI()
    
    @Published var noteNumber: UInt8 = 0 // Should update on midi fire
    
    
    func start() {
            midi.openInput(name: "Session 1")
            midi.openInput()
            midi.addListener(self)
            
        }

    func stop() {
        midi.closeAllInputs()
    }
    
    func receivedMIDINoteOn(noteNumber: MIDINoteNumber, velocity: MIDIVelocity, channel: MIDIChannel, portID: MIDIUniqueID? = nil, offset: MIDITimeStamp = 0) {
        print("MidiNote received: \(self.noteNumber) on channel: \(channel) and port ID: \(String(describing: portID)) ")
                DispatchQueue.main.async {
                    self.noteNumber = noteNumber
                }
    }
    
    func receivedMIDINoteOff(noteNumber: MIDINoteNumber, velocity: MIDIVelocity, channel: MIDIChannel, portID: MIDIUniqueID?, offset: MIDITimeStamp) {
        print("MidiNote off: \(noteNumber) on channel: \(channel) and port ID: \(String(describing: portID)) ")
                DispatchQueue.main.async {
                    self.noteNumber = 0
                }
    }
}

@aure
Copy link
Member

aure commented Sep 12, 2020

Does it say anything in your output console? You may have forgotten to add background modes / audio in your project settings. That is required for midi.

@aure
Copy link
Member

aure commented Sep 12, 2020

And this setting is even more hidden in Xcode 12 than it was before.

@brentbrinkley
Copy link
Author

This is what it says in the console:

2020-09-12 18:13:21.495925-0400 AKTestSetup[12126:6855669] [midi] AKMIDI.swift:init():51:Initializing MIDI (AKMIDI.swift:init():51)

Where are these setting located?

@brentbrinkley
Copy link
Author

I found them hang tight let me test this out

@aure
Copy link
Member

aure commented Sep 12, 2020

Hmmm, it should have triggered this bit of code:

        case kMIDINotPermitted:
            AKLog("kMIDINotPermitted: Have you enabled the audio background mode in your ios app?",
                  log: OSLog.midi,
                  type: .error)

so if that is the problem, then there's a new problem where CheckError is not being called.

@brentbrinkley
Copy link
Author

Yeah I definitely didn't get any errors and updating those settings didn't change anything as far as the output

@aure
Copy link
Member

aure commented Sep 12, 2020

I see you are using a SwiftUI app, that is pretty cutting edge, I have only done SwiftUI interfaces. Are you running on a device or on macOS? We could do a screenshare if you like.

@aure
Copy link
Member

aure commented Sep 12, 2020

Which version of AudioKit are you using and how do you have it installed?

@brentbrinkley
Copy link
Author

I'm using the v5-develop branch of AudioKit installed through swift package manager. The app is for iOS.

@aure
Copy link
Member

aure commented Sep 13, 2020

Nice. I'm pretty happy with the workflow now. Sorry your app isn't working, want to zoom and have a look together?

@brentbrinkley
Copy link
Author

Yeah we can should I post the link here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants