Skip to content

Commit 855b9ec

Browse files
committed
MIDI.Event.SysEx7: Added .midi1RawHexString() method
1 parent cafbca1 commit 855b9ec

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

Sources/MIDIKit/Events/Event/System Exclusive/SysEx7/SysEx7.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,34 @@ extension MIDI.Event {
6464

6565
}
6666

67+
extension MIDI.Event.SysEx7 {
68+
69+
/// Returns the raw MIDI 1.0 message bytes that comprise the event as a human-readable string of hex characters.
70+
///
71+
/// By default the string is returned separated by spaces (ie: `"F7 01 02 03 F0"`).
72+
///
73+
/// A custom separator may be used or pass `nil` for no separator (ie: `"F7010203F0"`).
74+
public func midi1RawHexString(
75+
leadingF0: Bool = true,
76+
trailingF7: Bool = true,
77+
separator: String? = " "
78+
) -> String {
79+
80+
let bytes = midi1RawBytes(leadingF0: leadingF0,
81+
trailingF7: trailingF7)
82+
83+
if let separator = separator {
84+
return bytes.hex.stringValues(padTo: 2, prefixes: false)
85+
.joined(separator: separator)
86+
} else {
87+
return bytes.hex.stringValues(padTo: 2, prefixes: false)
88+
.joined()
89+
}
90+
91+
}
92+
93+
}
94+
6795
extension MIDI.Event.SysEx7 {
6896

6997
/// Returns the raw MIDI 1.0 message bytes that comprise the event.

Tests/MIDIKitTests/Events/Event/SysEx/SysEx7 Tests.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,32 @@ final class SysEx7Tests: XCTestCase {
188188

189189
}
190190

191+
func testSysEx7_midi1RawHexString() throws {
192+
193+
let sysEx = MIDI.Event.SysEx7(manufacturer: .oneByte(0x41), data: [0x01, 0x34])
194+
195+
XCTAssertEqual(
196+
sysEx.midi1RawHexString(),
197+
"F0 41 01 34 F7"
198+
)
199+
200+
XCTAssertEqual(
201+
sysEx.midi1RawHexString(leadingF0: false, trailingF7: false),
202+
"41 01 34"
203+
)
204+
205+
XCTAssertEqual(
206+
sysEx.midi1RawHexString(separator: nil),
207+
"F0410134F7"
208+
)
209+
210+
XCTAssertEqual(
211+
sysEx.midi1RawHexString(separator: ", "),
212+
"F0, 41, 01, 34, F7"
213+
)
214+
215+
}
216+
191217
func testUniversalSysEx7RawHexString_Typical() throws {
192218

193219
// space delimiter

0 commit comments

Comments
 (0)