File: poly-dynamic-jack-gtk.cpp

package info (click to toggle)
faust 2.81.10%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 431,496 kB
  • sloc: cpp: 283,941; ansic: 116,215; javascript: 18,529; sh: 14,356; vhdl: 14,052; java: 5,900; python: 5,091; objc: 3,852; makefile: 2,725; cs: 1,672; lisp: 1,146; ruby: 954; yacc: 586; xml: 471; lex: 247; awk: 111; tcl: 26
file content (341 lines) | stat: -rw-r--r-- 11,216 bytes parent folder | download | duplicates (2)
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
/************************************************************************
 FAUST Architecture File
 Copyright (C) 2016 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.
 
 ************************************************************************/

#include <libgen.h>
#include <iostream>
#include <fstream>
#include <sstream>

/*
 #ifndef FAUSTFLOAT
 #define FAUSTFLOAT double
 #endif
 */

#include "faust/audio/coreaudio-dsp.h"
#include "faust/dsp/llvm-dsp.h"
#include "faust/dsp/proxy-dsp.h"
#include "faust/dsp/poly-llvm-dsp.h"
#include "faust/dsp/poly-interpreter-dsp.h"
#include "faust/gui/meta.h"
#include "faust/gui/FUI.h"
#include "faust/gui/GTKUI.h"
#include "faust/gui/MidiUI.h"
#include "faust/gui/httpdUI.h"
#include "faust/gui/OSCUI.h"
#include "faust/gui/SoundUI.h"
#include "faust/misc.h"
#include "faust/midi/rt-midi.h"
#include "faust/midi/RtMidi.cpp"

using namespace std;

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

struct malloc_memory_manager : public dsp_memory_manager {
    
    void* allocate(size_t size)
    {
        void* res = malloc(size);
        cout << "malloc_manager : " << size << " " << res << endl;
        return res;
    }
    virtual void destroy(void* ptr)
    {
        cout << "free_manager : " << ptr << endl;
        free(ptr);
    }
    
};

int main(int argc, char* argv[])
{
    char name[256];
    char filename[256];
    char rcfilename[256];
    char* home = getenv("HOME");
    int nvoices = 0;
    bool is_midi = false;
    bool midi_sync = false;
    
    snprintf(name, 255, "%s", basename(argv[0]));
    snprintf(filename, 255, "%s", basename(argv[argc-1]));
    snprintf(rcfilename, 255, "%s/.%s-%src", home, name, filename);
    
    bool is_llvm = isopt(argv, "-llvm");
    bool is_interp = isopt(argv, "-interp");
    bool is_osc = isopt(argv, "-osc");
    bool is_httpd = isopt(argv, "-httpd");
    bool is_resample = isopt(argv, "-resample");
    bool is_double = isopt(argv, "-double");
    
    malloc_memory_manager manager;
    
    if (isopt(argv, "-h") || isopt(argv, "-help") || (!is_llvm && !is_interp)) {
        cout << "poly-dynamic-jack-gtk [-llvm/interp] [-nvoices <num>] [-midi] [-osc] [-httpd] [-resample] [additional Faust options (-vec -vs 8...)] foo.dsp" << endl;
        cout << "Use '-llvm' to use LLVM backend\n";
        cout << "Use '-interp' to use Interpreter backend\n";
        cout << "Use '-nvoices <num>' to produce a polyphonic self-contained DSP with <num> voices, ready to be used with MIDI or OSC\n";
        cout << "Use '-midi' to activate MIDI control\n";
        cout << "Use '-osc' to activate OSC control\n";
        cout << "Use '-httpd' to activate HTTP control\n";
        cout << "Use '-resample' to resample soundfiles to the audio driver sample rate\n";
        exit(EXIT_FAILURE);
    }
    
    dsp_poly_factory* factory = nullptr;
    dsp* DSP = nullptr;
    MidiUI* midiinterface = nullptr;
    httpdUI* httpdinterface = nullptr;
    GUI* oscinterface = nullptr;
    
    coreaudio audio(44100, 512);
    string error_msg;
    
    cout << "Libfaust version : " << getCLibFaustVersion() << endl;
    
    int argc1 = 0;
    const char* argv1[64];
    
    cout << "Compiled with additional options : ";
    for (int i = 1; i < argc-1; i++) {
        if ((string(argv[i]) == "-llvm")
            || (string(argv[i]) == "-interp")
            || (string(argv[i]) == "-midi")
            || (string(argv[i]) == "-osc")
            || (string(argv[i]) == "-httpd")
            || (string(argv[i]) == "-resample")) {
            continue;
        } else if ((string(argv[i]) == "-nvoices")
                   || (string(argv[i]) == "-port")
                   || (string(argv[i]) == "-outport")
                   || (string(argv[i]) == "-errport")
                   || (string(argv[i]) == "-desthost")
                   || (string(argv[i]) == "-xmit")) {
            i++;
            continue;
        }
        argv1[argc1++] = argv[i];
        cout << argv[i] << " ";
    }
    cout << endl;
    
    argv1[argc1] = nullptr;  // NULL terminated argv
    
    if (is_llvm) {
        cout << "Using LLVM backend" << endl;
        // argc : without the filename (last element);
        factory = createPolyDSPFactoryFromFile(argv[argc-1], argc1, argv1, "", error_msg, -1);
        
        // Test Write/Read
        if (factory) {
            {
                cout << "Test writePolyDSPFactoryToBitcodeFile/readPolyDSPFactoryFromBitcodeFile" << endl;
                string path_name = factory->getName();
                writePolyDSPFactoryToBitcodeFile(factory, path_name);
                delete factory;
                factory = readPolyDSPFactoryFromBitcodeFile(path_name, "", error_msg, -1);
                if (!factory) {
                    cerr << "ERROR : readPolyDSPFactoryFromBitcodeFile " << error_msg;
                    exit(EXIT_FAILURE);
                }
            }
            
            {
                cout << "Test writePolyDSPFactoryToIRFile/readPolyDSPFactoryFromIRFile" << endl;
                string path_name = factory->getName();
                writePolyDSPFactoryToIRFile(factory, path_name);
                delete factory;
                factory = readPolyDSPFactoryFromIRFile(path_name, "", error_msg, -1);
                if (!factory) {
                    cerr << "readPolyDSPFactoryFromIRFile " << error_msg;
                    exit(EXIT_FAILURE);
                }
            }
            
            {
                cout << "Test writePolyDSPFactoryToMachineFile/readPolyDSPFactoryFromMachineFile" << endl;
                string path_name = factory->getName();
                writePolyDSPFactoryToMachineFile(factory, path_name, "");
                delete factory;
                factory = readPolyDSPFactoryFromBitcodeFile(path_name, "", error_msg, -1);
                if (!factory) {
                    cerr << "readPolyDSPFactoryFromBitcodeFile " << error_msg;
                    exit(EXIT_FAILURE);
                }
            }
        }
        
    } else {
        cout << "Using interpreter backend" << endl;
        // argc : without the filename (last element);
        factory = createInterpreterPolyDSPFactoryFromFile(argv[argc-1], argc1, argv1, error_msg);
        // Test Write/Read
        if (factory) {
            cout << "Test writeInterpreterPolyDSPFactoryToMachineFile/readInterpreterPolyDSPFactoryFromMachineFile" << endl;
            string path_name = factory->getName();
            writeInterpreterPolyDSPFactoryToMachineFile(factory, path_name);
            delete factory;
            factory = readInterpreterPolyDSPFactoryFromMachineFile(path_name, error_msg);
            if (!factory) {
                cerr << "readInterpreterPolyDSPFactoryFromMachineFile " << error_msg;
                exit(EXIT_FAILURE);
            }
        }
    }
    
    if (!factory) {
        cerr << error_msg;
        exit(EXIT_FAILURE);
    }
    
    // Before reading the -nvoices and -midi parameters
    DSP = factory->createDSPInstance();
    if (!DSP) {
        cerr << "Cannot create instance "<< endl;
        exit(EXIT_FAILURE);
    }
    MidiMeta::analyse(DSP, is_midi, midi_sync, nvoices);
    delete (DSP);
    
    // Possibly update the state read in "options" with command line parameters
    nvoices = lopt(argv, "-nvoices", nvoices);
    is_midi = isopt(argv, "-midi") || is_midi;
    
    //factory->setMemoryManager(&manager);  // causes crash in -fm mode
    DSP = factory->createPolyDSPInstance(nvoices, true, true, is_double);
    if (!DSP) {
        cerr << "Cannot create instance "<< endl;
        exit(EXIT_FAILURE);
    }
    
    cout << "getName " << factory->getName() << endl;
    //cout << "getSHAKey " << factory->getSHAKey() << endl;
    
    /*
     JSONUI json(DSP->getNumInputs(), DSP->getNumOutputs());
     DSP->buildUserInterface(&json);
     cout << "JSON : " << json.JSON() << endl;
     */
    
    GUI* interface = new GTKUI(filename, &argc, &argv);
    DSP->buildUserInterface(interface);
    
    FUI* finterface = new FUI();
    DSP->buildUserInterface(finterface);
    
    if (!audio.init(filename, DSP)) {
        exit(EXIT_FAILURE);
    }
    
    // After audio init to get SR
    SoundUI* soundinterface = nullptr;
    if (is_resample) {
        soundinterface = new SoundUI("", audio.getSampleRate());
    } else {
        soundinterface = new SoundUI();
    }
    DSP->buildUserInterface(soundinterface);
    
    if (is_httpd) {
        httpdinterface = new httpdUI(name, DSP->getNumInputs(), DSP->getNumOutputs(), argc, argv);
        DSP->buildUserInterface(httpdinterface);
    }
    
    if (is_osc) {
        oscinterface = new OSCUI(filename, argc, argv);
        DSP->buildUserInterface(oscinterface);
    }
   
    rt_midi midi_handler(name);
    if (is_midi) {
        midiinterface = new MidiUI(&midi_handler);
        DSP->buildUserInterface(midiinterface);
        cout << "MIDI is on" << endl;
    }
    
    // State (after UI construction)
    finterface->recallState(rcfilename);
    audio.start();
    if (is_httpd) {
        httpdinterface->run();
    }
    
    if (is_osc) {
        oscinterface->run();
    }
    
    if (is_midi) {
        midiinterface->run();
    }
    
    /*
     cout << DSP->getJSON();
     
     // Test setParamValue API
     DSP->setParamValue("/Polyphonic/Voices/clarinet/otherParams/bellOpening", 0.35);
     DSP->setParamValue("/Polyphonic/Voices/clarinet/midi/bend", 1.5);
     
     // Test MIDI API
     DSP->keyOn(0, 60, 100);
     DSP->keyOn(0, 64, 100);
     DSP->keyOn(0, 67, 100);
     
     usleep(1000000);
     
     DSP->keyOff(0, 60, 100);
     DSP->keyOff(0, 64, 100);
     DSP->keyOff(0, 67, 100);
     
     // Test MIDI API
     DSP->pitchWheel(0, 4000);
     DSP->keyOn(0, 60, 100);
     DSP->keyOn(0, 64, 100);
     DSP->keyOn(0, 67, 100);
     
     usleep(1000000);
     
     DSP->keyOff(0, 60, 100);
     DSP->keyOff(0, 64, 100);
     DSP->keyOff(0, 67, 100);
     */
    
    interface->run();
    
    audio.stop();
    
    finterface->saveState(rcfilename);
    
    delete DSP;
    delete interface;
    delete finterface;
    delete midiinterface;
    delete httpdinterface;
    delete oscinterface;
    delete factory;
    
    return 0;
}