Debian Codegraph @

myjack-dsp.h

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1
2 #ifndef __jack_dsp__
3 #define __jack_dsp__
4
5 #include <cstdlib>
6 #include <stdio.h>
7 #include <jack/jack.h>
8 #include "faust/audio/audio.h"
9 #include "faust/audio/dsp.h"
10
11 static int _srate(jack_nframes_t nframes, void *);
12 static void _jack_shutdown(void *);
13 static int _process (jack_nframes_t nframes, void *client);
14 #ifdef _OPENMP
15 static void* _jackthread(void* arg);
16 #endif
17
18 /******************************************************************************
19 *******************************************************************************
20
21 JACK AUDIO INTERFACE
22
23 *******************************************************************************
24 *******************************************************************************/
25
26 class jackaudio : public audio {
27 dsp* fDsp;
28 jack_client_t* fClient;
29 int fNumInChans; // number of input channels
30 int fNumOutChans; // number of output channels
31 jack_port_t * fInput_ports[256]; // Jack input ports
32 jack_port_t * fOutput_ports[256]; // Jack output ports
33 float* fInChannel[256]; // tables of noninterleaved input channels for FAUST
34 float* fOutChannel[256]; // tables of noninterleaved output channels for FAUST
35
36 public:
37 jackaudio() : fClient(0), fNumInChans(0), fNumOutChans(0) {}
38 virtual ~jackaudio() { stop(); }
39
40 virtual bool init(const char*name, dsp* DSP) {
41 fDsp = DSP;
42 if ((fClient = jack_client_open(name, JackNullOption, NULL)) == 0) {
43 fprintf(stderr, "jack server not running?\n");
44 return false;
45 }
46 #ifdef _OPENMP
47 jack_set_process_thread(fClient, _jackthread, this);
48 #else
49 jack_set_process_callback(fClient, _process, this);
50 #endif
51
52 jack_set_sample_rate_callback(fClient, _srate, 0);
53 jack_on_shutdown(fClient, _jack_shutdown, 0);
54
55 fNumInChans = fDsp->getNumInputs();
56 fNumOutChans = fDsp->getNumOutputs();
57
58 for (int i = 0; i < fNumInChans; i++) {
59 char buf[256];
60 snprintf(buf, 256, "in_%d", i);
61 fInput_ports[i] = jack_port_register(fClient, buf, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
62 }
63 for (int i = 0; i < fNumOutChans; i++) {
64 char buf[256];
65 snprintf(buf, 256, "out_%d", i);
66 fOutput_ports[i] = jack_port_register(fClient, buf, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
67 }
68 fDsp->init(jack_get_sample_rate(fClient));
69 return true;
70 }
71
72 virtual bool start(bool autoconnect) {
73 if (jack_activate(fClient)) {
74 fprintf(stderr, "cannot activate client");
75 return false;
76 }
77
78 if(autoconnect){
79 char** physicalInPorts = (char **)jack_get_ports(fClient, NULL, NULL, JackPortIsPhysical|JackPortIsInput);
80 char** physicalOutPorts = (char **)jack_get_ports(fClient, NULL, NULL, JackPortIsPhysical|JackPortIsOutput);
81 if (physicalOutPorts != NULL) {
82 for (int i = 0; i < fNumInChans && physicalOutPorts[i]; i++)
83 jack_connect(fClient, physicalOutPorts[i], jack_port_name(fInput_ports[i]));
84 }
85 if (physicalInPorts != NULL) {
86 for (int i = 0; i < fNumOutChans && physicalInPorts[i]; i++)
87 jack_connect(fClient, jack_port_name(fOutput_ports[i]), physicalInPorts[i]);
88 }
89 }
90
91 return true;
92 }
93
94 virtual bool start(){
95 return start(true);
96 }
97
98 virtual void stop() {
99 if (fClient) {
100 jack_deactivate(fClient);
101 for (int i = 0; i < fNumInChans; i++)
102 jack_port_unregister(fClient, fInput_ports[i]);
103 for (int i = 0; i < fNumOutChans; i++)
104 jack_port_unregister(fClient, fOutput_ports[i]);
105 jack_client_close(fClient);
106 fClient = 0;
107 }
108 }
109
110 // jack callbacks
111 int process (jack_nframes_t nframes) {
112 AVOIDDENORMALS;
113 for (int i = 0; i < fNumInChans; i++)
114 fInChannel[i] = (float *)jack_port_get_buffer(fInput_ports[i], nframes);
115 for (int i = 0; i < fNumOutChans; i++)
116 fOutChannel[i] = (float *)jack_port_get_buffer(fOutput_ports[i], nframes);
117 fDsp->compute(nframes, fInChannel, fOutChannel);
118 return 0;
119 }
120
121 #ifdef _OPENMP
122 void process_thread () {
123 jack_nframes_t nframes;
124 while (1) {
125 nframes = jack_cycle_wait(fClient);
126 process (nframes);
127 jack_cycle_signal(fClient, 0);
128 }
129 }
130 #endif
131 };
132
133 //----------------------------------------------------------------------------
134 // Jack Callbacks
135 //----------------------------------------------------------------------------
136 static int _srate(jack_nframes_t nframes, void *)
137 {
138 printf("the sample rate is now %u/sec\n", nframes);
139 return 0;
140 }
141
142 static void _jack_shutdown(void *)
143 {
144 exit(1);
145 }
146
147 static int _process(jack_nframes_t nframes, void *client)
148 {
149 jackaudio* jackclient = (jackaudio*)client;
150 return jackclient->process (nframes);
151 }
152
153 #ifdef _OPENMP
154 static void* _jackthread(void* arg)
155 {
156 jackaudio* jackclient = (jackaudio*)arg;
157 jackclient->process_thread();
158 return 0;
159 }
160 #endif
161
162
163
164 #endif