/
BBOSCPlugInSender.m
345 lines (283 loc) · 9.75 KB
/
BBOSCPlugInSender.m
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
338
339
340
341
342
343
344
345
//
// BBOSCPlugInSender.m
// BBOSC
//
// Created by Jonathan del Strother on 08/09/2009.
// Copyright (c) 2009 Best Before Media Ltd. All rights reserved.
//
/* It's highly recommended to use CGL macros instead of changing the current context for plug-ins that perform OpenGL rendering */
#import <OpenGL/CGLMacro.h>
#import "vvosc/FrameworkSrc/VVOSC.h"
#import "BBOSCPlugInSender.h"
#import "NSArrayExtensions.h"
#define kQCPlugIn_Name @"BBOSC Sender"
#define kQCPlugIn_Description @"Best Before Open Sound Control sender plugin"
NSString* BBOSCTypeKey=@"BBOSCType";
NSString* BBOSCPortKey=@"BBOSCPortKey";
typedef enum {
BBOSCTypeInt,
BBOSCTypeFloat,
BBOSCTypeBool,
BBOSCTypeString,
BBOSCTypeArrayOfInt,
BBOSCTypeArrayOfFloat,
BBOSCTypeArrayOfBool,
BBOSCTypeCount
} BBOSCType;
@interface OSCMessage(BBExtensions)
-(void)addNSValue:(id)newValue withBias:(BBOSCType)bias;
@end
@implementation OSCMessage(BBExtensions)
-(void)addNSValue:(id)newValue withBias:(BBOSCType)bias {
if ([newValue isKindOfClass:[NSString class]]) {
[self addString:newValue];
} else if ([newValue isKindOfClass:[NSNumber class]]) {
switch(bias) {
case BBOSCTypeBool:
[self addBOOL:[newValue boolValue]];
break;
case BBOSCTypeInt:
[self addInt:[newValue intValue]];
break;
case BBOSCTypeFloat:
[self addFloat:[newValue floatValue]];
break;
default:
NSAssert2(NO, @"Bad type %d for %@", bias, newValue);
}
} else if ([newValue isKindOfClass:[NSArray class]]) {
NSAssert2(bias>=BBOSCTypeArrayOfInt && bias<=BBOSCTypeArrayOfBool, @"Unexpected value %@ for type %d", newValue, bias);
// We've got an array - add all the subvalues into the message, using the appropriate type (eg if we're using BBOSCTypeArrayOfFloat, subvalues should use BBOSCTypeFloat)
for(id subvalue in newValue) {
[self addNSValue:subvalue withBias:bias-4];
}
} else {
[self addBOOL:!!newValue];
}
}
@end
@interface BBOSCTypeToStringTransformer : NSValueTransformer
+ (id) transformer;
@end
@implementation BBOSCTypeToStringTransformer
+ (id) transformer {
return [[[self alloc] init] autorelease];
}
+ (Class)transformedValueClass { return [NSString class]; }
+ (BOOL)allowsReverseTransformation { return NO; }
- (id)transformedValue:(id)value {
switch ([value intValue]) {
case BBOSCTypeInt: return @"Integer";
case BBOSCTypeFloat: return @"Float";
case BBOSCTypeBool: return @"Boolean";
case BBOSCTypeString: return @"String";
case BBOSCTypeArrayOfInt: return @"Array w/ Int bias";
case BBOSCTypeArrayOfFloat: return @"Array w/ Float bias";
case BBOSCTypeArrayOfBool: return @"Array w/ Bool bias";
}
NSAssert1(NO, @"Bad OSC type value %@", value);
}
@end
@implementation BBOSCPlugInSenderViewController
@synthesize portTypeDropDown, portArrayController;
-(void)setPortTypeDropDown:(NSPopUpButton*)popup {
// Initialize the popup with all our port types
portTypeDropDown = popup;
[portTypeDropDown removeAllItems];
NSMutableArray* portTypes = [NSMutableArray array];
BBOSCTypeToStringTransformer* transformer = [BBOSCTypeToStringTransformer transformer];
for(int i=0; i<BBOSCTypeCount; i++) {
[portTypes addObject:[transformer transformedValue:[NSNumber numberWithInt:i]]];
}
[portTypeDropDown addItemsWithTitles:portTypes];
}
-(void)addNewPort:(id)sender {
NSMutableDictionary* newPort = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:[portTypeDropDown indexOfSelectedItem]],BBOSCTypeKey,
[[NSProcessInfo processInfo] globallyUniqueString], BBOSCPortKey, nil
];
[portArrayController addObject:newPort];
}
@end
@interface BBOSCPlugInSender ()
@property (nonatomic, readwrite, retain) OSCManager *oscManager;
@property (nonatomic, readwrite, retain) OSCOutPort *oscPort;
@property (nonatomic, readwrite, retain) NSArray* oscParameters;
@end
@implementation BBOSCPlugInSender
@synthesize oscManager, oscPort, oscParameters;
@dynamic inputBroadcastPort, inputBroadcastPath;
+(void)initialize {
if (self == [BBOSCPlugInSender class]) {
[NSValueTransformer setValueTransformer:[BBOSCTypeToStringTransformer transformer] forName:@"BBOSCTypeToStringTransformer"];
}
}
+ (NSDictionary*) attributes
{
/*
Return a dictionary of attributes describing the plug-in (QCPlugInAttributeNameKey, QCPlugInAttributeDescriptionKey...).
*/
return [NSDictionary dictionaryWithObjectsAndKeys:kQCPlugIn_Name, QCPlugInAttributeNameKey, kQCPlugIn_Description, QCPlugInAttributeDescriptionKey, nil];
}
+ (NSDictionary*) attributesForPropertyPortWithKey:(NSString*)key {
if ([key isEqualToString:@"inputBroadcastPort"]) {
return [NSDictionary dictionaryWithObjectsAndKeys:@"Broadcast Port", QCPortAttributeNameKey,
[NSNumber numberWithInt:60000], QCPortAttributeDefaultValueKey, nil];
}
if ([key isEqualToString:@"inputBroadcastPath"]) {
return [NSDictionary dictionaryWithObjectsAndKeys:@"Broadcast Path", QCPortAttributeNameKey,
@"/test", QCPortAttributeDefaultValueKey, nil];
}
return nil;
}
+ (QCPlugInExecutionMode) executionMode
{
/*
Return the execution mode of the plug-in: kQCPlugInExecutionModeProvider, kQCPlugInExecutionModeProcessor, or kQCPlugInExecutionModeConsumer.
*/
return kQCPlugInExecutionModeConsumer;
}
+ (QCPlugInTimeMode) timeMode
{
/*
Return the time dependency mode of the plug-in: kQCPlugInTimeModeNone, kQCPlugInTimeModeIdle or kQCPlugInTimeModeTimeBase.
*/
return kQCPlugInTimeModeNone;
}
- (id) init
{
if(self = [super init]) {
/*
Allocate any permanent resource required by the plug-in.
*/
self.oscManager = [[[OSCManager alloc] init] autorelease];
self.oscParameters = [NSArray array];
}
return self;
}
- (void) finalize
{
/*
Release any non garbage collected resources created in -init.
*/
[super finalize];
}
- (void) dealloc
{
[oscPort release];
[oscManager release];
[super dealloc];
}
- (QCPlugInViewController*) createViewController
{
return [[BBOSCPlugInSenderViewController alloc] initWithPlugIn:self
viewNibName:@"BBOSCSender"];
}
+ (NSArray*) plugInKeys {
return [NSArray arrayWithObjects:@"oscParameters", nil];
}
-(void)setOscParameters:(NSArray*)params {
NSArray* originalPortKeys = [oscParameters map:^(id port){ return [port objectForKey:BBOSCPortKey]; }];
[self willChangeValueForKey:@"oscParameters"];
[oscParameters release];
oscParameters = [params retain];
[self didChangeValueForKey:@"oscParameters"];
// Bleh, just trash all the original input ports
for(NSString* portKey in originalPortKeys) {
[self removeInputPortForKey:portKey];
}
for(NSDictionary* port in oscParameters) {
NSString* key = [port objectForKey:BBOSCPortKey];
NSNumber* oscType = [port objectForKey:BBOSCTypeKey];
NSString* qcType;
switch([oscType intValue]) {
case BBOSCTypeBool:
qcType = QCPortTypeBoolean;
break;
case BBOSCTypeInt:
qcType = QCPortTypeIndex;
break;
case BBOSCTypeFloat:
qcType = QCPortTypeNumber;
break;
case BBOSCTypeString:
qcType = QCPortTypeString;
break;
default:
qcType = QCPortTypeStructure;
break;
}
NSString* name = [NSString stringWithFormat:@"OSC-%@", [[BBOSCTypeToStringTransformer transformer] transformedValue:oscType]];
NSDictionary* attributes = [NSDictionary dictionaryWithObjectsAndKeys:name, QCPortAttributeNameKey, nil];
[self addInputPortWithType:qcType forKey:key withAttributes:attributes];
}
}
-(NSArray*)oscParameters {
return oscParameters;
}
@end
@implementation BBOSCPlugInSender (Execution)
- (BOOL) startExecution:(id<QCPlugInContext>)context
{
/*
Called by Quartz Composer when rendering of the composition starts: perform any required setup for the plug-in.
Return NO in case of fatal failure (this will prevent rendering of the composition to start).
*/
return YES;
}
- (void) enableExecution:(id<QCPlugInContext>)context
{
/*
Called by Quartz Composer when the plug-in instance starts being used by Quartz Composer.
*/
}
- (BOOL) execute:(id<QCPlugInContext>)context atTime:(NSTimeInterval)time withArguments:(NSDictionary*)arguments
{
/*
Called by Quartz Composer whenever the plug-in instance needs to execute.
Only read from the plug-in inputs and produce a result (by writing to the plug-in outputs or rendering to the destination OpenGL context) within that method and nowhere else.
Return NO in case of failure during the execution (this will prevent rendering of the current frame to complete).
The OpenGL context for rendering can be accessed and defined for CGL macros using:
CGLContextObj cgl_ctx = [context CGLContextObj];
*/
BOOL inputsChanged = NO;
if ([self didValueForInputKeyChange:@"inputBroadcastPort"]) {
if (self.oscPort)
[self.oscManager removeOutput:self.oscPort];
self.oscPort = [self.oscManager createNewOutputToAddress:@"0.0.0.0" atPort:self.inputBroadcastPort withLabel:@"BB OSC"];
inputsChanged = YES;
}
for(NSDictionary* port in oscParameters) {
NSString* key = [port objectForKey:BBOSCPortKey];
if ([self didValueForInputKeyChange:key]) {
inputsChanged = YES;
break;
}
}
if ([self didValueForInputKeyChange:@"inputBroadcastPath"])
inputsChanged = YES;
if (!inputsChanged)
return YES;
OSCMessage* message = [OSCMessage createWithAddress:self.inputBroadcastPath];
for(NSDictionary* port in oscParameters) {
NSString* key = [port objectForKey:BBOSCPortKey];
id value = [self valueForInputKey:key];
BBOSCType oscType = [[port objectForKey:BBOSCTypeKey] intValue];
[message addNSValue:value withBias:oscType];
}
[self.oscPort sendThisMessage:message];
return YES;
}
- (void) disableExecution:(id<QCPlugInContext>)context
{
/*
Called by Quartz Composer when the plug-in instance stops being used by Quartz Composer.
*/
}
- (void) stopExecution:(id<QCPlugInContext>)context
{
/*
Called by Quartz Composer when rendering of the composition stops: perform any required cleanup for the plug-in.
*/
}
@end