File: owl.cpp

package info (click to toggle)
faust 2.14.4~repack2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 276,136 kB
  • sloc: cpp: 231,578; ansic: 15,403; sh: 10,871; java: 6,917; objc: 4,085; makefile: 3,002; cs: 1,077; ruby: 951; python: 885; xml: 550; yacc: 516; lex: 233; lisp: 201
file content (311 lines) | stat: -rw-r--r-- 11,956 bytes parent folder | download
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
/************************************************************************

	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 <new>
#include <cstddef>
#include <string.h>
#include <strings.h>
#include "Patch.h"

#ifndef __FaustCommonInfrastructure__
#define __FaustCommonInfrastructure__

#include "faust/dsp/dsp.h"
#include "faust/gui/UI.h"

struct Meta
{
    virtual void declare(const char* key, const char* value) = 0;
};

/**************************************************************************************

	OwlParameter : object used by OwlUI to ensures the connection between an owl parameter 
	and a faust widget
	
***************************************************************************************/

class OwlParameter
{
  protected:
	Patch* 	fPatch;		// needed to register and read owl parameters
	PatchParameterId	fParameter;		// OWL parameter code : PARAMETER_A,...
	FAUSTFLOAT* 		fZone;			// Faust widget zone
	const char*			fLabel;			// Faust widget label 
	float				fMin;			// Faust widget minimal value
	float				fSpan;			// Faust widget value span (max-min)
	
  public:
	OwlParameter() :
		fPatch(0), fParameter(PARAMETER_A), fZone(0), fLabel(""), fMin(0), fSpan(1) {}
	OwlParameter(const OwlParameter& w) :
		fPatch(w.fPatch), fParameter(w.fParameter), fZone(w.fZone), fLabel(w.fLabel), fMin(w.fMin), fSpan(w.fSpan) {}
	OwlParameter(Patch* pp, PatchParameterId param, FAUSTFLOAT* z, const char* l, float lo, float hi) :
		fPatch(pp), fParameter(param), fZone(z), fLabel(l), fMin(lo), fSpan(hi-lo) {}
	void bind() 	{ fPatch->registerParameter(fParameter, fLabel); }
	void update()	{ *fZone = fMin + fSpan*fPatch->getParameterValue(fParameter); }
	
};

class OwlButton
{
  protected:
	Patch* 	fPatch;                 // needed to register and read owl parameters
	PatchButtonId	fButton;		// OWL button id : PUSHBUTTON, ...
	FAUSTFLOAT* 	fZone;			// Faust widget zone
	const char*		fLabel;			// Faust widget label
  public:
	OwlButton() :
		fPatch(0), fButton(PUSHBUTTON), fZone(0), fLabel("") {}
	OwlButton(const OwlButton& w) :
		fPatch(w.fPatch), fButton(w.fButton), fZone(w.fZone), fLabel(w.fLabel) {}
	OwlButton(Patch* pp, PatchButtonId button, FAUSTFLOAT* z, const char* l) :
		fPatch(pp), fButton(button), fZone(z), fLabel(l) {}
	void bind() 	{}
	void update()	{ *fZone = fPatch->isButtonPressed(fButton); }
};

/**************************************************************************************

	OwlUI : Faust User Interface builder. Passed to buildUserInterface OwlUI 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:PARAMETER_B] will be controlled by PARAMETER_B 
	(the second knob).
	
***************************************************************************************/

// The maximun number of mappings between owl parameters and faust widgets 
#define MAXOWLPARAMETERS 40
#define MAXOWLBUTTONS    2
#define NO_PARAMETER     ((PatchParameterId)-1)
#define NO_BUTTON        ((PatchButtonId)-1)

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
	OwlParameter		fParameterTable[MAXOWLPARAMETERS];		// kind of static list of OwlParameters
        PatchButtonId fButton;
        int fButtonIndex;
        OwlButton fButtonTable[MAXOWLBUTTONS];
	// check if the widget is an Owl parameter and, if so, add the corresponding OwlParameter
	void addOwlParameter(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT lo, FAUSTFLOAT hi) {
		if ((fParameter >= PARAMETER_A) && (fParameterIndex < MAXOWLPARAMETERS)) {
			fParameterTable[fParameterIndex] = OwlParameter(fPatch, fParameter, zone, label, lo, hi);
			fParameterTable[fParameterIndex].bind();
			fParameterIndex++;
		}
		fParameter = NO_PARAMETER; 		// clear current parameter ID
	}
	void addOwlButton(const char* label, FAUSTFLOAT* zone) {
		if ((fButton >= PUSHBUTTON) && (fButtonIndex < MAXOWLBUTTONS)) {
			fButtonTable[fButtonIndex] = OwlButton(fPatch, fButton, zone, label);
			fButtonTable[fButtonIndex].bind();
			fButtonIndex++;
		}
		fButton = NO_BUTTON; 		// clear current button ID
	}

	// we dont want to create a widget by-ut we clear the current parameter ID just in case
	void skip() {
		fParameter = NO_PARAMETER; 		// clear current parameter ID
		fButton = NO_BUTTON;
	}

 public:

    OwlUI(Patch* pp) : fPatch(pp), fParameter(NO_PARAMETER), fParameterIndex(0), fButton(NO_BUTTON), fButtonIndex(0) {}
	
	virtual ~OwlUI() {}
	
	// 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();
		for (int i=0; i<fButtonIndex; i++)  fButtonTable[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) 																		{ addOwlButton(label, zone); }
    virtual void addVerticalSlider(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT init, FAUSTFLOAT lo, FAUSTFLOAT hi, FAUSTFLOAT step) 	{ addOwlParameter(label, zone, lo, hi); }
    virtual void addHorizontalSlider(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT init, FAUSTFLOAT lo, FAUSTFLOAT hi, FAUSTFLOAT step) 	{ addOwlParameter(label, zone, lo, hi); }
    virtual void addNumEntry(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT init, FAUSTFLOAT lo, FAUSTFLOAT hi, FAUSTFLOAT step) 			{ addOwlParameter(label, zone, lo, hi); }

    // -- passive widgets

    virtual void addHorizontalBargraph(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT lo, FAUSTFLOAT hi) 									{ skip(); }
    virtual void addVerticalBargraph  (const char* label, FAUSTFLOAT* zone, FAUSTFLOAT lo, FAUSTFLOAT hi) 									{ skip(); }
    
    // -- soundfiles
    virtual void addSoundfile(const char* label, const char* filename, Soundfile** sf_zone) {}

	// -- 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 (strcasecmp(id,"A") == 0)  fParameter = PARAMETER_A;
          else if (strcasecmp(id,"B") == 0)  fParameter = PARAMETER_B;
          else if (strcasecmp(id,"C") == 0)  fParameter = PARAMETER_C;
          else if (strcasecmp(id,"D") == 0)  fParameter = PARAMETER_D;
          else if (strcasecmp(id,"E") == 0)  fParameter = PARAMETER_E;
          else if (strcasecmp(id,"F") == 0)  fParameter = PARAMETER_F;
          else if (strcasecmp(id,"G") == 0)  fParameter = PARAMETER_G;
          else if (strcasecmp(id,"H") == 0)  fParameter = PARAMETER_H;
          else if (strcasecmp(id,"PUSH") == 0)  fButton = PUSHBUTTON;
          else if (strcasecmp(id,"BYPASS") == 0)  fButton = BYPASS_BUTTON;
        }
    }
};

  /* 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)
    {      
      fDSP = new mydsp();
      mydsp::fManager = &mem; // set custom memory manager
      mydsp::classInit(int(getSampleRate())); // initialise static tables
      fDSP->instanceInit(int(getSampleRate())); // initialise DSP instance
      fDSP->buildUserInterface(&fUI); // Map OWL parameters
    }

    ~FaustPatch(){
      delete fDSP;
      mydsp::classDestroy(); // destroy static tables
    }
    
    void processAudio(AudioBuffer &buffer)
    {
        // Reasonably assume we will not have more than 32 channels
        float*  ins[32];
        float*  outs[32];
        int     n = buffer.getChannels();
        
        if ((fDSP->getNumInputs() < 32) && (fDSP->getNumOutputs() < 32)) {

            // create the table of input channels
            for(int ch = 0; ch < fDSP->getNumInputs(); ++ch) {
                ins[ch] = buffer.getSamples(ch%n);
            }

            // create the table of output channels
            for(int ch = 0; ch < fDSP->getNumOutputs(); ++ch) {
                outs[ch] = buffer.getSamples(ch%n);
            }

            // read OWL parameters and updates corresponding Faust Widgets zones
            fUI.update(); 

            // Process the audio samples
            fDSP->compute(buffer.getSize(), ins, outs);
        }
    }

};

extern "C" {
  void doSetButton(uint8_t id, uint16_t state, uint16_t samples);
  int owl_pushbutton(int value){
    static bool state = 0;
    static uint16_t counter = 0;
    value = (bool)value;
    if(state != value){
      state = value;
      doSetButton(PUSHBUTTON, state, counter);
    }
    if(++counter > getProgramVector()->audio_blocksize)
      counter = 0;
    return value;
  }
}

#endif // __FaustPatch_h__

////////////////////////////////////////////////////////////////////////////////////////////////////