Open catiga opened 2 years ago
This is probably a no-brainer, but left me scratching my head for a minute...
builtinSynthesizer will return nil if the given track is being sent to a command scheduler.
`
let midiFileURL = Bundle.main.url(forResource: self.fileName, withExtension: "mid")! sequence = try! MIKMIDISequence(fileAt: midiFileURL)
let globalTrack = sequence.tracks[0]
let userTrack = sequence.tracks[1]
let keysTrack = sequence.tracks[2]
let accompanimentTrack = sequence.tracks[3]
let clickTrack = sequence.tracks[4]
globalTrack.isMuted = true
clickTrack.isMuted = !midiManager.metroEnabled
sequencer.sequence = sequence
sequencer.setCommandScheduler(self, for: keysTrack)
var i = 0
for track in sequencer.sequence.tracks {
do{
let url = SynthManager.soundFontURL
//will return nil on keysTrack
let seq = sequencer.builtinSynthesizer(for: track)!
try seq.loadSoundfontFromFile(at: url!)
}catch{
print("error on track \(i)")
}
}
` This works, though...
`let midiFileURL = Bundle.main.url(forResource: self.fileName, withExtension: "mid")! sequence = try! MIKMIDISequence(fileAt: midiFileURL)
let globalTrack = sequence.tracks[0]
let userTrack = sequence.tracks[1]
let keysTrack = sequence.tracks[2]
let accompanimentTrack = sequence.tracks[3]
let clickTrack = sequence.tracks[4]
globalTrack.isMuted = true
clickTrack.isMuted = !midiManager.metroEnabled
sequencer.sequence = sequence
sequencer.setCommandScheduler(self, for: keysTrack)
let url = SynthManager.soundFontURL!
let seq1 = sequencer.builtinSynthesizer(for: userTrack)!
try! seq1.loadSoundfontFromFile(at: url)
let seq2 = sequencer.builtinSynthesizer(for: userTrack)!
try! seq2.loadSoundfontFromFile(at: url)
`
test code is below:
MIKMIDISynthesizer synthesizer = [[MIKMIDISynthesizer alloc] init]; NSURL soundfont = [[NSBundle mainBundle] URLForResource:@"FancyNode" withExtension:@"sf2"]; NSError *error = nil; if (![synthesizer loadSoundfontFromFileAtURL:soundfont error:&error]) { NSLog(@"Error loading soundfont for synthesizer. Sound will be degraded. %@", error); }
midi could play but not to use aimed target sf2, anyone know how to handle it?