1 /************************************************************************
2  IMPORTANT NOTE : this file contains two clearly delimited sections :
3  the ARCHITECTURE section (in two parts) and the USER section. Each section
4  is governed by its own copyright and license. Please check individually
5  each section for license and copyright information.
6  *************************************************************************/
7 
8 /******************* BEGIN alsa-console.cpp ****************/
9 
10 /************************************************************************
11  FAUST Architecture File
12  Copyright (C) 2003-2019 GRAME, Centre National de Creation Musicale
13  ---------------------------------------------------------------------
14  This Architecture section is free software; you can redistribute it
15  and/or modify it under the terms of the GNU General Public License
16  as published by the Free Software Foundation; either version 3 of
17  the License, or (at your option) any later version.
18 
19  This program is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  GNU General Public License for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with this program; If not, see <http://www.gnu.org/licenses/>.
26 
27  EXCEPTION : As a special exception, you may create a larger work
28  that contains this FAUST architecture section and distribute
29  that work under terms of your choice, so long as this FAUST
30  architecture section is not modified.
31 
32  ************************************************************************
33  ************************************************************************/
34 
35 #include <libgen.h>
36 #include <stdlib.h>
37 #include <iostream>
38 #include <list>
39 #include <cmath>
40 
41 #include "faust/dsp/timed-dsp.h"
42 #include "faust/gui/FUI.h"
43 #include "faust/misc.h"
44 #include "faust/gui/GUI.h"
45 #include "faust/gui/JSONUI.h"
46 #include "faust/gui/console.h"
47 #include "faust/audio/alsa-dsp.h"
48 
49 #ifdef OSCCTRL
50 #include "faust/gui/OSCUI.h"
51 #endif
52 
53 #ifdef HTTPCTRL
54 #include "faust/gui/httpdUI.h"
55 #endif
56 
57 // Always include this file, otherwise -nvoices only mode does not compile....
58 #include "faust/gui/MidiUI.h"
59 
60 #ifdef MIDICTRL
61 #include "faust/midi/rt-midi.h"
62 #include "faust/midi/RtMidi.cpp"
63 #endif
64 
65 /******************************************************************************
66  *******************************************************************************
67 
68  VECTOR INTRINSICS
69 
70  *******************************************************************************
71  *******************************************************************************/
72 
73 <<includeIntrinsic>>
74 
75 /********************END ARCHITECTURE SECTION (part 1/2)****************/
76 
77 /**************************BEGIN USER SECTION **************************/
78 
79 <<includeclass>>
80 
81 /***************************END USER SECTION ***************************/
82 
83 /*******************BEGIN ARCHITECTURE SECTION (part 2/2)***************/
84 
85 #include "faust/dsp/poly-dsp.h"
86 
87 #ifdef POLY2
88 #include "faust/dsp/dsp-combiner.h"
89 #include "effect.h"
90 #endif
91 
92 /***************************END USER SECTION ***************************/
93 
94 /*******************BEGIN ARCHITECTURE SECTION (part 2/2)***************/
95 
96 using namespace std;
97 
98 dsp* DSP;
99 
100 list<GUI*> GUI::fGuiList;
101 ztimedmap GUI::gTimedZoneMap;
102 
103 //-------------------------------------------------------------------------
104 // 									MAIN
105 //-------------------------------------------------------------------------
106 
hasMIDISync()107 static bool hasMIDISync()
108 {
109     JSONUI jsonui;
110     mydsp* tmp_dsp = new mydsp();
111     tmp_dsp->buildUserInterface(&jsonui);
112     string json = jsonui.JSON();
113     delete tmp_dsp;
114 
115     return ((json.find("midi") != string::npos) &&
116             ((json.find("start") != string::npos) ||
117             (json.find("stop") != string::npos) ||
118             (json.find("clock") != string::npos)));
119 }
120 
main(int argc,char * argv[])121 int main(int argc, char *argv[] )
122 {
123     char* appname = basename (argv [0]);
124     char rcfilename[256];
125     char* home = getenv("HOME");
126     int nvoices = 0;
127     snprintf(rcfilename, 256, "%s/.%src", home, appname);
128 
129 #ifdef POLY2
130     nvoices = lopt(argv, "--nvoices", nvoices);
131     int group = lopt(argv, "--group", 1);
132     cout << "Started with " << nvoices << " voices\n";
133     DSP = new mydsp_poly(new mydsp(), nvoices, true, group);
134 
135 #if MIDICTRL
136     if (hasMIDISync()) {
137         DSP = new timed_dsp(new dsp_sequencer(DSP, new effect()));
138     } else {
139         DSP = new dsp_sequencer(DSP, new effect());
140     }
141 #else
142     DSP = new dsp_sequencer(DSP, new effect());
143 #endif
144 
145 #else
146     nvoices = lopt(argv, "--nvoices", nvoices);
147     int group = lopt(argv, "--group", 1);
148 
149     if (nvoices > 0) {
150         cout << "Started with " << nvoices << " voices\n";
151         DSP = new mydsp_poly(new mydsp(), nvoices, true, group);
152 
153 #if MIDICTRL
154         if (hasMIDISync()) {
155             DSP = new timed_dsp(DSP);
156         }
157 #endif
158     } else {
159 #if MIDICTRL
160         if (hasMIDISync()) {
161             DSP = new timed_dsp(new mydsp());
162         } else {
163             DSP = new mydsp();
164         }
165 #else
166         DSP = new mydsp();
167 #endif
168     }
169 #endif
170 
171     if (!DSP) {
172         cerr << "Unable to allocate Faust DSP object" << endl;
173         exit(1);
174     }
175 
176     CMDUI* interface = new CMDUI(argc, argv, true);
177     FUI* finterface	= new FUI();
178     DSP->buildUserInterface(interface);
179     DSP->buildUserInterface(finterface);
180 
181 #ifdef MIDICTRL
182     rt_midi midi_handler(appname);
183     MidiUI midiinterface(&midi_handler);
184     DSP->buildUserInterface(&midiinterface);
185     cout << "MIDI is on" << endl;
186 #endif
187 
188 #ifdef HTTPCTRL
189     httpdUI* httpdinterface = new httpdUI(appname, DSP->getNumInputs(), DSP->getNumOutputs(), argc, argv);
190     DSP->buildUserInterface(httpdinterface);
191     cout << "HTTPD is on" << endl;
192 #endif
193 
194 #ifdef OSCCTRL
195     GUI* oscinterface = new OSCUI(appname, argc, argv);
196     DSP->buildUserInterface(oscinterface);
197 #endif
198 
199     alsaaudio audio(argc, argv, DSP);
200     audio.init(appname, DSP);
201     finterface->recallState(rcfilename);
202     audio.start();
203 
204 #ifdef HTTPCTRL
205     httpdinterface->run();
206 #endif
207 
208 #ifdef OSCCTRL
209     oscinterface->run();
210 #endif
211 #ifdef MIDICTRL
212     if (!midiinterface.run()) {
213         cerr << "MidiUI run error\n";
214     }
215 #endif
216     interface->run();
217 
218 #ifdef MIDICTRL
219     midiinterface.stop();
220 #endif
221 
222     audio.stop();
223     finterface->saveState(rcfilename);
224 
225     // desallocation
226     delete interface;
227     delete finterface;
228 #ifdef HTTPCTRL
229     delete httpdinterface;
230 #endif
231 #ifdef OSCCTRL
232     delete oscinterface;
233 #endif
234 
235     return 0;
236 }
237 
238 /******************** END alsa-console.cpp ****************/
239