File: StkInst.cpp

package info (click to toggle)
supercollider-sc3-plugins 3.7.1~repack-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 14,332 kB
  • ctags: 11,704
  • sloc: cpp: 140,180; lisp: 14,746; ansic: 2,133; xml: 86; makefile: 82; haskell: 21; sh: 8
file content (421 lines) | stat: -rw-r--r-- 12,087 bytes parent folder | download | duplicates (3)
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
#include "SC_PlugIn.h"
InterfaceTable *ft;
World * gWorld;

// STK Instrument Classes
#include <Clarinet.h>
#include <BlowHole.h>
#include <Saxofony.h>
#include <Flute.h>
#include <Brass.h>
#include <BlowBotl.h>
#include <Bowed.h>
#include <Plucked.h>
#include <StifKarp.h>
#include <Sitar.h>
#include <Mandolin.h>
#include <Rhodey.h>
#include <Wurley.h>
#include <TubeBell.h>
#include <HevyMetl.h>
#include <PercFlut.h>
#include <BeeThree.h>
#include <FMVoices.h>
#include <VoicForm.h>
#include <Moog.h>
#include <Simple.h>
#include <Drummer.h>
#include <BandedWG.h>
#include <Shakers.h>
#include <ModalBar.h>
#include <Mesh2D.h>
#include <Resonate.h>
#include <Whistle.h>

/*
void* operator new (unsigned int size)
{
    void* ptr = RTAlloc(gWorld, size);
    return ptr;
}
void operator delete(void* ptr) noexcept
{
    RTFree(gWorld, ptr);
}
*/

using namespace stk;


#define NUM_INSTS 28

// The order of the following list is important.  The location of a particular
// instrument in the list should correspond to that instrument's ProgramChange
// number (i.e. Clarinet = ProgramChange 0).
char insts[NUM_INSTS][10] = { "Clarinet", "BlowHole", "Saxofony", "Flute", "Brass",
                              "BlowBotl", "Bowed", "Plucked", "StifKarp", "Sitar", "Mandolin",
                              "Rhodey", "Wurley", "TubeBell", "HevyMetl", "PercFlut",
                              "BeeThree", "FMVoices", "VoicForm", "Moog", "Simple", "Drummer",
                              "BandedWG", "Shakers", "ModalBar", "Mesh2D", "Resonate", "Whistle" };
#define GETMEM(Inst) RTAlloc(gWorld, sizeof(Inst))
                        
void * getmem(int number){
    
  void *mem=0;
  if     (number==0)  mem = GETMEM(Clarinet);
  else if (number==1)  mem = GETMEM(BlowHole);
  else if (number==2)  mem = GETMEM(Saxofony);
  else if (number==3)  mem = GETMEM(Flute);
  else if (number==4)  mem = GETMEM(Brass);
  else if (number==5)  mem = GETMEM(BlowBotl);
  else if (number==6)  mem = GETMEM(Bowed);
  else if (number==7)  mem = GETMEM(Plucked);
  else if (number==8)  mem = GETMEM(StifKarp);
  else if (number==9)  mem = GETMEM(Sitar);
  else if (number==10) mem = GETMEM(Mandolin);

  else if (number==11) mem = GETMEM(Rhodey);
  else if (number==12) mem = GETMEM(Wurley);
  else if (number==13) mem = GETMEM(TubeBell);
  else if (number==14) mem = GETMEM(HevyMetl);
  else if (number==15) mem = GETMEM(PercFlut);
  else if (number==16) mem = GETMEM(BeeThree);
  else if (number==17) mem = GETMEM(FMVoices);

  else if (number==18) mem = GETMEM(VoicForm);
  else if (number==19) mem = GETMEM(Moog);
  else if (number==20) mem = GETMEM(Simple);
  else if (number==21) mem = GETMEM(Drummer);
  else if (number==22) mem = GETMEM(BandedWG);
  else if (number==23) mem = GETMEM(Shakers);
  else if (number==24) mem = GETMEM(ModalBar);
  else if (number==25) mem = GETMEM(Mesh2D);
  else if (number==26) mem = GETMEM(Resonate);
  else if (number==27) mem = GETMEM(Whistle);

  return mem;
}
int voiceByNumber(int number, Instrmnt **instrument)
{
  int temp = number;
  void *mem = getmem(number);
  if     (number==0)  *instrument = new(mem) Clarinet(10.0);
  else if (number==1)  *instrument = new(mem) BlowHole(10.0);
  else if (number==2)  *instrument = new(mem) Saxofony(10.0);
  else if (number==3)  *instrument = new(mem) Flute(10.0);
  else if (number==4)  *instrument = new(mem) Brass(10.0);
  else if (number==5)  *instrument = new(mem) BlowBotl;
  else if (number==6)  *instrument = new(mem) Bowed(10.0);
  else if (number==7)  *instrument = new(mem) Plucked(5.0);
  else if (number==8)  *instrument = new(mem) StifKarp(5.0);
  else if (number==9)  *instrument = new(mem) Sitar(5.0);
  else if (number==10) *instrument = new(mem) Mandolin(5.0);

  else if (number==11) *instrument = new(mem) Rhodey;
  else if (number==12) *instrument = new(mem) Wurley;
  else if (number==13) *instrument = new(mem) TubeBell;
  else if (number==14) *instrument = new(mem) HevyMetl;
  else if (number==15) *instrument = new(mem) PercFlut;
  else if (number==16) *instrument = new(mem) BeeThree;
  else if (number==17) *instrument = new(mem) FMVoices;

  else if (number==18) *instrument = new(mem) VoicForm;
  else if (number==19) *instrument = new(mem) Moog;
  else if (number==20) *instrument = new(mem) Simple;
  else if (number==21) *instrument = new(mem) Drummer;
  else if (number==22) *instrument = new(mem) BandedWG;
  else if (number==23) *instrument = new(mem) Shakers;
  else if (number==24) *instrument = new(mem) ModalBar;
  else if (number==25) *instrument = new(mem) Mesh2D(10, 10);
  else if (number==26) *instrument = new(mem) Resonate;
  else if (number==27) *instrument = new(mem) Whistle;

  else {
    printf("\nUnknown instrument or program change requested!\n");
    temp = -1;
  }

  return temp;
}


struct StkInst : public Unit
{
    Instrmnt * inst;
    bool  gate;
    float freq;
    float ampat;
    float amprel;

    int m_valueSize;
    int m_valueOffset;
    float *m_values;
    float *old_values;
    bool do_freq;
    int instNumber;
    //StkInst(Unit *unit);
    //~StkInst(){};
};

extern "C"
{
    void StkInst_next(StkInst *unit, int inNumSamples);
    void StkInst_Ctor(StkInst* unit);
    void StkInst_Dtor(StkInst* unit);
}

static void Unit_next_nop(StkInst * unit, int inNumSamples)
{}
void StkInst_Ctor(StkInst* unit) {

    gWorld = unit->mWorld;
    Stk::setSampleRate( SAMPLERATE );

    unit->gate = false;
    unit->freq = IN0(0);
    unit->ampat = 1;
    unit->amprel = 1;
    unit->m_values = 0;
    unit->old_values = 0;
    unit->do_freq = true;
    const int kVarOffset = 5;

    unit->instNumber = IN0(4);
    unit->m_valueSize = unit->mNumInputs - kVarOffset;
    //Print("StkInst\n");
    //Print("StkInst %d,%f;%f,%d,%d\n",unit->instNumber,unit->freq,unit->ampat,unit->mNumInputs,unit->m_valueSize);
    //make it pair
    unit->m_valueSize = floor(unit->m_valueSize /2.0) * 2.0;
    if(unit->m_valueSize > 0){
        const int valuesAllocSize = unit->m_valueSize * sizeof(float);
        char * chunk = (char*)RTAlloc(unit->mWorld, valuesAllocSize*2);
        if (!chunk) {
            Print("StkInst: RT memory allocation failed on values\n");
            SETCALC(Unit_next_nop);
            return;
        }
        unit->m_values  = (float*)chunk;
        unit->old_values = (float*)(chunk + valuesAllocSize);
        for (int i=0; i<unit->m_valueSize; i++)
            unit->old_values[i] = -1; //set old values to something imposible
    }

    unit->inst = NULL;
    try {
        if(voiceByNumber(unit->instNumber,&(unit->inst))==-1){
            Print("StkInst: RT memory allocation failed on instrument\n");
            SETCALC(Unit_next_nop);
            return;
        }
    }
    catch ( StkError & e) {
        Print("StkInst: Error on creation %s",e.getMessageCString());
        SETCALC(Unit_next_nop);
        return;
    }

    SETCALC(StkInst_next);
}

void StkInst_Dtor(StkInst* unit)
{
    //delete unit->m_values;
    //delete unit->inst;
    RTFree(unit->mWorld, unit->m_values);
    RTFree(unit->mWorld, unit->inst);

}

void StkInst_next(StkInst *unit, int inNumSamples)
{
    float *out = OUT(0);

    float freq      = IN0(0);
    bool  gate      = IN0(1) > 0.f;
    float  ampat        = IN0(2);
    float  amprel       = IN0(3);

    
    float *values = unit->m_values;
    float *old_values = unit->old_values;
    int valueSize = unit->m_valueSize;
    int valueOffset = 5;
    for (int i=0; i<valueSize; i++)
        values[i] = IN0(i + valueOffset);

    for(int i=0; i < valueSize ; i+=2){
        if(values[i+1] != old_values[i + 1]){
            unit->inst->controlChange(values[i], values[i+1]);
            old_values[i + 1] = values[i+1];
        }
    }
    if(unit->do_freq && freq != unit->freq){
        unit->inst->setFrequency(freq);
        unit->freq = freq;
    }
    
    if(gate != unit->gate){
        if(gate){
            //Print("Starting freq %f\n",freq);
            unit->inst->noteOn(freq, ampat);
        }else{
            //Print("Stopping\n");
            unit->inst->noteOff(amprel);
        }
        unit->gate = gate;
    }

    for (int i=0; i < inNumSamples; ++i)
    {
        out[i] = unit->inst->tick();// * 7.5f; // Scaled to approx +-1
    }

}

////////////////StkMesh2D
struct StkMesh2D : public Unit
{
    Mesh2D * inst;
    bool  gate;
    float ampat;
    float XD;
    float YD;
    float Xpos;
    float Ypos;
    float decay;

};

extern "C"
{
    void StkMesh2D_next(StkMesh2D *unit, int inNumSamples);
    void StkMesh2D_Ctor(StkMesh2D* unit);
    void StkMesh2D_Dtor(StkMesh2D* unit);
}

static void StkMesh2D_next_nop(StkMesh2D * unit, int inNumSamples)
{}
void StkMesh2D_Ctor(StkMesh2D* unit) {
    gWorld = unit->mWorld;
    Stk::setSampleRate( SAMPLERATE );
    unit->gate = false;
    unit->ampat = IN0(2);
    unit->XD = IN0(3);
    unit->YD = IN0(4);
    unit->Xpos = IN0(5);
    unit->Ypos = IN0(6);
    unit->decay = IN0(7);
    
    void* mem = RTAlloc(unit->mWorld,sizeof(Mesh2D));
    if (!mem) {
            Print("StkMesh2D: RT memory allocation failed on values\n");
            SETCALC(Unit_next_nop);
            return;
        }
    unit->inst = new(mem) Mesh2D(unit->XD,unit->YD); 
    if(unit->inst==NULL){
        Print("StkMesh2D: RT memory allocation failed on instrument\n");
        SETCALC(StkMesh2D_next_nop);
        return;
    }
    //Print("Mesh created\n");
    unit->inst->setDecay(unit->decay);
    SETCALC(StkMesh2D_next);
    //Print("Mesh created2\n");
}

void StkMesh2D_Dtor(StkMesh2D* unit)
{

    //delete unit->inst;
    RTFree(unit->mWorld, unit->inst);
}

void StkMesh2D_next(StkMesh2D *unit, int inNumSamples)
{
    float *out = OUT(0);

    float *inp = IN(0);
    bool  gate = IN0(1) > 0.f;
    unit->ampat = IN0(2);
    unit->XD = IN0(3);
    unit->YD = IN0(4);
    unit->Xpos = IN0(5);
    unit->Ypos = IN0(6);
    unit->decay = IN0(7);
    //for(int i=1;i<8;i++)
    //  Print("IN(%d) is %f",i,IN0(i));
    //Print("\n");
    unit->inst->setInputPosition(unit->Xpos,unit->Ypos);
    if(gate != unit->gate){
        if(gate){
            Print("Starting mesh %f\n",unit->ampat);
            unit->inst->noteOn(10, unit->ampat);
        }else{
            Print("Stopping\n");
            unit->inst->noteOff(0);
        }
        unit->gate = gate;
    }

    for (int i=0; i < inNumSamples; ++i)
    {
        //out[i] = unit->inst->tick();
        out[i] = unit->inst->inputTick(inp[i]);
    }

}
///////////////////////////////////////
struct StkGlobals : public Unit
{
    char *rawwavePath;
    bool showWarnings;
    bool printErrors;
    int m_valueSize;

};
extern "C"
{
    void StkGlobals_next(StkGlobals *unit, int inNumSamples);
    void StkGlobals_Ctor(StkGlobals* unit);
}

void StkGlobals_next(StkGlobals *unit, int inNumSamples){
    NodeEnd(&unit->mParent->mNode);
}
void StkGlobals_Ctor(StkGlobals* unit) {
    gWorld = unit->mWorld;
    Stk::setSampleRate( SAMPLERATE );
    unit->showWarnings = IN0(0) > 0;
    unit->printErrors = IN0(1) > 0;

    const int kVarOffset = 2;
    unit->m_valueSize = unit->mNumInputs - kVarOffset;
    
    if(unit->m_valueSize > 0){
        const int valuesAllocSize = (unit->m_valueSize +1)* sizeof(char);
        char * chunk = (char*)RTAlloc(unit->mWorld, valuesAllocSize);
        if (!chunk) {
            Print("StkGlobals: RT memory allocation failed on values\n");
            SETCALC(Unit_next_nop);
            return;
        }
        unit->rawwavePath  = chunk;
        for(int i=0; i< unit->m_valueSize; i++)
            unit->rawwavePath[i] = IN0(i + kVarOffset);
        
        unit->rawwavePath[unit->m_valueSize] = 0; //null termination
        Stk::setRawwavePath(unit->rawwavePath);
    }
    Stk::showWarnings(unit->showWarnings);
    Stk::printErrors(unit->printErrors);
    RTFree(unit->mWorld,unit->rawwavePath);
    SETCALC(StkGlobals_next);
}
////////////////////////////////////////
PluginLoad(StkUnit){
    ft = inTable;
    DefineDtorUnit(StkInst);
    DefineDtorUnit(StkMesh2D);
    DefineSimpleUnit(StkGlobals);
}