I’m looking for a way to mix 2 or more midi files, each with their own sound font files. I’ve found the following code for one file and tried to do multiple music players but i guess that shouldn’t be the right approach. Also i get some weird pop sound every second.
So is there any other way, maybe without the musicplayer and musicsequence methods, using only au units?
Here’s the code i found in another thread:
-(void) playMusic:(NSString*) name
{
NSString *presetURLPath = [[NSBundle mainBundle] pathForResource:@"GortsMiniPianoJ1" ofType:@"SF2"];
NSURL * presetURL = [NSURL fileURLWithPath:presetURLPath];
[self loadFromDLSOrSoundFont: (NSURL *)presetURL withPatch: (int)3];
NSString *midiFilePath = [[NSBundle mainBundle] pathForResource:name ofType:@"mid"];
NSURL * midiFileURL = [NSURL fileURLWithPath:midiFilePath];
NewMusicPlayer(&musicPlayer);
if (NewMusicSequence(&musicSequence) != noErr)
{
[NSException raise:@"play" format:@"Can't create MusicSequence"];
}
if(MusicSequenceFileLoad(musicSequence, (CFURLRef)midiFileURL, 0, 0 != noErr))
{
[NSException raise:@"play" format:@"Can't load MusicSequence"];
}
MusicPlayerSetSequence(musicPlayer, musicSequence);
MusicSequenceSetAUGraph(musicSequence, _processingGraph);
MusicPlayerPreroll(musicPlayer);
MusicPlayerStart(musicPlayer);
}
Multiple MusicPlayer instances sounds a bit awkward (to keep in sync) and I have doubts that multiple AUGraphs would work. I haven’t tried it myself.
One approach using a sole instance of
MusicPlayerwould be to load the tracks from each midi file into a ‘master’ sequence. You can assign an AUSampler node (with unique soundFont) to each track (MusicTrackSetDestNode) and connect them all through anAUMixer. You would need to manage the tempos from each midi file (usingMusicTrackNewExtendedTempoEvent) to assign file tempos to the relevant tracks. Muting tracks + track volume is easily handled at the track or mixer level.I guess much depends on the nature of the midi files you are working with. Fwiw – I did lots of research on midi file playback on iOS and there is nothing out there that is as easy to use as MusicPlayer. However, the Bass lib might be worth looking at if MusicPlayer turns out not to work for you.
Specifying the number of mixer inputs: