-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVGenActor.c++
More file actions
483 lines (414 loc) · 9.9 KB
/
Copy pathVGenActor.c++
File metadata and controls
483 lines (414 loc) · 9.9 KB
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#include "VGenActor.h"
#include "VHandler.h"
#include "VAlgorithm.h" // just for ScalarFromdB()
VGeneratorActor::VGeneratorActor(void):
VActor(),
zAmpl(1.),
zScaleAmp(1.),
pan(0.),
elev(0.),
distance(0.), // cave coords
distanceHorizon(70.), // cave coords
xPos(0.), yPos(5.3), zPos(0.), // cave coords
fLinearEnv(0),
fDying(false)
{
}
VGeneratorActor::~VGeneratorActor()
{
fDying = true; // Prevent removeChild from updating children, which would invalidate our HandlerListIterator (2011).
for (auto child: children) delete child;
}
// When a child is created a a result of a BeginSound message being received, a new
// handler (from newHandler()) is added to this actor's list of progeny using addChild().
void
VGeneratorActor::addChild(VHandler* h)
{
if (!children.insert(h).second)
std::cerr << "vss internal error: VGeneratorActor::addChild() failed.\n";
h->setParent(handle());
}
// When a handler is deleted, it informs the parent generator actor so
// that the latter's children list can be kept up to date.
void
VGeneratorActor::removeChild(VHandler* h)
{
if (fDying)
return;
const auto it = children.find(h);
if (it == children.end())
std::cerr << "vss internal error: VGeneratorActor::removeChild() failed.\n";
else
children.erase(it);
}
// Derived classes that have parameters of their own should override
// this member to send the default values to a handler instance.
// DON'T FORGET TO CALL THE PARENT'S SENDDEFAULTS() SO THAT INHERITED
// DEFAULTS ARE ALSO SENT.
void
VGeneratorActor::sendDefaults(VHandler * h)
{
if (!h)
{
fprintf(stderr, "vss error: VGeneratorActor::sendDefaults failing, null VHandler*\n");
return;
}
if (!h->getAlg())
{
// getAlg() already printed something when it returned NULL.
fprintf(stderr, "\tVGeneratorActor::sendDefaults failing because of NULL getAlg().\n");
return;
}
h->setMute(false);
h->setLinear(fLinearEnv);
// Start at zero, immediately.
h->setPause(true);
h->setAmp(0.);
h->scaleAmp(0.);
h->setPan(pan);
h->setElev(elev);
h->setDistance(distance);
h->setDistanceHorizon(distanceHorizon);
h->setPause(false); // so the notes don't "click" on, if dampingTime>0.
h->setAmp(zAmpl);
h->scaleAmp(zScaleAmp);
}
// Derived classes should override this member if they receive messages.
// DON'T FORGET TO CALL THE PARENT'S RECEIVEMESSAGE() FOR MESSAGES YOU
// DON'T HANDLE.
int
VGeneratorActor::receiveMessage(const char * Message)
{
CommandFromMessage(Message);
if (CommandIs("BeginNote"))
{
fprintf(stderr, "vss: you mean BeginSound not BeginNote.\n");
strcpy(sscanf_cmd, "BeginSound");
goto LBeginNote;
}
if (CommandIs("BeginNotePaused"))
{
fprintf(stderr, "vss: you mean BeginSoundPaused not BeginNotePaused.\n");
strcpy(sscanf_cmd, "BeginSoundPaused");
goto LBeginNote;
}
if (CommandIs("BeginSound") || CommandIs("BeginSoundPaused"))
{
LBeginNote:
auto phandler = newHandler(); // fooHand's ctor cannot return nullptr.
addChild(phandler);
sendDefaults(phandler);
// save this because CommandIs() doesn't seem
// to work after parseInitializers().
int fPause = CommandIs("BeginSoundPaused");
phandler->setPause(1);
parseInitializers(sscanf_msg, phandler);
phandler->setPause(fPause);
// Return handle to client.
ReturnFloatToClient(phandler->handle());
return Catch();
}
if (CommandIs("InvertAmp"))
{
ifD( f, invertAmp(f) );
return Uncatch();
}
if (CommandIs("InvertAllAmp"))
{
ifD( f, invertAllAmp(f));
return Uncatch();
}
if (CommandIs("SetInputAmp"))
{
ifF( z, setInputAmp(z) );
return Uncatch();
}
if (CommandIs("SetAllInputAmp"))
{
ifFF( z, time, setAllInputAmp(z, time));
ifF( z, setAllInputAmp(z));
return Uncatch();
}
if (CommandIs("SetInputGain"))
{
ifF( z, setInputGain(z) );
return Uncatch();
}
if (CommandIs("SetAllInputGain"))
{
ifFF( z, time, setAllInputGain(z, time));
ifF( z, setAllInputGain(z));
return Uncatch();
}
if (CommandIs("SetAmpl"))
{
fprintf(stderr, "vss: I assume you meant SetAmp not SetAmpl.\n");
goto LSetAmp;
}
if (CommandIs("ScaleAmp"))
{
ifF( z, scaleAmp(z) );
ifFF( z, time,
fprintf(stderr, "vss error: scaleAmp with duration works only with handlers, not actors such as %s:\n\t\"%s\"",
typeName(), Message) );
return Uncatch();
}
if (CommandIs("ScaleAllAmp"))
{
ifFF( z, time, scaleAllAmp(z, time));
ifF( z, scaleAllAmp(z));
return Uncatch();
}
if (CommandIs("SetAmp"))
{
LSetAmp:
ifF( z, setAmp(z) );
ifFF( z, time,
fprintf(stderr, "vss error: setAmp with duration works only with handlers, not actors such as %s:\n\t\"%s\"",
typeName(), Message) );
return Uncatch();
}
if (CommandIs("SetAllAmp"))
{
ifFF( z, time, setAllAmp(z, time));
ifF( z, setAllAmp(z));
return Uncatch();
}
if (CommandIs("SetGain"))
{
ifF( z, setGain(z) );
return Uncatch();
}
if (CommandIs("SetAllGain"))
{
ifFF( z, time, setAllGain(z, time));
ifF( z, setAllGain(z));
return Uncatch();
}
if (CommandIs("SetPan"))
{
ifF( z, setPan(z) );
return Uncatch();
}
if (CommandIs("SetAllPan"))
{
ifFF( z, time, setAllPan(z, time));
ifF( z, setAllPan(z) );
return Uncatch();
}
if (CommandIs("SetElev"))
{
ifF( z, setElev(z) );
return Uncatch();
}
if (CommandIs("SetAllElev"))
{
ifFF( z, time, setAllElev(z, time));
ifF( z, setAllElev(z) );
return Uncatch();
}
if (CommandIs("SetDistance"))
{
ifF( d, setDistance(d) );
return Uncatch();
}
if (CommandIs("SetAllDistance"))
{
ifFF( d, time, setAllDistance(d, time));
ifF( d, setAllDistance(d) );
return Uncatch();
}
if (CommandIs("SetXYZ"))
{
ifFFF( x,y,z, setXYZ(x,y,z) );
return Uncatch();
}
if (CommandIs("SetAllXYZ"))
{
ifFFFF( x,y,z, time, setAllXYZ(x,y,z, time));
ifFFF( x,y,z, setAllXYZ(x,y,z) );
return Uncatch();
}
if (CommandIs("SetDistanceHorizon"))
{
ifF( d, setDistanceHorizon(d) );
return Uncatch();
}
if (CommandIs("SetAllDistanceHorizon"))
{
ifF( d, setAllDistanceHorizon(d) );
return Uncatch();
}
if (CommandIs("SetLinearEnv"))
{
ifD( lin, setLinear(lin) );
return Uncatch();
}
return VActor::receiveMessage(Message);
}
// Called when BeginSound is followed by a list of initializers.
void VGeneratorActor::parseInitializers(const char* inits_msg, VHandler* phandler)
{
// parse this: list of { sz z, or sz sz }
// z is [*]tNUMBER -- use sscanf(%f)
// sz is tSTRING -- use sscanf(%s)
// receiveMessage($1 hHandler $2);
const char* pch = inits_msg;
int cch = 0;
int ichMax = strlen(pch);
char sz0[100];
char sz1[500];
char sz2[50];
char sz3[50];
char szCommand[700];
while (pch-inits_msg < ichMax)
{
//printf("\t\tso far: <%s>, ich=%d\n", pch, pch-inits_msg);;
if (1 != sscanf(pch, "%s %n", sz0, &cch)) // keyword string
break;
if (!isalpha(sz0[0]))
{
fprintf(stderr, "vss warning: expected a keyword, at \"%s\" in \"%s\"\n",
sz0, inits_msg);
break;
}
pch += cch;
if (1 != sscanf(pch, "%s %n", sz1, &cch)) // value, string or float
{
fprintf(stderr, "vss warning: keyword '%s' not followed by its value in \"%s\"\n",
sz0, inits_msg);
break;
}
pch += cch;
if (!strcmp(sz0, "SetXYZ"))
{
if (2 != sscanf(pch, "%s %s %n", sz2, sz3, &cch)) // 2 more floats
{
fprintf(stderr, "vss syntax error: incorrect x-y-z value for \"SetXYZ %s\" in \"%s\"\n", sz1, inits_msg);
break;
}
pch += cch;
strcat(sz1, " ");
strcat(sz1, sz2);
strcat(sz1, " ");
strcat(sz1, sz3);
}
#if 0
// This is dangerous. Some ifFF's (samp/sampHand.c++, chant/chantHand.c++) do NOT have the second F meaning a duration of modulation.
strcat(sz1, " 0"); // Do it immediately not in dampingTime() seconds.
#endif
sprintf(szCommand, "%s %f %s", sz0, phandler->handle(), sz1);
phandler->receiveMessage(szCommand);
}
}
void VGeneratorActor::setInputAmp(float a)
{
zInputAmpl = a;
}
void VGeneratorActor::setInputGain(float a)
{
zInputAmpl = ScalarFromdB(a);
}
void VGeneratorActor::invertAmp(int fInvert)
{
fInvertAmp = fInvert;
}
void VGeneratorActor::setAmp(float a)
{
zAmpl = a;
}
void VGeneratorActor::scaleAmp(float a)
{
zScaleAmp = a;
}
void VGeneratorActor::setGain(float a)
{
zAmpl = ScalarFromdB(a);
}
void VGeneratorActor::setPan(float a)
{
pan = a;
}
void VGeneratorActor::setElev(float a)
{
elev = a;
}
void VGeneratorActor::setDistance(float a)
{
distance = a;
}
void VGeneratorActor::setDistanceHorizon(float a)
{
distanceHorizon = a;
}
void VGeneratorActor::setXYZ(float x, float y, float z)
{
xPos = x; yPos = y; zPos = z;
}
void VGeneratorActor::setLinear(int fLin)
{
fLinearEnv = fLin;
}
#define LoopHandlers(statement) for (auto child: children) child->statement
void VGeneratorActor::setAllInputAmp(float a, float t)
{
LoopHandlers(setInputAmp(a, t));
setInputAmp(a);
}
void VGeneratorActor::setAllInputGain(float a, float t)
{
LoopHandlers(setInputGain(a, t));
setInputGain(a);
}
void VGeneratorActor::invertAllAmp(int fInvert)
{
LoopHandlers(invertAmp(fInvert));
invertAmp(fInvert);
}
void VGeneratorActor::setAllAmp(float a, float t)
{
LoopHandlers(setAmp(a, t));
setAmp(a);
}
void VGeneratorActor::scaleAllAmp(float a, float t)
{
LoopHandlers(scaleAmp(a, t));
scaleAmp(a);
}
void VGeneratorActor::setAllGain(float a, float t)
{
LoopHandlers(setGain(a, t));
setGain(a);
}
void VGeneratorActor::setAllPan(float a, float t)
{
LoopHandlers(setPan(a, t));
setPan(a);
}
void VGeneratorActor::setAllElev(float a, float t)
{
LoopHandlers(setElev(a, t));
setElev(a);
}
void VGeneratorActor::setAllDistance(float a, float t)
{
LoopHandlers(setDistance(a, t));
setDistance(a);
}
void VGeneratorActor::setAllDistanceHorizon(float a)
{
LoopHandlers(setDistanceHorizon(a));
setDistanceHorizon(a);
}
void VGeneratorActor::setAllXYZ(float x, float y, float z, float t)
{
LoopHandlers(setXYZ(x,y,z, t));
setXYZ(x,y,z);
}
ostream& VGeneratorActor::dump(ostream& os, int tabs)
{
VActor::dump(os, tabs);
indent(os, tabs) << " Amp: " << zAmpl << endl;
// indent(os, tabs) << "ScaleAmp: " << zScaleAmp << endl;
return os;
}