Files
Lyrebird/Sources/LyrebirdCLI/Resources/XcodeTemplates/LyrebirdMIDIHandler.xctemplate/___FILEBASENAME___.swift
T
2026-06-01 18:27:40 -07:00

93 lines
3.3 KiB
Swift

//___FILEHEADER___
import Foundation
import LyrebirdSynth
// MARK: - ___FILEBASENAMEASIDENTIFIER___
/// Factory that wires up CoreMIDI input for `engine` and returns a started
/// `MIDI` instance.
///
/// The handler closure runs on the `lyrebird.midi` dispatch queue — not the
/// RT audio thread. From here you can:
/// - Mutate `@LiveParam` slots on Synthesizers (atomic, RT-safe)
/// - Call `engine.add(_:)` / `engine.remove(_:)` (lock-free, applied at the
/// next block boundary)
/// - Start / stop / pause `LyrebirdTask`s
///
/// ## Usage
/// ```swift
/// let midi = try make___FILEBASENAMEASIDENTIFIER___(engine: engine)
/// // …
/// midi.stop() // before engine deallocates
/// ```
///
/// - Parameter engine: Running `SynthesisEngine` whose Synthesizers receive
/// MIDI-driven parameter changes.
/// - Returns: Started `MIDI` with one handler registered.
/// - Throws: `MIDIError.alreadyStarted` if `start()` is called twice.
public func make___FILEBASENAMEASIDENTIFIER___(
engine: SynthesisEngine,
clientName: String = "Lyrebird"
) throws -> MIDI {
let midi = MIDI(engine: engine, clientName: clientName)
_ = midi.addHandler(name: "___FILEBASENAMEASIDENTIFIER___") { message, _ in
switch message {
case .noteOn(let channel, let note, let velocity):
// velocity == 0 is a "running-status" note off — handle as note off.
if velocity == 0 {
handleNoteOff(channel: channel, note: note, velocity: 0)
} else {
handleNoteOn(channel: channel, note: note, velocity: velocity)
}
case .noteOff(let channel, let note, let velocity):
handleNoteOff(channel: channel, note: note, velocity: velocity)
case .controlChange(let channel, let controller, let value):
handleControlChange(channel: channel, controller: controller, value: value)
default:
// pitch bend, channel pressure, polyphonic aftertouch, program
// change, system messages — extend MIDIMessage handling here.
break
}
}
try midi.start()
return midi
}
// MARK: - Per-message handlers (replace bodies with your own)
private func handleNoteOn(channel: UInt8, note: UInt8, velocity: UInt8) {
// TODO: convert (note, velocity) to (Hz, amp) and spawn a voice synth, e.g.
//
// let synth = MyVoice()
// synth.frequency = midiNoteToHz(note)
// synth.amplitude = Double(velocity) / 127.0
// try engine.add(synth)
// voicesByNote[note] = synth
_ = (channel, note, velocity)
}
private func handleNoteOff(channel: UInt8, note: UInt8, velocity: UInt8) {
// TODO: release / remove the voice synth that handleNoteOn started.
_ = (channel, note, velocity)
}
private func handleControlChange(channel: UInt8, controller: UInt8, value: UInt8) {
// TODO: map CCs to Synthesizer @LiveParams. E.g.:
//
// switch controller {
// case 7: masterSynth.amp = Double(value) / 127.0 // CC7 = volume
// case 74: filterSynth.cutoff = ccToCutoff(value) // CC74 = brightness
// default: break
// }
_ = (channel, controller, value)
}
/// Standard MIDI note number → Hz using the 12-tone equal-tempered scale
/// anchored at A4 = 69 = 440 Hz.
private func midiNoteToHz(_ note: UInt8) -> Double {
440.0 * pow(2.0, (Double(note) - 69.0) / 12.0)
}