~ubuntu-branches/ubuntu/saucy/faust/saucy

« back to all changes in this revision

Viewing changes to architecture/module.cpp

  • Committer: Package Import Robot
  • Author(s): Mario Lang
  • Date: 2012-04-04 13:52:01 UTC
  • mfrom: (1.1.6) (3.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20120404135201-hpsrk87x3hga94tc
Tags: 0.9.46-2
* Fix "ftbfs with GCC-4.7":
  - debian/patches/unistd: Include <unistd.h> where necessary.
    (Closes: #667163)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/************************************************************************
 
2
 ************************************************************************
 
3
    FAUST Architecture File
 
4
        Copyright (C) 2003-2011 GRAME, Centre National de Creation Musicale
 
5
    ---------------------------------------------------------------------
 
6
 
 
7
        This is sample code. This file is provided as an example of minimal
 
8
        FAUST architecture file. Redistribution and use in source and binary
 
9
        forms, with or without modification, in part or in full are permitted.
 
10
        In particular you can create a derived work of this FAUST architecture
 
11
        and distribute that work under terms of your choice.
 
12
 
 
13
        This sample code is distributed in the hope that it will be useful,
 
14
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
16
 ************************************************************************
 
17
 ************************************************************************/
 
18
 
1
19
#include <stdlib.h>
2
20
#include <stdio.h>
3
21
#include <string.h>
9
27
#include <map>
10
28
#include <list>
11
29
 
 
30
#include "gui/GUI.h"
 
31
#include "misc.h"
 
32
#include "audio/dsp.h"
 
33
 
12
34
using namespace std;
13
35
 
14
 
// On Intel set FZ (Flush to Zero) and DAZ (Denormals Are Zero)
15
 
// flags to avoid costly denormals
16
 
#ifdef __SSE__
17
 
    #include <xmmintrin.h>
18
 
    #ifdef __SSE2__
19
 
        #define AVOIDDENORMALS _mm_setcsr(_mm_getcsr() | 0x8040)
20
 
    #else
21
 
        #define AVOIDDENORMALS _mm_setcsr(_mm_getcsr() | 0x8000)
22
 
    #endif
23
 
#else
24
 
    #define AVOIDDENORMALS 
25
 
#endif
26
 
 
27
 
struct Meta : map<const char*, const char*>
28
 
{
29
 
    void declare (const char* key, const char* value) { (*this)[key]=value; }
30
 
};
31
 
 
32
 
#define max(x,y) (((x)>(y)) ? (x) : (y))
33
 
#define min(x,y) (((x)<(y)) ? (x) : (y))
34
 
 
35
 
// abs is now predefined
36
 
//template<typename T> T abs (T a)                      { return (a<T(0)) ? -a : a; }
37
 
 
38
 
inline int              lsr (int x, int n)                      { return int(((unsigned int)x) >> n); }
39
 
 
40
36
/******************************************************************************
41
37
*******************************************************************************
42
38
 
45
41
*******************************************************************************
46
42
*******************************************************************************/
47
43
 
48
 
//inline void *aligned_calloc(size_t nmemb, size_t size) { return (void*)((unsigned)(calloc((nmemb*size)+15,sizeof(char)))+15 & 0xfffffff0); }
49
 
//inline void *aligned_calloc(size_t nmemb, size_t size) { return (void*)((size_t)(calloc((nmemb*size)+15,sizeof(char)))+15 & ~15); }
50
 
 
51
44
<<includeIntrinsic>>
52
45
 
53
 
/******************************************************************************
54
 
*******************************************************************************
55
 
 
56
 
                                                                GRAPHIC USER INTERFACE (v2)
57
 
                                                                  abstract interfaces
58
 
 
59
 
*******************************************************************************
60
 
*******************************************************************************/
61
 
 
62
 
#include <map>
63
 
#include <list>
64
 
 
65
 
using namespace std;
66
 
 
67
 
 
68
 
struct uiItem;
69
 
typedef void (*uiCallback)(float val, void* data);
70
 
 
71
 
/**
72
 
 * Graphic User Interface : abstract definition
73
 
 */
74
 
 
75
 
class UI 
76
 
{
77
 
        typedef list<uiItem*> clist;
78
 
        typedef map<float*, clist*> zmap;
79
 
        
80
 
 private:
81
 
        static list<UI*>        fGuiList;
82
 
        zmap                            fZoneMap;
83
 
        bool                            fStopped;
84
 
        
85
 
 public:
86
 
                
87
 
        UI() : fStopped(false) {        
88
 
                fGuiList.push_back(this);
89
 
        }
90
 
        
91
 
        virtual ~UI() {
92
 
                // suppression de this dans fGuiList
93
 
        }
94
 
 
95
 
        // -- zone management
96
 
        
97
 
        void registerZone(float* z, uiItem* c)
98
 
        {
99
 
                if (fZoneMap.find(z) == fZoneMap.end()) fZoneMap[z] = new clist();
100
 
                fZoneMap[z]->push_back(c);
101
 
        }       
102
 
        
103
 
        void updateAllZones();
104
 
        
105
 
        void updateZone(float* z);
106
 
        
107
 
        static void updateAllGuis()
108
 
        {
109
 
                list<UI*>::iterator g;
110
 
                for (g = fGuiList.begin(); g != fGuiList.end(); g++) {
111
 
                        (*g)->updateAllZones();
112
 
                }
113
 
        }
114
 
        
115
 
        // -- active widgets
116
 
        
117
 
        virtual void addButton(const char* label, float* zone) = 0;
118
 
        virtual void addToggleButton(const char* label, float* zone) = 0;
119
 
        virtual void addCheckButton(const char* label, float* zone) = 0;
120
 
        virtual void addVerticalSlider(const char* label, float* zone, float init, float min, float max, float step) = 0;
121
 
        virtual void addHorizontalSlider(const char* label, float* zone, float init, float min, float max, float step) = 0;
122
 
        virtual void addNumEntry(const char* label, float* zone, float init, float min, float max, float step) = 0;
123
 
        
124
 
        // -- passive widgets
125
 
        
126
 
        virtual void addNumDisplay(const char* label, float* zone, int precision) = 0;
127
 
        virtual void addTextDisplay(const char* label, float* zone, char* names[], float min, float max) = 0;
128
 
        virtual void addHorizontalBargraph(const char* label, float* zone, float min, float max) = 0;
129
 
        virtual void addVerticalBargraph(const char* label, float* zone, float min, float max) = 0;
130
 
        
131
 
        void addCallback(float* zone, uiCallback foo, void* data);
132
 
        
133
 
        // -- widget's layouts
134
 
        
135
 
        virtual void openFrameBox(const char* label) = 0;
136
 
        virtual void openTabBox(const char* label) = 0;
137
 
        virtual void openHorizontalBox(const char* label) = 0;
138
 
        virtual void openVerticalBox(const char* label) = 0;
139
 
        virtual void closeBox() = 0;
140
 
        
141
 
        virtual void show() = 0;
142
 
        virtual void run() = 0;
143
 
        
144
 
        void stop()             { fStopped = true; }
145
 
        bool stopped()  { return fStopped; }
146
 
};
147
 
 
148
 
 
149
 
/**
150
 
 * User Interface Item: abstract definition
151
 
 */
152
 
 
153
 
class uiItem
154
 
{
155
 
  protected :
156
 
                  
157
 
        UI*             fGUI;
158
 
        float*          fZone;
159
 
        float           fCache;
160
 
        
161
 
        uiItem (UI* ui, float* zone) : fGUI(ui), fZone(zone), fCache(-123456.654321) 
162
 
        { 
163
 
                ui->registerZone(zone, this); 
164
 
        }
165
 
        
166
 
        
167
 
  public :
168
 
 
169
 
        virtual ~uiItem() {}
170
 
        
171
 
        void modifyZone(float v)        
172
 
        { 
173
 
                fCache = v;
174
 
                if (*fZone != v) {
175
 
                        *fZone = v;
176
 
                        fGUI->updateZone(fZone);
177
 
                }
178
 
        }
179
 
                        
180
 
        float                   cache()                 { return fCache; }
181
 
        virtual void    reflectZone()   = 0;    
182
 
};
183
 
 
184
 
 
185
 
/**
186
 
 * Callback Item
187
 
 */
188
 
 
189
 
struct uiCallbackItem : public uiItem
190
 
{
191
 
        uiCallback      fCallback;
192
 
        void*           fData;
193
 
        
194
 
        uiCallbackItem(UI* ui, float* zone, uiCallback foo, void* data) 
195
 
                        : uiItem(ui, zone), fCallback(foo), fData(data) {}
196
 
        
197
 
        virtual void    reflectZone() {         
198
 
                float   v = *fZone;
199
 
                fCache = v; 
200
 
                fCallback(v, fData);    
201
 
        }
202
 
};
203
 
 
204
 
 
205
 
/**
206
 
 * Update all user items reflecting zone z
207
 
 */
208
 
 
209
 
inline void UI::updateZone(float* z)
210
 
{
211
 
        float   v = *z;
212
 
        clist*  l = fZoneMap[z];
213
 
        for (clist::iterator c = l->begin(); c != l->end(); c++) {
214
 
                if ((*c)->cache() != v) (*c)->reflectZone();
215
 
        }
216
 
}
217
 
 
218
 
 
219
 
/**
220
 
 * Update all user items not up to date
221
 
 */
222
 
 
223
 
inline void UI::updateAllZones()
224
 
{
225
 
        for (zmap::iterator m = fZoneMap.begin(); m != fZoneMap.end(); m++) {
226
 
                float*  z = m->first;
227
 
                clist*  l = m->second;
228
 
                float   v = *z;
229
 
                for (clist::iterator c = l->begin(); c != l->end(); c++) {
230
 
                        if ((*c)->cache() != v) (*c)->reflectZone();
231
 
                }
232
 
        }
233
 
}
234
 
 
235
 
inline void UI::addCallback(float* zone, uiCallback foo, void* data) 
236
 
237
 
        new uiCallbackItem(this, zone, foo, data); 
238
 
};
239
 
 
240
 
 
241
 
                
242
 
 
243
 
//----------------------------------------------------------------
244
 
//  d�inition du processeur de signal
245
 
//----------------------------------------------------------------
246
 
                        
247
 
class dsp {
248
 
 protected:
249
 
        int fSamplingFreq;
250
 
 public:
251
 
        dsp() {}
252
 
        virtual ~dsp() {}
253
 
        
254
 
        virtual int getNumInputs()                                                                              = 0;
255
 
        virtual int getNumOutputs()                                                                     = 0;
256
 
        virtual void buildUserInterface(UI* interface)                                  = 0;
257
 
        virtual void init(int samplingRate)                                                     = 0;
258
 
        virtual void compute(int len, float** inputs, float** outputs)  = 0;
259
 
        virtual void conclude()                                                                                 {}
260
 
};
261
 
                
262
 
                
 
46
//----------------------------------------------------------------
 
47
//  Signal processor definition
 
48
//----------------------------------------------------------------
 
49
 
263
50
<<includeclass>>
264
51
 
265
 
 
266
52
extern "C" dsp* newDsp()                                                                        { return new mydsp(); }
267
53
extern "C" void deleteDsp(dsp* self)                                            { delete self; }
268
54
 
271
57
extern "C" void buildUserInterface(dsp* self,UI* interface) { self->buildUserInterface(interface); }
272
58
extern "C" void init(dsp* self, int freq)                                       { self->init(freq); }
273
59
extern "C" void compute(dsp* self, int len, float** inputs, float** outputs) { self->compute(len, inputs, outputs); }
274
 
extern "C" void conclude(dsp* self)                                                     { self->conclude(); }