Skip to content

Commit

Permalink
Merge pull request #81 from NickCulbertson/MidiTestFixes
Browse files Browse the repository at this point in the history
Update MIDIPortTest
  • Loading branch information
aure committed Mar 7, 2022
2 parents fa36e40 + 21d1fb8 commit 9c93d18
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 60 deletions.
94 changes: 50 additions & 44 deletions Cookbook/Cookbook/Recipes/MIDIPortTest/MIDIPortTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Foundation
import SwiftUI

struct MIDIPortTestView: View {
@StateObject var MIDIConductor: MIDIPortTestConductor = MIDIPortTestConductor()
@StateObject var conductor: MIDIPortTestConductor = MIDIPortTestConductor()
@State private var selectedPort1Uid: MIDIUniqueID?
@State private var selectedPort2Uid: MIDIUniqueID?

Expand All @@ -24,35 +24,35 @@ struct MIDIPortTestView: View {
// .font(.title2)
}
HStack {
ForEach(0..<MIDIConductor.inputNames.count, id: \.self) { index in
ForEach(0..<conductor.inputNames.count, id: \.self) { index in
VStack {
Text("\(MIDIConductor.inputNames[index])")
Text("\(MIDIConductor.inputUIDs[index])")
Text("\(conductor.inputNames[index])")
Text("\(conductor.inputUIDs[index])")
.foregroundColor(.secondary)
}
}
Spacer()
ForEach(0..<MIDIConductor.destinationNames.count, id: \.self) { index in
ForEach(0..<conductor.destinationNames.count, id: \.self) { index in
VStack {
Text("\(MIDIConductor.destinationNames[index])")
Text("\(MIDIConductor.destinationUIDs[index])")
Text("\(conductor.destinationNames[index])")
Text("\(conductor.destinationUIDs[index])")
.foregroundColor(.secondary)

}
}
Spacer()
ForEach(0..<MIDIConductor.virtualInputNames.count, id: \.self) { index in
ForEach(0..<conductor.virtualInputNames.count, id: \.self) { index in
VStack {
Text("\(MIDIConductor.virtualInputNames[index])")
Text("\(MIDIConductor.virtualInputUIDs[index])")
Text("\(conductor.virtualInputNames[index])")
Text("\(conductor.virtualInputUIDs[index])")
.foregroundColor(.secondary)
}
}
Spacer()
ForEach(0..<MIDIConductor.virtualOutputNames.count, id: \.self) { index in
ForEach(0..<conductor.virtualOutputNames.count, id: \.self) { index in
VStack {
Text("\(MIDIConductor.virtualOutputNames[index])")
Text("\(MIDIConductor.virtualOutputUIDs[index])")
Text("\(conductor.virtualOutputNames[index])")
Text("\(conductor.virtualOutputUIDs[index])")
.foregroundColor(.secondary)
}
}
Expand All @@ -63,7 +63,7 @@ struct MIDIPortTestView: View {
VStack {
HStack {
Button("Reset") {
MIDIConductor.resetLog()
conductor.resetLog()
}
Spacer()
}
Expand All @@ -79,16 +79,16 @@ struct MIDIPortTestView: View {
}
.foregroundColor(.secondary)
ScrollView(.vertical) {
ForEach(0..<MIDIConductor.log.count, id: \.self) { index in
let event = MIDIConductor.log[index]
ForEach(0..<conductor.log.count, id: \.self) { index in
let event = conductor.log[index]
HStack {
Text("\(event.statusDescription)")
Text("\(event.channelDescription)")
Text("\(event.data1Description)")
Text("\(event.data2Description)")
Text("\(MIDIConductor.inputPortDescription(forUID: event.portUniqueID).UID)")
Text("\(MIDIConductor.inputPortDescription(forUID: event.portUniqueID).device)")
Text("\(MIDIConductor.inputPortDescription(forUID: event.portUniqueID).manufacturer)")
Text("\(conductor.inputPortDescription(forUID: event.portUniqueID).UID)")
Text("\(conductor.inputPortDescription(forUID: event.portUniqueID).device)")
Text("\(conductor.inputPortDescription(forUID: event.portUniqueID).manufacturer)")
}
.foregroundColor(index == 0 ? .yellow : .primary)
}
Expand All @@ -101,10 +101,10 @@ struct MIDIPortTestView: View {
) {
Text("All")
.tag(nil as MIDIUniqueID?)
ForEach(0..<MIDIConductor.destinationUIDs.count, id: \.self) { index in
ForEach(0..<conductor.destinationNames.count, id: \.self) { index in

Text("\(MIDIConductor.destinationNames[index])")
.tag(MIDIConductor.destinationUIDs[index] as MIDIUniqueID?)
Text("\(conductor.destinationNames[index])")
.tag(conductor.destinationUIDs[index] as MIDIUniqueID?)
}
}
}
Expand All @@ -115,9 +115,9 @@ struct MIDIPortTestView: View {
data1: 60,
data2: 90)
if selectedPort1Uid != nil {
MIDIConductor.sendEvent(eventToSend: eventToSend, portIDs: [selectedPort1Uid!])
conductor.sendEvent(eventToSend: eventToSend, portIDs: [selectedPort1Uid!])
} else {
MIDIConductor.sendEvent(eventToSend: eventToSend, portIDs: nil)
conductor.sendEvent(eventToSend: eventToSend, portIDs: nil)
}
}
Button("Send NoteOff 60") {
Expand All @@ -126,9 +126,9 @@ struct MIDIPortTestView: View {
data1: 60,
data2: 90)
if selectedPort1Uid != nil {
MIDIConductor.sendEvent(eventToSend: eventToSend, portIDs: [selectedPort1Uid!])
conductor.sendEvent(eventToSend: eventToSend, portIDs: [selectedPort1Uid!])
} else {
MIDIConductor.sendEvent(eventToSend: eventToSend, portIDs: nil)
conductor.sendEvent(eventToSend: eventToSend, portIDs: nil)
}
}
Button("Send Controller 82 - 127") {
Expand All @@ -137,9 +137,9 @@ struct MIDIPortTestView: View {
data1: 82,
data2: 127)
if selectedPort1Uid != nil {
MIDIConductor.sendEvent(eventToSend: eventToSend, portIDs: [selectedPort1Uid!])
conductor.sendEvent(eventToSend: eventToSend, portIDs: [selectedPort1Uid!])
} else {
MIDIConductor.sendEvent(eventToSend: eventToSend, portIDs: nil)
conductor.sendEvent(eventToSend: eventToSend, portIDs: nil)
}
}
Button("Send Controller 82 - 0") {
Expand All @@ -149,9 +149,9 @@ struct MIDIPortTestView: View {
data2: 0)

if selectedPort1Uid != nil {
MIDIConductor.sendEvent(eventToSend: eventToSend, portIDs: [selectedPort1Uid!])
conductor.sendEvent(eventToSend: eventToSend, portIDs: [selectedPort1Uid!])
} else {
MIDIConductor.sendEvent(eventToSend: eventToSend, portIDs: nil)
conductor.sendEvent(eventToSend: eventToSend, portIDs: nil)
}
}
}
Expand All @@ -161,9 +161,9 @@ struct MIDIPortTestView: View {
) {
Text("All")
.tag(nil as MIDIUniqueID?)
ForEach(0..<MIDIConductor.virtualOutputUIDs.count, id: \.self) { index in
Text("\(MIDIConductor.virtualOutputNames[index])")
.tag(MIDIConductor.virtualOutputUIDs[index] as MIDIUniqueID?)
ForEach(0..<conductor.virtualOutputUIDs.count, id: \.self) { index in
Text("\(conductor.virtualOutputNames[index])")
.tag(conductor.virtualOutputUIDs[index] as MIDIUniqueID?)
}
}
}
Expand All @@ -174,9 +174,9 @@ struct MIDIPortTestView: View {
data1: 72,
data2: 90)
if selectedPort2Uid != nil {
MIDIConductor.sendEvent(eventToSend: eventToSend, portIDs: [selectedPort2Uid!])
conductor.sendEvent(eventToSend: eventToSend, portIDs: [selectedPort2Uid!])
} else {
MIDIConductor.sendEvent(eventToSend: eventToSend, portIDs: nil)
conductor.sendEvent(eventToSend: eventToSend, portIDs: nil)
}
}
Button("Send NoteOff 72") {
Expand All @@ -185,9 +185,9 @@ struct MIDIPortTestView: View {
data1: 72,
data2: 90)
if selectedPort2Uid != nil {
MIDIConductor.sendEvent(eventToSend: eventToSend, portIDs: [selectedPort2Uid!])
conductor.sendEvent(eventToSend: eventToSend, portIDs: [selectedPort2Uid!])
} else {
MIDIConductor.sendEvent(eventToSend: eventToSend, portIDs: nil)
conductor.sendEvent(eventToSend: eventToSend, portIDs: nil)
}
}
Button("Send Controller 82 - 127") {
Expand All @@ -196,9 +196,9 @@ struct MIDIPortTestView: View {
data1: 82,
data2: 127)
if selectedPort2Uid != nil {
MIDIConductor.sendEvent(eventToSend: eventToSend, portIDs: [selectedPort2Uid!])
conductor.sendEvent(eventToSend: eventToSend, portIDs: [selectedPort2Uid!])
} else {
MIDIConductor.sendEvent(eventToSend: eventToSend, portIDs: nil)
conductor.sendEvent(eventToSend: eventToSend, portIDs: nil)
}
}
Button("Send Controller 82 - 0") {
Expand All @@ -207,19 +207,19 @@ struct MIDIPortTestView: View {
data1: 82,
data2: 0)
if selectedPort2Uid != nil {
MIDIConductor.sendEvent(eventToSend: eventToSend, portIDs: [selectedPort2Uid!])
conductor.sendEvent(eventToSend: eventToSend, portIDs: [selectedPort2Uid!])
} else {
MIDIConductor.sendEvent(eventToSend: eventToSend, portIDs: nil)
conductor.sendEvent(eventToSend: eventToSend, portIDs: nil)
}
}
}
Divider()
HStack {
Toggle(isOn: $MIDIConductor.outputIsOpen) {
Toggle(isOn: $conductor.outputIsOpen) {
}
Toggle(isOn: $MIDIConductor.inputPortIsSwapped) {
Toggle(isOn: $conductor.inputPortIsSwapped) {
}
Toggle(isOn: $MIDIConductor.outputPortIsSwapped) {
Toggle(isOn: $conductor.outputPortIsSwapped) {
}
}
HStack {
Expand All @@ -229,6 +229,12 @@ struct MIDIPortTestView: View {
}
}
}
.onAppear {
self.conductor.start()
}
.onDisappear {
self.conductor.stop()
}
}
}

Expand Down
41 changes: 25 additions & 16 deletions Cookbook/Cookbook/Recipes/MIDIPortTest/MIDIPortTestConductor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,6 @@ class MIDIPortTestConductor: ObservableObject, MIDIListener {
}
@Published var outputPortIsSwapped: Bool = false
@Published var inputPortIsSwapped: Bool = false
init() {
midi.createVirtualInputPorts(count: 1, uniqueIDs: [inputUIDDevelop])
midi.createVirtualOutputPorts(count: 1, uniqueIDs: [outputUIDDevelop])
// midi.createVirtualInputPorts(numberOfPort: 1, [inputUIDMain], names: ["MIDI Test Input Port_Main"])
// midi.createVirtualOutputPorts(numberOfPort: 1, [outputUIDMain], names: ["MIDI Test Output Port_Main"])
midi.openInput()
midi.addListener(self)
}
func openOutputs () {
for uid in midi.destinationUIDs {
midi.openOutput(uid: uid)
}
for uid in midi.virtualOutputUIDs {
midi.openOutput(uid: uid)
}
}
var inputNames: [String] {
midi.inputNames
}
Expand Down Expand Up @@ -128,6 +112,31 @@ class MIDIPortTestConductor: ObservableObject, MIDIListener {
midi.virtualOutputInfos
}

func start() {
midi.openInput()
}

func stop() {
midi.closeAllInputs()
}

init() {
midi.createVirtualInputPorts(count: 1, uniqueIDs: [inputUIDDevelop])
midi.createVirtualOutputPorts(count: 1, uniqueIDs: [outputUIDDevelop])
// midi.createVirtualInputPorts(numberOfPort: 1, [inputUIDMain], names: ["MIDI Test Input Port_Main"])
// midi.createVirtualOutputPorts(numberOfPort: 1, [outputUIDMain], names: ["MIDI Test Output Port_Main"])
midi.addListener(self)
}

func openOutputs () {
for uid in midi.destinationUIDs {
midi.openOutput(uid: uid)
}
for uid in midi.virtualOutputUIDs {
midi.openOutput(uid: uid)
}
}

struct PortDescription {
var UID: String
var manufacturer: String
Expand Down

0 comments on commit 9c93d18

Please sign in to comment.