/
SC_GraphDef.cpp
664 lines (568 loc) · 18.7 KB
/
SC_GraphDef.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
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
/*
SuperCollider real time audio synthesis system
Copyright (c) 2002 James McCartney. All rights reserved.
http://www.audiosynth.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "clz.h"
#include "SC_Graph.h"
#include "SC_GraphDef.h"
#include "SC_Wire.h"
#include "SC_WireSpec.h"
#include "SC_UnitSpec.h"
#include "SC_UnitDef.h"
#include "SC_HiddenWorld.h"
#include <stdio.h>
#include <stdlib.h>
#ifndef _MSC_VER
#include <dirent.h>
#endif //_MSC_VER
#include <stdexcept>
#include "ReadWriteMacros.h"
#include "SC_Prototypes.h"
#include "SC_CoreAudio.h"
#include "SC_DirUtils.h"
#ifdef SC_WIN32
#include "SC_Win32Utils.h"
#endif
extern Malloc gMalloc;
int32 GetHash(ParamSpec* inParamSpec)
{
return inParamSpec->mHash;
}
int32* GetKey(ParamSpec* inParamSpec)
{
return inParamSpec->mName;
}
void ReadName(char*& buffer, int32* name);
void ReadName(char*& buffer, int32* name)
{
uint32 namelen = readInt8(buffer);
if (namelen >= kSCNameByteLen) {
throw std::runtime_error("name too long > 31 chars");
namelen = 31;
}
memset(name, 0, kSCNameByteLen);
readData(buffer, (char*)name, namelen);
}
void ParamSpec_Read(ParamSpec* inParamSpec, char*& buffer);
void ParamSpec_Read(ParamSpec* inParamSpec, char*& buffer)
{
ReadName(buffer, inParamSpec->mName);
inParamSpec->mIndex = readInt16_be(buffer);
inParamSpec->mHash = Hash(inParamSpec->mName);
}
void InputSpec_Read(InputSpec* inInputSpec, char*& buffer);
void InputSpec_Read(InputSpec* inInputSpec, char*& buffer)
{
inInputSpec->mFromUnitIndex = (int16)readInt16_be(buffer);
inInputSpec->mFromOutputIndex = (int16)readInt16_be(buffer);
inInputSpec->mWireIndex = -1;
}
void OutputSpec_Read(OutputSpec* inOutputSpec, char*& buffer);
void OutputSpec_Read(OutputSpec* inOutputSpec, char*& buffer)
{
inOutputSpec->mCalcRate = readInt8(buffer);
inOutputSpec->mWireIndex = -1;
inOutputSpec->mBufferIndex = -1;
inOutputSpec->mNumConsumers = 0;
}
void UnitSpec_Read(UnitSpec* inUnitSpec, char*& buffer);
void UnitSpec_Read(UnitSpec* inUnitSpec, char*& buffer)
{
int32 name[kSCNameLen];
ReadName(buffer, name);
inUnitSpec->mUnitDef = GetUnitDef(name);
if (!inUnitSpec->mUnitDef) {
char str[256];
scprintf(str, "UGen '%s' not installed.", (char*)name);
throw std::runtime_error(str);
return;
}
inUnitSpec->mCalcRate = readInt8(buffer);
inUnitSpec->mNumInputs = readInt16_be(buffer);
inUnitSpec->mNumOutputs = readInt16_be(buffer);
inUnitSpec->mSpecialIndex = readInt16_be(buffer);
inUnitSpec->mInputSpec = (InputSpec*)malloc(sizeof(InputSpec) * inUnitSpec->mNumInputs);
inUnitSpec->mOutputSpec = (OutputSpec*)malloc(sizeof(OutputSpec) * inUnitSpec->mNumOutputs);
for (uint32 i=0; i<inUnitSpec->mNumInputs; ++i) {
InputSpec_Read(inUnitSpec->mInputSpec + i, buffer);
}
for (uint32 i=0; i<inUnitSpec->mNumOutputs; ++i) {
OutputSpec_Read(inUnitSpec->mOutputSpec + i, buffer);
}
int numPorts = inUnitSpec->mNumInputs + inUnitSpec->mNumOutputs;
inUnitSpec->mAllocSize = inUnitSpec->mUnitDef->mAllocSize + numPorts * (sizeof(Wire*) + sizeof(float*));
}
GraphDef* GraphDef_Read(World *inWorld, char*& buffer, GraphDef* inList, int32 inVersion);
GraphDef* GraphDefLib_Read(World *inWorld, char* buffer, GraphDef* inList);
GraphDef* GraphDefLib_Read(World *inWorld, char* buffer, GraphDef* inList)
{
int32 magic = readInt32_be(buffer);
if (magic != (('S'<<24)|('C'<<16)|('g'<<8)|'f') /*'SCgf'*/) return inList;
int32 version = readInt32_be(buffer);
if (version > 1) return inList;
uint32 numDefs = readInt16_be(buffer);
for (uint32 i=0; i<numDefs; ++i) {
inList = GraphDef_Read(inWorld, buffer, inList, version);
}
return inList;
}
void ChooseMulAddFunc(GraphDef *graphDef, UnitSpec* unitSpec);
void DoBufferColoring(World *inWorld, GraphDef *inGraphDef);
void GraphDef_ReadVariant(World *inWorld, char*& buffer, GraphDef* inGraphDef, GraphDef* inVariant)
{
memcpy(inVariant, inGraphDef, sizeof(GraphDef));
inVariant->mNumVariants = 0;
inVariant->mVariants = 0;
ReadName(buffer, inVariant->mNodeDef.mName);
inVariant->mNodeDef.mHash = Hash(inVariant->mNodeDef.mName);
inVariant->mInitialControlValues = (float32*)malloc(sizeof(float32) * inGraphDef->mNumControls);
for (uint32 i=0; i<inGraphDef->mNumControls; ++i) {
inVariant->mInitialControlValues[i] = readFloat_be(buffer);
}
}
GraphDef* GraphDef_Read(World *inWorld, char*& buffer, GraphDef* inList, int32 inVersion)
{
int32 name[kSCNameLen];
ReadName(buffer, name);
GraphDef* graphDef = (GraphDef*)calloc(1, sizeof(GraphDef));
graphDef->mOriginal = graphDef;
graphDef->mNodeDef.mAllocSize = sizeof(Graph);
memcpy((char*)graphDef->mNodeDef.mName, (char*)name, kSCNameByteLen);
graphDef->mNodeDef.mHash = Hash(graphDef->mNodeDef.mName);
graphDef->mNumConstants = readInt16_be(buffer);
graphDef->mConstants = (float*)malloc(graphDef->mNumConstants * sizeof(float));
for (uint32 i=0; i<graphDef->mNumConstants; ++i) {
graphDef->mConstants[i] = readFloat_be(buffer);
}
graphDef->mNumControls = readInt16_be(buffer);
graphDef->mInitialControlValues = (float32*)malloc(sizeof(float32) * graphDef->mNumControls);
for (uint32 i=0; i<graphDef->mNumControls; ++i) {
graphDef->mInitialControlValues[i] = readFloat_be(buffer);
}
graphDef->mNumParamSpecs = readInt16_be(buffer);
if (graphDef->mNumParamSpecs) {
int hashTableSize = NEXTPOWEROFTWO(graphDef->mNumParamSpecs);
graphDef->mParamSpecTable = new ParamSpecTable(&gMalloc, hashTableSize, false);
graphDef->mParamSpecs = (ParamSpec*)malloc(graphDef->mNumParamSpecs * sizeof(ParamSpec));
for (uint32 i=0; i<graphDef->mNumParamSpecs; ++i) {
ParamSpec *paramSpec = graphDef->mParamSpecs + i;
ParamSpec_Read(paramSpec, buffer);
graphDef->mParamSpecTable->Add(paramSpec);
}
} else {
// empty table to eliminate test in Graph_SetControl
graphDef->mParamSpecTable = new ParamSpecTable(&gMalloc, 4, false);
graphDef->mParamSpecs = 0;
}
graphDef->mNumWires = graphDef->mNumConstants;
graphDef->mNumUnitSpecs = readInt16_be(buffer);
graphDef->mUnitSpecs = (UnitSpec*)malloc(sizeof(UnitSpec) * graphDef->mNumUnitSpecs);
graphDef->mNumCalcUnits = 0;
for (uint32 i=0; i<graphDef->mNumUnitSpecs; ++i) {
UnitSpec *unitSpec = graphDef->mUnitSpecs + i;
UnitSpec_Read(unitSpec, buffer);
switch (unitSpec->mCalcRate)
{
case calc_ScalarRate :
unitSpec->mRateInfo = &inWorld->mBufRate;
break;
case calc_BufRate :
graphDef->mNumCalcUnits++;
unitSpec->mRateInfo = &inWorld->mBufRate;
break;
case calc_FullRate :
graphDef->mNumCalcUnits++;
unitSpec->mRateInfo = &inWorld->mFullRate;
break;
case calc_DemandRate :
unitSpec->mRateInfo = &inWorld->mBufRate;
break;
}
graphDef->mNodeDef.mAllocSize += unitSpec->mAllocSize;
graphDef->mNumWires += unitSpec->mNumOutputs;
}
DoBufferColoring(inWorld, graphDef);
graphDef->mWiresAllocSize = graphDef->mNumWires * sizeof(Wire);
graphDef->mUnitsAllocSize = graphDef->mNumUnitSpecs * sizeof(Unit*);
graphDef->mCalcUnitsAllocSize = graphDef->mNumCalcUnits * sizeof(Unit*);
graphDef->mNodeDef.mAllocSize += graphDef->mWiresAllocSize;
graphDef->mNodeDef.mAllocSize += graphDef->mUnitsAllocSize;
graphDef->mNodeDef.mAllocSize += graphDef->mCalcUnitsAllocSize;
graphDef->mControlAllocSize = graphDef->mNumControls * sizeof(float);
graphDef->mNodeDef.mAllocSize += graphDef->mControlAllocSize;
graphDef->mMapControlsAllocSize = graphDef->mNumControls * sizeof(float*);
graphDef->mNodeDef.mAllocSize += graphDef->mMapControlsAllocSize;
graphDef->mMapControlRatesAllocSize = graphDef->mNumControls * sizeof(int*);
graphDef->mNodeDef.mAllocSize += graphDef->mMapControlRatesAllocSize;
graphDef->mNext = inList;
graphDef->mRefCount = 1;
if (inVersion >= 1) {
graphDef->mNumVariants = readInt16_be(buffer);
if (graphDef->mNumVariants) {
graphDef->mVariants = (GraphDef*)calloc(graphDef->mNumVariants, sizeof(GraphDef));
for (uint32 i=0; i<graphDef->mNumVariants; ++i) {
GraphDef_ReadVariant(inWorld, buffer, graphDef, graphDef->mVariants + i);
}
}
}
return graphDef;
}
void GraphDef_Define(World *inWorld, GraphDef *inList)
{
GraphDef *graphDef = inList;
while (graphDef) {
GraphDef *next = graphDef->mNext;
GraphDef* previousDef = World_GetGraphDef(inWorld, graphDef->mNodeDef.mName);
if (previousDef) {
World_RemoveGraphDef(inWorld, previousDef);
if (--previousDef->mRefCount == 0) {
GraphDef_DeleteMsg(inWorld, previousDef);
}
}
World_AddGraphDef(inWorld, graphDef);
graphDef->mNext = 0;
graphDef = next;
}
}
void GraphDef_Remove(World *inWorld, int32 *inName)
{
GraphDef* graphDef = World_GetGraphDef(inWorld, inName);
if (graphDef) {
World_RemoveGraphDef(inWorld, graphDef);
if (--graphDef->mRefCount == 0) {
GraphDef_DeleteMsg(inWorld, graphDef);
}
}
}
void GraphDef_DeleteMsg(World *inWorld, GraphDef *inDef)
{
DeleteGraphDefMsg msg;
msg.mDef = inDef;
inWorld->hw->mDeleteGraphDefs.Write(msg);
}
GraphDef* GraphDef_Recv(World *inWorld, char *buffer, GraphDef *inList)
{
try {
inList = GraphDefLib_Read(inWorld, buffer, inList);
} catch (std::exception& exc) {
scprintf("exception in GraphDef_Recv: %s\n", exc.what());
} catch (...) {
scprintf("unknown exception in GraphDef_Recv\n");
}
return inList;
}
GraphDef* GraphDef_LoadGlob(World *inWorld, const char *pattern, GraphDef *inList)
{
SC_GlobHandle* glob = sc_Glob(pattern);
if (!glob) return inList;
const char* filename;
while ((filename = sc_GlobNext(glob)) != 0) {
int len = strlen(filename);
if (strncmp(filename+len-9, ".scsyndef", 9)==0) {
inList = GraphDef_Load(inWorld, filename, inList);
}
// why? <sk>
GraphDef_Load(inWorld, filename, inList);
}
sc_GlobFree(glob);
return inList;
}
GraphDef* GraphDef_Load(World *inWorld, const char *filename, GraphDef *inList)
{
#ifdef SC_WIN32
FILE *file = fopenLocalOrRemote(filename, "rb");
#else
FILE *file = fopenLocalOrRemote(filename, "r");
#endif
if (!file) {
scprintf("*** ERROR: can't fopen '%s'\n", filename);
return inList;
}
fseek(file, 0, SEEK_END);
int size = ftell(file);
char *buffer = (char*)malloc(size);
if (!buffer) {
scprintf("*** ERROR: can't malloc buffer size %d\n", size);
return inList;
}
fseek(file, 0, SEEK_SET);
#ifdef SC_WIN32
size_t readSize = fread(buffer, 1, size, file);
if (readSize!= size)
*((int*)0) = 0;
#else
fread(buffer, 1, size, file);
#endif
fclose(file);
try {
inList = GraphDefLib_Read(inWorld, buffer, inList);
} catch (std::exception& exc) {
scprintf("exception in GrafDef_Load: %s\n", exc.what());
scprintf("while reading file '%s'\n", filename);
} catch (...) {
scprintf("unknown exception in GrafDef_Load\n");
scprintf("while reading file '%s'\n", filename);
}
free(buffer);
return inList;
}
GraphDef* GraphDef_LoadDir(World *inWorld, char *dirname, GraphDef *inList)
{
SC_DirHandle *dir = sc_OpenDir(dirname);
if (!dir) {
scprintf("*** ERROR: open directory failed '%s'\n", dirname);
return inList;
}
for (;;) {
char diritem[MAXPATHLEN];
bool skipItem = false;
bool validItem = sc_ReadDir(dir, dirname, diritem, skipItem);
if (!validItem) break;
if (skipItem) continue;
if (sc_DirectoryExists(diritem)) {
inList = GraphDef_LoadDir(inWorld, diritem, inList);
} else {
int dnamelen = strlen(diritem);
if (strncmp(diritem+dnamelen-9, ".scsyndef", 9) == 0) {
inList = GraphDef_Load(inWorld, diritem, inList);
}
}
}
sc_CloseDir(dir);
return inList;
}
void UnitSpec_Free(UnitSpec *inUnitSpec);
void UnitSpec_Free(UnitSpec *inUnitSpec)
{
free(inUnitSpec->mInputSpec);
free(inUnitSpec->mOutputSpec);
}
void GraphDef_Free(GraphDef *inGraphDef)
{
if (inGraphDef != inGraphDef->mOriginal) return;
for (uint32 i=0; i<inGraphDef->mNumUnitSpecs; ++i) {
UnitSpec_Free(inGraphDef->mUnitSpecs + i);
}
for (uint32 i=0; i<inGraphDef->mNumVariants; ++i) {
free(inGraphDef->mVariants[i].mInitialControlValues);
}
delete inGraphDef->mParamSpecTable;
free(inGraphDef->mParamSpecs);
free(inGraphDef->mInitialControlValues);
free(inGraphDef->mConstants);
free(inGraphDef->mUnitSpecs);
free(inGraphDef->mVariants);
free(inGraphDef);
}
void NodeDef_Dump(NodeDef *inNodeDef)
{
scprintf("mName '%s'\n", (char*)inNodeDef->mName);
scprintf("mHash %d\n", inNodeDef->mHash);
scprintf("mAllocSize %lu\n", inNodeDef->mAllocSize);
}
void GraphDef_Dump(GraphDef *inGraphDef)
{
NodeDef_Dump(&inGraphDef->mNodeDef);
scprintf("mNumControls %d\n", inGraphDef->mNumControls);
scprintf("mNumWires %d\n", inGraphDef->mNumWires);
scprintf("mNumUnitSpecs %d\n", inGraphDef->mNumUnitSpecs);
scprintf("mNumWireBufs %d\n", inGraphDef->mNumWireBufs);
for (uint32 i=0; i<inGraphDef->mNumControls; ++i) {
scprintf(" %d mInitialControlValues %g\n", i, inGraphDef->mInitialControlValues[i]);
}
for (uint32 i=0; i<inGraphDef->mNumWires; ++i) {
//WireSpec_Dump(inGraphDef->mWireSpec + i);
}
for (uint32 i=0; i<inGraphDef->mNumUnitSpecs; ++i) {
//UnitSpec_Dump(inGraphDef->mUnitSpecs + i);
}
}
/*
SynthBufferAllocator
{
var nextBufIndex = 0;
var stack;
var refs;
*new {
^super.new.init
}
init {
refs = Bag.new;
}
alloc { arg count;
var bufNumber;
if (stack.size > 0, {
bufNumber = stack.pop
},{
bufNumber = nextBufIndex;
nextBufIndex = nextBufIndex + 1;
});
refs.add(bufNumber, count);
^bufNumber
}
release { arg bufNumber;
refs.remove(bufNumber);
if (refs.includes(bufNumber).not, { stack = stack.add(bufNumber) });
}
numBufs { ^nextBufIndex }
}
*/
struct BufColorAllocator
{
int16 *refs;
int16 *stack;
int16 stackPtr;
int16 nextIndex;
int16 refsMaxSize;
int16 stackMaxSize;
BufColorAllocator();
~BufColorAllocator();
int alloc(int count);
bool release(int inIndex);
int NumBufs() { return nextIndex; }
};
inline BufColorAllocator::BufColorAllocator()
{
refsMaxSize = 32;
stackMaxSize = 32;
refs = (int16*)calloc(refsMaxSize, sizeof(int16));
stack = (int16*)calloc(stackMaxSize, sizeof(int16));
stackPtr = 0;
nextIndex = 0;
}
inline BufColorAllocator::~BufColorAllocator()
{
free(refs);
free(stack);
}
inline int BufColorAllocator::alloc(int count)
{
int outIndex;
if (stackPtr) {
outIndex = stack[--stackPtr];
} else {
outIndex = nextIndex++;
}
if (outIndex >= refsMaxSize) {
refs = (int16*)realloc(refs, refsMaxSize*2*sizeof(int16));
memset(refs + refsMaxSize, 0, refsMaxSize*sizeof(int16));
refsMaxSize *= 2;
}
refs[outIndex] = count;
return outIndex;
}
inline bool BufColorAllocator::release(int inIndex)
{
if (refs[inIndex] == 0) return false;
if (--refs[inIndex] == 0) {
if (stackPtr >= stackMaxSize) {
stack = (int16*)realloc(stack, stackMaxSize*2*sizeof(int16));
memset(stack + stackMaxSize, 0, stackMaxSize*sizeof(int16));
stackMaxSize *= 2;
}
stack[stackPtr++] = inIndex;
}
return true;
}
void ReleaseInputBuffers(GraphDef *inGraphDef, UnitSpec *unitSpec, BufColorAllocator& bufColor)
{
//scprintf("ReleaseInputBuffers %s numinputs %d\n", unitSpec->mUnitDef->mUnitDefName, unitSpec->mNumInputs);
for (int i=unitSpec->mNumInputs-1; i>=0; --i) {
InputSpec *inputSpec = unitSpec->mInputSpec + i;
if (inputSpec->mFromUnitIndex >= 0) {
UnitSpec *outUnit = inGraphDef->mUnitSpecs + inputSpec->mFromUnitIndex;
OutputSpec *outputSpec = outUnit->mOutputSpec + inputSpec->mFromOutputIndex;
inputSpec->mWireIndex = outputSpec->mWireIndex;
if (outputSpec->mCalcRate == calc_FullRate) {
if (!bufColor.release(outputSpec->mBufferIndex)) {
scprintf("buffer coloring error: tried to release output with zero count\n");
scprintf("output: %d %s %d\n", inputSpec->mFromUnitIndex,
outUnit->mUnitDef->mUnitDefName, inputSpec->mFromOutputIndex);
scprintf("input: %s %d\n", unitSpec->mUnitDef->mUnitDefName, i);
throw std::runtime_error("buffer coloring error.");
}
}
} else {
inputSpec->mWireIndex = inputSpec->mFromOutputIndex;
}
}
}
void AllocOutputBuffers(UnitSpec *unitSpec, BufColorAllocator& bufColor, int& wireIndexCtr)
{
//scprintf("AllocOutputBuffers %s numoutputs %d\n", unitSpec->mUnitDef->mUnitDefName, unitSpec->mNumOutputs);
for (uint32 i=0; i<unitSpec->mNumOutputs; ++i) {
OutputSpec *outputSpec = unitSpec->mOutputSpec + i;
outputSpec->mWireIndex = wireIndexCtr++;
if (outputSpec->mCalcRate == calc_FullRate) {
int bufIndex = bufColor.alloc(outputSpec->mNumConsumers);
outputSpec->mBufferIndex = bufIndex;
}
}
}
void DoBufferColoring(World *inWorld, GraphDef *inGraphDef)
{
// count consumers of outputs
for (uint32 j=0; j<inGraphDef->mNumUnitSpecs; ++j) {
UnitSpec *unitSpec = inGraphDef->mUnitSpecs + j;
for (uint32 i=0; i<unitSpec->mNumInputs; ++i) {
InputSpec *inputSpec = unitSpec->mInputSpec + i;
if (inputSpec->mFromUnitIndex >= 0) {
UnitSpec *outUnit = inGraphDef->mUnitSpecs + inputSpec->mFromUnitIndex;
OutputSpec *outputSpec = outUnit->mOutputSpec + inputSpec->mFromOutputIndex;
outputSpec->mNumConsumers ++;
}
}
}
// buffer coloring
{
BufColorAllocator bufColor;
int wireIndexCtr = inGraphDef->mNumConstants;
for (uint32 j=0; j<inGraphDef->mNumUnitSpecs; ++j) {
UnitSpec *unitSpec = inGraphDef->mUnitSpecs + j;
if (unitSpec->mUnitDef->mFlags & kUnitDef_CantAliasInputsToOutputs) {
// set wire index, alloc outputs
AllocOutputBuffers(unitSpec, bufColor, wireIndexCtr);
// set wire index, release inputs
ReleaseInputBuffers(inGraphDef, unitSpec, bufColor);
} else {
// set wire index, release inputs
ReleaseInputBuffers(inGraphDef, unitSpec, bufColor);
// set wire index, alloc outputs
AllocOutputBuffers(unitSpec, bufColor, wireIndexCtr);
}
}
inGraphDef->mNumWireBufs = bufColor.NumBufs();
if (inWorld->mRunning)
{
// cannot reallocate interconnect buffers while running audio.
if (inGraphDef->mNumWireBufs > inWorld->hw->mMaxWireBufs) {
throw std::runtime_error("exceeded number of interconnect buffers.");
}
} else {
inWorld->hw->mMaxWireBufs = sc_max(inWorld->hw->mMaxWireBufs, inGraphDef->mNumWireBufs);
}
}
// multiply buf indices by buf length for proper offset
int bufLength = inWorld->mBufLength;
for (uint32 j=0; j<inGraphDef->mNumUnitSpecs; ++j) {
UnitSpec *unitSpec = inGraphDef->mUnitSpecs + j;
for (uint32 i=0; i<unitSpec->mNumOutputs; ++i) {
OutputSpec *outputSpec = unitSpec->mOutputSpec + i;
if (outputSpec->mCalcRate == calc_FullRate) {
outputSpec->mBufferIndex *= bufLength;
}
}
}
}