-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathAUMIDIBase.cpp
251 lines (215 loc) · 8.24 KB
/
AUMIDIBase.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*!
@file AudioUnitSDK/AUMIDIBase.cpp
@copyright © 2000-2021 Apple Inc. All rights reserved.
*/
#include <AudioUnitSDK/AUMIDIBase.h>
#include <CoreMIDI/CoreMIDI.h>
namespace ausdk {
// MIDI CC data bytes
constexpr uint8_t kMIDIController_AllSoundOff = 120u;
constexpr uint8_t kMIDIController_ResetAllControllers = 121u;
constexpr uint8_t kMIDIController_AllNotesOff = 123u;
OSStatus AUMIDIBase::DelegateGetPropertyInfo(AudioUnitPropertyID inID, AudioUnitScope inScope,
AudioUnitElement inElement, UInt32& outDataSize, bool& outWritable)
{
(void)inScope;
(void)inElement;
(void)outDataSize;
(void)outWritable;
switch (inID) { // NOLINT if/else?!
#if AUSDK_HAVE_XML_NAMES
case kMusicDeviceProperty_MIDIXMLNames:
AUSDK_Require(inScope == kAudioUnitScope_Global, kAudioUnitErr_InvalidScope);
AUSDK_Require(inElement == 0, kAudioUnitErr_InvalidElement);
AUSDK_Require(GetXMLNames(nullptr) == noErr, kAudioUnitErr_InvalidProperty);
outDataSize = sizeof(CFURLRef);
outWritable = false;
return noErr;
#endif
#if AUSDK_HAVE_MIDI_MAPPING
case kAudioUnitProperty_AllParameterMIDIMappings:
AUSDK_Require(mMIDIMapper, kAudioUnitErr_InvalidProperty);
AUSDK_Require(inScope == kAudioUnitScope_Global, kAudioUnitErr_InvalidScope);
AUSDK_Require(inElement == 0, kAudioUnitErr_InvalidElement);
outWritable = true;
outDataSize = sizeof(AUParameterMIDIMapping) * mMIDIMapper->GetNumberMaps();
return noErr;
case kAudioUnitProperty_HotMapParameterMIDIMapping:
case kAudioUnitProperty_AddParameterMIDIMapping:
case kAudioUnitProperty_RemoveParameterMIDIMapping:
AUSDK_Require(mMIDIMapper, kAudioUnitErr_InvalidProperty);
AUSDK_Require(inScope == kAudioUnitScope_Global, kAudioUnitErr_InvalidScope);
AUSDK_Require(inElement == 0, kAudioUnitErr_InvalidElement);
outWritable = true;
outDataSize = sizeof(AUParameterMIDIMapping);
return noErr;
#endif
default:
return kAudioUnitErr_InvalidProperty;
}
}
OSStatus AUMIDIBase::DelegateGetProperty(
AudioUnitPropertyID inID, AudioUnitScope inScope, AudioUnitElement inElement, void* outData)
{
(void)inScope;
(void)inElement;
(void)outData;
switch (inID) { // NOLINT if/else?!
#if AUSDK_HAVE_XML_NAMES
case kMusicDeviceProperty_MIDIXMLNames:
AUSDK_Require(inScope == kAudioUnitScope_Global, kAudioUnitErr_InvalidScope);
AUSDK_Require(inElement == 0, kAudioUnitErr_InvalidElement);
return GetXMLNames(static_cast<CFURLRef*>(outData));
#endif
#if AUSDK_HAVE_MIDI_MAPPING
case kAudioUnitProperty_AllParameterMIDIMappings: {
AUSDK_Require(mMIDIMapper, kAudioUnitErr_InvalidProperty);
AUSDK_Require(inScope == kAudioUnitScope_Global, kAudioUnitErr_InvalidScope);
AUSDK_Require(inElement == 0, kAudioUnitErr_InvalidElement);
AUParameterMIDIMapping* const maps = (static_cast<AUParameterMIDIMapping*>(outData));
mMIDIMapper->GetMaps(maps);
return noErr;
}
case kAudioUnitProperty_HotMapParameterMIDIMapping: {
AUSDK_Require(mMIDIMapper, kAudioUnitErr_InvalidProperty);
AUSDK_Require(inScope == kAudioUnitScope_Global, kAudioUnitErr_InvalidScope);
AUSDK_Require(inElement == 0, kAudioUnitErr_InvalidElement);
AUParameterMIDIMapping* const map = (static_cast<AUParameterMIDIMapping*>(outData));
mMIDIMapper->GetHotParameterMap(*map);
return noErr;
}
#endif
default:
return kAudioUnitErr_InvalidProperty;
}
}
OSStatus AUMIDIBase::DelegateSetProperty(AudioUnitPropertyID inID, AudioUnitScope inScope,
AudioUnitElement inElement, const void* inData, UInt32 inDataSize)
{
(void)inScope;
(void)inElement;
(void)inData;
(void)inDataSize;
switch (inID) {
#if AUSDK_HAVE_MIDI_MAPPING
case kAudioUnitProperty_AddParameterMIDIMapping: {
AUSDK_Require(mMIDIMapper, kAudioUnitErr_InvalidProperty);
AUSDK_Require(inScope == kAudioUnitScope_Global, kAudioUnitErr_InvalidScope);
AUSDK_Require(inElement == 0, kAudioUnitErr_InvalidElement);
const auto* const maps = static_cast<const AUParameterMIDIMapping*>(inData);
mMIDIMapper->AddParameterMapping(
maps, (inDataSize / sizeof(AUParameterMIDIMapping)), mAUBaseInstance);
mAUBaseInstance.PropertyChanged(
kAudioUnitProperty_AllParameterMIDIMappings, kAudioUnitScope_Global, 0);
return noErr;
}
case kAudioUnitProperty_RemoveParameterMIDIMapping: {
AUSDK_Require(mMIDIMapper, kAudioUnitErr_InvalidProperty);
AUSDK_Require(inScope == kAudioUnitScope_Global, kAudioUnitErr_InvalidScope);
AUSDK_Require(inElement == 0, kAudioUnitErr_InvalidElement);
const auto* const maps = static_cast<const AUParameterMIDIMapping*>(inData);
bool didChange = false;
mMIDIMapper->RemoveParameterMapping(
maps, (inDataSize / sizeof(AUParameterMIDIMapping)), didChange);
if (didChange) {
mAUBaseInstance.PropertyChanged(
kAudioUnitProperty_AllParameterMIDIMappings, kAudioUnitScope_Global, 0);
}
return noErr;
}
case kAudioUnitProperty_HotMapParameterMIDIMapping: {
AUSDK_Require(mMIDIMapper, kAudioUnitErr_InvalidProperty);
AUSDK_Require(inScope == kAudioUnitScope_Global, kAudioUnitErr_InvalidScope);
AUSDK_Require(inElement == 0, kAudioUnitErr_InvalidElement);
const auto& map = *static_cast<const AUParameterMIDIMapping*>(inData);
mMIDIMapper->SetHotMapping(map);
return noErr;
}
case kAudioUnitProperty_AllParameterMIDIMappings: {
AUSDK_Require(mMIDIMapper, kAudioUnitErr_InvalidProperty);
AUSDK_Require(inScope == kAudioUnitScope_Global, kAudioUnitErr_InvalidScope);
AUSDK_Require(inElement == 0, kAudioUnitErr_InvalidElement);
const auto* const mappings = static_cast<const AUParameterMIDIMapping*>(inData);
mMIDIMapper->ReplaceAllMaps(
mappings, (inDataSize / sizeof(AUParameterMIDIMapping)), mAUBaseInstance);
return noErr;
}
#endif
default:
return kAudioUnitErr_InvalidProperty;
}
}
constexpr uint8_t MIDIStatusNibbleValue(uint8_t status) noexcept { return (status & 0xF0U) >> 4u; }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// AUMIDIBase::HandleMIDIEvent
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
OSStatus AUMIDIBase::HandleMIDIEvent(
UInt8 status, UInt8 channel, UInt8 data1, UInt8 data2, UInt32 inStartFrame)
{
if (!mAUBaseInstance.IsInitialized()) {
return kAudioUnitErr_Uninitialized;
}
#if AUSDK_HAVE_MIDI_MAPPING
// you potentially have a choice to make here - if a param mapping matches, do you still want to
// process the MIDI event or not. The default behaviour is to continue on with the MIDI event.
if (mMIDIMapper) {
if (mMIDIMapper->HandleHotMapping(status, channel, data1, mAUBaseInstance)) {
mAUBaseInstance.PropertyChanged(
kAudioUnitProperty_HotMapParameterMIDIMapping, kAudioUnitScope_Global, 0);
} else {
mMIDIMapper->FindParameterMapEventMatch(
status, channel, data1, data2, inStartFrame, mAUBaseInstance);
}
}
#endif
switch (MIDIStatusNibbleValue(status)) {
case kMIDICVStatusNoteOn:
if (data2 != 0u) {
return HandleNoteOn(channel, data1, data2, inStartFrame);
} else {
// zero velocity translates to note off
return HandleNoteOff(channel, data1, data2, inStartFrame);
}
case kMIDICVStatusNoteOff:
return HandleNoteOff(channel, data1, data2, inStartFrame);
default:
return HandleNonNoteEvent(status, channel, data1, data2, inStartFrame);
}
}
OSStatus AUMIDIBase::HandleNonNoteEvent(
UInt8 status, UInt8 channel, UInt8 data1, UInt8 data2, UInt32 inStartFrame)
{
switch (MIDIStatusNibbleValue(status)) {
case kMIDICVStatusPitchBend:
return HandlePitchWheel(channel, data1, data2, inStartFrame);
case kMIDICVStatusProgramChange:
return HandleProgramChange(channel, data1);
case kMIDICVStatusChannelPressure:
return HandleChannelPressure(channel, data1, inStartFrame);
case kMIDICVStatusControlChange: {
switch (data1) {
case kMIDIController_AllNotesOff:
return HandleAllNotesOff(channel);
case kMIDIController_ResetAllControllers:
return HandleResetAllControllers(channel);
case kMIDIController_AllSoundOff:
return HandleAllSoundOff(channel);
default:
return HandleControlChange(channel, data1, data2, inStartFrame);
}
}
case kMIDICVStatusPolyPressure:
return HandlePolyPressure(channel, data1, data2, inStartFrame);
default:
return noErr;
}
}
OSStatus AUMIDIBase::SysEx(const UInt8* inData, UInt32 inLength)
{
if (!mAUBaseInstance.IsInitialized()) {
return kAudioUnitErr_Uninitialized;
}
return HandleSysEx(inData, inLength);
}
} // namespace ausdk