何时在MusicTrack中播放MIDINoteMessage? iOS版

时间:2015-06-01 12:40:59

标签: ios sequence midi audiotoolbox coremidi

我正在创建一个使用

成功播放的MIDI音乐的MusicTrack
  

MusicPlayerStart(sequencePlayer)

但是我想知道他们什么时候玩,所以我可以更新播放的每个MIDI Note的UI。

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

修改

更详细地展示我如何构建MIDI轨道。

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

}

使用此代码,我成功播放了4个音符的循环,设置了MusicSequenceSetMIDIEndpoint(musicSequence,virtualEndpoint),指定了read proc。我使用相同的代码,但加载一个MIDI文件和读取proc它成功调用每个音符。

1 个答案:

答案 0 :(得分:1)

使用MIDIDestinationCreate创建虚拟端点,然后调用MusicSequenceSetMIDIEndpoint将序列连接到它。您指定的读取过程将在每个事件上调用。