Chris@43
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@43
|
2
|
Chris@43
|
3 /*
|
Chris@43
|
4 Sonic Visualiser
|
Chris@43
|
5 An audio file viewer and annotation editor.
|
Chris@43
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@43
|
7 This file copyright 2006 Chris Cannam.
|
Chris@43
|
8
|
Chris@43
|
9 This program is free software; you can redistribute it and/or
|
Chris@43
|
10 modify it under the terms of the GNU General Public License as
|
Chris@43
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@43
|
12 License, or (at your option) any later version. See the file
|
Chris@43
|
13 COPYING included with this distribution for more information.
|
Chris@43
|
14 */
|
Chris@43
|
15
|
Chris@43
|
16 #ifdef HAVE_JACK
|
Chris@43
|
17
|
Chris@43
|
18 #include "AudioJACKTarget.h"
|
Chris@43
|
19 #include "AudioCallbackPlaySource.h"
|
Chris@43
|
20
|
Chris@43
|
21 #include <iostream>
|
Chris@43
|
22 #include <cmath>
|
Chris@43
|
23
|
Chris@43
|
24 //#define DEBUG_AUDIO_JACK_TARGET 1
|
Chris@43
|
25
|
Chris@43
|
26 #ifdef BUILD_STATIC
|
Chris@43
|
27 #ifdef Q_OS_LINUX
|
Chris@43
|
28
|
Chris@43
|
29 // Some lunacy to enable JACK support in static builds. JACK isn't
|
Chris@43
|
30 // supposed to be linked statically, because it depends on a
|
Chris@43
|
31 // consistent shared memory layout between client library and daemon,
|
Chris@43
|
32 // so it's very fragile in the face of version mismatches.
|
Chris@43
|
33 //
|
Chris@43
|
34 // Therefore for static builds on Linux we avoid linking against JACK
|
Chris@43
|
35 // at all during the build, instead using dlopen and runtime symbol
|
Chris@43
|
36 // lookup to switch on JACK support at runtime. The following big
|
Chris@43
|
37 // mess (down to the #endifs) is the code that implements this.
|
Chris@43
|
38
|
Chris@43
|
39 static void *symbol(const char *name)
|
Chris@43
|
40 {
|
Chris@43
|
41 static bool attempted = false;
|
Chris@43
|
42 static void *library = 0;
|
Chris@43
|
43 static std::map<const char *, void *> symbols;
|
Chris@43
|
44 if (symbols.find(name) != symbols.end()) return symbols[name];
|
Chris@43
|
45 if (!library) {
|
Chris@43
|
46 if (!attempted) {
|
Chris@43
|
47 library = ::dlopen("libjack.so.1", RTLD_NOW);
|
Chris@43
|
48 if (!library) library = ::dlopen("libjack.so.0", RTLD_NOW);
|
Chris@43
|
49 if (!library) library = ::dlopen("libjack.so", RTLD_NOW);
|
Chris@43
|
50 if (!library) {
|
Chris@43
|
51 std::cerr << "WARNING: AudioJACKTarget: Failed to load JACK library: "
|
Chris@43
|
52 << ::dlerror() << " (tried .so, .so.0, .so.1)"
|
Chris@43
|
53 << std::endl;
|
Chris@43
|
54 }
|
Chris@43
|
55 attempted = true;
|
Chris@43
|
56 }
|
Chris@43
|
57 if (!library) return 0;
|
Chris@43
|
58 }
|
Chris@43
|
59 void *symbol = ::dlsym(library, name);
|
Chris@43
|
60 if (!symbol) {
|
Chris@43
|
61 std::cerr << "WARNING: AudioJACKTarget: Failed to locate symbol "
|
Chris@43
|
62 << name << ": " << ::dlerror() << std::endl;
|
Chris@43
|
63 }
|
Chris@43
|
64 symbols[name] = symbol;
|
Chris@43
|
65 return symbol;
|
Chris@43
|
66 }
|
Chris@43
|
67
|
Chris@43
|
68 static int dynamic_jack_set_process_callback(jack_client_t *client,
|
Chris@43
|
69 JackProcessCallback process_callback,
|
Chris@43
|
70 void *arg)
|
Chris@43
|
71 {
|
Chris@43
|
72 typedef int (*func)(jack_client_t *client,
|
Chris@43
|
73 JackProcessCallback process_callback,
|
Chris@43
|
74 void *arg);
|
Chris@43
|
75 void *s = symbol("jack_set_process_callback");
|
Chris@43
|
76 if (!s) return 1;
|
Chris@43
|
77 func f = (func)s;
|
Chris@43
|
78 return f(client, process_callback, arg);
|
Chris@43
|
79 }
|
Chris@43
|
80
|
Chris@43
|
81 static int dynamic_jack_set_xrun_callback(jack_client_t *client,
|
Chris@43
|
82 JackXRunCallback xrun_callback,
|
Chris@43
|
83 void *arg)
|
Chris@43
|
84 {
|
Chris@43
|
85 typedef int (*func)(jack_client_t *client,
|
Chris@43
|
86 JackXRunCallback xrun_callback,
|
Chris@43
|
87 void *arg);
|
Chris@43
|
88 void *s = symbol("jack_set_xrun_callback");
|
Chris@43
|
89 if (!s) return 1;
|
Chris@43
|
90 func f = (func)s;
|
Chris@43
|
91 return f(client, xrun_callback, arg);
|
Chris@43
|
92 }
|
Chris@43
|
93
|
Chris@43
|
94 static const char **dynamic_jack_get_ports(jack_client_t *client,
|
Chris@43
|
95 const char *port_name_pattern,
|
Chris@43
|
96 const char *type_name_pattern,
|
Chris@43
|
97 unsigned long flags)
|
Chris@43
|
98 {
|
Chris@43
|
99 typedef const char **(*func)(jack_client_t *client,
|
Chris@43
|
100 const char *port_name_pattern,
|
Chris@43
|
101 const char *type_name_pattern,
|
Chris@43
|
102 unsigned long flags);
|
Chris@43
|
103 void *s = symbol("jack_get_ports");
|
Chris@43
|
104 if (!s) return 0;
|
Chris@43
|
105 func f = (func)s;
|
Chris@43
|
106 return f(client, port_name_pattern, type_name_pattern, flags);
|
Chris@43
|
107 }
|
Chris@43
|
108
|
Chris@43
|
109 static jack_port_t *dynamic_jack_port_register(jack_client_t *client,
|
Chris@43
|
110 const char *port_name,
|
Chris@43
|
111 const char *port_type,
|
Chris@43
|
112 unsigned long flags,
|
Chris@43
|
113 unsigned long buffer_size)
|
Chris@43
|
114 {
|
Chris@43
|
115 typedef jack_port_t *(*func)(jack_client_t *client,
|
Chris@43
|
116 const char *port_name,
|
Chris@43
|
117 const char *port_type,
|
Chris@43
|
118 unsigned long flags,
|
Chris@43
|
119 unsigned long buffer_size);
|
Chris@43
|
120 void *s = symbol("jack_port_register");
|
Chris@43
|
121 if (!s) return 0;
|
Chris@43
|
122 func f = (func)s;
|
Chris@43
|
123 return f(client, port_name, port_type, flags, buffer_size);
|
Chris@43
|
124 }
|
Chris@43
|
125
|
Chris@43
|
126 static int dynamic_jack_connect(jack_client_t *client,
|
Chris@43
|
127 const char *source,
|
Chris@43
|
128 const char *dest)
|
Chris@43
|
129 {
|
Chris@43
|
130 typedef int (*func)(jack_client_t *client,
|
Chris@43
|
131 const char *source,
|
Chris@43
|
132 const char *dest);
|
Chris@43
|
133 void *s = symbol("jack_connect");
|
Chris@43
|
134 if (!s) return 1;
|
Chris@43
|
135 func f = (func)s;
|
Chris@43
|
136 return f(client, source, dest);
|
Chris@43
|
137 }
|
Chris@43
|
138
|
Chris@43
|
139 static void *dynamic_jack_port_get_buffer(jack_port_t *port,
|
Chris@43
|
140 jack_nframes_t sz)
|
Chris@43
|
141 {
|
Chris@43
|
142 typedef void *(*func)(jack_port_t *, jack_nframes_t);
|
Chris@43
|
143 void *s = symbol("jack_port_get_buffer");
|
Chris@43
|
144 if (!s) return 0;
|
Chris@43
|
145 func f = (func)s;
|
Chris@43
|
146 return f(port, sz);
|
Chris@43
|
147 }
|
Chris@43
|
148
|
Chris@43
|
149 static int dynamic_jack_port_unregister(jack_client_t *client,
|
Chris@43
|
150 jack_port_t *port)
|
Chris@43
|
151 {
|
Chris@43
|
152 typedef int(*func)(jack_client_t *, jack_port_t *);
|
Chris@43
|
153 void *s = symbol("jack_port_unregister");
|
Chris@43
|
154 if (!s) return 0;
|
Chris@43
|
155 func f = (func)s;
|
Chris@43
|
156 return f(client, port);
|
Chris@43
|
157 }
|
Chris@43
|
158
|
Chris@43
|
159 #define dynamic1(rv, name, argtype, failval) \
|
Chris@43
|
160 static rv dynamic_##name(argtype arg) { \
|
Chris@43
|
161 typedef rv (*func) (argtype); \
|
Chris@43
|
162 void *s = symbol(#name); \
|
Chris@43
|
163 if (!s) return failval; \
|
Chris@43
|
164 func f = (func) s; \
|
Chris@43
|
165 return f(arg); \
|
Chris@43
|
166 }
|
Chris@43
|
167
|
Chris@43
|
168 dynamic1(jack_client_t *, jack_client_new, const char *, 0);
|
Chris@43
|
169 dynamic1(jack_nframes_t, jack_get_buffer_size, jack_client_t *, 0);
|
Chris@43
|
170 dynamic1(jack_nframes_t, jack_get_sample_rate, jack_client_t *, 0);
|
Chris@43
|
171 dynamic1(int, jack_activate, jack_client_t *, 1);
|
Chris@43
|
172 dynamic1(int, jack_deactivate, jack_client_t *, 1);
|
Chris@43
|
173 dynamic1(int, jack_client_close, jack_client_t *, 1);
|
Chris@43
|
174 dynamic1(jack_nframes_t, jack_port_get_latency, jack_port_t *, 0);
|
Chris@43
|
175 dynamic1(const char *, jack_port_name, const jack_port_t *, 0);
|
Chris@43
|
176
|
Chris@43
|
177 #define jack_client_new dynamic_jack_client_new
|
Chris@43
|
178 #define jack_get_buffer_size dynamic_jack_get_buffer_size
|
Chris@43
|
179 #define jack_get_sample_rate dynamic_jack_get_sample_rate
|
Chris@43
|
180 #define jack_set_process_callback dynamic_jack_set_process_callback
|
Chris@43
|
181 #define jack_set_xrun_callback dynamic_jack_set_xrun_callback
|
Chris@43
|
182 #define jack_activate dynamic_jack_activate
|
Chris@43
|
183 #define jack_deactivate dynamic_jack_deactivate
|
Chris@43
|
184 #define jack_client_close dynamic_jack_client_close
|
Chris@43
|
185 #define jack_get_ports dynamic_jack_get_ports
|
Chris@43
|
186 #define jack_port_register dynamic_jack_port_register
|
Chris@43
|
187 #define jack_port_unregister dynamic_jack_port_unregister
|
Chris@43
|
188 #define jack_port_get_latency dynamic_jack_port_get_latency
|
Chris@43
|
189 #define jack_port_name dynamic_jack_port_name
|
Chris@43
|
190 #define jack_connect dynamic_jack_connect
|
Chris@43
|
191 #define jack_port_get_buffer dynamic_jack_port_get_buffer
|
Chris@43
|
192
|
Chris@43
|
193 #endif
|
Chris@43
|
194 #endif
|
Chris@43
|
195
|
Chris@43
|
196 AudioJACKTarget::AudioJACKTarget(AudioCallbackPlaySource *source) :
|
Chris@43
|
197 AudioCallbackPlayTarget(source),
|
Chris@43
|
198 m_client(0),
|
Chris@43
|
199 m_bufferSize(0),
|
Chris@43
|
200 m_sampleRate(0)
|
Chris@43
|
201 {
|
Chris@43
|
202 char name[100];
|
Chris@43
|
203 strcpy(name, "Sonic Visualiser");
|
Chris@43
|
204 m_client = jack_client_new(name);
|
Chris@43
|
205
|
Chris@43
|
206 if (!m_client) {
|
Chris@43
|
207 sprintf(name, "Sonic Visualiser (%d)", (int)getpid());
|
Chris@43
|
208 m_client = jack_client_new(name);
|
Chris@43
|
209 if (!m_client) {
|
Chris@43
|
210 std::cerr
|
Chris@43
|
211 << "ERROR: AudioJACKTarget: Failed to connect to JACK server"
|
Chris@43
|
212 << std::endl;
|
Chris@43
|
213 }
|
Chris@43
|
214 }
|
Chris@43
|
215
|
Chris@43
|
216 if (!m_client) return;
|
Chris@43
|
217
|
Chris@43
|
218 m_bufferSize = jack_get_buffer_size(m_client);
|
Chris@43
|
219 m_sampleRate = jack_get_sample_rate(m_client);
|
Chris@43
|
220
|
Chris@43
|
221 jack_set_xrun_callback(m_client, xrunStatic, this);
|
Chris@43
|
222 jack_set_process_callback(m_client, processStatic, this);
|
Chris@43
|
223
|
Chris@43
|
224 if (jack_activate(m_client)) {
|
Chris@43
|
225 std::cerr << "ERROR: AudioJACKTarget: Failed to activate JACK client"
|
Chris@43
|
226 << std::endl;
|
Chris@43
|
227 }
|
Chris@43
|
228
|
Chris@43
|
229 if (m_source) {
|
Chris@43
|
230 sourceModelReplaced();
|
Chris@43
|
231 }
|
Chris@43
|
232 }
|
Chris@43
|
233
|
Chris@43
|
234 AudioJACKTarget::~AudioJACKTarget()
|
Chris@43
|
235 {
|
Chris@43
|
236 std::cerr << "AudioJACKTarget::~AudioJACKTarget()" << std::endl;
|
Chris@43
|
237 if (m_client) {
|
Chris@43
|
238 jack_deactivate(m_client);
|
Chris@43
|
239 jack_client_close(m_client);
|
Chris@43
|
240 }
|
Chris@43
|
241 std::cerr << "AudioJACKTarget::~AudioJACKTarget() done" << std::endl;
|
Chris@43
|
242 }
|
Chris@43
|
243
|
Chris@43
|
244 bool
|
Chris@43
|
245 AudioJACKTarget::isOK() const
|
Chris@43
|
246 {
|
Chris@43
|
247 return (m_client != 0);
|
Chris@43
|
248 }
|
Chris@43
|
249
|
Chris@43
|
250 int
|
Chris@43
|
251 AudioJACKTarget::processStatic(jack_nframes_t nframes, void *arg)
|
Chris@43
|
252 {
|
Chris@43
|
253 return ((AudioJACKTarget *)arg)->process(nframes);
|
Chris@43
|
254 }
|
Chris@43
|
255
|
Chris@43
|
256 int
|
Chris@43
|
257 AudioJACKTarget::xrunStatic(void *arg)
|
Chris@43
|
258 {
|
Chris@43
|
259 return ((AudioJACKTarget *)arg)->xrun();
|
Chris@43
|
260 }
|
Chris@43
|
261
|
Chris@43
|
262 void
|
Chris@43
|
263 AudioJACKTarget::sourceModelReplaced()
|
Chris@43
|
264 {
|
Chris@43
|
265 m_mutex.lock();
|
Chris@43
|
266
|
Chris@43
|
267 m_source->setTargetBlockSize(m_bufferSize);
|
Chris@43
|
268 m_source->setTargetSampleRate(m_sampleRate);
|
Chris@43
|
269
|
Chris@43
|
270 size_t channels = m_source->getSourceChannelCount();
|
Chris@43
|
271
|
Chris@43
|
272 // Because we offer pan, we always want at least 2 channels
|
Chris@43
|
273 if (channels < 2) channels = 2;
|
Chris@43
|
274
|
Chris@43
|
275 if (channels == m_outputs.size() || !m_client) {
|
Chris@43
|
276 m_mutex.unlock();
|
Chris@43
|
277 return;
|
Chris@43
|
278 }
|
Chris@43
|
279
|
Chris@43
|
280 const char **ports =
|
Chris@43
|
281 jack_get_ports(m_client, NULL, NULL,
|
Chris@43
|
282 JackPortIsPhysical | JackPortIsInput);
|
Chris@43
|
283 size_t physicalPortCount = 0;
|
Chris@43
|
284 while (ports[physicalPortCount]) ++physicalPortCount;
|
Chris@43
|
285
|
Chris@43
|
286 #ifdef DEBUG_AUDIO_JACK_TARGET
|
Chris@43
|
287 std::cerr << "AudioJACKTarget::sourceModelReplaced: have " << channels << " channels and " << physicalPortCount << " physical ports" << std::endl;
|
Chris@43
|
288 #endif
|
Chris@43
|
289
|
Chris@43
|
290 while (m_outputs.size() < channels) {
|
Chris@43
|
291
|
Chris@43
|
292 char name[20];
|
Chris@43
|
293 jack_port_t *port;
|
Chris@43
|
294
|
Chris@43
|
295 sprintf(name, "out %d", m_outputs.size() + 1);
|
Chris@43
|
296
|
Chris@43
|
297 port = jack_port_register(m_client,
|
Chris@43
|
298 name,
|
Chris@43
|
299 JACK_DEFAULT_AUDIO_TYPE,
|
Chris@43
|
300 JackPortIsOutput,
|
Chris@43
|
301 0);
|
Chris@43
|
302
|
Chris@43
|
303 if (!port) {
|
Chris@43
|
304 std::cerr
|
Chris@43
|
305 << "ERROR: AudioJACKTarget: Failed to create JACK output port "
|
Chris@43
|
306 << m_outputs.size() << std::endl;
|
Chris@43
|
307 } else {
|
Chris@43
|
308 m_source->setTargetPlayLatency(jack_port_get_latency(port));
|
Chris@43
|
309 }
|
Chris@43
|
310
|
Chris@43
|
311 if (m_outputs.size() < physicalPortCount) {
|
Chris@43
|
312 jack_connect(m_client, jack_port_name(port), ports[m_outputs.size()]);
|
Chris@43
|
313 }
|
Chris@43
|
314
|
Chris@43
|
315 m_outputs.push_back(port);
|
Chris@43
|
316 }
|
Chris@43
|
317
|
Chris@43
|
318 while (m_outputs.size() > channels) {
|
Chris@43
|
319 std::vector<jack_port_t *>::iterator itr = m_outputs.end();
|
Chris@43
|
320 --itr;
|
Chris@43
|
321 jack_port_t *port = *itr;
|
Chris@43
|
322 if (port) jack_port_unregister(m_client, port);
|
Chris@43
|
323 m_outputs.erase(itr);
|
Chris@43
|
324 }
|
Chris@43
|
325
|
Chris@43
|
326 m_mutex.unlock();
|
Chris@43
|
327 }
|
Chris@43
|
328
|
Chris@43
|
329 int
|
Chris@43
|
330 AudioJACKTarget::process(jack_nframes_t nframes)
|
Chris@43
|
331 {
|
Chris@43
|
332 if (!m_mutex.tryLock()) {
|
Chris@43
|
333 return 0;
|
Chris@43
|
334 }
|
Chris@43
|
335
|
Chris@43
|
336 if (m_outputs.empty()) {
|
Chris@43
|
337 m_mutex.unlock();
|
Chris@43
|
338 return 0;
|
Chris@43
|
339 }
|
Chris@43
|
340
|
Chris@43
|
341 #ifdef DEBUG_AUDIO_JACK_TARGET
|
Chris@43
|
342 std::cout << "AudioJACKTarget::process(" << nframes << "): have a source" << std::endl;
|
Chris@43
|
343 #endif
|
Chris@43
|
344
|
Chris@43
|
345 #ifdef DEBUG_AUDIO_JACK_TARGET
|
Chris@43
|
346 if (m_bufferSize != nframes) {
|
Chris@43
|
347 std::cerr << "WARNING: m_bufferSize != nframes (" << m_bufferSize << " != " << nframes << ")" << std::endl;
|
Chris@43
|
348 }
|
Chris@43
|
349 #endif
|
Chris@43
|
350
|
Chris@43
|
351 float **buffers = (float **)alloca(m_outputs.size() * sizeof(float *));
|
Chris@43
|
352
|
Chris@43
|
353 for (size_t ch = 0; ch < m_outputs.size(); ++ch) {
|
Chris@43
|
354 buffers[ch] = (float *)jack_port_get_buffer(m_outputs[ch], nframes);
|
Chris@43
|
355 }
|
Chris@43
|
356
|
Chris@43
|
357 size_t received = 0;
|
Chris@43
|
358
|
Chris@43
|
359 if (m_source) {
|
Chris@43
|
360 received = m_source->getSourceSamples(nframes, buffers);
|
Chris@43
|
361 }
|
Chris@43
|
362
|
Chris@43
|
363 for (size_t ch = 0; ch < m_outputs.size(); ++ch) {
|
Chris@43
|
364 for (size_t i = received; i < nframes; ++i) {
|
Chris@43
|
365 buffers[ch][i] = 0.0;
|
Chris@43
|
366 }
|
Chris@43
|
367 }
|
Chris@43
|
368
|
Chris@43
|
369 float peakLeft = 0.0, peakRight = 0.0;
|
Chris@43
|
370
|
Chris@43
|
371 for (size_t ch = 0; ch < m_outputs.size(); ++ch) {
|
Chris@43
|
372
|
Chris@43
|
373 float peak = 0.0;
|
Chris@43
|
374
|
Chris@43
|
375 for (size_t i = 0; i < nframes; ++i) {
|
Chris@43
|
376 buffers[ch][i] *= m_outputGain;
|
Chris@43
|
377 float sample = fabsf(buffers[ch][i]);
|
Chris@43
|
378 if (sample > peak) peak = sample;
|
Chris@43
|
379 }
|
Chris@43
|
380
|
Chris@43
|
381 if (ch == 0) peakLeft = peak;
|
Chris@43
|
382 if (ch > 0 || m_outputs.size() == 1) peakRight = peak;
|
Chris@43
|
383 }
|
Chris@43
|
384
|
Chris@43
|
385 if (m_source) {
|
Chris@43
|
386 m_source->setOutputLevels(peakLeft, peakRight);
|
Chris@43
|
387 }
|
Chris@43
|
388
|
Chris@43
|
389 m_mutex.unlock();
|
Chris@43
|
390 return 0;
|
Chris@43
|
391 }
|
Chris@43
|
392
|
Chris@43
|
393 int
|
Chris@43
|
394 AudioJACKTarget::xrun()
|
Chris@43
|
395 {
|
Chris@43
|
396 std::cerr << "AudioJACKTarget: xrun!" << std::endl;
|
Chris@43
|
397 if (m_source) m_source->audioProcessingOverload();
|
Chris@43
|
398 return 0;
|
Chris@43
|
399 }
|
Chris@43
|
400
|
Chris@43
|
401 #endif /* HAVE_JACK */
|
Chris@43
|
402
|