File: template-llvm.cpp

package info (click to toggle)
faust 2.79.3%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: 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 (186 lines) | stat: -rw-r--r-- 5,257 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
/************************************************************************
 ************************************************************************
 FAUST API Architecture File
 Copyright (C) 2022 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  __template_llvm__
#define  __template_llvm__

#include <algorithm>
#include <cmath>
#include <cstdint>
#include <string>
#include <iostream>

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

#if defined(_WIN32)
#define RESTRICT __restrict
#else
#define RESTRICT __restrict__
#endif

using namespace std;

/*
 A template that uses libfaust + LLVM backend to dynamically compile a foo.dsp file.
 To be used with the -inj option in faust2xx tools like:
 
 faust2cagtk -inj template-llvm.cpp faust2cagtk-llvm.dsp (a dummy DSP)
 to generate a monophonic 'faust2cagtk-llvm' application.
  
 or:
 
 faust2cagtk -inj template-llvm.cpp -midi -nvoices 8 faust2cagtk-llvm.dsp
 to generate a polyphonic (8 voices), MIDI controllable 'faust2cagtk-llvm' application.
 
 Note that libfaust and LLVM libraries still have to be added at the link stage,
 so a '-dyn : create libfaust + LLVM backend dynamic version' option has been added to the faust2cagtk tool and some others.

 Then 'faust2cagtk-llvm' will ask for a DSP to compile:
 
 ./faust2cagtk-llvm
 <Enter a foo.dsp file>
 
 Note that the resulting binary keeps its own control options, like:
 
 ./faust2cagtk-llvm -h
 ./faust2cagtk-llvm [--sample-rate <val>] [--buffer <val>] [--nvoices <val>]
    [--control <0/1>] [--group <0/1>] [--virtual-midi <0/1>]
 
So ./faust2cagtk-llvm --nvoices 16 starts the program with 16 voices.
*/

class mydsp final : public dsp {
    
 private:
    
    llvm_dsp_factory* fFactory;
    llvm_dsp* fDSP;
    static string gFileName;
    
 public:
    
    // May be called several times (for instance with the polyphonic architecture),
    // so get the wanted filename only once at first call.
    mydsp()
    {
        if (gFileName == "") {
            // Reading the DSP filename on the standard input
            cout << "<Enter a foo.dsp file>\n";
            getline(cin, gFileName);
        }
        string error_msg;
        if (sizeof(FAUSTFLOAT) == 8) {
            const char* argv[] = {"-double"};
            fFactory = createDSPFactoryFromFile(gFileName, 1, argv, "", error_msg, -1);
        } else {
            fFactory = createDSPFactoryFromFile(gFileName, 0, nullptr, "", error_msg, -1);
        }
        if (!fFactory) {
            cerr << "Cannot create factory : " << error_msg;
            exit(EXIT_FAILURE);
        }
        fDSP = fFactory->createDSPInstance();
        if (!fDSP) {
            cerr << "Cannot create instance "<< endl;
            exit(EXIT_FAILURE);
        }
    }
    
    // Called by the 'clone' method.
    // The factory is kept once in the first mydsp actually allocating it.
    mydsp(llvm_dsp* dsp):fFactory(nullptr), fDSP(dsp)
    {}
    
    ~mydsp()
    {
        // All kept DSP are finally deallocated by the factory
        deleteDSPFactory(fFactory);
    }
    
    void metadata(Meta* m)
    {
        fDSP->metadata(m);
    }

    int getNumInputs()
    {
        return fDSP->getNumInputs();
    }
    int getNumOutputs()
    {
        return fDSP->getNumOutputs();
    }
        
    void instanceConstants(int sample_rate)
    {
        fDSP->instanceConstants(sample_rate);
    }
    
    void instanceResetUserInterface()
    {
        fDSP->instanceResetUserInterface();
    }
    
    void instanceClear()
    {
        fDSP->instanceClear();
    }
    
    void init(int sample_rate)
    {
        fDSP->init(sample_rate);
    }
    
    void instanceInit(int sample_rate)
    {
        fDSP->instanceInit(sample_rate);
    }
    
    mydsp* clone()
    {
        return new mydsp(fDSP->clone());
    }
    
    int getSampleRate()
    {
        return fDSP->getSampleRate();
    }
    
    void buildUserInterface(UI* ui_interface)
    {
        fDSP->buildUserInterface(ui_interface);
    }
    
    void compute(int count, FAUSTFLOAT** RESTRICT inputs, FAUSTFLOAT** RESTRICT outputs)
    {
        fDSP->compute(count, inputs, outputs);
    }

};

string mydsp::gFileName;

#endif