Chris@0: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@0: Chris@0: /* Chris@0: Sonic Visualiser Chris@0: An audio file viewer and annotation editor. Chris@0: Centre for Digital Music, Queen Mary, University of London. Chris@0: This file copyright 2006 Chris Cannam. Chris@0: Chris@0: This program is free software; you can redistribute it and/or Chris@0: modify it under the terms of the GNU General Public License as Chris@0: published by the Free Software Foundation; either version 2 of the Chris@0: License, or (at your option) any later version. See the file Chris@0: COPYING included with this distribution for more information. Chris@0: */ Chris@0: Chris@0: #ifdef HAVE_JACK Chris@0: Chris@0: #include "AudioJACKTarget.h" Chris@0: #include "AudioCallbackPlaySource.h" Chris@0: Chris@0: #include Chris@0: #include Chris@0: Chris@0: //#define DEBUG_AUDIO_JACK_TARGET 1 Chris@0: Chris@2: #ifdef BUILD_STATIC Chris@2: #ifdef Q_OS_LINUX Chris@2: Chris@2: // Some lunacy to enable JACK support in static builds. JACK isn't Chris@2: // supposed to be linked statically, because it depends on a Chris@2: // consistent shared memory layout between client library and daemon, Chris@2: // so it's very fragile in the face of version mismatches. Chris@2: // Chris@2: // Therefore for static builds on Linux we avoid linking against JACK Chris@2: // at all during the build, instead using dlopen and runtime symbol Chris@2: // lookup to switch on JACK support at runtime. The following big Chris@2: // mess (down to the #endifs) is the code that implements this. Chris@2: Chris@2: static void *symbol(const char *name) Chris@2: { Chris@2: static bool attempted = false; Chris@2: static void *library = 0; Chris@2: static std::map symbols; Chris@2: if (symbols.find(name) != symbols.end()) return symbols[name]; Chris@2: if (!library) { Chris@2: if (!attempted) { Chris@2: library = ::dlopen("libjack.so.1", RTLD_NOW); Chris@2: if (!library) library = ::dlopen("libjack.so.0", RTLD_NOW); Chris@2: if (!library) library = ::dlopen("libjack.so", RTLD_NOW); Chris@2: if (!library) { Chris@2: std::cerr << "WARNING: AudioJACKTarget: Failed to load JACK library: " Chris@2: << ::dlerror() << " (tried .so, .so.0, .so.1)" Chris@2: << std::endl; Chris@2: } Chris@2: attempted = true; Chris@2: } Chris@2: if (!library) return 0; Chris@2: } Chris@2: void *symbol = ::dlsym(library, name); Chris@2: if (!symbol) { Chris@2: std::cerr << "WARNING: AudioJACKTarget: Failed to locate symbol " Chris@2: << name << ": " << ::dlerror() << std::endl; Chris@2: } Chris@2: symbols[name] = symbol; Chris@2: return symbol; Chris@2: } Chris@2: Chris@2: static int dynamic_jack_set_process_callback(jack_client_t *client, Chris@2: JackProcessCallback process_callback, Chris@2: void *arg) Chris@2: { Chris@2: typedef int (*func)(jack_client_t *client, Chris@2: JackProcessCallback process_callback, Chris@2: void *arg); Chris@2: void *s = symbol("jack_set_process_callback"); Chris@2: if (!s) return 1; Chris@2: func f = (func)s; Chris@2: return f(client, process_callback, arg); Chris@2: } Chris@2: Chris@2: static const char **dynamic_jack_get_ports(jack_client_t *client, Chris@2: const char *port_name_pattern, Chris@2: const char *type_name_pattern, Chris@2: unsigned long flags) Chris@2: { Chris@2: typedef const char **(*func)(jack_client_t *client, Chris@2: const char *port_name_pattern, Chris@2: const char *type_name_pattern, Chris@2: unsigned long flags); Chris@2: void *s = symbol("jack_get_ports"); Chris@2: if (!s) return 0; Chris@2: func f = (func)s; Chris@2: return f(client, port_name_pattern, type_name_pattern, flags); Chris@2: } Chris@2: Chris@2: static jack_port_t *dynamic_jack_port_register(jack_client_t *client, Chris@2: const char *port_name, Chris@2: const char *port_type, Chris@2: unsigned long flags, Chris@2: unsigned long buffer_size) Chris@2: { Chris@2: typedef jack_port_t *(*func)(jack_client_t *client, Chris@2: const char *port_name, Chris@2: const char *port_type, Chris@2: unsigned long flags, Chris@2: unsigned long buffer_size); Chris@2: void *s = symbol("jack_port_register"); Chris@2: if (!s) return 0; Chris@2: func f = (func)s; Chris@2: return f(client, port_name, port_type, flags, buffer_size); Chris@2: } Chris@2: Chris@2: static int dynamic_jack_connect(jack_client_t *client, Chris@2: const char *source, Chris@2: const char *dest) Chris@2: { Chris@2: typedef int (*func)(jack_client_t *client, Chris@2: const char *source, Chris@2: const char *dest); Chris@2: void *s = symbol("jack_connect"); Chris@2: if (!s) return 1; Chris@2: func f = (func)s; Chris@2: return f(client, source, dest); Chris@2: } Chris@2: Chris@2: static void *dynamic_jack_port_get_buffer(jack_port_t *port, Chris@2: jack_nframes_t sz) Chris@2: { Chris@2: typedef void *(*func)(jack_port_t *, jack_nframes_t); Chris@2: void *s = symbol("jack_port_get_buffer"); Chris@2: if (!s) return 0; Chris@2: func f = (func)s; Chris@2: return f(port, sz); Chris@2: } Chris@2: Chris@2: static int dynamic_jack_port_unregister(jack_client_t *client, Chris@2: jack_port_t *port) Chris@2: { Chris@2: typedef int(*func)(jack_client_t *, jack_port_t *); Chris@2: void *s = symbol("jack_port_unregister"); Chris@2: if (!s) return 0; Chris@2: func f = (func)s; Chris@2: return f(client, port); Chris@2: } Chris@2: Chris@2: #define dynamic1(rv, name, argtype, failval) \ Chris@2: static rv dynamic_##name(argtype arg) { \ Chris@2: typedef rv (*func) (argtype); \ Chris@2: void *s = symbol(#name); \ Chris@2: if (!s) return failval; \ Chris@2: func f = (func) s; \ Chris@2: return f(arg); \ Chris@2: } Chris@2: Chris@2: dynamic1(jack_client_t *, jack_client_new, const char *, 0); Chris@2: dynamic1(jack_nframes_t, jack_get_buffer_size, jack_client_t *, 0); Chris@2: dynamic1(jack_nframes_t, jack_get_sample_rate, jack_client_t *, 0); Chris@2: dynamic1(int, jack_activate, jack_client_t *, 1); Chris@2: dynamic1(int, jack_deactivate, jack_client_t *, 1); Chris@2: dynamic1(int, jack_client_close, jack_client_t *, 1); Chris@2: dynamic1(jack_nframes_t, jack_port_get_latency, jack_port_t *, 0); Chris@2: dynamic1(const char *, jack_port_name, const jack_port_t *, 0); Chris@2: Chris@2: #define jack_client_new dynamic_jack_client_new Chris@2: #define jack_get_buffer_size dynamic_jack_get_buffer_size Chris@2: #define jack_get_sample_rate dynamic_jack_get_sample_rate Chris@2: #define jack_set_process_callback dynamic_jack_set_process_callback Chris@2: #define jack_activate dynamic_jack_activate Chris@2: #define jack_deactivate dynamic_jack_deactivate Chris@2: #define jack_client_close dynamic_jack_client_close Chris@2: #define jack_get_ports dynamic_jack_get_ports Chris@2: #define jack_port_register dynamic_jack_port_register Chris@2: #define jack_port_unregister dynamic_jack_port_unregister Chris@2: #define jack_port_get_latency dynamic_jack_port_get_latency Chris@2: #define jack_port_name dynamic_jack_port_name Chris@2: #define jack_connect dynamic_jack_connect Chris@2: #define jack_port_get_buffer dynamic_jack_port_get_buffer Chris@2: Chris@2: #endif Chris@2: #endif Chris@2: Chris@0: AudioJACKTarget::AudioJACKTarget(AudioCallbackPlaySource *source) : Chris@0: AudioCallbackPlayTarget(source), Chris@0: m_client(0), Chris@0: m_bufferSize(0), Chris@0: m_sampleRate(0) Chris@0: { Chris@0: char name[20]; Chris@0: strcpy(name, "Sonic Visualiser"); Chris@0: m_client = jack_client_new(name); Chris@0: Chris@0: if (!m_client) { Chris@0: sprintf(name, "Sonic Visualiser (%d)", (int)getpid()); Chris@0: m_client = jack_client_new(name); Chris@0: if (!m_client) { Chris@0: std::cerr Chris@0: << "ERROR: AudioJACKTarget: Failed to connect to JACK server" Chris@0: << std::endl; Chris@0: } Chris@0: } Chris@0: Chris@0: if (!m_client) return; Chris@0: Chris@0: m_bufferSize = jack_get_buffer_size(m_client); Chris@0: m_sampleRate = jack_get_sample_rate(m_client); Chris@0: Chris@0: jack_set_process_callback(m_client, processStatic, this); Chris@0: Chris@0: if (jack_activate(m_client)) { Chris@0: std::cerr << "ERROR: AudioJACKTarget: Failed to activate JACK client" Chris@0: << std::endl; Chris@0: } Chris@0: Chris@0: if (m_source) { Chris@0: sourceModelReplaced(); Chris@0: } Chris@0: } Chris@0: Chris@0: AudioJACKTarget::~AudioJACKTarget() Chris@0: { Chris@0: if (m_client) { Chris@0: jack_deactivate(m_client); Chris@0: jack_client_close(m_client); Chris@0: } Chris@0: } Chris@0: Chris@0: bool Chris@0: AudioJACKTarget::isOK() const Chris@0: { Chris@0: return (m_client != 0); Chris@0: } Chris@0: Chris@0: int Chris@0: AudioJACKTarget::processStatic(jack_nframes_t nframes, void *arg) Chris@0: { Chris@0: return ((AudioJACKTarget *)arg)->process(nframes); Chris@0: } Chris@0: Chris@0: void Chris@0: AudioJACKTarget::sourceModelReplaced() Chris@0: { Chris@0: m_mutex.lock(); Chris@0: Chris@0: m_source->setTargetBlockSize(m_bufferSize); Chris@0: m_source->setTargetSampleRate(m_sampleRate); Chris@0: Chris@0: size_t channels = m_source->getSourceChannelCount(); Chris@0: Chris@0: // Because we offer pan, we always want at least 2 channels Chris@0: if (channels < 2) channels = 2; Chris@0: Chris@0: if (channels == m_outputs.size() || !m_client) { Chris@0: m_mutex.unlock(); Chris@0: return; Chris@0: } Chris@0: Chris@0: const char **ports = Chris@0: jack_get_ports(m_client, NULL, NULL, Chris@0: JackPortIsPhysical | JackPortIsInput); Chris@0: size_t physicalPortCount = 0; Chris@0: while (ports[physicalPortCount]) ++physicalPortCount; Chris@0: Chris@0: #ifdef DEBUG_AUDIO_JACK_TARGET Chris@0: std::cerr << "AudioJACKTarget::sourceModelReplaced: have " << channels << " channels and " << physicalPortCount << " physical ports" << std::endl; Chris@0: #endif Chris@0: Chris@0: while (m_outputs.size() < channels) { Chris@0: Chris@0: char name[20]; Chris@0: jack_port_t *port; Chris@0: Chris@0: sprintf(name, "out %d", m_outputs.size() + 1); Chris@0: Chris@0: port = jack_port_register(m_client, Chris@0: name, Chris@0: JACK_DEFAULT_AUDIO_TYPE, Chris@0: JackPortIsOutput, Chris@0: 0); Chris@0: Chris@0: if (!port) { Chris@0: std::cerr Chris@0: << "ERROR: AudioJACKTarget: Failed to create JACK output port " Chris@0: << m_outputs.size() << std::endl; Chris@0: } else { Chris@0: m_source->setTargetPlayLatency(jack_port_get_latency(port)); Chris@0: } Chris@0: Chris@0: if (m_outputs.size() < physicalPortCount) { Chris@0: jack_connect(m_client, jack_port_name(port), ports[m_outputs.size()]); Chris@0: } Chris@0: Chris@0: m_outputs.push_back(port); Chris@0: } Chris@0: Chris@0: while (m_outputs.size() > channels) { Chris@0: std::vector::iterator itr = m_outputs.end(); Chris@0: --itr; Chris@0: jack_port_t *port = *itr; Chris@0: if (port) jack_port_unregister(m_client, port); Chris@0: m_outputs.erase(itr); Chris@0: } Chris@0: Chris@0: m_mutex.unlock(); Chris@0: } Chris@0: Chris@0: int Chris@0: AudioJACKTarget::process(jack_nframes_t nframes) Chris@0: { Chris@0: if (!m_mutex.tryLock()) { Chris@0: return 0; Chris@0: } Chris@0: Chris@0: if (m_outputs.empty()) { Chris@0: m_mutex.unlock(); Chris@0: return 0; Chris@0: } Chris@0: Chris@0: #ifdef DEBUG_AUDIO_JACK_TARGET Chris@0: std::cout << "AudioJACKTarget::process(" << nframes << "): have a source" << std::endl; Chris@0: #endif Chris@0: Chris@0: #ifdef DEBUG_AUDIO_JACK_TARGET Chris@0: if (m_bufferSize != nframes) { Chris@0: std::cerr << "WARNING: m_bufferSize != nframes (" << m_bufferSize << " != " << nframes << ")" << std::endl; Chris@0: } Chris@0: #endif Chris@0: Chris@0: float **buffers = (float **)alloca(m_outputs.size() * sizeof(float *)); Chris@0: Chris@0: for (size_t ch = 0; ch < m_outputs.size(); ++ch) { Chris@0: buffers[ch] = (float *)jack_port_get_buffer(m_outputs[ch], nframes); Chris@0: } Chris@0: Chris@0: if (m_source) { Chris@0: m_source->getSourceSamples(nframes, buffers); Chris@0: } else { Chris@0: for (size_t ch = 0; ch < m_outputs.size(); ++ch) { Chris@0: for (size_t i = 0; i < nframes; ++i) { Chris@0: buffers[ch][i] = 0.0; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: float peakLeft = 0.0, peakRight = 0.0; Chris@0: Chris@0: for (size_t ch = 0; ch < m_outputs.size(); ++ch) { Chris@0: Chris@0: float peak = 0.0; Chris@0: Chris@0: for (size_t i = 0; i < nframes; ++i) { Chris@0: buffers[ch][i] *= m_outputGain; Chris@0: float sample = fabsf(buffers[ch][i]); Chris@0: if (sample > peak) peak = sample; Chris@0: } Chris@0: Chris@0: if (ch == 0) peakLeft = peak; Chris@0: if (ch > 0 || m_outputs.size() == 1) peakRight = peak; Chris@0: } Chris@0: Chris@0: if (m_source) { Chris@0: m_source->setOutputLevels(peakLeft, peakRight); Chris@0: } Chris@0: Chris@0: m_mutex.unlock(); Chris@0: return 0; Chris@0: } Chris@0: Chris@0: Chris@0: #ifdef INCLUDE_MOCFILES Chris@0: #include "AudioJACKTarget.moc.cpp" Chris@0: #endif Chris@0: Chris@0: #endif /* HAVE_JACK */ Chris@0: