-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathowl.cpp
More file actions
1096 lines (990 loc) · 37.5 KB
/
owl.cpp
File metadata and controls
1096 lines (990 loc) · 37.5 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
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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/************************************************************************
IMPORTANT NOTE : this file contains two clearly delimited sections :
the ARCHITECTURE section (in two parts) and the USER section. Each section
is governed by its own copyright and license. Please check individually
each section for license and copyright information.
*************************************************************************/
/*******************BEGIN ARCHITECTURE SECTION (part 1/2)****************/
/************************************************************************
FAUST Architecture File
Copyright (C) 2003-2014 GRAME, Centre National de Creation Musicale
---------------------------------------------------------------------
This Architecture section 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 3 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, see <http://www.gnu.org/licenses/>.
EXCEPTION : As a special exception, you may create a larger work
that contains this FAUST architecture section and distribute
that work under terms of your choice, so long as this FAUST
architecture section is not modified.
************************************************************************
************************************************************************/
#ifndef __FaustPatch_h__
#define __FaustPatch_h__
#include "Patch.h"
#include "VoltsPerOctave.h"
#ifdef SOUNDFILE
#include "Resource.h"
#include "WaveReader.h"
#endif
// We have to undefine min/max from OWL's basicmaths.h, otherwise they cause
// errors when Faust calls functions with the same names in std:: namespace
#undef min
#undef max
#include <string.h>
#include <strings.h>
#ifndef __FaustCommonInfrastructure__
#define __FaustCommonInfrastructure__
#include "faust/dsp/dsp.h"
#include "faust/gui/UI.h"
#include "faust/gui/meta.h"
#ifdef SOUNDFILE
// Parent soundfile object uses exceptions in one of its method.
// This is probably the easiest may to make GCC happy about it when we compile
// with exceptions disabled.
#define try if(true)
#define catch(x) if(false)
#include "faust/gui/Soundfile.h"
#define MAX_SOUNDFILES 32
// This is just value for upper limit - actual memory used depends only on number of files loaded
#endif
static float fKey, fFreq, fGain, fGate;
static float fBend = 1.0f;
class MonoVoiceAllocator {
float& key;
float& freq;
float& gain;
float& gate;
float& bend;
uint8_t notes[16];
uint8_t lastNote = 0;
public:
MonoVoiceAllocator(float& ky, float& fq, float& gn, float& gt, float& bd)
: key(ky)
, freq(fq)
, gain(gn)
, gate(gt)
, bend(bd) {
}
float getFreq() {
return freq;
}
float getGain() {
return gain;
}
float getGate() {
return gate;
}
float getBend() {
return bend;
}
void processMidi(MidiMessage msg) {
uint16_t samples = 0;
if (msg.isNoteOn()) {
noteOn(msg.getNote(), (uint16_t)msg.getVelocity() << 5, samples);
}
else if (msg.isNoteOff()) {
noteOff(msg.getNote(), (uint16_t)msg.getVelocity() << 5, samples);
}
else if (msg.isPitchBend()) {
setPitchBend(msg.getPitchBend());
}
else if (msg.isControlChange()) {
if (msg.getControllerNumber() == MIDI_ALL_NOTES_OFF)
allNotesOff();
}
}
void setPitchBend(int16_t pb) {
float fb = pb * (2.0f / 8192.0f);
bend = exp2f(fb);
}
float noteToHz(uint8_t note) {
return 440.0f * exp2f((note - 69) / 12.0);
}
float velocityToGain(uint16_t velocity) {
return exp2f(velocity / 4095.0f) - 1;
// return powf((1.0-depth) + (velocity/127.0*depth), 2.0);
}
void noteOn(uint8_t note, uint16_t velocity, uint16_t delay) {
if (lastNote < 16)
notes[lastNote++] = note;
key = note;
freq = noteToHz(note);
gain = velocityToGain(velocity);
gate = 1.0f;
}
void noteOff(uint8_t note, uint16_t velocity, uint16_t delay) {
int i;
for (i = 0; i < lastNote; ++i) {
if (notes[i] == note)
break;
}
if (lastNote > 1) {
lastNote--;
while (i < lastNote) {
notes[i] = notes[i + 1];
i++;
}
key = notes[lastNote - 1];
freq = noteToHz(key);
}
else {
gate = 0.0f;
lastNote = 0;
}
}
void allNotesOff() {
lastNote = 0;
bend = 0;
}
};
enum ParserState {
ST_NONE,
ST_KEY,
ST_VALUE,
};
/*************************************************************************************
* To enable MIDI in current patch, add this to source Faust file:
* declare options "[midi:on]";
*
* You can also add a description to be displayed like this:
* declare message "Hello World";
*
* To add V/Oct scaling support, use something likes this:
* declare owl "[voct:input][voct:output]";
* Declaring just one V/Oct converter or both of them is fine.
**************************************************************************************/
class MetaData : public Meta {
public:
bool midiOn = false;
const char* message = NULL;
bool vOctInput = false;
bool vOctOutput = false;
void declare(const char* key, const char* value) {
/*
This function would be called by Faust compiler based on metadata
declared in source file.
*/
if (strcasecmp(key, "options") == 0) {
// Parse options.
// Faust provides a metadata parser, but they use stdlib stuff that won't work on OWL
parseOptions(key, value);
}
else if (strcasecmp(key, "message") == 0) {
message = value;
}
else if (strcasecmp(key, "owl") == 0) {
parseOptions(key, value);
}
}
private:
void processItem(const char* key, const char* item_key, const char* item_value) {
if (strcasecmp(key, "options") == 0 && strcasecmp(item_key, "midi") == 0 &&
strcasecmp(item_value, "on") == 0) {
midiOn = true;
}
else if (strcasecmp(key, "owl") == 0 && strcasecmp(item_key, "voct") == 0) {
if (strcasecmp(item_value, "input") == 0) {
vOctInput = true;
}
else if (strcasecmp(item_value, "output") == 0) {
vOctOutput = true;
}
}
}
void parseOptions(const char* key, const char* value) {
const char* c = value;
const uint8_t max_len = 32;
// We don't need to read long item values, so set a limit that would be used for truncating them.
char item_key[max_len] = "";
char item_value[max_len] = "";
uint8_t key_pos;
uint8_t value_pos;
ParserState state = ST_NONE;
while (*c) {
if (state == ST_NONE) {
if (*c == '[') {
// Start reading key
state = ST_KEY;
key_pos = 0;
value_pos = 0;
}
}
else if (state == ST_KEY) {
if (*c == ':') {
// Found end of key
item_key[key_pos + 1] = '\n';
// Start reading value
state = ST_VALUE;
}
else if (key_pos < max_len - 1)
// Add next character to key
item_key[key_pos++] = *c;
}
else if (state == ST_VALUE) {
if (*c == ']') {
// Found end of value
item_value[value_pos + 1] = '\n';
processItem(key, item_key, item_value);
// Reset state
state = ST_NONE;
}
else if (value_pos < max_len - 1)
// Add next character to value
item_value[value_pos++] = *c;
};
c++;
}
}
};
/**************************************************************************************
*
* OwlParameter : object used by OwlUI to ensures the connection between an OWL
* parameter and a FAUST widget
*
***************************************************************************************/
class OwlParameterBase {
protected:
Patch* fPatch; // needed to register and read owl parameters
FAUSTFLOAT* fZone; // Faust widget zone
bool isOutput;
public:
OwlParameterBase(Patch* pp, FAUSTFLOAT* z, bool output)
: fPatch(pp)
, fZone(z)
, isOutput(output) {
}
virtual void update() {
}
virtual ~OwlParameterBase() {}; // Not adding virtual destructor triggers undefined behavior
};
class OwlParameter : public OwlParameterBase {
protected:
PatchParameterId fParameter; // OWL parameter code : PARAMETER_A,...
float fMin; // Faust widget minimal value
float fSpan; // Faust widget value span (max-min)
public:
OwlParameter(Patch* pp, PatchParameterId param, FAUSTFLOAT* z,
const char* l, float init, float lo, float hi, bool output = false)
: OwlParameterBase(pp, z, output)
, fParameter(param)
, fMin(lo)
, fSpan(hi - lo) {
fPatch->registerParameter(param, l);
fPatch->setParameterValue(fParameter, (init - lo) / fSpan);
}
void update() {
if (isOutput) {
fPatch->setParameterValue(fParameter, (*fZone - fMin) / fSpan);
}
else
*fZone = fMin + fSpan * fPatch->getParameterValue(fParameter);
}
~OwlParameter() {};
};
class OwlVariable : public OwlParameterBase {
protected:
float* fValue;
float fLo; // Faust widget minimal value
float fHi; // Faust widget value span (max-min)
public:
OwlVariable(Patch* pp, float* t, FAUSTFLOAT* z, const char* l, float init,
float lo, float hi, bool output = false)
: OwlParameterBase(pp, z, output)
, fValue(t)
, fLo(lo)
, fHi(hi) {
*fZone = init;
}
void update() {
if (isOutput) {
// We don't output OwlVariable anywhere, so would this even be useful?
*fValue = *fZone;
}
else {
float value = *fValue;
// clip value to min/max
value = value > fLo ? (value < fHi ? value : fHi) : fLo;
*fZone = value;
}
}
~OwlVariable() {};
};
class OwlButton : public OwlParameterBase {
protected:
PatchButtonId fButton; // OWL button id : PUSHBUTTON, ...
public:
OwlButton(Patch* pp, PatchButtonId button, FAUSTFLOAT* z, const char* l, bool output=false) :
OwlParameterBase(pp, z, output), fButton(button) {}
void update() {
if(isOutput){
fPatch->setButton(fButton, *fZone, 0);
}else{
*fZone = fPatch->isButtonPressed(fButton);
}
}
};
class OwlCheckbox : public OwlParameterBase {
protected:
PatchButtonId fButton; // OWL button id : PUSHBUTTON, ...
bool wasHigh = false; // Flag for edge detection
bool state = false; // Current state
public:
OwlCheckbox(Patch* pp, PatchButtonId button, FAUSTFLOAT* z, const char* l)
: OwlParameterBase(pp, z, false)
, fButton(button) {}
void update() {
bool isHigh = fPatch->isButtonPressed(fButton);
state ^= isHigh && !wasHigh;
wasHigh = isHigh;
fPatch->setButton(fButton, state, 0);
*fZone = state;
}
};
/**************************************************************************************
*
* OwlResourceReader: Reads resources from flash storage and returns Soundfile objects.
*
* This is based on SoundfileReader in faust/gui/Soundfile.h with some changes:
* - no allocation of excessive amounts of RAM for empty buffers
* - no stdlib usage of vectors, maps,
* - exception handling removed
*
***************************************************************************************/
#define EMPTY_BUFFER_SIZE 0
#ifdef SOUNDFILE
class OwlResourceReader {
public:
virtual ~OwlResourceReader() {}
void setSampleRate(int sample_rate) { fDriverSR = sample_rate; }
Soundfile* createSoundfile(const char** path_name_list, int path_name_size, int max_chan) {
Soundfile *soundfile = NULL;
int cur_chan = 1; // At least one channel
int total_length = 1; // At least one byte - otherwise we can't allocate empty channels
// Compute total length and channels max of all files
for (int i = 0; i < path_name_size; i++) {
int chan, length;
if (strcmp(path_name_list[i], "__empty_sound__") == 0) {
length = EMPTY_BUFFER_SIZE;
chan = 1;
} else {
getParamsFile(path_name_list[i], chan, length);
}
cur_chan = std::max<int>(cur_chan, chan);
total_length += length;
}
// Complete with empty parts
total_length += (MAX_SOUNDFILE_PARTS - path_name_size) * EMPTY_BUFFER_SIZE;
// Create the soundfile
soundfile = createSoundfile(cur_chan, total_length, max_chan);
// Init offset
int offset = 0;
// Read all files
for (int i = 0; i < path_name_size; i++) {
if (strcmp(path_name_list[i], "__empty_sound__") == 0){
emptyFile(soundfile, i, offset);
} else {
readFile(soundfile, path_name_list[i], i, offset, max_chan);
}
}
// Complete with empty parts
for (int i = path_name_size; i < MAX_SOUNDFILE_PARTS; i++) {
emptyFile(soundfile, i, offset);
}
// Share the same buffers for all other channels so that we have max_chan channels available
for (int chan = cur_chan; chan < max_chan; chan++) {
soundfile->fBuffers[chan] = soundfile->fBuffers[chan % cur_chan];
}
return soundfile;
}
/**
* Check the availability of a sound resource.
*
* @param resource_name - the name of the file, or sound resource identified this way
*
* @return true if the sound resource is available, false otherwise.
*/
bool checkFile(const char* resource_name) {
Resource* resource = Resource::open(resource_name);
bool result = resource != NULL;
Resource::destroy(resource);
if (!result){
debugMessage("Resource not found");
}
return result;
}
protected:
int fDriverSR;
void emptyFile(Soundfile* soundfile, int part, int& offset) {
soundfile->fLength[part] = EMPTY_BUFFER_SIZE;
soundfile->fSR[part] = SAMPLE_RATE;
soundfile->fOffset[part] = offset;
// Update offset
offset += soundfile->fLength[part];
}
Soundfile* createSoundfile(int cur_chan, int length, int max_chan) {
Soundfile* soundfile = new Soundfile();
soundfile->fBuffers = new FAUSTFLOAT*[max_chan];
for (int chan = 0; chan < cur_chan; chan++) {
soundfile->fBuffers[chan] = new FAUSTFLOAT[length];
memset(soundfile->fBuffers[chan], 0, sizeof(FAUSTFLOAT) * length);
}
soundfile->fChannels = cur_chan;
return soundfile;
}
void getBuffersOffset(Soundfile* soundfile, FAUSTFLOAT** buffers, int offset) {
for (int chan = 0; chan < soundfile->fChannels; chan++) {
buffers[chan] = &soundfile->fBuffers[chan][offset];
}
}
/**
* Get the channels and length values of the given sound resource.
*
* @param path_name - the name of the file, or sound resource identified this way
* @param channels - the channels value to be filled with the sound resource number of channels
* @param length - the length value to be filled with the sound resource length in frames
*
*/
void getParamsFile(const char* path_name, int& channels, int& length) {
WaveResourceReader reader(path_name);
channels = 1;
length = 0;
if (reader.loadWaveHeader()){
channels = reader.fWave->num_channels;
length = (reader.fWave->subchunk_2_size * 8) / (reader.fWave->num_channels * reader.fWave->bits_per_sample);
}
}
/**
* Read one sound resource and fill the 'soundfile' structure accordingly
*
* @param soundfile - the soundfile to be filled
* @param path_name - the name of the file, or sound resource identified this way
* @param part - the part number to be filled in the soundfile
* @param offset - the offset value to be incremented with the actual sound resource length in frames
* @param max_chan - the maximum number of mono channels to fill
*
*/
void readFile(Soundfile* soundfile, const char* path_name, int part, int& offset, int max_chan) {
WaveResourceReader reader(path_name);
if (!reader.loadWaveHeader()){
return;
}
reader.loadWave();
soundfile->fLength[part] = (reader.fWave->subchunk_2_size * 8) / (reader.fWave->num_channels * reader.fWave->bits_per_sample);
soundfile->fSR[part] = reader.fWave->sample_rate;
soundfile->fOffset[part] = offset;
// Audio frames have to be written for each chan
switch (reader.fWave->bits_per_sample){
case 16:
// 16 bit samples are signed ints
for (int sample = 0; sample < soundfile->fLength[part]; sample++) {
float factor = 1.f/32767.f;
int16_t* frame = (int16_t*)&reader.fWave->data[reader.fWave->block_align * sample];
for (int chan = 0; chan < reader.fWave->num_channels; chan++) {
soundfile->fBuffers[chan][offset + sample] = frame[chan] * factor;
}
}
break;
case 8:
// 8 bit samples are unsigned ints
for (int sample = 0; sample < soundfile->fLength[part]; sample++) {
float factor = 1.f/255.f;
uint8_t* frame = (uint8_t*)&reader.fWave->data[reader.fWave->block_align * sample];
for (int chan = 0; chan < reader.fWave->num_channels; chan++) {
soundfile->fBuffers[chan][offset + sample] = frame[chan] * factor - 0.5f;
}
}
break;
default:
debugMessage("Unsupported format");
return;
}
//
// Update offset
offset += soundfile->fLength[part];
}
};
#endif /* SOUNDFILE */
/**************************************************************************************
*
* OwlUI : Faust User Interface builder. Passed to buildUserInterface OwlU
* ensures the mapping between owl parameters and faust widgets. It relies on
* specific metadata "...[OWL:PARAMETER_X]..." in widget's label for that. For
* example any faust widget with metadata [OWL:B] will be controlled by
* PARAMETER_B (the second knob).
*
***************************************************************************************/
// The maximun number of mappings between owl parameters and faust widgets
#define MAXOWLPARAMETERS 20
#define NO_PARAMETER ((PatchParameterId)-1)
#define NO_BUTTON ((PatchButtonId)-1)
#define PATH_LEN 24
MonoVoiceAllocator allocator(fKey, fFreq, fGain, fGate, fBend);
VoltsPerOctave* fVOctInput;
VoltsPerOctave* fVOctOutput;
Soundfile* defaultsound;
class OwlUI : public UI {
Patch* fPatch;
PatchParameterId fParameter; // current parameter ID, value NO_PARAMETER means not set
int fParameterIndex; // number of OwlParameters collected so far
int fSampleRate;
//std::map<std::string, Soundfile*> fSoundfileMap; // Map to share loaded soundfiles
OwlParameterBase* fParameterTable[MAXOWLPARAMETERS];
#ifdef SOUNDFILE
int fSoundfileIndex;
OwlResourceReader* fSoundReader = NULL;
Soundfile* fSoundfiles[MAX_SOUNDFILES];
#endif
PatchButtonId fButton;
// check if the widget is an Owl parameter and, if so, add the corresponding OwlParameter
void addInputOwlParameter(const char* label, FAUSTFLOAT* zone,
FAUSTFLOAT init, FAUSTFLOAT lo, FAUSTFLOAT hi) {
if (fParameterIndex < MAXOWLPARAMETERS) {
if (meta.midiOn && strcasecmp(label, "freq") == 0) {
fParameterTable[fParameterIndex++] =
new OwlVariable(fPatch, &fFreq, zone, label, init, lo, hi);
}
else if (meta.midiOn && strcasecmp(label, "gain") == 0) {
fParameterTable[fParameterIndex++] =
new OwlVariable(fPatch, &fGain, zone, label, init, lo, hi);
}
else if (meta.midiOn && strcasecmp(label, "bend") == 0) {
fParameterTable[fParameterIndex++] =
new OwlVariable(fPatch, &fBend, zone, label, init, lo, hi);
}
else if (meta.midiOn && strcasecmp(label, "key") == 0) {
fParameterTable[fParameterIndex++] =
new OwlVariable(fPatch, &fKey, zone, label, init, lo, hi);
}
else if (fParameter != NO_PARAMETER) {
fParameterTable[fParameterIndex++] =
new OwlParameter(fPatch, fParameter, zone, label, init, lo, hi);
}
}
fParameter = NO_PARAMETER; // clear current parameter ID
fButton = NO_BUTTON;
}
void addOutputOwlParameter(
const char* label, FAUSTFLOAT* zone, FAUSTFLOAT lo, FAUSTFLOAT hi) {
if(label[strlen(label) - 1] != '>')
debugMessage("Add '>' character for output parameters");
if (fParameterIndex < MAXOWLPARAMETERS) {
if (fParameter != NO_PARAMETER) {
fParameterTable[fParameterIndex++] = new OwlParameter(
fPatch, fParameter, zone, label, lo, lo, hi, true);
}
else if(fButton != NO_BUTTON){
fParameterTable[fParameterIndex++] = new OwlButton(
fPatch, fButton, zone, label, true);
}
}
fParameter = NO_PARAMETER;
fButton = NO_BUTTON;
}
void addOwlButton(const char* label, FAUSTFLOAT* zone) {
if (fParameterIndex < MAXOWLPARAMETERS) {
if (meta.midiOn && strcasecmp(label, "gate") == 0) {
fParameterTable[fParameterIndex++] =
new OwlVariable(fPatch, &fGate, zone, label, 0.0f, 0.0f, 1.0f);
}
else if (fButton != NO_BUTTON) {
fParameterTable[fParameterIndex++] =
new OwlButton(fPatch, fButton, zone, label);
}
}
fParameter = NO_PARAMETER;
fButton = NO_BUTTON; // clear current button ID
}
#ifdef SOUNDFILE
void addOwlResources(const char** resource_names, int names_size, Soundfile** sf_zone){
if (fSoundfileIndex < MAX_SOUNDFILES){
Soundfile* sound_file = fSoundReader->createSoundfile(resource_names, names_size, MAX_CHAN);
fSoundfiles[fSoundfileIndex++] = sound_file;
if (sound_file != NULL) {
*sf_zone = sound_file;
return;
}
}
// If failure, use 'defaultsound'
*sf_zone = defaultsound;
}
#endif /* SOUNDFILE */
void addOwlCheckbox(const char* label, FAUSTFLOAT* zone) {
if (fParameterIndex < MAXOWLPARAMETERS) {
if (meta.midiOn && strcasecmp(label, "gate") == 0) {
fParameterTable[fParameterIndex++] =
new OwlVariable(fPatch, &fGate, zone, label, 0.0f, 0.0f, 1.0f);
}
else if (fButton != NO_BUTTON) {
fParameterTable[fParameterIndex++] =
new OwlCheckbox(fPatch, fButton, zone, label);
}
}
fParameter = NO_PARAMETER;
fButton = NO_BUTTON; // clear current button ID
}
// we dont want to create a widget but we clear the current parameter ID just in case
void skip() {
fParameter = NO_PARAMETER; // clear current parameter ID
fButton = NO_BUTTON;
}
public:
MetaData meta;
OwlUI(Patch* pp)
: fPatch(pp)
, fParameter(NO_PARAMETER)
, fParameterIndex(0)
#ifdef SOUNDFILE
, fSoundfileIndex(0)
#endif
, fButton(NO_BUTTON) {
}
virtual ~OwlUI() {
for (int i = 0; i < fParameterIndex; i++)
delete fParameterTable[i];
if (meta.vOctInput)
delete fVOctInput;
if (meta.vOctOutput)
delete fVOctOutput;
#ifdef SOUNDFILE
if (fSoundReader != NULL) {
delete fSoundReader;
}
for (int i = 0; i < fSoundfileIndex; i++){
delete fSoundfiles[i];
}
#endif
}
// should be called before compute() to update widget's zones registered as OWL parameters
void update() {
for (int i = 0; i < fParameterIndex; i++)
fParameterTable[i]->update();
}
//---------------------- virtual methods called by buildUserInterface ----------------
// -- widget's layouts
virtual void openTabBox(const char* label) {
}
virtual void openHorizontalBox(const char* label) {
}
virtual void openVerticalBox(const char* label) {
}
virtual void closeBox() {
}
// -- active widgets
virtual void addButton(const char* label, FAUSTFLOAT* zone) {
addOwlButton(label, zone);
}
virtual void addCheckButton(const char* label, FAUSTFLOAT* zone) {
addOwlCheckbox(label, zone);
}
virtual void addVerticalSlider(const char* label, FAUSTFLOAT* zone,
FAUSTFLOAT init, FAUSTFLOAT lo, FAUSTFLOAT hi, FAUSTFLOAT step) {
addInputOwlParameter(label, zone, init, lo, hi);
}
virtual void addHorizontalSlider(const char* label, FAUSTFLOAT* zone,
FAUSTFLOAT init, FAUSTFLOAT lo, FAUSTFLOAT hi, FAUSTFLOAT step) {
addInputOwlParameter(label, zone, init, lo, hi);
}
virtual void addNumEntry(const char* label, FAUSTFLOAT* zone,
FAUSTFLOAT init, FAUSTFLOAT lo, FAUSTFLOAT hi, FAUSTFLOAT step) {
addInputOwlParameter(label, zone, init, lo, hi);
}
// -- passive widgets
virtual void addHorizontalBargraph(
const char* label, FAUSTFLOAT* zone, FAUSTFLOAT lo, FAUSTFLOAT hi) {
addOutputOwlParameter(label, zone, lo, hi);
}
virtual void addVerticalBargraph(
const char* label, FAUSTFLOAT* zone, FAUSTFLOAT lo, FAUSTFLOAT hi) {
addOutputOwlParameter(label, zone, lo, hi);
}
// -- soundfiles
virtual void addSoundfile(const char* label, const char* url, Soundfile** sf_zone) {
#ifdef SOUNDFILE
if (fSoundReader == NULL){
fSoundReader = new OwlResourceReader();
fSoundReader->setSampleRate(fPatch->getSampleRate());
defaultsound = fSoundReader->createSoundfile(nullptr, 0, 1);
}
if (url[0] == '{'){
char* paths[MAX_SOUNDFILE_PARTS];
int total_paths = 0;
bool reading_path = false;
const char* c = url;
const char* start_path;
while (c){
if (reading_path){
if (*c++ == '\''){
// Closing quote
reading_path = false;
uint32_t path_len = std::min<uint32_t>(c - start_path, PATH_LEN - 1);
paths[total_paths] = new char[path_len];
strncpy(paths[total_paths], start_path, path_len - 1);
paths[total_paths][path_len -1] = 0;
if (fSoundReader->checkFile(paths[total_paths]))
total_paths++;
else {
delete[] paths[total_paths];
}
}
}
else {
switch (*c){
case '\'':
// Opening quote
start_path = ++c;
reading_path = true;
break;
case '}':
c = 0; // Done!
break;
default:
// Path separator will end here
c++;
break;
}
}
}
addOwlResources((const char**)paths, total_paths, sf_zone);
// Parsed path names are not used after loading resources
for (int i = 0; i < total_paths; i++){
delete[] paths[i];
}
}
#else
fParameter = NO_PARAMETER; // clear current parameter ID
fButton = NO_BUTTON;
#endif /* SOUNDFILE */
}
// -- metadata declarations
virtual void declare(FAUSTFLOAT* z, const char* k, const char* id) {
if (strcasecmp(k, "OWL") == 0) {
if (strncasecmp(id, "PARAMETER_", 10) == 0)
id += 10;
if (strlen(id) == 1) {
// Single char parameter name.
// Note that we can use I - P as aliases for AA-AH.
// Rationale: Magus uses only single letters param names, has
// no free space on display for second letter.
if (*id >= 'A' && *id <= 'P') {
// Uppercase parameter name
fParameter = PatchParameterId(*id - 'A');
}
else if (*id >= '0' && *id <= '9') {
// Parameter can be numeric as well
fParameter = PatchParameterId(*id - '0');
}
else if (*id >= 'a' && *id <= 'P') {
// Lowercase parameter name
fParameter = PatchParameterId(*id - 'a');
}
}
else if (strlen(id) == 2) {
int param_tmp = -1;
bool is_digit = false;
if (*id >= 'A' && *id <= 'D') {
// Uppercase parameter name
param_tmp = *id - 'A';
}
else if (*id >= '0' && *id <= '9') {
// Parameter can be numeric as well
is_digit = true;
fParameter = PatchParameterId(*id - '0');
}
else if (*id >= 'a' && *id <= 'd') {
// Lowercase parameter name
param_tmp = *id - 'a';
}
if (param_tmp != -1) {
id++;
if (is_digit) {
// Get second digit for decimal param
param_tmp *= 10;
if (*id >= '0' && *id <= '9') {
fParameter = PatchParameterId(
std::min(int(PARAMETER_DH), param_tmp + *id - '0'));
}
}
else if (param_tmp == PARAMETER_B && *id > '0' && *id <= '9') {
fButton = PatchButtonId(BUTTON_A + *id - '1');
}
else {
// Inc to skip 1-character params
param_tmp++;
// This is first character for groups of 8 params
param_tmp *= 8;
// Second char for parameter name
if (*id >= 'A' && *id <= 'H') {
// Uppercase parameter name
fParameter = PatchParameterId(param_tmp + *id - 'A');
}
else if (*id >= 'a' && *id <= 'h') {
// Lowercase parameter name
fParameter = PatchParameterId(param_tmp + *id - 'a');
}
}
}
}
else if (strcasecmp(id, "PUSH") == 0)
fButton = PUSHBUTTON;
else if (strcasecmp(id, "ButtonA") == 0)
fButton = BUTTON_A;
else if (strcasecmp(id, "ButtonB") == 0)
fButton = BUTTON_B;
else if (strcasecmp(id, "ButtonC") == 0)
fButton = BUTTON_C;
else if (strcasecmp(id, "ButtonD") == 0)
fButton = BUTTON_D;
}
// else if (strcasecmp(k, "midi") == 0) {
// if (strcasecmp(id, "pitchwheel") == 0){ // PB
// }else if (strcasecmp(id, "ctrl") == 0){ // CC
// }else if (strcasecmp(id, "chanpress") == 0){ // AT
// }else if (strcasecmp(id, "pgm") == 0){ // PC
// }
// }
}
// -- V/Oct
void addVOct() {
if (meta.vOctInput)
fVOctInput = new VoltsPerOctave(true);
if (meta.vOctOutput)
fVOctOutput = new VoltsPerOctave(false);
}
};
/* Simple heap based memory manager.
* Uses overloaded new/delete operators on OWL hardware.
*/
struct OwlMemoryManager : public dsp_memory_manager {
void* allocate(size_t size) {
void* res = new uint8_t[size];
return res;
}
virtual void destroy(void* ptr) {
delete (uint8_t*)ptr;
}
};
#endif // __FaustCommonInfrastructure__
/**************************BEGIN USER SECTION **************************/
<< includeIntrinsic >>
<< includeclass >>
/***************************END USER SECTION ***************************/
/*******************BEGIN ARCHITECTURE SECTION (part 2/2)***************/
/**************************************************************************************
FaustPatch : an OWL patch that calls Faust generated DSP code
***************************************************************************************/
class FaustPatch : public Patch {
mydsp* fDSP;
OwlUI fUI;
OwlMemoryManager mem;
public:
FaustPatch()
: fUI(this) {
// // Allocate memory for dsp object first to ensure high priority
// memory is used void* allocated = mem.allocate(sizeof(mydsp));
// // DSP static data is initialised using classInit.
// mydsp::classInit(int(getSampleRate()), &mem);
// // call mydsp constructor
// fDSP = new (allocated) mydsp();
// fDSP->instanceInit(int(getSampleRate()));
// // Map OWL parameters and faust widgets
// fDSP->buildUserInterface(&fUI);
fBend = 1.0f;
mydsp::fManager = &mem; // set custom memory manager
fDSP = mydsp::create();
fDSP->metadata(&fUI.meta);
if (fUI.meta.message != NULL)
debugMessage(fUI.meta.message);
fUI.addVOct();
mydsp::classInit(int(getSampleRate())); // initialise static tables
fDSP->instanceInit(int(getSampleRate())); // initialise DSP instance