Skip to content

Commit

Permalink
MIDI.Event.SysEx7: Added .midi1RawHexString() method
Browse files Browse the repository at this point in the history
  • Loading branch information
orchetect committed Jul 25, 2022
1 parent cafbca1 commit 855b9ec
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Sources/MIDIKit/Events/Event/System Exclusive/SysEx7/SysEx7.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,34 @@ extension MIDI.Event {

}

extension MIDI.Event.SysEx7 {

/// Returns the raw MIDI 1.0 message bytes that comprise the event as a human-readable string of hex characters.
///
/// By default the string is returned separated by spaces (ie: `"F7 01 02 03 F0"`).
///
/// A custom separator may be used or pass `nil` for no separator (ie: `"F7010203F0"`).
public func midi1RawHexString(
leadingF0: Bool = true,
trailingF7: Bool = true,
separator: String? = " "
) -> String {

let bytes = midi1RawBytes(leadingF0: leadingF0,
trailingF7: trailingF7)

if let separator = separator {
return bytes.hex.stringValues(padTo: 2, prefixes: false)
.joined(separator: separator)
} else {
return bytes.hex.stringValues(padTo: 2, prefixes: false)
.joined()
}

}

}

extension MIDI.Event.SysEx7 {

/// Returns the raw MIDI 1.0 message bytes that comprise the event.
Expand Down
26 changes: 26 additions & 0 deletions Tests/MIDIKitTests/Events/Event/SysEx/SysEx7 Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,32 @@ final class SysEx7Tests: XCTestCase {

}

func testSysEx7_midi1RawHexString() throws {

let sysEx = MIDI.Event.SysEx7(manufacturer: .oneByte(0x41), data: [0x01, 0x34])

XCTAssertEqual(
sysEx.midi1RawHexString(),
"F0 41 01 34 F7"
)

XCTAssertEqual(
sysEx.midi1RawHexString(leadingF0: false, trailingF7: false),
"41 01 34"
)

XCTAssertEqual(
sysEx.midi1RawHexString(separator: nil),
"F0410134F7"
)

XCTAssertEqual(
sysEx.midi1RawHexString(separator: ", "),
"F0, 41, 01, 34, F7"
)

}

func testUniversalSysEx7RawHexString_Typical() throws {

// space delimiter
Expand Down

0 comments on commit 855b9ec

Please sign in to comment.