-
Notifications
You must be signed in to change notification settings - Fork 0
/
VHandler.c++
717 lines (635 loc) · 16.2 KB
/
VHandler.c++
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
#include "VHandler.h"
#include <algorithm>
#include <vector>
#include "VGenActor.h"
VHandler* VHandler::rgDyingHandler [cDyingHandlerLim];
VHandler* VHandler::rgDyingHandler2[cDyingHandlerLim];
int VHandler::cDyingHandler = 0;
int VHandler::cDyingHandler2 = 0;
VHandler** VHandler::pDyingHandler = rgDyingHandler;
VHandler** VHandler::pDyingHandler2 = rgDyingHandler2;
VHandler::VHandler(VAlgorithm* const a) :
zAmp(1.),
zInputAmp(1.),
fLinearEnv(0),
input(NULL),
valg(a),
parentHandle(hNil)
{
if (!valg)
printf("vss internal error: new Handler got a NULL Algorithm.\n");
setTypeName("VHandler");
}
// Remove myself from my parent's brood.
VHandler::~VHandler()
{
pDyingHandler[cDyingHandler++] = this; // For this->FValid().
const auto p = getParent();
if (p)
p->removeChild(this);
}
// Any entry point of a handler (act(), receiveMessage(), etc.)
// which does CommandIs("SetInput"), setInput(), getAlg()->setSource()
// or other such thing which makes use of a handler foo,
// must be preceded by a call to foo->FValid().
// Otherwise, foo may have been already deleted.
// This clever function works even after the destructor's been called,
// but for a limited time only (two calls to doActors()).
//
int VHandler::FValid()
{
int i;
for (i=0; i<cDyingHandler; i++)
if (this == pDyingHandler[i])
return 0; // I've been deleted!
for (i=0; i<cDyingHandler2; i++)
if (this == pDyingHandler2[i])
return 0; // I've been deleted!
// Phew, I still exist.
return 1;
}
VGeneratorActor* VHandler::getParent() const {
if (parentHandle == hNil) {
printf("vss internal error: Handler has null parent.\n");
return nullptr;
}
const auto p = getByHandle(parentHandle);
if (!p) {
printf("vss internal error: Handler has missing parent.\n");
return nullptr;
}
return p->as_generator();
}
void VHandler::allAct()
{
// Swap buffers for the FValid() test.
if (pDyingHandler == rgDyingHandler)
{
pDyingHandler = rgDyingHandler2;
pDyingHandler2 = rgDyingHandler;
}
else
{
pDyingHandler = rgDyingHandler;
pDyingHandler2 = rgDyingHandler2;
}
// if (cDyingHandler!=0 || cDyingHandler2!=0) printf("\t\t\t\tcDyingHandler = %d, prev = %d\n", cDyingHandler, cDyingHandler2);;
cDyingHandler2 = cDyingHandler;
cDyingHandler = 0;
}
// Control-rate behavior.
void VHandler::act()
{
modpool.act(this);
VActor::act();
}
int VHandler::receiveMessage(const char * Message)
{
CommandFromMessage(Message);
if (CommandIs("BeginNote"))
{
fprintf(stderr, "vss warning: you mean BeginSound not BeginNote.\n");
strcpy(sscanf_cmd, "BeginSound");
goto LBeginSound;
}
if (CommandIs("BeginNotePaused"))
{
fprintf(stderr, "vss warning: you mean BeginSoundPaused not BeginNotePaused.\n");
strcpy(sscanf_cmd, "BeginSoundPaused");
goto LBeginSound;
}
if (CommandIs("BeginSound") || CommandIs("BeginSoundPaused"))
// re-attack the note, possibly with new initializers
{
LBeginSound:
restrike( sscanf_msg );
setPause(CommandIs("BeginSoundPaused"));
return Catch();
}
if (CommandIs("SetPause"))
{
ifD(f, setPause(f) );
return Uncatch();
}
if (CommandIs("SetMute"))
{
ifD(f, setMute(f) );
return Uncatch();
}
if (CommandIs("SetInputAmp"))
{
ifFF(z,z2, setInputAmp(z, z2));
ifF(z, setInputAmp(z));
return Uncatch();
}
if (CommandIs("SetInputGain"))
{
ifFF(z,z2, setInputGain(z, z2));
ifF( z, setInputGain(z) );
return Uncatch();
}
if (CommandIs("InvertAmp"))
{
ifD(f, invertAmp(f) );
return Uncatch();
}
if (CommandIs("SetAmp"))
{
LSetAmp:
ifFF(z,z2, setAmp(z, z2));
ifF(z, setAmp(z));
return Uncatch();
}
if (CommandIs("SetAmpl"))
{
fprintf(stderr, "vss warning: you mean SetAmp not SetAmpl.\n");
goto LSetAmp;
}
if (CommandIs("SetGain"))
{
ifFF(z,z2, setGain(z, z2));
ifF( z, setGain(z) );
return Uncatch();
}
if (CommandIs("ScaleGain"))
{
ifFF(z,z2, scaleGain(z, z2));
ifF(z, scaleGain(z));
return Uncatch();
}
if (CommandIs("ScaleAmp"))
{
ifFF(z,z2, scaleAmp(z, z2));
ifF(z, scaleAmp(z));
return Uncatch();
}
if (CommandIs("SetPan"))
{
ifFF(z,z2, setPan(z, z2));
ifF( z, setPan(z) );
return Uncatch();
}
if (CommandIs("SetElev"))
{
ifFF(z,z2, setElev(z, z2));
ifF( z, setElev(z) );
return Uncatch();
}
if (CommandIs("SetDistance"))
{
ifFF(z,z2, setDistance(z, z2));
ifF( z, setDistance(z) );
return Uncatch();
}
if (CommandIs("SetDistanceHorizon"))
{
ifF( z, setDistanceHorizon(z) );
return Uncatch();
}
if (CommandIs("SetXYZ"))
{
ifFFFF(x,y,z,z2, setXYZ(x,y,z, z2));
ifFFF( x,y,z, setXYZ(x,y,z) );
return Uncatch();
}
if (CommandIs("SetLinearEnv"))
{
ifD( lin, setLinear(lin) );
return Uncatch();
}
if (CommandIs("SetInput"))
{
ifF(z, setInput(z) );
ifNil(setInput());
}
if (CommandIs("SetNumChans"))
{
ifD(d, getAlg()->Nchan(d) );
return Uncatch();
}
if (CommandIs("SetChannelAmps"))
{
ifFloatArray( rgz, cz, setChannelAmps(rgz, cz) );
return Uncatch();
}
if (CommandIs("SetChannel"))
{
ifD( d, setChannel(d) );
return Uncatch();
}
if (CommandIs("Debug"))
{
ifD(f, setDebug(f) );
return Uncatch();
}
return VActor::receiveMessage(Message);
}
void VHandler::setDebug(int f)
{
VActor::setDebug(f);
getAlg()->setDebug(f);
}
// Specify a source for ring modulation.
void VHandler::setInput(float hSrc)
{
input = getByHandle( hSrc )->as_handler();
if (!input || !input->FValid())
{
// That handler must have just got deleted. Oh well.
fprintf(stderr, "vss %s error: invalid input\n", typeName());
setInput();
return;
}
getAlg()->setSource(input->getAlg());
}
void VHandler::setInput(void)
{
input = NULL;
getAlg()->setSource(NULL);
}
// Derived classes that need to do something fancier to initialize
// themselves should override restrike(). In most cases, the derived
// class should call its parent class' restrike() in addition to
// performing its own state initialization.
void
VHandler::restrike(const char * inits_msg)
{
const auto p = getParent();
if (!p)
{
printf("vss warning: Handler has no parent, so it can't restrike.\n");
return;
}
p->sendDefaults(this);
p->parseInitializers(inits_msg, this);
}
void VHandler::SetAttribute(IParam, float)
{
cerr << "vss warning: SetAttribute() unimplemented in handler " << typeName() << endl;
}
void VHandler::SetAttribute(IParam, const float*)
{
cerr << "vss warning: SetAttribute() unimplemented in handler " << typeName() << endl;
}
//===========================================================================
// setGain, etc.
// Send these values to the algorithm.
float VHandler::AdjustTime(float& time)
{
if (time == timeDefault)
time = getPause() ? 0. : dampingTime();
else if (time < 0.)
time = 0.;
return time;
}
void
VHandler::setInputGain(float a, float time)
{
getAlg()->setInputGain(a, AdjustTime(time));
}
void
VHandler::setInputAmp(float a, float time)
{
if (a<0.)
{
fprintf(stderr, "vss warning: negative SetInputAmp amplitude %g rounded up to zero\n", a);
a=0.;
}
getAlg()->setInputAmp( zInputAmp=a, AdjustTime(time));
}
void
VHandler::invertAmp(int f)
{
getAlg()->invertAmp(f);
}
void
VHandler::setGain(float a, float time)
{
getAlg()->setGain(a, AdjustTime(time));
}
void
VHandler::setAmp(float a, float time)
{
if (a<0.)
{
fprintf(stderr, "vss warning: negative SetAmp amplitude %g rounded up to zero\n", a);
a=0.;
}
getAlg()->setAmp( zAmp=a, AdjustTime(time) );
}
void
VHandler::scaleGain(float a, float time)
{
getAlg()->scaleGain(a, AdjustTime(time));
}
void
VHandler::scaleAmp(float a, float time)
{
if (a<0.)
{
fprintf(stderr, "vss warning: negative ScaleAmp amplitude %g rounded up to zero\n", a);
a=0.;
}
getAlg()->scaleAmp(a, AdjustTime(time));
}
void
VHandler::setPan(float a, float time)
{
if (a < -1.0) a = -1.0;
else if (a > 1.0) a = 1.0;
getAlg()->setPan(a, AdjustTime(time));
}
void
VHandler::setElev(float a, float time)
{
if (a < -1.0) a = -1.0;
else if (a > 1.0) a = 1.0;
getAlg()->setElev(a, AdjustTime(time));
}
void
VHandler::setDistance(float a, float time)
{
if (a<0.) a=0.;
getAlg()->setDistance(a, AdjustTime(time));
}
void VHandler::setDistanceHorizon(float a)
{
getAlg()->setDistanceHorizon(a);
}
void VHandler::setXYZ(float x, float y, float z, float time)
{
// Calls SetPan SetElev SetDistance, in cave coords.
// Passes on "time" literally to SetPan SetElev SetDistance.
AdjustTime(time);
y -= 5.; // Origin is (0, 5, 0) in cave coords.
float myPan;
if (Nchans() == 2)
{
// A *very* crude approximation, so stereo stands a chance.
// Pan hard left or right for sounds with azimuth > 45 degrees
// from straight ahead or behind; linearly interpolate, in between.
// Ignore y-coordinate.
// (We shouldn't really: for large |y|, pan should be near 0.)
// Tweak: sounds less than 6 feet away (ignoring y),
// pan closer to centered the smaller their distance is.
myPan = fabs(z) < .001 ? x : x / fabs(z);
if (myPan < -1.) myPan = -1.;
else if (myPan > 1.) myPan = 1.;
float centerweighting = (6. - fsqrt(x*x + z*z)) / 6.;
if (centerweighting > 0.)
myPan *= (1 - centerweighting);
}
else
myPan = atan2(x, -z) / M_PI;
setPan(myPan, time);
// Kinda bogus: can't distinguish elevations > .61 radians (atan(1/sqrt(2)).
const auto e = std::clamp(atan2(double(y), double(hypot(x, z))), -0.61548, 0.61548) / 0.61548;
setElev(e, time);
setDistance(fsqrt(x*x + y*y + z*z), time);
}
void
VHandler::setChannel(int iChan)
{
if (iChan<0 || iChan>Nchans())
{
fprintf(stderr, "vss error: SetChannel(%d) out of range 0..%d\n",
iChan, Nchans()-1);
return;
}
float rgz[MaxNumChannels] = {0};
rgz[iChan] = 1.;
setChannelAmps(rgz, Nchans());
}
void
VHandler::setChannelAmps(float* rgz, int cz)
{
if (cz != Nchans())
{
fprintf(stderr, "vss error: SetChannelAmps got %d values instead of %d.\n",
cz, Nchans());
return;
}
getAlg()->setAmplsDirectly(1); // disable setPan etc.
getAlg()->setPanAmps(rgz);
}
void
VHandler::setLinear(int fLin)
{
getAlg()->setLinear( fLinearEnv = fLin );
}
// Start at zero, then ramp amplitudes up to what they are right now.
// This thing is nonorthogonal to the rest of this code. Only gran/granHand.c++ uses it.
void
VHandler::RampUpAmps(float time)
{
float a = zAmp;
setAmp( 0., 0.);
setAmp(a, time);
}
ostream& VHandler::dump(ostream& os, int tabs)
{
VActor::dump(os, tabs);
indent(os, tabs) << "Parent generator actor handle: " << parentHandle << endl;
indent(os, tabs) << "Amp: " << zAmp << endl;
return os;
}
//===========================================================================
// VModulator stuff.
VFloatParam::VFloatParam(float oldVal, float newVal, float modTime) :
VModulator(),
dstVal(newVal)
{
// special case, which is quite common actually
if (oldVal == newVal)
{
// Don't bother to initialize dstSamp, because when fDone, only newVal is used.
fDone = 1;
return;
}
const float modSamps = modTime * globs.SampleRate;
slope = modSamps < 1. ? 0. : (newVal - oldVal) / modSamps;
#ifdef DEBUG
if (oldVal != oldVal)
cerr << "vss internal error: VFloatParam::VFloatParam(oldval) bogus, probably from an uninitialized member variable.\n";
printf("\t\tVFloatParam::VFloatParam(%g %g %g)\n", oldVal, newVal, modTime);
#endif
dstSamp = SamplesToDate() + modSamps;
}
float VFloatParam::currentValue()
{
if (dstSamp <= SamplesToDate())
{
// We're finished.
fDone = 1;
return dstVal;
}
return dstVal - ((double)(dstSamp - SamplesToDate()) * slope);
}
float VFloatArrayElement::currentValue()
{
if (dstSamp <= SamplesToDate())
{
// We're finished.
fDone = 1;
return dstVal;
}
return dstVal - ((double)(dstSamp - SamplesToDate()) * slope);
}
VFloatArrayElement::VFloatArrayElement(float oldVal, float newVal, float modTime) :
VModulator(),
dstVal(newVal)
{
// special case, which is quite common actually
if (oldVal == newVal)
{
dstSamp = -1000000L;
fDone = 1;
return;
}
float modSamps = modTime * globs.SampleRate;
slope = modSamps < 1. ? 0. : (newVal - oldVal) / modSamps;
dstSamp = SamplesToDate() + modSamps;
}
VFloatArray::VFloatArray(int sizeArg, const float* oldVals, const float* newVals, float modTime) :
VModulator(),
size(sizeArg),
dstVals(new float[sizeArg]),
curVals(new float[sizeArg]),
slopes(new float[sizeArg])
{
FloatCopy(dstVals, newVals, size);
// special case, which is quite common actually: old=new
if (!memcmp(oldVals, newVals, size*sizeof(float)))
{
dstSamp = -1000000L;
fDone = 1;
return;
}
float modSamps = modTime * globs.SampleRate;
for (int i=0; i<size; i++)
slopes[i] = modSamps < 1. ? 0. :
(newVals[i] - oldVals[i]) / modSamps;
dstSamp = SamplesToDate() + modSamps;
}
VFloatArray::~VFloatArray()
{
delete [] dstVals;
delete [] curVals;
delete [] slopes;
}
float* VFloatArray::currentValue()
{
// If our time is up, set the slope to zero and return dstVals.
if (dstSamp <= SamplesToDate())
{
fDone = 1;
return dstVals;
}
for (int i=0; i<size; i++)
{
curVals[i] = dstVals[i] -
((double)(dstSamp - SamplesToDate()) * slopes[i]);
}
return curVals;
}
int VFloatParam::SetAttribute(VHandler* phandler, IParam iParam)
{
// This if() is the only difference between classes VFloatParam and VFloatArrayElement.
if (!iParam.FOnlyI())
{
cerr << "vss internal error in VFloatParam::SetAttribute()\n";
return 0;
}
phandler->SetAttribute(iParam, currentValue());
return !fDone;
}
int VFloatArrayElement::SetAttribute(VHandler* phandler, IParam iParam)
{
// This if() is the only difference between classes VFloatParam and VFloatArrayElement.
if (iParam.Iz() < 0)
{
cerr << "vss internal error in VFloatArrayElement::SetAttribute()\n";
return 0;
}
phandler->SetAttribute(iParam, currentValue());
return !fDone;
}
int VFloatArray::SetAttribute(VHandler* phandler, IParam iParam)
{
if (iParam.Cz() < 0)
{
cerr << "vss internal error in VFloatArray::SetAttribute()\n";
return 0;
}
phandler->SetAttribute(iParam, currentValue());
return !fDone;
}
void VModulatorPool::insert(VHandler& handler, int iParam,
float duration, float zCur, float zEnd)
{
IParam i(iParam);
insertPrep(i);
// This comparison could just be "if duration == 0."
// It should be if dur < "one interval of 'control rate behavior'", how often allAct() is called.
// But VActor.h says that "doesn't occur at any guaranteed rate."
// allAct() is called by doActors(), called in platform.c++
// by either a callback, or LiveTick/BatchTick from schedulerMain's fast inner loop
// where Scount() aka snd_pcm_avail_update or MaxSampsPerBuffer
// reports how many samples to compute.
// But doSynth() ignores its Scount() arg, instead using MaxSampsPerBuffer!
// todo: cache this multiply in VSSglobals, in a getter, when that class is revamped.
if (duration <= MaxSampsPerBuffer * globs.OneOverSR)
handler.SetAttribute(i, zEnd);
else
modmap.emplace(i, std::make_unique<VFloatParam>(zCur, zEnd, duration));
}
void VModulatorPool::insert(VHandler& handler, int iParam,
float duration, int iz, float zCur, float zEnd)
{
IParam i(iParam, iz);
insertPrep(i);
if (duration <= MaxSampsPerBuffer * globs.OneOverSR)
handler.SetAttribute(i, zEnd);
else
modmap.emplace(i, std::make_unique<VFloatArrayElement>(zCur, zEnd, duration));
}
void VModulatorPool::insert(VHandler& handler, int iParam,
float duration, int cz, const float* rgzCur, const float* rgzEnd)
{
IParam i(iParam, cz);
insertPrep(i);
if (duration <= MaxSampsPerBuffer * globs.OneOverSR)
handler.SetAttribute(i, rgzEnd);
else
modmap.emplace(i, std::make_unique<VFloatArray>(cz, rgzCur, rgzEnd, duration));
}
// If iparam already has a modulator,
// delete that in preparation for giving it a new one.
void VModulatorPool::insertPrep(const IParam iparam)
{
if (modmap.find(iparam) != modmap.end())
modmap.erase(iparam);
}
void VModulatorPool::act(VHandler* phandler)
{
if (modmap.empty())
return;
SanityCheck(phandler);
std::vector<Modmap::const_iterator> deletia;
for (auto i = modmap.cbegin(); i != modmap.cend(); ++i) {
// i->first is an IParam
// i->second is a VModulator*
if (!i->second->SetAttribute(phandler, i->first)) {
// It finished modulating, so we can clean it up.
deletia.push_back(i);
}
}
for (auto i: deletia) modmap.erase(i);
SanityCheck(phandler);
}
void VModulatorPool::SanityCheck(VHandler* phandler)
{
// Do other checking here too.
if (!phandler)
cerr << "vss internal error: VModulatorPool::SanityCheck() null phandler\n";
}