Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to use customed sf2 to play midi file? #328

Open
catiga opened this issue Feb 18, 2022 · 1 comment
Open

how to use customed sf2 to play midi file? #328

catiga opened this issue Feb 18, 2022 · 1 comment
Assignees
Labels

Comments

@catiga
Copy link

catiga commented Feb 18, 2022

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);
}

NSURL *midifile = [[NSBundle mainBundle] URLForResource:@"loop" withExtension:@"mid"];
MIKMIDISequence *seq = [[MIKMIDISequence alloc] initWithFileAtURL:midifile error:nil];
NSArray* alltracks = [seq tracks];

MIKMIDISequencer *seqr = [[MIKMIDISequencer alloc] initWithSequence:seq];
if(!seqr.isPlaying) {
    NSLog(@"prepare to start play");
}
for(int i=0; i<alltracks.count; i++) {
    MIKMIDITrack* track = alltracks[i];
    MIKMIDISynthesizer *synth = [seqr builtinSynthesizerForTrack:track];
    
    //[synth scheduleMIDICommands:<#(nonnull NSArray<MIKMIDICommand *> *)#>]
    
    NSLog(@"track done");
}
[synthesizer ]
[seqr setCreateSynthsIfNeeded:TRUE];
[seqr shouldCreateSynthsIfNeeded]
[seqr startPlayback];

midi could play but not to use aimed target sf2, anyone know how to handle it?

@armadsen armadsen self-assigned this Feb 22, 2022
@timothystulman
Copy link

timothystulman commented May 19, 2022

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)

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants