Skip to content

Commit 1eb158b

Browse files
authored
Merge pull request #36 from orchetect/dev
API selection, fixes for packet iteration
2 parents 9f324fd + 93654c3 commit 1eb158b

File tree

56 files changed

+543
-375
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+543
-375
lines changed

Docs/MIDIKit.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
Docs will be added in future. This is a placeholder file in the meantime.
1+
Docs will be added in future.
2+
3+
In the meantime, check out the Examples folder for sample projects demonstrating usage.

Examples/MIDIEventLogger/MIDIEventLogger.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@
225225
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
226226
GCC_WARN_UNUSED_FUNCTION = YES;
227227
GCC_WARN_UNUSED_VARIABLE = YES;
228-
MACOSX_DEPLOYMENT_TARGET = 11.5;
228+
MACOSX_DEPLOYMENT_TARGET = 10.15;
229229
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
230230
MTL_FAST_MATH = YES;
231231
ONLY_ACTIVE_ARCH = YES;
@@ -280,7 +280,7 @@
280280
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
281281
GCC_WARN_UNUSED_FUNCTION = YES;
282282
GCC_WARN_UNUSED_VARIABLE = YES;
283-
MACOSX_DEPLOYMENT_TARGET = 11.5;
283+
MACOSX_DEPLOYMENT_TARGET = 10.15;
284284
MTL_ENABLE_DEBUG_INFO = NO;
285285
MTL_FAST_MATH = YES;
286286
SDKROOT = macosx;

Examples/MIDIEventLogger/MIDIEventLogger/ContentView SubViews.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ extension ContentView {
1717

1818
GroupBox(label: Text("MIDI Subsystem Status")) {
1919

20-
Text("Using "
21-
+ (midiManager.coreMIDIVersion == .legacy ? "legacy" : "new")
22-
+ " CoreMIDI API"
23-
)
20+
Text("Using " + midiManager.preferredAPI.description)
2421
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
2522

2623
}
@@ -51,7 +48,7 @@ extension ContentView {
5148
}
5249
}
5350
.frame(maxWidth: 200)
54-
.disabled(midiManager.coreMIDIVersion == .legacy)
51+
.disabled(midiManager.preferredAPI == .legacyCoreMIDI)
5552

5653
Spacer()
5754
.frame(height: 10)

Examples/MIDIEventLogger/MIDIEventLogger/ContentView.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ struct ContentView: View {
5050
try midiManager.addInput(
5151
name: kInputName,
5252
tag: kInputTag,
53-
uniqueID: .none,
53+
uniqueID: .userDefaultsManaged(key: kInputTag),
5454
receiveHandler: .eventsLogging()
5555
)
5656

5757
try midiManager.addOutput(
5858
name: kOutputName,
5959
tag: kOutputTag,
60-
uniqueID: .none
60+
uniqueID: .userDefaultsManaged(key: kOutputTag)
6161
)
6262
} catch {
6363
Log.error(error)
@@ -87,7 +87,7 @@ struct ContentView: View {
8787
.padding([.leading, .trailing])
8888

8989
.onAppear {
90-
// wait a short delay in order to give CoreMIDI time
90+
// wait a short delay in order to give Core MIDI time
9191
// to set up the virtual endpoints we created in the view's init()
9292
DispatchQueue.main
9393
.asyncAfter(deadline: DispatchTime.now()

Sources/MIDIKit/Common/Utilities/String.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import Foundation
1818
extension String {
1919

2020
/// Wraps a string with double-quotes (`"`)
21-
@inlinable internal var otcQuoted: Self {
21+
@inlinable internal var quoted: Self {
2222

2323
"\"\(self)\""
2424

@@ -37,7 +37,7 @@ extension StringProtocol {
3737
///
3838
/// "A string 123".only(.alphanumerics)`
3939
///
40-
public func otcOnly(_ characterSet: CharacterSet) -> String {
40+
public func only(_ characterSet: CharacterSet) -> String {
4141

4242
self.map { characterSet.contains(UnicodeScalar("\($0)")!) ? "\($0)" : "" }
4343
.joined()
@@ -46,23 +46,23 @@ extension StringProtocol {
4646

4747
/// **OTCore:**
4848
/// Returns a string preserving only characters from the passed string and removing all other characters.
49-
public func otcOnly(characters: String) -> String {
49+
public func only(characters: String) -> String {
5050

51-
self.otcOnly(CharacterSet(charactersIn: characters))
51+
self.only(CharacterSet(charactersIn: characters))
5252

5353
}
5454

5555
/// **OTCore:**
5656
/// Returns a string containing only alphanumeric characters and removing all other characters.
57-
public var otcOnlyAlphanumerics: String {
57+
public var onlyAlphanumerics: String {
5858

59-
self.otcOnly(.alphanumerics)
59+
self.only(.alphanumerics)
6060

6161
}
6262

6363
/// **OTCore:**
6464
/// Returns a string removing all characters from the passed CharacterSet.
65-
public func otcRemoving(_ characterSet: CharacterSet) -> String {
65+
public func removing(_ characterSet: CharacterSet) -> String {
6666

6767
self.components(separatedBy: characterSet)
6868
.joined()
@@ -71,7 +71,7 @@ extension StringProtocol {
7171

7272
/// **OTCore:**
7373
/// Returns a string removing all characters from the passed string.
74-
public func otcRemoving(characters: String) -> String {
74+
public func removing(characters: String) -> String {
7575

7676
self.components(separatedBy: CharacterSet(charactersIn: characters))
7777
.joined()
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
//
2+
// APIVersion.swift
3+
// MIDIKit • https://github.com/orchetect/MIDIKit
4+
//
5+
6+
import Darwin
7+
8+
extension MIDI.IO {
9+
10+
/// Enum describing which underlying Core MIDI API is being used internally.
11+
public enum APIVersion {
12+
13+
/// Legacy Core MIDI API first introduced in early versions of OSX.
14+
///
15+
/// Internally using `MIDIPacketList` / `MIDIPacket`.
16+
case legacyCoreMIDI
17+
18+
/// New Core MIDI API introduced in macOS 11, iOS 14, macCatalyst 14, tvOS 14, and watchOS 7.
19+
///
20+
/// Internally using `MIDIEventList` / `MIDIEventPacket`.
21+
case newCoreMIDI
22+
23+
}
24+
25+
}
26+
27+
extension MIDI.IO.APIVersion {
28+
29+
/// Returns the recommended API version for the current platform (operating system).
30+
public static func bestForPlatform() -> Self {
31+
32+
if #available(macOS 11, iOS 14, macCatalyst 14, tvOS 14, watchOS 7, *) {
33+
// return legacy for now, since new API is buggy;
34+
// in future, this should return .newCoreMIDI when new API is more stable
35+
return .legacyCoreMIDI
36+
37+
} else {
38+
return .legacyCoreMIDI
39+
}
40+
41+
}
42+
43+
}
44+
45+
extension MIDI.IO.APIVersion {
46+
47+
/// Returns true if API version can be used on the current platform (operating system).
48+
public var isValidOnCurrentPlatform: Bool {
49+
50+
switch self {
51+
case .legacyCoreMIDI:
52+
#if os(macOS)
53+
if #available(macOS 12, *) { return false }
54+
return true
55+
#elseif os(iOS)
56+
if #available(iOS 15, *) { return false }
57+
return true
58+
#elseif os(tvOS) || os(watchOS)
59+
// only new API is supported on tvOS and watchOS
60+
return false
61+
#else
62+
// future or unknown/unsupported platform
63+
return false
64+
#endif
65+
66+
case .newCoreMIDI:
67+
if #available(macOS 11, iOS 14, macCatalyst 14, tvOS 14, watchOS 7, *) {
68+
return true
69+
}
70+
71+
return false
72+
}
73+
74+
}
75+
76+
}
77+
78+
extension MIDI.IO.APIVersion: CustomStringConvertible {
79+
80+
public var description: String {
81+
82+
switch self {
83+
case .legacyCoreMIDI:
84+
return "Legacy Core MIDI API"
85+
86+
case .newCoreMIDI:
87+
return "New Core MIDI API"
88+
}
89+
90+
}
91+
92+
}

Sources/MIDIKit/IO/API/CoreMIDIVersion.swift

Lines changed: 0 additions & 23 deletions
This file was deleted.

Sources/MIDIKit/IO/API/ProtocolVersion.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
// MIDIKit • https://github.com/orchetect/MIDIKit
44
//
55

6-
extension MIDI {
6+
extension MIDI.IO {
77

88
#warning("> this is currently unused")
99

10-
/// Enum describing which underlying CoreMIDI API is being used internally.
10+
/// MIDI protocol version.
1111
public enum ProtocolVersion {
1212

1313
/// MIDI 1.0

Sources/MIDIKit/IO/CoreMIDI/CoreMIDI Connections.swift renamed to Sources/MIDIKit/IO/Core MIDI/Core MIDI Connections.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
//
2-
// CoreMIDI Connections.swift
2+
// Core MIDI Connections.swift
33
// MIDIKit • https://github.com/orchetect/MIDIKit
44
//
55

66
import CoreMIDI
77

88
extension MIDI.IO {
99

10-
/// Queries CoreMIDI for existing persistent play-thru connections stored in the system matching the specified persistent owner ID.
10+
/// Queries Core MIDI for existing persistent play-thru connections stored in the system matching the specified persistent owner ID.
1111
///
1212
/// To delete them all, see sister function `removeAllSystemThruConnectionsPersistentEntries(:)`.
1313
///
@@ -52,7 +52,7 @@ extension MIDI.IO {
5252

5353
}
5454

55-
/// Deletes all system-held CoreMIDI MIDI play-thru connections matching an owner ID.
55+
/// Deletes all system-held Core MIDI MIDI play-thru connections matching an owner ID.
5656
///
5757
/// - Parameter persistentOwnerID: reverse-DNS domain that was used when the connection was first made
5858
///

Sources/MIDIKit/IO/CoreMIDI/CoreMIDI Devices.swift renamed to Sources/MIDIKit/IO/Core MIDI/Core MIDI Devices.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// CoreMIDI Devices.swift
2+
// Core MIDI Devices.swift
33
// MIDIKit • https://github.com/orchetect/MIDIKit
44
//
55

Sources/MIDIKit/IO/CoreMIDI/CoreMIDI Endpoints.swift renamed to Sources/MIDIKit/IO/Core MIDI/Core MIDI Endpoints.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// CoreMIDI Endpoints.swift
2+
// Core MIDI Endpoints.swift
33
// MIDIKit • https://github.com/orchetect/MIDIKit
44
//
55

Sources/MIDIKit/IO/CoreMIDI/CoreMIDI Entities.swift renamed to Sources/MIDIKit/IO/Core MIDI/Core MIDI Entities.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// CoreMIDI Entities.swift
2+
// Core MIDI Entities.swift
33
// MIDIKit • https://github.com/orchetect/MIDIKit
44
//
55

Sources/MIDIKit/IO/CoreMIDI/CoreMIDI Properties Get.swift renamed to Sources/MIDIKit/IO/Core MIDI/Core MIDI Properties Get.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// CoreMIDI Properties Get.swift
2+
// Core MIDI Properties Get.swift
33
// MIDIKit • https://github.com/orchetect/MIDIKit
44
//
55

@@ -9,7 +9,7 @@ extension MIDI.IO {
99

1010
// MARK: - Property Readers
1111

12-
/// Retrieves the entire properties dictionary as the CoreMIDI-native `CFPropertyList`.
12+
/// Retrieves the entire properties dictionary as the Core MIDI-native `CFPropertyList`.
1313
///
1414
/// - Parameter deep: Returns nested results for all children if `True`.
1515
/// - Throws: `MIDI.IO.MIDIError`
@@ -83,7 +83,7 @@ extension MIDI.IO {
8383
guard let unwrappedVal = val?.takeRetainedValue() else {
8484
val?.release()
8585
throw MIDI.IO.MIDIError.readError(
86-
"Got nil while reading MIDIEndpointRef property value \((forProperty as String).otcQuoted)"
86+
"Got nil while reading MIDIEndpointRef property value \((forProperty as String).quoted)"
8787
)
8888
}
8989

@@ -514,7 +514,7 @@ extension MIDI.IO {
514514
// / / Entity
515515
// / / / Endpoint
516516
// / / / /
517-
// *|D|E|E| CoreMIDI Constant value type
517+
// *|D|E|E| Core MIDI Constant value type
518518
// -|-|-|-|------------------------------------------- ----------
519519
// | | | | Identification:
520520
// *|•|•|•| kMIDIPropertyName string

Sources/MIDIKit/IO/CoreMIDI/CoreMIDI Properties Set.swift renamed to Sources/MIDIKit/IO/Core MIDI/Core MIDI Properties Set.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// CoreMIDI Properties Set.swift
2+
// Core MIDI Properties Set.swift
33
// MIDIKit • https://github.com/orchetect/MIDIKit
44
//
55

Sources/MIDIKit/IO/CoreMIDI/MIDIEventList/MIDIEventList MKUnsafeSequence.swift renamed to Sources/MIDIKit/IO/Core MIDI/MIDIEventList/MIDIEventList MKUnsafeSequence.swift

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,38 @@ import CoreMIDI
88

99
extension UnsafePointer where Pointee == MIDIEventList {
1010

11-
/// MIDIKit backwards-compatible implementation of CoreMIDI's `MIDIPacketList.UnsafeSequence`
11+
/// MIDIKit sequence on `UnsafePointer<MIDIEventList>` to return `[MIDIEventPacket]`
1212
@available(macOS 11, iOS 14, macCatalyst 14, tvOS 14, watchOS 7, *)
13-
public func mkUnsafeSequence() -> MIDIEventList.MKUnsafeSequence {
13+
public func mkSequence() -> MIDIEventList.MKSequence {
1414

15-
MIDIEventList.MKUnsafeSequence(self)
15+
MIDIEventList.MKSequence(self)
1616

1717
}
1818

1919
}
2020

2121
extension MIDIEventList {
2222

23-
/// MIDIKit backwards-compatible implementation of CoreMIDI's `MIDIEventList.UnsafeSequence`
23+
/// MIDIKit sequence on `UnsafePointer<MIDIEventList>` to return `[MIDIEventPacket]`
2424
@available(macOS 11, iOS 14, macCatalyst 14, tvOS 14, watchOS 7, *)
25-
public struct MKUnsafeSequence: Sequence {
25+
public struct MKSequence: Sequence {
2626

27-
public typealias Element = UnsafePointer<MIDIEventPacket>
27+
public typealias Element = MIDIEventPacket
2828

29-
internal var pointers: [UnsafePointer<MIDIEventPacket>] = []
29+
internal var packets: [MIDIEventPacket] = []
3030

3131
public init(_ midiPacketListPtr: UnsafePointer<MIDIEventList>) {
3232

3333
CMIDIEventListIterate(midiPacketListPtr) {
34-
guard let unwrappedPtr = $0 else { return }
35-
pointers.append(unwrappedPtr)
34+
if let packet = $0?.pointee {
35+
packets.append(packet)
36+
}
3637
}
3738

3839
}
3940

4041
public func makeIterator() -> Iterator {
41-
Iterator(pointers)
42+
Iterator(packets)
4243
}
4344

4445
public struct Iterator: IteratorProtocol {

0 commit comments

Comments
 (0)