Debian Codegraph @

myjack-dsp.h

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