Refactoring of rec process "~" in faustexp.ml.
[Faustine.git] / interpretor / faust-0.9.47mr3 / architecture / audio / netjack-dsp.h
1 /************************************************************************
2
3 IMPORTANT NOTE : this file contains two clearly delimited sections :
4 the ARCHITECTURE section (in two parts) and the USER section. Each section
5 is governed by its own copyright and license. Please check individually
6 each section for license and copyright information.
7 *************************************************************************/
8
9 /*******************BEGIN ARCHITECTURE SECTION (part 1/2)****************/
10
11 /************************************************************************
12 FAUST Architecture File
13 Copyright (C) 2003-2011 GRAME, Centre National de Creation Musicale
14 ---------------------------------------------------------------------
15 This Architecture section is free software; you can redistribute it
16 and/or modify it under the terms of the GNU General Public License
17 as published by the Free Software Foundation; either version 3 of
18 the License, or (at your option) any later version.
19
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with this program; If not, see <http://www.gnu.org/licenses/>.
27
28 EXCEPTION : As a special exception, you may create a larger work
29 that contains this FAUST architecture section and distribute
30 that work under terms of your choice, so long as this FAUST
31 architecture section is not modified.
32
33
34 ************************************************************************
35 ************************************************************************/
36
37 #ifndef __netjack_dsp__
38 #define __netjack_dsp__
39
40 #include "audio.h"
41 #include "dsp.h"
42 #include <string>
43 #include <jack/net.h>
44
45 class netjackaudio : public audio {
46
47 dsp* fDsp;
48 jack_net_slave_t* fNet;
49 int fCelt;
50 std::string fMasterIP;
51 int fMasterPort;
52
53 static void net_shutdown(void *)
54 {
55 printf("Network failure, restart...\n");
56 }
57
58 static int net_process(jack_nframes_t buffer_size,
59 int,
60 float** audio_input_buffer,
61 int,
62 void**,
63 int,
64 float** audio_output_buffer,
65 int,
66 void**,
67 void* arg)
68 {
69 AVOIDDENORMALS;
70 netjackaudio* obj = (netjackaudio*)arg;
71 obj->fDsp->compute(buffer_size, audio_input_buffer, audio_output_buffer);
72 return 0;
73 }
74
75 public:
76
77 netjackaudio(int celt, const std::string master_ip, int master_port)
78 :fCelt(celt), fMasterIP(master_ip), fMasterPort(master_port)
79 {}
80
81 bool init(const char* name, dsp* DSP)
82 {
83 fDsp = DSP;
84 jack_slave_t request = {
85 DSP->getNumInputs(),
86 DSP->getNumOutputs(),
87 0, 0,
88 DEFAULT_MTU,
89 -1,
90 (fCelt > 0) ? JackCeltEncoder: JackFloatEncoder,
91 (fCelt > 0) ? fCelt : 0,
92 2
93 };
94
95 jack_master_t result;
96 if ((fNet = jack_net_slave_open(fMasterIP.c_str(), fMasterPort, name, &request, &result)) == 0) {
97 printf("jack remote server not running ?\n");
98 return false;
99 }
100
101 jack_set_net_slave_process_callback(fNet, net_process, this);
102 jack_set_net_slave_shutdown_callback(fNet, net_shutdown, 0);
103
104 fDsp->init(result.sample_rate);
105 return true;
106 }
107
108 bool start()
109 {
110 if (jack_net_slave_activate(fNet)) {
111 printf("cannot activate net");
112 return false;
113 }
114 return true;
115 }
116
117 void stop()
118 {
119 jack_net_slave_deactivate(fNet);
120 jack_net_slave_close(fNet);
121 }
122
123 };
124
125
126 #endif
127
128
129 /********************END ARCHITECTURE SECTION (part 2/2)****************/
130