On Sep 9, 2008, at 21:24 , Evan Laforge wrote: Hi, new to mac programming here.
Could someone explain the behaviour of the MIDIFlushOutput function? I'm seeing something strange. It looks like scheduled note-offs are retained, but played right after the flush, and *sometimes* a pitch bend becomes a pitchbend to 0 (i.e. lowest position). The following program reproduces this on my machine (System 10.5.4):
#include <CoreAudio/HostTime.h> #include <CoreMIDI/CoreMIDI.h>
MIDIClientRef client; MIDIPortRef port; MIDIEndpointRef dest; MIDITimeStamp start;
void send(double offset, Byte b0, Byte b1, Byte b2) { Byte bytes[3] = {b0, b1, b2}; MIDIPacketList packets; MIDIPacket *packet = MIDIPacketListInit(&packets); MIDITimeStamp ts = start + offset * 1000000;
I don't know where you're getting the idea that time stamp ticks are 1/100000ths of a second. The right constant is CAHostTimeBase::GetFrequency().
MIDIPacketListAdd(&packets, sizeof packets, packet, ts, 3, bytes); MIDISend(port, dest, &packets); }
int main(int argc, char **argv) { MIDIClientCreate(CFSTR("core midi"), NULL, NULL, &client); MIDIOutputPortCreate(client, CFSTR("output port"), &port); dest = MIDIGetDestination(0); start = AudioGetCurrentHostTime();
send(0, 144, 53, 70); send(400, 128, 53, 70); send(500, 144, 55, 70); send(900, 128, 55, 70); send(1000, 144, 57, 70); send(1000, 224, 102, 76); // pitchbend send(1400, 128, 57, 70); usleep(500000); MIDIFlushOutput(NULL); sleep(3); return 0; }
When run, here's what a separate midi monitor shows:
(175987598,"IAC Driver IAC Bus 1",ChannelMessage 0 (NoteOn 53 70)) (175987998,"IAC Driver IAC Bus 1",ChannelMessage 0 (NoteOff 53 70)) (175988098,"IAC Driver IAC Bus 1",ChannelMessage 0 (NoteOn 55 70)) (175988099,"IAC Driver IAC Bus 1",ChannelMessage 0 (NoteOff 55 64)) (175988099,"IAC Driver IAC Bus 1",ChannelMessage 0 (NoteOff 57 64)) (175988099,"IAC Driver IAC Bus 1",ChannelMessage 0 (PitchBend (-1.0)))
Notice how I got an extra NoteOff 57, even though the NoteOn was descheduled by the flush. And more worryingly, the PitchBend -1, which screws up synth state. Obviously, this is over IAC, I haven't tried an external port yet.
The extra note-off is because flush is protecting itself against a race and generating a note-off for a scheduled note-on without regard for whether the note-on has really been played.
Thanks for catching a bug about pitch bend; CoreMIDI is stupidly sending 0xEn 0x00 0x00 instead 0xEn 0x00 0x40 to reset it to the center.
Doug
-- Doug Wyatt Core Audio, Apple
|