Skip to content

Instantly share code, notes, and snippets.

@jranson
Created December 21, 2020 19:17
Show Gist options
  • Save jranson/32f3f0fbd52c0f16167b2f8afcb1e7f5 to your computer and use it in GitHub Desktop.
Save jranson/32f3f0fbd52c0f16167b2f8afcb1e7f5 to your computer and use it in GitHub Desktop.
set MIDIPacket data from an array
//
// packet.swift
//
// As CoreMIDI/MIDIPacket exposes the 'data' member as a Tuple in Swift, this
// solution accepts a varying size [UInt8] and returns the corresponding tuple.
// Usage is as follows:
//
// packet.data = packetDataFromBytes(data: myArray)
//
// Created by James Ranson on 12/19/20.
//
import Foundation
let packetDataSize: Int = 256
// Swift exposes MIDIPacket.data as a 256-byte tuple. This accepts a command of any size,
// up to 256 bytes, and returns the corresponding tuple for constructing a packet
func packetDataFromBytes(data: [UInt8]) -> PacketData {
var padded: [UInt8]
if data.count == packetDataSize {
padded = data
} else {
padded = Array(repeating: UInt8(0), count: packetDataSize)
// take the provided data and put it into a fixed 256-byte 0-padded version
for i in 0..<data.count {
if i >= packetDataSize {
break
}
padded[i] = data[i]
}
}
// make a tuple from the padded array and return it
return padded.withUnsafeBytes {buf in
return buf.bindMemory(to: PacketData.self)[0]
}
}
typealias PacketData = (UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment