Skip to content

Commit

Permalink
Merge pull request #30 from orchetect/dev
Browse files Browse the repository at this point in the history
Fixed crash on macOS Big Sur and above
  • Loading branch information
orchetect authored Aug 12, 2021
2 parents 2a26c8d + 5b53a27 commit f11fba9
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 178 deletions.
12 changes: 4 additions & 8 deletions Examples/MIDISystemInfo/MIDISystemInfo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,27 @@
/* Begin PBXBuildFile section */
E205B44425E86AA7001A07D1 /* WebKitView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E205B44325E86AA7001A07D1 /* WebKitView.swift */; };
E230C4A325AE7B31005DCB55 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E230C4A225AE7B31005DCB55 /* AppDelegate.swift */; };
E230C4A525AE7B31005DCB55 /* ContentViewCatalina.swift in Sources */ = {isa = PBXBuildFile; fileRef = E230C4A425AE7B31005DCB55 /* ContentViewCatalina.swift */; };
E230C4A525AE7B31005DCB55 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E230C4A425AE7B31005DCB55 /* ContentView.swift */; };
E230C4AD25AE7B33005DCB55 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = E230C4AB25AE7B33005DCB55 /* Main.storyboard */; };
E24D200525BE75A90095BDE5 /* OTCore in Frameworks */ = {isa = PBXBuildFile; productRef = E24D200425BE75A90095BDE5 /* OTCore */; };
E24D200B25BE75D90095BDE5 /* MIDIKit in Frameworks */ = {isa = PBXBuildFile; productRef = E24D200A25BE75D90095BDE5 /* MIDIKit */; };
E26B849C25EDA4F400080052 /* DetailsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E26B849B25EDA4F400080052 /* DetailsView.swift */; };
E293070425E90D750011904C /* HostingWindowKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = E293070325E90D750011904C /* HostingWindowKey.swift */; };
E2AC6C7B25EDF96C008A4558 /* ContentViewManual.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2AC6C7A25EDF96C008A4558 /* ContentViewManual.swift */; };
E2FFD48325EDAF8700396632 /* ContentViewBigSur.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2FFD48225EDAF8700396632 /* ContentViewBigSur.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
E205B44325E86AA7001A07D1 /* WebKitView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebKitView.swift; sourceTree = "<group>"; };
E230C49F25AE7B31005DCB55 /* MIDISystemInfo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MIDISystemInfo.app; sourceTree = BUILT_PRODUCTS_DIR; };
E230C4A225AE7B31005DCB55 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
E230C4A425AE7B31005DCB55 /* ContentViewCatalina.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentViewCatalina.swift; sourceTree = "<group>"; };
E230C4A425AE7B31005DCB55 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
E230C4AC25AE7B33005DCB55 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
E230C4AE25AE7B33005DCB55 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
E230C4AF25AE7B33005DCB55 /* MIDISystemInfo.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = MIDISystemInfo.entitlements; sourceTree = "<group>"; };
E26B849B25EDA4F400080052 /* DetailsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DetailsView.swift; sourceTree = "<group>"; };
E293070325E90D750011904C /* HostingWindowKey.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HostingWindowKey.swift; sourceTree = "<group>"; };
E2AC6C7A25EDF96C008A4558 /* ContentViewManual.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ContentViewManual.swift; sourceTree = "<group>"; };
E2BE8483268BCA570034F62C /* MIDIKit */ = {isa = PBXFileReference; lastKnownFileType = folder; name = MIDIKit; path = ../..; sourceTree = "<group>"; };
E2FFD48225EDAF8700396632 /* ContentViewBigSur.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentViewBigSur.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -82,9 +80,8 @@
E2AC6C7925EDF8FF008A4558 /* ContentView */ = {
isa = PBXGroup;
children = (
E230C4A425AE7B31005DCB55 /* ContentViewCatalina.swift */,
E230C4A425AE7B31005DCB55 /* ContentView.swift */,
E2AC6C7A25EDF96C008A4558 /* ContentViewManual.swift */,
E2FFD48225EDAF8700396632 /* ContentViewBigSur.swift */,
);
path = ContentView;
sourceTree = "<group>";
Expand Down Expand Up @@ -165,9 +162,8 @@
buildActionMask = 2147483647;
files = (
E205B44425E86AA7001A07D1 /* WebKitView.swift in Sources */,
E2FFD48325EDAF8700396632 /* ContentViewBigSur.swift in Sources */,
E26B849C25EDA4F400080052 /* DetailsView.swift in Sources */,
E230C4A525AE7B31005DCB55 /* ContentViewCatalina.swift in Sources */,
E230C4A525AE7B31005DCB55 /* ContentView.swift in Sources */,
E293070425E90D750011904C /* HostingWindowKey.swift in Sources */,
E230C4A325AE7B31005DCB55 /* AppDelegate.swift in Sources */,
E2AC6C7B25EDF96C008A4558 /* ContentViewManual.swift in Sources */,
Expand Down
90 changes: 38 additions & 52 deletions Examples/MIDISystemInfo/MIDISystemInfo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,61 +23,47 @@ class AppDelegate: NSObject, NSApplicationDelegate {
defaultSubsystem: Globals.bundle.bundleID,
useEmoji: .all)

// set up midi manager

midiManager = {
let newManager =
MIDI.IO.Manager(clientName: "MIDISystemInfo",
model: "TestApp",
manufacturer: "Orchetect")
do {
Log.debug("Starting MIDI manager client")
try newManager.start()
} catch {
Log.default(error)
}

return newManager
}()

// Create the window and set the content view.
window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 950, height: 850),
styleMask: [.titled, .miniaturizable, .resizable],
backing: .buffered, defer: false)

// Create the SwiftUI view that provides the window contents.
window.isReleasedWhenClosed = false

window.center()
window.setFrameAutosaveName("Main Window")

window.contentView = {
if #available(macOS 11.0, *) {

// for Big Sur, demonstrate using @StateObject to hold the MIDI.IO.Manager in a subordinate scope and not in AppDelegate
window.title = "MIDIKit Test Harness (Big Sur)"
return NSHostingView(
rootView: ContentViewBigSur()
.environment(\.hostingWindow, { [weak window] in window })
)

} else {

// for Catalina, since @StateObject is not available for use, demonstrate storing the MIDI.IO.Manager instance in AppDelegate and passing it by reference into ContentView's weak @ObservedObject storage.
midiManager = {
let newManager =
MIDI.IO.Manager(clientName: "MIDISystemInfo",
model: "TestApp",
manufacturer: "Orchetect")
do {
Log.debug("Starting MIDI manager client")
try newManager.start()
} catch {
Log.default(error)
}

return newManager
}()

window.title = "MIDIKit Test Harness (Catalina)"
return NSHostingView(
rootView: ContentViewCatalina(midiManager: midiManager!)
.environment(\.hostingWindow, { [weak window] in window })
)

}
}()

window.makeKeyAndOrderFront(nil)

}

func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
backing: .buffered, defer: false)

// Create the SwiftUI view that provides the window contents.
window.isReleasedWhenClosed = false

window.center()
window.setFrameAutosaveName("Main Window")

window.title = "MIDIKit Test Harness (Catalina)"
window.contentView = NSHostingView(
rootView: ContentViewCatalina(midiManager: midiManager!)
.environment(\.hostingWindow, { [weak window] in window })
)

window.makeKeyAndOrderFront(nil)

}

func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ struct ContentViewManual: View {

ZStack { }
.onAppear {
// onAppear has the potential to trigger more than once during the lifecycle of the view, so it's not best practise to put setup code here
// however it won't have any dire consequences if this onAppear block runs more than once, and
// we don't care too much that this setup code may happen lazily while the rest of the view loads
// onAppear has the potential to trigger more than once
// during the lifecycle of the view, so it's not best
// practise to put setup code here. however it won't have
// any dire consequences if this onAppear block runs more
// than once, and we don't care too much that this setup
// code may happen lazily while the rest of the view loads

// set up MIDI manager notifications callback handler
// so we can be notified when MIDI endpoints in the system change,
Expand Down Expand Up @@ -76,7 +79,7 @@ struct ContentViewManual: View {

}

Section(header: Text("MIDI Outputs")) {
Section(header: Text("MIDI Output Endpoints")) {

ForEach(outputs.sortedByName()) { item in
NavigationLink(destination: DetailsView(endpoint: item)) {
Expand All @@ -96,7 +99,7 @@ struct ContentViewManual: View {

}

Section(header: Text("MIDI Inputs")) {
Section(header: Text("MIDI Input Endpoints")) {

ForEach(inputs.sortedByName()) { item in
NavigationLink(destination: DetailsView(endpoint: item)) {
Expand Down
18 changes: 12 additions & 6 deletions Sources/MIDIKit/Packet/Packet UniversalPacketData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,21 @@ extension MIDI.Packet {
/// Flat array of raw bytes
@inline(__always) public var bytes: [MIDI.Byte] {

#warning("> this is dumb?")
var bytes: [MIDI.Byte] = []
bytes.reserveCapacity(4 * words.count)
words.forEach {
bytes.append(MIDI.Byte($0 & 0xFF000000) >> (6*4))
bytes.append(MIDI.Byte($0 & 0x00FF0000) >> (4*4))
bytes.append(MIDI.Byte($0 & 0x0000FF00) >> (2*4))
bytes.append(MIDI.Byte($0 & 0x000000FF))

words.forEach { word in
let byte1 = MIDI.Byte((word & 0xFF000000) >> 24)
let byte2 = MIDI.Byte((word & 0x00FF0000) >> 16)
let byte3 = MIDI.Byte((word & 0x0000FF00) >> 8)
let byte4 = MIDI.Byte(word & 0x000000FF)

bytes.append(byte1)
bytes.append(byte2)
bytes.append(byte3)
bytes.append(byte4)
}

return bytes

}
Expand Down

0 comments on commit f11fba9

Please sign in to comment.