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