File: unityplugin.cpp

package info (click to toggle)
faust 2.79.3%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 397,496 kB
  • sloc: cpp: 278,433; ansic: 116,164; javascript: 18,529; vhdl: 14,052; sh: 13,884; java: 5,900; objc: 3,852; python: 3,222; makefile: 2,655; cs: 1,672; lisp: 1,146; ruby: 954; yacc: 586; xml: 471; lex: 247; awk: 110; tcl: 26
file content (307 lines) | stat: -rw-r--r-- 10,442 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
/************************************************************************
 
 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) 2004-2021 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 Lesser 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 Lesser General Public License for more details.
 
 You should have received a copy of the GNU Lesser 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.
 
 ************************************************************************
 ************************************************************************/

//
// UnityPLugin.cpp
//
//  Created by Théophile Dupré on 07/06/2017.
//  Copyright © 2017 Théophile Dupré. All rights reserved.
//

#include <string.h>
#include <iostream>
#include <algorithm>

#include "faust/dsp/dsp.h"
#include "faust/gui/APIUI.h"
#include "faust/gui/UI.h"
#include "faust/gui/meta.h"
#include "faust/misc.h"
#include "faust/dsp/dsp-tools.h"

#ifdef _WIN32
#define DllExport __declspec(dllexport)
#else
#define DllExport
#endif

// we require macro declarations
#define FAUST_UIMACROS

// but we will ignore most of them
#define FAUST_ADDBUTTON(l,f)
#define FAUST_ADDCHECKBOX(l,f)
#define FAUST_ADDVERTICALSLIDER(l,f,i,a,b,s)
#define FAUST_ADDHORIZONTALSLIDER(l,f,i,a,b,s)
#define FAUST_ADDNUMENTRY(l,f,i,a,b,s)
#define FAUST_ADDVERTICALBARGRAPH(l,f,a,b)
#define FAUST_ADDHORIZONTALBARGRAPH(l,f,a,b)

<<includeIntrinsic>>

<<includeclass>>

//=============================================================================
// unitydsp : a Faust dsp combined with an APIUI and AudioChannels that
// implements the various Unity callbacks.
//=============================================================================

#ifdef POLY

#include "faust/dsp/poly-dsp.h"

template <int INPUTS, int OUTPUTS>
class unitypolydsp : public decorator_dsp
{
    
    private:
    
        APIUI fUI;
        AudioChannels* fInputs;
        AudioChannels* fOutputs;
    
    public:
    
        unitypolydsp(int buffer_size, int nvoices)
        {
            /*
             bool midi_sync = false;
             bool midi = false;
             // Analyse NVOICES
             mydsp* tmp_dsp = new mydsp();
             MidiMeta::analyse(tmp_dsp, midi, midi_sync, nvoices);
             delete tmp_dsp;
             */
            
            fDSP = new mydsp_poly(new mydsp(), nvoices, true, true);
            fDSP->buildUserInterface(&fUI);
            
            if (buffer_size > 0) {
                fInputs = (INPUTS > 0) ? new AudioChannels(buffer_size, INPUTS) : nullptr;
                fOutputs = (OUTPUTS > 0) ? new AudioChannels(buffer_size, OUTPUTS) : nullptr;
            } else {
                fInputs = nullptr;
                fOutputs = nullptr;
            }
        }
    
        ~unitypolydsp()
        {
            delete fInputs;
            delete fOutputs;
        }
    
        void unityProcess(float* inbuffer, float* outbuffer, int length, int inchannels, int outchannels)
        {
            if (INPUTS > 0) fInputs->interleavedRead(inbuffer, length, inchannels);
            fDSP->compute(length, ((INPUTS > 0) ? fInputs->buffers() : nullptr), ((OUTPUTS > 0) ? fOutputs->buffers() : nullptr));
            if (OUTPUTS > 0) fOutputs->interleavedWrite(outbuffer, length, outchannels);
        }
    
        void setParamValue(int pnum, float pval) { fUI.setParamValue(pnum, pval); }
    
        float getParamValue(int pnum) { return fUI.getParamValue(pnum); }
    
        float getParamMin(int pnum) { return fUI.getParamMin(pnum); }
    
        float getParamMax(int pnum) { return fUI.getParamMax(pnum); }
    
        void keyOn(int channel, int pitch, int velocity)
        {
            static_cast<mydsp_poly*>(fDSP)->keyOn(channel, pitch, velocity);
        }
    
        void keyOff(int channel, int pitch, int velocity = 127)
        {
            static_cast<mydsp_poly*>(fDSP)->keyOff(channel, pitch, velocity);
        }
    
        void ctrlChange(int channel, int ctrl, int value)
        {
            static_cast<mydsp_poly*>(fDSP)->ctrlChange(channel, ctrl, value);
        }
    
        void allNotesOff(bool hard = false)
        {
            static_cast<mydsp_poly*>(fDSP)->allNotesOff(hard);
        }
    
};

std::list<GUI*> GUI::fGuiList;
ztimedmap GUI::gTimedZoneMap;

extern "C"
{
    #define UNITY_POLY_DSP unitypolydsp<FAUST_INPUTS, FAUST_OUTPUTS>
    
    DllExport UNITY_POLY_DSP* Faust_contextNew(int bufferSize, int nvoices)
    {
        return new UNITY_POLY_DSP(bufferSize, nvoices);
    }
    
    DllExport void Faust_contextInit(UNITY_POLY_DSP* ctx, int sampleRate) { ctx->init(sampleRate); }
    
    DllExport void Faust_process(UNITY_POLY_DSP* ctx, FAUSTFLOAT* inbuffer, FAUSTFLOAT* oubuffer, int nframes, int channels)
    {
        ctx->unityProcess(inbuffer, oubuffer, nframes, channels, channels);
    }
    
    DllExport void Faust_delete(UNITY_POLY_DSP* ctx) { delete ctx; }
    
    DllExport int Faust_getSampleRate(UNITY_POLY_DSP* ctx) { return ctx->getSampleRate(); }
    
    DllExport int Faust_getNumInputChannels(UNITY_POLY_DSP* ctx) { return ctx->getNumInputs(); }
    
    DllExport int Faust_getNumOutputChannels(UNITY_POLY_DSP* ctx) { return ctx->getNumOutputs(); }
    
    DllExport void Faust_setParameterValue(UNITY_POLY_DSP* ctx, int param, float value) { ctx->setParamValue(param, value); }
    
    DllExport float Faust_getParameterValue(UNITY_POLY_DSP* ctx, int param) { return ctx->getParamValue(param); }
    
    DllExport float Faust_getParamMin(UNITY_POLY_DSP* ctx, int param) { return ctx->getParamMin(param); }
    
    DllExport float Faust_getParamMax(UNITY_POLY_DSP* ctx, int param) { return ctx->getParamMax(param); }
    
    // MIDI API
    DllExport void Faust_keyOn(UNITY_POLY_DSP* ctx, int channel, int pitch, int velocity)
    {
        ctx->keyOn(channel, pitch, velocity);
    }
    
    DllExport void Faust_keyOff(UNITY_POLY_DSP* ctx, int channel, int pitch, int velocity = 127)
    {
        ctx->keyOff(channel, pitch, velocity);
    }
    
    DllExport void Faust_ctrlChange(UNITY_POLY_DSP* ctx, int channel, int ctrl, int value)
    {
        ctx->ctrlChange(channel, ctrl, value);
    }
    
    DllExport void Faust_allNotesOff(UNITY_POLY_DSP* ctx, bool hard = false)
    {
        ctx->allNotesOff(hard);
    }
    
}

#else

template <int INPUTS, int OUTPUTS>
class unitydsp : public mydsp
{
    
    private:
    
        APIUI fUI;
        AudioChannels* fInputs;
        AudioChannels* fOutputs;
    
    public:
    
        unitydsp(int buffer_size)
        {
            buildUserInterface(&fUI);
            if (buffer_size > 0) {
                fInputs = (INPUTS > 0) ? new AudioChannels(buffer_size, INPUTS) : nullptr;
                fOutputs = (OUTPUTS > 0) ? new AudioChannels(buffer_size, OUTPUTS) : nullptr;
            } else {
                fInputs = nullptr;
                fOutputs = nullptr;
            }
        }
    
        ~unitydsp()
        {
            delete fInputs;
            delete fOutputs;
        }
    
        void unityProcess(float* inbuffer, float* outbuffer, int length, int inchannels, int outchannels)
        {
            if (INPUTS > 0) fInputs->interleavedRead(inbuffer, length, inchannels);
            compute(length, ((INPUTS > 0) ? fInputs->buffers() : nullptr), ((OUTPUTS > 0) ? fOutputs->buffers() : nullptr));
            if (OUTPUTS > 0) fOutputs->interleavedWrite(outbuffer, length, outchannels);
        }
    
        void setParamValue(int pnum, float pval) { fUI.setParamValue(pnum, pval); }
    
        float getParamValue(int pnum) { return fUI.getParamValue(pnum); }
    
        float getParamMin(int pnum) { return fUI.getParamMin(pnum); }
    
        float getParamMax(int pnum) { return fUI.getParamMax(pnum); }
    
};

extern "C"
{
    
    #define UNITY_DSP unitydsp<FAUST_INPUTS, FAUST_OUTPUTS>
    
    DllExport UNITY_DSP* Faust_contextNew(int bufferSize)
    {
        return new UNITY_DSP(bufferSize);
    }
    
    DllExport void Faust_contextInit(UNITY_DSP* ctx, int sampleRate) { ctx->init(sampleRate); }
    
    DllExport void Faust_process(UNITY_DSP* ctx, FAUSTFLOAT* inbuffer, FAUSTFLOAT* oubuffer, int nframes, int channels)
    {
        ctx->unityProcess(inbuffer, oubuffer, nframes, channels, channels);
    }
    
    DllExport void Faust_delete(UNITY_DSP* ctx) { delete ctx; }
    
    DllExport int Faust_getSampleRate(UNITY_DSP* ctx) { return ctx->getSampleRate(); }
    
    DllExport int Faust_getNumInputChannels(UNITY_DSP* ctx) { return ctx->getNumInputs(); }
    
    DllExport int Faust_getNumOutputChannels(UNITY_DSP* ctx) { return ctx->getNumOutputs(); }
    
    DllExport void Faust_setParameterValue(UNITY_DSP* ctx, int param, float value) { ctx->setParamValue(param, value); }
    
    DllExport float Faust_getParameterValue(UNITY_DSP* ctx, int param) { return ctx->getParamValue(param); }
    
    DllExport float Faust_getParamMin(UNITY_DSP* ctx, int param) { return ctx->getParamMin(param); }
    
    DllExport float Faust_getParamMax(UNITY_DSP* ctx, int param) { return ctx->getParamMax(param); }
    
}
#endif

/********************END ARCHITECTURE SECTION (part 2/2)****************/