Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Playing midi files with MusicPlayer & Music Sequence

I've successfully gotten iOS to play a .mid (midi) file with a soundfont sample using the following code:

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

However, the problem comes when I then try to play a second file when the first is still playing.

I've tried many variations. Firstly, the above code will play both tracks simultaneously. Or, I've tried:

DisposeMusicPlayer(musicPlayer);
DisposeMusicSequence(musicSequence);

Before the NewMusicPlayer(&musicPlayer), but this produces a weird version of the tune with only sporadic notes being played.

I'd love to simply call this method, and the next track to be played.

like image 505
ilikejames Avatar asked Apr 20 '12 15:04

ilikejames


People also ask

How to play MIDI files on your computer?

MIDI is commonly used for playing, editing and recording music. Sadly, MIDI files are not compatible with all media players. To play MIDI files on your computer, you’d better use a MIDI player. Here offers you the top 5 best MIDI players.

What is the best MIDI player for Windows?

Sweet MIDI Player - A MIDI player for both Windows and Mac. Sweet MIDI Player is a MIDI audio player that can not only audition all types of MIDI files, but also modify MIDI files themselves. It can help you easily edit the control messages, transpose the music, change the tempo, mute desired MIDI channels and save the end results to disk.

Why does Windows Media Player not play MIDI?

When Windows Media Player does not play MIDI, you need to check the following conditions: The sound card is not installed properly. A MIDI driver is not installed. The MIDI Mapper is set incorrectly. Here is a page for solving Windows Media Player playback issue. 2. Winamp Winamp is a free multimedia player made by Nullsoft.

How to play MIDI audio on QuickTime Pro?

In order to play MIDI audio on QuickTime, you just need to choose the MIDI file to add in the QuickTime Pro with choose File > Open File and select the MIDI file you want to play. 2. Apple's Logic Pro - More than a MIDI Player


1 Answers

Ok, I found the answer on how to properly dispose of a MusicPlayer and MusicSequence.

-(void) stop
{
   OSStatus result = noErr;

   result = MusicPlayerStop(musicPlayer);

   UInt32 trackCount;
   MusicSequenceGetTrackCount(musicSequence, &trackCount);

   MusicTrack track;
   for(int i=0;i<trackCount;i++)
   {
      MusicSequenceGetIndTrack (musicSequence, i, &track);
      result = MusicSequenceDisposeTrack(musicSequence, track);
   }

   result = DisposeMusicPlayer(musicPlayer);
   result = DisposeMusicSequence(musicSequence);
   result = DisposeAUGraph(_processingGraph);
}
like image 132
ilikejames Avatar answered Sep 17 '22 11:09

ilikejames