Re: MIDI synth vs. MusicDevice
Re: MIDI synth vs. MusicDevice
- Subject: Re: MIDI synth vs. MusicDevice
- From: Antoine Misout <email@hidden>
- Date: Sun, 26 Jun 2005 15:14:24 -0400
On 26-Jun-05, at 1:52 PM, Max Horn wrote:
Hi there,
I guess my previous question was too long and complicated, so here
again in short form:
* Is there a way I can address a "real" MIDI synth like a
MusicDevice (the Apple DLS softsynth in particular) ?
* Is there any way I can have a single code path that lets the user
somehow choose whether to output my MIDI data to the DLS softsynth,
or to a (user specified) real MIDI device ?
* If not (i.e. if I have to write one code set for the MidiSynth
and one to address the 'real' MIDI devices): Is there any small (or
not so small) example code out there which I could look at? Maybe
even another game with MIDI output... ?
Cheers,
Max
Here's some sample code to help you get started:
// sending midi events to an audiounit
// ie dls synth
// very simple once you have the component instance
MusicDeviceMIDIEvent( (MusicDeviceComponent) mAudioUnit,
status, data1, data2,
offsetToChange );
// sending midi events to an external midi device
// by creating a virtual midi source
@interface MidiClass : NSObject
{
MIDIClientRef mClient;
MIDIPortRef mOutPort;
MIDIEndpointRef mSink;
}
@end
- (id) init
{
if ((self = [super init]))
{
// create client
OSStatus err;
err = MIDIClientCreate( (CFStringRef)([[NSProcessInfo
processInfo] globallyUniqueString]),
midiNotifyCallback,
self,
&mClient);
if (err != noErr || !mClient)
{
[self release];
return nil;
}
// create output & connect dests
err = MIDIOutputPortCreate( mClient,
(CFStringRef)(NSString*)
(@"pp_port"),
&mOutPort);
if (err != noErr || !mOutPort)
{
[self release];
return nil;
}
err = MIDISourceCreate( mClient,
(CFStringRef)(NSString*)(@"pp_source"),
&mSink);
if (err != noErr || !mSink)
{
[self release];
return nil;
}
}
retrun self;
}
- (void) dealloc
{
if (mSink) MIDIEndpointDispose(mSink);
if (mOutPort) MIDIPortDispose(mOutPort);
if (mClient) MIDIClientDispose(mClient);
[super dealloc];
}
- (void) sendMidi // adapt as needed
{
struct
{
UInt32 numPackets;
MIDIPacket packets[kMaxNotes*2];
} packetList;
packetList.numPackets = 0;
// force immediate turn off
int i;
MIDIPacket *packet = packetList.packets;
for (i = 0; i < kMaxNotes; i++)
{
packet->timeStamp = 0;
packet->length = 3;
packet->data[0] = kMidiMessage_NoteOn | 0; //
channel
packet->data[1] = i; // note
packet->data[2] = 0; // vol
packetList.numPackets++;
packet = MIDIPacketNext(packet);
}
for (i = 0; i < kMaxNotes; i++)
if (midi note i is on)
{
packet->timeStamp = 1;
packet->length = 3;
packet->data[0] = kMidiMessage_NoteOn | 0; //
channel
packet->data[1] = i; // note
packet->data[2] = 100; // vol
packetList.numPackets++;
packet = MIDIPacketNext(packet);
}
//NSLog(@"Sending notes, %i packets.\n",
packetList.numPackets);
MIDIReceived(mSink, (MIDIPacketList*)&packetList);
}
Hope this helps..
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Coreaudio-api mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden