I have successfully done the following in Swift:
- created an MIKMIDI sequencer
- added a track to the sequencer
- loaded a soundfont to that tracks's built-in synthesizer
- added MIDI events to the track
- triggered playback on the sequencer
Is is possible to select a preset in a multi-timbral sound font? Inspection of the availableInstruments property of the MIKMIDISynthesizer instance referenced by the sequencer track reports an empty array to the log console. Here's the code:
let _ = try sequence.addTrack()
let track = sequence.tracks[0]
let trackSynth = sequencer.builtinSynthesizerForTrack(track)
if let soundfont = NSBundle.mainBundle().URLForResource("fluid_gm", withExtension: "sf2") {
do {
try trackSynth?.loadSoundfontFromFileAtURL(soundfont)
print(trackSynth!.availableInstruments)
} catch {
}
}
let note1 = MIKMIDINoteEvent(timeStamp:0.0,note:60,velocity:100,duration:5,channel:0)
let note2 = MIKMIDINoteEvent(timeStamp:0.0,note:63,velocity:100,duration:5,channel:0)
let note3 = MIKMIDINoteEvent(timeStamp:1.0,note:60,velocity:0,duration:5,channel:0)
let note4 = MIKMIDINoteEvent(timeStamp:1.0,note:63,velocity:0,duration:5,channel:0)
track.addEvents([note1!,note2!,note3!,note4!])
sequencer.sequence = sequence
sequencer.startPlayback()