Re: MIDISend & MIDIPacketList
Re: MIDISend & MIDIPacketList
- Subject: Re: MIDISend & MIDIPacketList
- From: Philippe Wicker <email@hidden>
- Date: Tue, 21 Jan 2003 21:23:27 +0100
On Tuesday, January 21, 2003, at 07:25 PM, Vigour Vigour wrote:
>
I have made the following code:
>
>
----------
>
>
// Create client and port
>
MIDIClientRef client = NULL;
>
MIDIClientCreate(CFSTR("Converter"), NULL, NULL, &client);
>
>
//MIDIPortRef inPort = NULL;
>
MIDIOutputPortCreate(client, CFSTR("Output port"), &gOutPort);
>
>
// find the first destination
>
gDest = MIDIGetDestination(0);
>
>
MIDISend(gOutPort, gDest, pktlist);
>
>
----------
>
>
My problem is the pktlist. The only thing I want to do is to send a
>
MIDI Note On message. Can someone plase help me with this code? The
>
thing I want to learn is to build a MIDIPacketList from scratch.
Here is how you can do to simply send a note on:
// Instantiate an instance of packet list: in this case the
packet list has the default
// size, ie a 256 bytes buffer for the MIDI events (see
MIDIServices.h line 410)
MIDIPacketList packet_list ;
// Initialize the packet list and by the same way get the address
of the packet
// where you can write the events
MIDIPacket* packet = MIDIPacketListInit (&packet_list) ;
// Initialize the note on data
char the_note_on[3] ;
the_note_on[0] = 0x90 + the_midi_channel ; // midi channel from 0
to 15
the_note_on[1] = the_midi_note ; // Something between 0 and 0x7F
the_note_on[2] = the_velocity ; // Same range
// Add the note on to the packet list
packet = MIDIPacketListAdd (
&packet_list
, sizeof(packet_list)
, packet
, the_note_on_time
, 3 // Event length
in bytes
, the_note_on // Event data address
) ;
// Send the packet list (here a simple note on)
MIDISend(the_port, the_endpoint, packet_list);
If you had to send a number of MIDI events, here is how you could do it:
MIDIPacketList packet_list ;
MIDIPacket* packet = MIDIPacketListInit (&packet_list) ;
// First loop level: here you walk through the events you want
to send
while (there is an event to send) {
// Get address, length and time of the current event to send.
The actual code
// depends on your own use case. If you are fetching the events
from a MusicTrack,
// then the "note event" (kMusicEventType_MIDINoteMessage) must
be split in
// a note on followed by a note off. If the packet list is send
using MIDISend(), then
// both note on and off can be added in sequence to the packet
list because the
// MIDIServer schedules the events according to their time
stamp.
the_event_data = ...... ;
the_event_size = ...... ;
the_event_time = ...... ;
// Second level of loop: this loop is just a technique to
manage the case of
// a packet list becoming full at some point
while (1) {
packet = MIDIPacketListAdd (
&packet_list
, sizeof(packet_list)
, packet
, the_event_time
, the_event_size
, the_event_data
) ;
// If packet is not NULL, then the write was successful,
break out the loop
// and process next event (if any)
if (packet != NULL) break ;
// At this point, the "ListAdd" have failed because the data
buffer is full, so flush
// the packet list, reinitialize it and retry to add the
event (this is the rationale of
// the inner loop).
MIDISend(the_port, the_endpoint, packet_list);
packet = MIDIPacketListInit (&packet_list) ;
} // End while (1)
} // End while (there is an event to send)
// Done !! All events have been added to a list.
// Don't forget this one!
if (packet_list.numPackets) {
MIDISend(the_port, the_endpoint, packet_list);
}
NOTE: this code will **NOT** work (it will loop indefinitely) for
events greater than 256 bytes (eg a big sysex). It should be reworked
to split large events in slices smaller than 256 bytes.
Philippe Wicker
email@hidden
_______________________________________________
coreaudio-api mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/coreaudio-api
Do not post admin requests to the list. They will be ignored.