Skip to content

Commit

Permalink
Made MIDI.Event.sysEx(rawBytes:group:) public access level
Browse files Browse the repository at this point in the history
  • Loading branch information
orchetect committed Nov 5, 2021
1 parent 96e13e9 commit b4c3e9e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import Foundation

extension MIDI.Event {

/// Parse a raw System Exclusive message and return a `.sysEx()` or `.universalSysEx()` case if successful.
///
/// - Throws: `MIDI.Event.ParseError` if message is malformed.
@inline(__always)
internal static func sysEx(
from rawBytes: [MIDI.Byte],
public static func sysEx(
rawBytes: [MIDI.Byte],
group: MIDI.UInt4 = 0
) throws -> Self {

Expand Down
2 changes: 1 addition & 1 deletion Sources/MIDIKit/Parser/MIDI1Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ extension MIDI {
case 0xF: // system message
switch statusByte.nibbles.low {
case 0x0: // System Common - SysEx Start
guard let parsedSysEx = try? MIDI.Event.sysEx(from: bytes)
guard let parsedSysEx = try? MIDI.Event.sysEx(rawBytes: bytes)
else { return events }

events.append(parsedSysEx)
Expand Down
2 changes: 1 addition & 1 deletion Sources/MIDIKit/Parser/MIDI2Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ extension MIDI {
]

guard let parsedSysEx = try? MIDI.Event.sysEx(
from: [0xF0] + payloadBytes + [0xF7],
rawBytes: [0xF0] + payloadBytes + [0xF7],
group: group
)
else { return nil }
Expand Down
40 changes: 20 additions & 20 deletions Tests/MIDIKitTests/Events/Event/SysEx/SysEx Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ class SysExTests: XCTestCase {
let sourceRawBytes: [MIDI.Byte] = [0xF0, 0x41, 0x01, 0x34, 0xF7]

XCTAssertNoThrow(
try MIDI.Event.sysEx(from: sourceRawBytes)
try MIDI.Event.sysEx(rawBytes: sourceRawBytes)
)

let event = try! MIDI.Event.sysEx(from: sourceRawBytes)
let event = try! MIDI.Event.sysEx(rawBytes: sourceRawBytes)
guard case .sysEx(let innerEvent) = event
else { XCTFail() ; return }

Expand All @@ -38,10 +38,10 @@ class SysExTests: XCTestCase {
let sourceRawBytes: [MIDI.Byte] = [0xF0, 0x41, 0xF7]

XCTAssertNoThrow(
try MIDI.Event.sysEx(from: sourceRawBytes)
try MIDI.Event.sysEx(rawBytes: sourceRawBytes)
)

let event = try! MIDI.Event.sysEx(from: sourceRawBytes)
let event = try! MIDI.Event.sysEx(rawBytes: sourceRawBytes)
guard case .sysEx(let innerEvent) = event
else { XCTFail() ; return }

Expand All @@ -59,10 +59,10 @@ class SysExTests: XCTestCase {
let sourceRawBytes: [MIDI.Byte] = [0xF0, 0x41]

XCTAssertNoThrow(
try MIDI.Event.sysEx(from: sourceRawBytes)
try MIDI.Event.sysEx(rawBytes: sourceRawBytes)
)

let event = try! MIDI.Event.sysEx(from: sourceRawBytes)
let event = try! MIDI.Event.sysEx(rawBytes: sourceRawBytes)
guard case .sysEx(let innerEvent) = event
else { XCTFail() ; return }

Expand All @@ -80,7 +80,7 @@ class SysExTests: XCTestCase {
let sourceRawBytes: [MIDI.Byte] = [0xF0, 0xF7]

XCTAssertThrowsError(
try MIDI.Event.sysEx(from: sourceRawBytes)
try MIDI.Event.sysEx(rawBytes: sourceRawBytes)
)

}
Expand All @@ -90,15 +90,15 @@ class SysExTests: XCTestCase {
// valid - maximum byte length (256 bytes)
XCTAssertNoThrow(
try MIDI.Event.sysEx(
from: [0xF0, 0x41]
rawBytes: [0xF0, 0x41]
+ [MIDI.Byte](repeating: 0x20, count: 256-3)
+ [0xF7])
)

// valid - length is larger than default 256 bytes (257 bytes)
XCTAssertNoThrow(
try MIDI.Event.sysEx(
from: [0xF0, 0x41]
rawBytes: [0xF0, 0x41]
+ [MIDI.Byte](repeating: 0x20, count: 256-2)
+ [0xF7])
)
Expand All @@ -109,27 +109,27 @@ class SysExTests: XCTestCase {

// empty raw bytes - invalid
XCTAssertThrowsError(
try MIDI.Event.sysEx(from: [])
try MIDI.Event.sysEx(rawBytes: [])
)

// start byte only - invalid
XCTAssertThrowsError(
try MIDI.Event.sysEx(from: [0xF0])
try MIDI.Event.sysEx(rawBytes: [0xF0])
)

// end byte only - invalid
XCTAssertThrowsError(
try MIDI.Event.sysEx(from: [0xF7])
try MIDI.Event.sysEx(rawBytes: [0xF7])
)

// start and end bytes only - invalid
XCTAssertThrowsError(
try MIDI.Event.sysEx(from: [0xF0, 0xF7])
try MIDI.Event.sysEx(rawBytes: [0xF0, 0xF7])
)

// correct start byte, valid length, but incorrect end byte
XCTAssertThrowsError(
try MIDI.Event.sysEx(from: [0xF0, 0x41, 0x01, 0x34, 0xF6])
try MIDI.Event.sysEx(rawBytes: [0xF0, 0x41, 0x01, 0x34, 0xF6])
)

}
Expand All @@ -138,10 +138,10 @@ class SysExTests: XCTestCase {

// ensure instances equate correctly

let event1A = try! MIDI.Event.sysEx(from: [0xF0, 0x41, 0x01, 0x34, 0xF7])
let event1B = try! MIDI.Event.sysEx(from: [0xF0, 0x41, 0x01, 0x34, 0xF7])
let event1A = try! MIDI.Event.sysEx(rawBytes: [0xF0, 0x41, 0x01, 0x34, 0xF7])
let event1B = try! MIDI.Event.sysEx(rawBytes: [0xF0, 0x41, 0x01, 0x34, 0xF7])

let event2 = try! MIDI.Event.sysEx(from: [0xF0, 0x41, 0x01, 0x64, 0xF7])
let event2 = try! MIDI.Event.sysEx(rawBytes: [0xF0, 0x41, 0x01, 0x64, 0xF7])

XCTAssert(event1A == event1B)

Expand All @@ -153,10 +153,10 @@ class SysExTests: XCTestCase {

// ensure instances hash correctly

let event1A = try! MIDI.Event.sysEx(from: [0xF0, 0x41, 0x01, 0x34, 0xF7])
let event1B = try! MIDI.Event.sysEx(from: [0xF0, 0x41, 0x01, 0x34, 0xF7])
let event1A = try! MIDI.Event.sysEx(rawBytes: [0xF0, 0x41, 0x01, 0x34, 0xF7])
let event1B = try! MIDI.Event.sysEx(rawBytes: [0xF0, 0x41, 0x01, 0x34, 0xF7])

let event2 = try! MIDI.Event.sysEx(from: [0xF0, 0x41, 0x01, 0x64, 0xF7])
let event2 = try! MIDI.Event.sysEx(rawBytes: [0xF0, 0x41, 0x01, 0x64, 0xF7])

let set1: Set<MIDI.Event> = [event1A, event1B]

Expand Down

0 comments on commit b4c3e9e

Please sign in to comment.