forked from osoumen/C700
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC700_CocoaViewFactory.mm
337 lines (294 loc) · 10.1 KB
/
C700_CocoaViewFactory.mm
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
//
// C700_CocoaViewFactory.m
// C700
//
// Created by osoumen on 12/10/01.
// Copyright 2012 __MyCompanyName__. All rights reserved.
//
#import <CAAUParameter.h>
#import "C700_CocoaViewFactory.h"
#import "C700Edit.h"
@implementation C700_CocoaViewFactory
// version 0
- (unsigned) interfaceVersion {
return 0;
}
// string description of the Cocoa UI
- (NSString *) description {
return @" Cocoa View";
}
- (void)dealloc {
[super dealloc];
}
- (NSView *)uiViewForAudioUnit:(AudioUnit)inAU withSize:(NSSize)inPreferredSize
{
NSRect r;
r.origin.x = 0;
r.origin.y = 0;
r.size.width = 200;
r.size.height = 200;
C700_CocoaView *returnView = [[C700_CocoaView alloc] initWithFrame:r audioUnit:inAU];
[returnView setEditorFrame];
return [returnView autorelease];
}
@end
void EventListenerDispatcher(
void * inCallbackRefCon,
void * inObject,
const AudioUnitEvent * inEvent,
UInt64 inEventHostTime,
AudioUnitParameterValue inParameterValue
)
{
C700Edit *editor = (C700Edit *)inCallbackRefCon;
if ( inEvent->mEventType == kAudioUnitEvent_ParameterValueChange ) {
editor->setParameter(inEvent->mArgument.mParameter.mParameterID, inParameterValue);
}
if ( inEvent->mEventType == kAudioUnitEvent_PropertyChange ) {
AudioUnitPropertyID propertyId = inEvent->mArgument.mProperty.mPropertyID;
float value;
char outDataPtr[16];
UInt32 outDataSize=16;
if (
propertyId != kAudioUnitCustomProperty_PGDictionary &&
propertyId != kAudioUnitCustomProperty_XIData &&
propertyId != kAudioUnitCustomProperty_SourceFileRef )
{
AudioUnitGetProperty((AudioUnit)editor->getEffect(), propertyId,
kAudioUnitScope_Global, 0, &outDataPtr, &outDataSize);
}
switch (propertyId) {
case kAudioUnitCustomProperty_BaseKey:
case kAudioUnitCustomProperty_LowKey:
case kAudioUnitCustomProperty_HighKey:
case kAudioUnitCustomProperty_AR:
case kAudioUnitCustomProperty_DR:
case kAudioUnitCustomProperty_SL:
case kAudioUnitCustomProperty_SR:
case kAudioUnitCustomProperty_VolL:
case kAudioUnitCustomProperty_VolR:
case kAudioUnitCustomProperty_EditingProgram:
case kAudioUnitCustomProperty_TotalRAM:
case kAudioUnitCustomProperty_EditingChannel:
case kAudioUnitCustomProperty_LoopPoint:
case kAudioUnitCustomProperty_Bank:
case kAudioUnitCustomProperty_PortamentoRate:
case kAudioUnitCustomProperty_NoteOnPriority:
case kAudioUnitCustomProperty_ReleasePriority:
value = *((int*)outDataPtr);
break;
case kAudioUnitCustomProperty_Rate:
value = *((double*)outDataPtr);
break;
case kAudioUnitCustomProperty_Loop:
case kAudioUnitCustomProperty_Echo:
case kAudioUnitCustomProperty_IsEmaphasized:
case kAudioUnitCustomProperty_SustainMode:
case kAudioUnitCustomProperty_MonoMode:
case kAudioUnitCustomProperty_PortamentoOn:
value = *((bool*)outDataPtr);
break;
case kAudioUnitCustomProperty_Band1:
case kAudioUnitCustomProperty_Band2:
case kAudioUnitCustomProperty_Band3:
case kAudioUnitCustomProperty_Band4:
case kAudioUnitCustomProperty_Band5:
value = *((Float32*)outDataPtr);
break;
case kAudioUnitCustomProperty_NoteOnTrack_1:
case kAudioUnitCustomProperty_NoteOnTrack_2:
case kAudioUnitCustomProperty_NoteOnTrack_3:
case kAudioUnitCustomProperty_NoteOnTrack_4:
case kAudioUnitCustomProperty_NoteOnTrack_5:
case kAudioUnitCustomProperty_NoteOnTrack_6:
case kAudioUnitCustomProperty_NoteOnTrack_7:
case kAudioUnitCustomProperty_NoteOnTrack_8:
case kAudioUnitCustomProperty_NoteOnTrack_9:
case kAudioUnitCustomProperty_NoteOnTrack_10:
case kAudioUnitCustomProperty_NoteOnTrack_11:
case kAudioUnitCustomProperty_NoteOnTrack_12:
case kAudioUnitCustomProperty_NoteOnTrack_13:
case kAudioUnitCustomProperty_NoteOnTrack_14:
case kAudioUnitCustomProperty_NoteOnTrack_15:
case kAudioUnitCustomProperty_NoteOnTrack_16:
case kAudioUnitCustomProperty_MaxNoteTrack_1:
case kAudioUnitCustomProperty_MaxNoteTrack_2:
case kAudioUnitCustomProperty_MaxNoteTrack_3:
case kAudioUnitCustomProperty_MaxNoteTrack_4:
case kAudioUnitCustomProperty_MaxNoteTrack_5:
case kAudioUnitCustomProperty_MaxNoteTrack_6:
case kAudioUnitCustomProperty_MaxNoteTrack_7:
case kAudioUnitCustomProperty_MaxNoteTrack_8:
case kAudioUnitCustomProperty_MaxNoteTrack_9:
case kAudioUnitCustomProperty_MaxNoteTrack_10:
case kAudioUnitCustomProperty_MaxNoteTrack_11:
case kAudioUnitCustomProperty_MaxNoteTrack_12:
case kAudioUnitCustomProperty_MaxNoteTrack_13:
case kAudioUnitCustomProperty_MaxNoteTrack_14:
case kAudioUnitCustomProperty_MaxNoteTrack_15:
case kAudioUnitCustomProperty_MaxNoteTrack_16:
value = *((UInt32*)outDataPtr);
break;
case kAudioUnitCustomProperty_BRRData:
{
BRRData *brrdata = (BRRData*)outDataPtr;
editor->SetBRRData( brrdata );
outDataSize = 0;
break;
}
case kAudioUnitCustomProperty_ProgramName:
{
CFStringRef cfpgname = *((CFStringRef*)outDataPtr);
if ( cfpgname == NULL )
{
editor->SetProgramName( "" );
}
else {
char pgname[PROGRAMNAME_MAX_LEN];
CFStringGetCString(cfpgname, pgname, PROGRAMNAME_MAX_LEN, kCFStringEncodingUTF8);
editor->SetProgramName( pgname );
}
CFRelease(cfpgname);
outDataSize = 0;
break;
}
// case kAudioUnitCustomProperty_PGDictionary:
// case kAudioUnitCustomProperty_XIData:
// case kAudioUnitCustomProperty_SourceFileRef:
default:
outDataSize = 0;
}
if ( outDataSize > 0 ) {
editor->setParameter( propertyId, value );
}
}
}
@implementation C700_CocoaView
- (C700_CocoaView*)initWithFrame:(NSRect)frameRect audioUnit:(AudioUnit)inAU
{
C700_CocoaView *view = [super initWithFrame:frameRect];
mAU = inAU;
#ifndef __LP64__
CFrame::setCocoaMode(true);
#endif
editor = new C700Edit(inAU);
if ( editor ) {
editor->open(self);
}
//タイマーの設定
timer = [NSTimer scheduledTimerWithTimeInterval:1.0/60
target:self
selector:@selector(respondToEventTimer:)
userInfo:nil
repeats:YES];
//パラメーターリスナーの登録
[self _addListeners];
//設定値の反映
[self _synchronizeUIWithParameterValues];
return view;
}
- (void)_addListeners
{
//イベントリスナーの作成
NSAssert( AUEventListenerCreate( EventListenerDispatcher, editor,
CFRunLoopGetCurrent(),
kCFRunLoopDefaultMode, 1.0/60, 1.0/60,
&mEventListener ) == noErr,
@"[CocoaView _addListeners] AUListenerCreate()");
//パラメータリスナーの登録
for (int i = 0; i < kNumberOfParameters; ++i) {
AudioUnitParameter parameter = { mAU, i, kAudioUnitScope_Global, 0 };
NSAssert ( AUListenerAddParameter (mEventListener, editor, ¶meter) == noErr,
@"[CocoaView _addListeners] AUListenerAddParameter()");
}
//エフェクターアクセッサの作成
efxAcc = new EfxAccess( mAU );
efxAcc->SetEventListener(mEventListener);
editor->SetEfxAccess(efxAcc);
//プロパティリスナーの登録
for (int i=0; i<kNumberOfProperties; ++i) {
AudioUnitProperty property = { mAU, kAudioUnitCustomProperty_Begin+i, kAudioUnitScope_Global, 0 };
AudioUnitEvent event;
event.mEventType = kAudioUnitEvent_PropertyChange;
event.mArgument.mProperty = property;
NSAssert ( AUEventListenerAddEventType (mEventListener, editor, &event) == noErr,
@"[CocoaView _addListeners] AUListenerAddParameter()");
//初期値を反映させる
//AUEventListenerNotify(mEventListener, editor, &event);
EventListenerDispatcher(editor, editor, &event, 0, 0);
}
}
- (void)_removeListeners
{
for (int i=0; i<kNumberOfProperties; ++i) {
AudioUnitProperty property = { mAU, i+kAudioUnitCustomProperty_Begin, kAudioUnitScope_Global, 0 };
AudioUnitEvent event;
event.mEventType = kAudioUnitEvent_PropertyChange;
event.mArgument.mProperty = property;
NSAssert ( AUEventListenerRemoveEventType (mEventListener, editor, &event) == noErr,
@"[CocoaView _removeListeners] AUEventListenerRemoveEventType()");
}
for (int i = 0; i < kNumberOfParameters; ++i) {
AudioUnitParameter parameter = { mAU, i, kAudioUnitScope_Global, 0 };
NSAssert ( AUListenerRemoveParameter(mEventListener, editor, ¶meter) == noErr,
@"[CocoaView _removeListeners] AUListenerRemoveParameter()");
}
NSAssert ( AUListenerDispose(mEventListener) == noErr,
@"[CocoaView _removeListeners] AUListenerDispose()");
}
- (void)_synchronizeUIWithParameterValues
{
Float32 value;
for (int i = 0; i < kNumberOfParameters; ++i)
{
//最大値、最小値、デフォルト値をコントロールに反映
CAAUParameter tParam(mAU, i, kAudioUnitScope_Global, 0);
editor->SetParameterInfo( i, tParam.ParamInfo().minValue, tParam.ParamInfo().maxValue, tParam.ParamInfo().defaultValue );
if ((i != kParam_program) && ((i < kParam_program_2) || (i > kParam_program_16))) {
AudioUnitParameter parameter = { mAU, i, kAudioUnitScope_Global, 0 };
// only has global parameters
NSAssert ( AudioUnitGetParameter(mAU, parameter.mParameterID, kAudioUnitScope_Global, 0, &value) == noErr,
@"[CocoaView synchronizeUIWithParameterValues] (x.1)");
NSAssert ( AUParameterSet (mEventListener, self, ¶meter, value, 0) == noErr,
@"[CocoaView synchronizeUIWithParameterValues] (x.2)");
NSAssert ( AUParameterListenerNotify (mEventListener, self, ¶meter) == noErr,
@"[CocoaView synchronizeUIWithParameterValues] (x.3)");
}
}
}
- (void)respondToEventTimer:(NSTimer*)inTimer
{
if (editor) {
editor->doIdleStuff();
}
}
- (void)setEditorFrame
{
CRect fsize;
if ( editor ) {
fsize = editor->getFrame()->getViewSize(fsize);
}
NSRect r;
r.origin.x = fsize.left;
r.origin.y = fsize.top;
r.size.width = fsize.getWidth();
r.size.height = fsize.getHeight();
[self setFrame:r];
}
- (void)dealloc
{
delete efxAcc;
//パラメータリスナーの削除
[self _removeListeners];
//タイマーの停止
if ( timer ) {
[timer invalidate];
}
if (editor) {
editor->close();
delete editor;
editor = NULL;
}
[super dealloc];
}
@end