I’m using CoreAudio on iOS 5 to play MIDI files. I have everything setup, it works, everything’s peachy, except when I want to start over the sequence from the start during the playback.
To do that I naturally call:
if (MusicPlayerSetTime(musicPlayer, (MusicTimeStamp)0.0) != noErr)
[NSException raise:@"playMIDI" format:@"Can't reset the player"];
I don’t get any error, but making this call stops the playback altogether, instead of (as is said in the documentation) continuing playing from the new position.
I have tried this also (and many other combinations of the same type):
Boolean isPlaying = NO;
if (MusicPlayerIsPlaying(musicPlayer, &isPlaying) != noErr)
[NSException raise:@"playMIDI:" format:@"Can't get MusicPlayer play state"];
if (isPlaying) {
if (MusicPlayerStop(musicPlayer) != noErr)
[NSException raise:@"playMIDI:" format:@"Can't stop MusicPlayer"];
}
if (MusicPlayerSetSequence(musicPlayer, musicSequence) != noErr)
[NSException raise:@"playMIDI:" format:@"Can't set sequence for MusicPlayer"];
if (MusicPlayerSetTime(musicPlayer, (MusicTimeStamp)0.0) != noErr)
[NSException raise:@"playMIDI" format:@"Can't reset the player"];
if (MusicPlayerSetPlayRateScalar(musicPlayer, 1.0) != noErr)
[NSException raise:@"playMIDI" format:@"Can't set speed"];
if (MusicPlayerStart(musicPlayer) != noErr)
[NSException raise:@"playMIDI" format:@"Can't start MusicPlayer"];
No joy, same effect.
If I change the MusicSequence however, the new MusicSequence plays fine. So I’ve gone to the extreme of creating two MusicSequence from the same MIDI file and switching from one to the other. No joy, iOS knows I want to play the same file twice and forbids it. I have to wait until the sequence is completely played before I can play it again. Bewildering.
Any idea?
Thanks
OK, here’s what I think is happening: Core Audio is pull-based, that is, when a unit needs data, it requests it from its source. Now when I reset the time on a MusicPlayer, it’s done at a certain time, say t0. Shortly after, at time t1, the AU Node fed by this MusicPlayer asks for data. The MusicPlayer takes t1 and computes where it falls in its sequence of events. Since it is invariably greater than t0, any event that starts exactly at t0 (e.g. events that starts at MIDI tick 0) are considered played and are thus not played at all. In other words, the first note(s) of the midi files is/are not played.
Now, since my files are very short and in fact contain only a chord starting at tick 0, none of the notes are played and I get silence.
Why this isn’t happening when I load a new sequence or start a sequence when it is not already playing is not clear to me…
Anyway, one possible fix is to: stop the player, set the sequence, set the time to 0 then delay the start of the player by e.g. 25ms. That is not ideal for responsiveness, but it works.
A slightly more convoluted way to fix this is the following:
– set up a callback on render on the audio unit the music player is feeding (AudioUnitAddRenderNotify())
– at the time of the reset of the sequence, don’t reset the time on the player but set a boolean flag
– in the render callback, if the flag is set, reset the time on the player and unset the flag
There is no delay introduced and it also works, which tends to validate my interpretation of the problem.