Search code examples
iossequencemidiaudiotoolboxcoremidi

When is a MIDINoteMessage played in a MusicTrack? iOS


I'm creating a MusicTrack of MIDI Notes that are successfully played using

MusicPlayerStart(sequencePlayer)

But I would like to know when are they played, so I can update the UI for each MIDI Note played.

// Creating MIDI Track
MusicTrack track;
UInt32 trackIndex = 0;
musicTrackAtIndex(trackIndex, &track);

//Adding Tempo
addTempoEvent(0.0, 120);

// Adding MIDI notes
const int channel = 0;
MIDINoteMessage note;
note.channel = channel;
note.velocity = 100.0;
note.duration = 0.3;
note.releaseVelocity = 0;

note.note = 68;
addMIDINoteToTrack(track, note, 1);

note.note = 78;
addMIDINoteToTrack(track, note, 2);

note.note = 88;
addMIDINoteToTrack(track, note, 3);

note.note = 98;
addMIDINoteToTrack(track, note, 4);

MusicTimeStamp trackEnd = 4;
addEndOfTrackAtTime(trackEnd);

setLoopTrack(true); 

playSequence();

EDIT

Showing in more detail how I'm building the MIDI Track.

-(void) playing4NotesInLoop {

MusicTrack musicTrack;
MusicSequence musicSequence;
MusicPlayer musicPlayer;

const int loop_length = 4;

NewMusicSequence(&musicSequence);

// Create a client
MIDIClientRef virtualMidi;
MIDIClientCreate(CFSTR("Virtual Client"),
                          MyMIDINotifyProc,
                          NULL,
                          &virtualMidi);


// Create an endpoint
MIDIEndpointRef virtualEndpoint;
MIDIDestinationCreate(virtualMidi, @"Virtual Destination", MyMIDIReadProc, self.samplerUnit, &virtualEndpoint);


MusicSequenceNewTrack(musicSequence, &musicTrack);

MIDINoteMessage aMessage;
aMessage.channel = 1;

aMessage.duration = 0.5f;
aMessage.velocity = 100;

for(int i=0; i<4; ++i)
{
    if (i==0) {
        aMessage.note = 30;
    }else {
        aMessage.note = 60;
    }

    MusicTrackNewMIDINoteEvent(musicTrack, i, &aMessage);
}

MusicTrackLoopInfo loop_info;
loop_info.loopDuration = loop_length;
loop_info.numberOfLoops = 0;

MusicTrackSetProperty(musicTrack, kSequenceTrackProperty_LoopInfo, &loop_info, sizeof(MusicTrackLoopInfo));

MusicTimeStamp track_length;
track_length = loop_length;

MusicTrackSetProperty(musicTrack, kSequenceTrackProperty_TrackLength, &track_length, sizeof(MusicTimeStamp));

MusicSequenceSetMIDIEndpoint(musicSequence, virtualEndpoint);

NewMusicPlayer(&musicPlayer);
MusicPlayerSetSequence(musicPlayer, musicSequence);

MusicPlayerStart(musicPlayer);

}

With this code I'm successfully playing a loop of 4 notes, a MusicSequenceSetMIDIEndpoint(musicSequence, virtualEndpoint) has been set up specifying the read proc. I used the same code but loading a MIDI file and the read proc it's successfully called for each note.


Solution

  • You create a virtual endpoint with MIDIDestinationCreate and then call MusicSequenceSetMIDIEndpoint to connect your sequence to it. The read proc that you specify will be called on each event.