1 /* Copyright © 2007 Apple Inc. All Rights Reserved.
3 Disclaimer: IMPORTANT: This Apple software is supplied to you by
4 Apple Inc. ("Apple") in consideration of your agreement to the
5 following terms, and your use, installation, modification or
6 redistribution of this Apple software constitutes acceptance of these
7 terms. If you do not agree with these terms, please do not use,
8 install, modify or redistribute this Apple software.
10 In consideration of your agreement to abide by the following terms, and
11 subject to these terms, Apple grants you a personal, non-exclusive
12 license, under Apple's copyrights in this original Apple software (the
13 "Apple Software"), to use, reproduce, modify and redistribute the Apple
14 Software, with or without modifications, in source and/or binary forms;
15 provided that if you redistribute the Apple Software in its entirety and
16 without modifications, you must retain this notice and the following
17 text and disclaimers in all such redistributions of the Apple Software.
18 Neither the name, trademarks, service marks or logos of Apple Inc.
19 may be used to endorse or promote products derived from the Apple
20 Software without specific prior written permission from Apple. Except
21 as expressly stated in this notice, no other rights or licenses, express
22 or implied, are granted by Apple herein, including but not limited to
23 any patent rights that may be infringed by your derivative works or by
24 other works in which the Apple Software may be incorporated.
26 The Apple Software is provided by Apple on an "AS IS" basis. APPLE
27 MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
28 THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
29 FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
30 OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
32 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
33 OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
36 MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
37 AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
38 STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
39 POSSIBILITY OF SUCH DAMAGE.
41 /*=============================================================================
44 =============================================================================*/
46 #include "AUMIDIBase.h"
47 #include <CoreMIDI/CoreMIDI.h>
52 kMidiMessage_NoteOff
= 0x80,
53 kMidiMessage_NoteOn
= 0x90,
54 kMidiMessage_PolyPressure
= 0xA0,
55 kMidiMessage_ControlChange
= 0xB0,
56 kMidiMessage_ProgramChange
= 0xC0,
57 kMidiMessage_ChannelPressure
= 0xD0,
58 kMidiMessage_PitchWheel
= 0xE0,
60 kMidiController_AllSoundOff
= 120,
61 kMidiController_ResetAllControllers
= 121,
62 kMidiController_AllNotesOff
= 123
65 AUMIDIBase::AUMIDIBase(AUBase
* inBase
)
66 : mAUBaseInstance (*inBase
)
69 mMapManager
= new CAAUMIDIMapManager();
73 AUMIDIBase::~AUMIDIBase()
81 #if TARGET_API_MAC_OSX
82 ComponentResult
AUMIDIBase::DelegateGetPropertyInfo(AudioUnitPropertyID inID
,
83 AudioUnitScope inScope
,
84 AudioUnitElement inElement
,
86 Boolean
& outWritable
)
88 if (inScope
!= kAudioUnitScope_Global
) return kAudioUnitErr_InvalidScope
;
89 if (inElement
!= 0) return kAudioUnitErr_InvalidElement
;
91 ComponentResult result
= noErr
;
94 case kMusicDeviceProperty_MIDIXMLNames
:
95 if (GetXMLNames(NULL
) == noErr
) {
96 outDataSize
= sizeof(CFURLRef
);
99 result
= kAudioUnitErr_InvalidProperty
;
103 case kAudioUnitProperty_AllParameterMIDIMappings
:
105 outDataSize
= sizeof (AUParameterMIDIMapping
)*mMapManager
->NumMaps();
109 case kAudioUnitProperty_HotMapParameterMIDIMapping
:
111 outDataSize
= sizeof (AUParameterMIDIMapping
);
115 case kAudioUnitProperty_AddParameterMIDIMapping
:
117 outDataSize
= sizeof (AUParameterMIDIMapping
);
121 case kAudioUnitProperty_RemoveParameterMIDIMapping
:
123 outDataSize
= sizeof (AUParameterMIDIMapping
);
129 result
= kAudioUnitErr_InvalidProperty
;
135 ComponentResult
AUMIDIBase::DelegateGetProperty( AudioUnitPropertyID inID
,
136 AudioUnitScope inScope
,
137 AudioUnitElement inElement
,
140 if (inScope
!= kAudioUnitScope_Global
) return kAudioUnitErr_InvalidScope
;
141 if (inElement
!= 0) return kAudioUnitErr_InvalidElement
;
143 ComponentResult result
;
146 case kMusicDeviceProperty_MIDIXMLNames
:
147 result
= GetXMLNames((CFURLRef
*)outData
);
151 case kAudioUnitProperty_AllParameterMIDIMappings
:{
152 AUParameterMIDIMapping
* maps
= (static_cast<AUParameterMIDIMapping
*>(outData
));
153 mMapManager
->GetMaps(maps
);
154 // printf ("GETTING MAPS\n");
155 // mMapManager->Print();
160 case kAudioUnitProperty_HotMapParameterMIDIMapping
:{
161 AUParameterMIDIMapping
* map
= (static_cast<AUParameterMIDIMapping
*>(outData
));
162 mMapManager
->GetHotParameterMap (*map
);
169 result
= kAudioUnitErr_InvalidProperty
;
175 ComponentResult
AUMIDIBase::DelegateSetProperty( AudioUnitPropertyID inID
,
176 AudioUnitScope inScope
,
177 AudioUnitElement inElement
,
181 if (inScope
!= kAudioUnitScope_Global
) return kAudioUnitErr_InvalidScope
;
182 if (inElement
!= 0) return kAudioUnitErr_InvalidElement
;
188 case kAudioUnitProperty_AddParameterMIDIMapping
:{
189 AUParameterMIDIMapping
* maps
= (AUParameterMIDIMapping
*)inData
;
190 mMapManager
->SortedInsertToParamaterMaps (maps
, (inDataSize
/ sizeof(AUParameterMIDIMapping
)), mAUBaseInstance
);
191 mAUBaseInstance
.PropertyChanged (kAudioUnitProperty_AllParameterMIDIMappings
, kAudioUnitScope_Global
, 0);
196 case kAudioUnitProperty_RemoveParameterMIDIMapping
:{
197 AUParameterMIDIMapping
* maps
= (AUParameterMIDIMapping
*)inData
;
199 mMapManager
->SortedRemoveFromParameterMaps(maps
, (inDataSize
/ sizeof(AUParameterMIDIMapping
)), didChange
);
201 mAUBaseInstance
.PropertyChanged (kAudioUnitProperty_AllParameterMIDIMappings
, kAudioUnitScope_Global
, 0);
206 case kAudioUnitProperty_HotMapParameterMIDIMapping
:{
207 AUParameterMIDIMapping
& map
= *((AUParameterMIDIMapping
*)inData
);
208 mMapManager
->SetHotMapping (map
);
212 case kAudioUnitProperty_AllParameterMIDIMappings
:{
213 AUParameterMIDIMapping
* mappings
= (AUParameterMIDIMapping
*)inData
;
214 mMapManager
->ReplaceAllMaps (mappings
, (inDataSize
/ sizeof(AUParameterMIDIMapping
)), mAUBaseInstance
);
221 result
= kAudioUnitErr_InvalidProperty
;
229 #endif //TARGET_API_MAC_OSX
232 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
233 #pragma mark ____MidiDispatch
236 inline const Byte
* NextMIDIEvent(const Byte
*event
, const Byte
*end
)
240 default: // data byte -- assume in sysex
241 while ((*++event
& 0x80) == 0 && event
< end
)
258 while ((*++event
& 0x80) == 0 && event
< end
)
273 return (event
>= end
) ? end
: event
;
276 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
277 // AUMIDIBase::HandleMIDIPacketList
279 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
280 ComponentResult
AUMIDIBase::HandleMIDIPacketList(const MIDIPacketList
*pktlist
)
282 if (!mAUBaseInstance
.IsInitialized()) return kAudioUnitErr_Uninitialized
;
284 int nPackets
= pktlist
->numPackets
;
285 const MIDIPacket
*pkt
= pktlist
->packet
;
287 while (nPackets
-- > 0) {
288 const Byte
*event
= pkt
->data
, *packetEnd
= event
+ pkt
->length
;
289 long startFrame
= (long)pkt
->timeStamp
;
290 while (event
< packetEnd
) {
291 Byte status
= event
[0];
293 // really a status byte (not sysex continuation)
294 HandleMidiEvent(status
& 0xF0, status
& 0x0F, event
[1], event
[2], startFrame
);
295 // note that we're generating a bogus channel number for system messages (0xF0-FF)
297 event
= NextMIDIEvent(event
, packetEnd
);
299 pkt
= reinterpret_cast<const MIDIPacket
*>(packetEnd
);
304 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
305 // AUMIDIBase::HandleMidiEvent
307 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
308 OSStatus
AUMIDIBase::HandleMidiEvent(UInt8 status
, UInt8 channel
, UInt8 data1
, UInt8 data2
, UInt32 inStartFrame
)
310 if (!mAUBaseInstance
.IsInitialized()) return kAudioUnitErr_Uninitialized
;
313 // you potentially have a choice to make here - if a param mapping matches, do you still want to process the
314 // MIDI event or not. The default behaviour is to continue on with the MIDI event.
315 if (mMapManager
->HandleHotMapping (status
, channel
, data1
, mAUBaseInstance
)) {
316 mAUBaseInstance
.PropertyChanged (kAudioUnitProperty_HotMapParameterMIDIMapping
, kAudioUnitScope_Global
, 0);
319 mMapManager
->FindParameterMapEventMatch(status
, channel
, data1
, data2
, inStartFrame
, mAUBaseInstance
);
323 OSStatus result
= noErr
;
327 case kMidiMessage_NoteOn
:
330 result
= HandleNoteOn(channel
, data1
, data2
, inStartFrame
);
334 // zero velocity translates to note off
335 result
= HandleNoteOff(channel
, data1
, data2
, inStartFrame
);
339 case kMidiMessage_NoteOff
:
340 result
= HandleNoteOff(channel
, data1
, data2
, inStartFrame
);
344 result
= HandleNonNoteEvent (status
, channel
, data1
, data2
, inStartFrame
);
351 OSStatus
AUMIDIBase::HandleNonNoteEvent (UInt8 status
, UInt8 channel
, UInt8 data1
, UInt8 data2
, UInt32 inStartFrame
)
353 OSStatus result
= noErr
;
357 case kMidiMessage_PitchWheel
:
358 result
= HandlePitchWheel(channel
, data1
, data2
, inStartFrame
);
361 case kMidiMessage_ProgramChange
:
362 result
= HandleProgramChange(channel
, data1
);
365 case kMidiMessage_ChannelPressure
:
366 result
= HandleChannelPressure(channel
, data1
, inStartFrame
);
369 case kMidiMessage_ControlChange
:
372 case kMidiController_AllNotesOff
:
373 result
= HandleAllNotesOff(channel
);
376 case kMidiController_ResetAllControllers
:
377 result
= HandleResetAllControllers(channel
);
380 case kMidiController_AllSoundOff
:
381 result
= HandleAllSoundOff(channel
);
385 result
= HandleControlChange(channel
, data1
, data2
, inStartFrame
);
390 case kMidiMessage_PolyPressure
:
391 result
= HandlePolyPressure (channel
, data1
, data2
, inStartFrame
);
397 ComponentResult
AUMIDIBase::SysEx (const UInt8
* inData
,
400 if (!mAUBaseInstance
.IsInitialized()) return kAudioUnitErr_Uninitialized
;
402 return HandleSysEx(inData
, inLength
);
409 // comp instance, parameters in forward order
410 #define PARAM(_typ, _name, _index, _nparams) \
411 _typ _name = *(_typ *)¶ms->params[_index + 1];
413 // parameters in reverse order, then comp instance
414 #define PARAM(_typ, _name, _index, _nparams) \
415 _typ _name = *(_typ *)¶ms->params[_nparams - 1 - _index];
417 #elif TARGET_OS_WIN32
418 // (no comp instance), parameters in forward order
419 #define PARAM(_typ, _name, _index, _nparams) \
420 _typ _name = *(_typ *)¶ms->params[_index];
424 ComponentResult
AUMIDIBase::ComponentEntryDispatch( ComponentParameters
* params
,
427 if (This
== NULL
) return paramErr
;
429 ComponentResult result
;
431 switch (params
->what
) {
432 case kMusicDeviceMIDIEventSelect
:
434 PARAM(UInt32
, pbinStatus
, 0, 4);
435 PARAM(UInt32
, pbinData1
, 1, 4);
436 PARAM(UInt32
, pbinData2
, 2, 4);
437 PARAM(UInt32
, pbinOffsetSampleFrame
, 3, 4);
438 result
= This
->MIDIEvent(pbinStatus
, pbinData1
, pbinData2
, pbinOffsetSampleFrame
);
441 case kMusicDeviceSysExSelect
:
443 PARAM(const UInt8
*, pbinData
, 0, 2);
444 PARAM(UInt32
, pbinLength
, 1, 2);
445 result
= This
->SysEx(pbinData
, pbinLength
);
450 result
= badComponentSelector
;