/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ /* Rosegarden A MIDI and audio sequencer and musical notation editor. This program is Copyright 2000-2008 Guillaume Laurent , Chris Cannam , Richard Bown The moral rights of Guillaume Laurent, Chris Cannam, and Richard Bown to claim authorship of this work have been asserted. Other copyrights also apply to some parts of this work. Please see the AUTHORS file and individual file headers for details. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. See the file COPYING included with this distribution for more information. */ #ifdef HAVE_LIBLO #include #include #include "AudioPluginOSCGUIManager.h" #include "sound/Midi.h" #include #include "misc/Debug.h" #include "misc/Strings.h" #include "AudioPluginOSCGUI.h" #include "base/AudioPluginInstance.h" #include "base/Exception.h" #include "base/Instrument.h" #include "base/MidiProgram.h" #include "base/RealTime.h" #include "base/Studio.h" #include "gui/application/RosegardenGUIApp.h" #include "OSCMessage.h" #include "sound/MappedEvent.h" #include "sound/PluginIdentifier.h" #include "StudioControl.h" #include "TimerCallbackAssistant.h" #include namespace Rosegarden { static void osc_error(int num, const char *msg, const char *path) { std::cerr << "Rosegarden: ERROR: liblo server error " << num << " in path " << path << ": " << msg << std::endl; } static int osc_message_handler(const char *path, const char *types, lo_arg **argv, int argc, lo_message, void *user_data) { AudioPluginOSCGUIManager *manager = (AudioPluginOSCGUIManager *)user_data; InstrumentId instrument; int position; TQString method; if (!manager->parseOSCPath(path, instrument, position, method)) { return 1; } OSCMessage *message = new OSCMessage(); message->setTarget(instrument); message->setTargetData(position); message->setMethod(qstrtostr(method)); int arg = 0; while (types && arg < argc && types[arg]) { message->addArg(types[arg], argv[arg]); ++arg; } manager->postMessage(message); return 0; } AudioPluginOSCGUIManager::AudioPluginOSCGUIManager(RosegardenGUIApp *app) : m_app(app), m_studio(0), m_haveOSCThread(false), m_oscBuffer(1023), m_dispatchTimer(0) {} AudioPluginOSCGUIManager::~AudioPluginOSCGUIManager() { delete m_dispatchTimer; for (TargetGUIMap::iterator i = m_guis.begin(); i != m_guis.end(); ++i) { for (IntGUIMap::iterator j = i->second.begin(); j != i->second.end(); ++j) { delete j->second; } } m_guis.clear(); #ifdef HAVE_LIBLO_THREADSTOP if (m_haveOSCThread) lo_server_thread_stop(m_serverThread); #endif } void AudioPluginOSCGUIManager::checkOSCThread() { if (m_haveOSCThread) return ; m_serverThread = lo_server_thread_new(NULL, osc_error); lo_server_thread_add_method(m_serverThread, NULL, NULL, osc_message_handler, this); lo_server_thread_start(m_serverThread); RG_DEBUG << "AudioPluginOSCGUIManager: Base OSC URL is " << lo_server_thread_get_url(m_serverThread) << endl; m_dispatchTimer = new TimerCallbackAssistant(20, timerCallback, this); m_haveOSCThread = true; } bool AudioPluginOSCGUIManager::hasGUI(InstrumentId instrument, int position) { PluginContainer *container = 0; container = m_studio->getContainerById(instrument); if (!container) return false; AudioPluginInstance *pluginInstance = container->getPlugin(position); if (!pluginInstance) return false; try { TQString filePath = AudioPluginOSCGUI::getGUIFilePath (strtoqstr(pluginInstance->getIdentifier())); return (!filePath.isNull() && filePath != ""); } catch (Exception e) { // that's OK return false; } } void AudioPluginOSCGUIManager::startGUI(InstrumentId instrument, int position) { RG_DEBUG << "AudioPluginOSCGUIManager::startGUI: " << instrument << "," << position << endl; checkOSCThread(); if (m_guis.find(instrument) != m_guis.end() && m_guis[instrument].find(position) != m_guis[instrument].end()) { RG_DEBUG << "stopping GUI first" << endl; stopGUI(instrument, position); } // check the label PluginContainer *container = 0; container = m_studio->getContainerById(instrument); if (!container) { RG_DEBUG << "AudioPluginOSCGUIManager::startGUI: no such instrument or buss as " << instrument << endl; return; } AudioPluginInstance *pluginInstance = container->getPlugin(position); if (!pluginInstance) { RG_DEBUG << "AudioPluginOSCGUIManager::startGUI: no plugin at position " << position << " for instrument " << instrument << endl; return ; } try { AudioPluginOSCGUI *gui = new AudioPluginOSCGUI(pluginInstance, getOSCUrl(instrument, position, strtoqstr(pluginInstance->getIdentifier())), getFriendlyName(instrument, position, strtoqstr(pluginInstance->getIdentifier()))); m_guis[instrument][position] = gui; } catch (Exception e) { RG_DEBUG << "AudioPluginOSCGUIManager::startGUI: failed to start GUI: " << e.getMessage() << endl; } } void AudioPluginOSCGUIManager::showGUI(InstrumentId instrument, int position) { RG_DEBUG << "AudioPluginOSCGUIManager::showGUI: " << instrument << "," << position << endl; if (m_guis.find(instrument) != m_guis.end() && m_guis[instrument].find(position) != m_guis[instrument].end()) { m_guis[instrument][position]->show(); } else { startGUI(instrument, position); } } void AudioPluginOSCGUIManager::stopGUI(InstrumentId instrument, int position) { if (m_guis.find(instrument) != m_guis.end() && m_guis[instrument].find(position) != m_guis[instrument].end()) { delete m_guis[instrument][position]; m_guis[instrument].erase(position); if (m_guis[instrument].empty()) m_guis.erase(instrument); } } void AudioPluginOSCGUIManager::stopAllGUIs() { while (!m_guis.empty()) { while (!m_guis.begin()->second.empty()) { delete (m_guis.begin()->second.begin()->second); m_guis.begin()->second.erase(m_guis.begin()->second.begin()); } m_guis.erase(m_guis.begin()); } } void AudioPluginOSCGUIManager::postMessage(OSCMessage *message) { RG_DEBUG << "AudioPluginOSCGUIManager::postMessage" << endl; m_oscBuffer.write(&message, 1); } void AudioPluginOSCGUIManager::updateProgram(InstrumentId instrument, int position) { RG_DEBUG << "AudioPluginOSCGUIManager::updateProgram(" << instrument << "," << position << ")" << endl; if (m_guis.find(instrument) == m_guis.end() || m_guis[instrument].find(position) == m_guis[instrument].end()) return ; PluginContainer *container = 0; container = m_studio->getContainerById(instrument); if (!container) return; AudioPluginInstance *pluginInstance = container->getPlugin(position); if (!pluginInstance) return; unsigned long rv = StudioControl::getPluginProgram (pluginInstance->getMappedId(), strtoqstr(pluginInstance->getProgram())); int bank = rv >> 16; int program = rv - (bank << 16); RG_DEBUG << "AudioPluginOSCGUIManager::updateProgram(" << instrument << "," << position << "): rv " << rv << ", bank " << bank << ", program " << program << endl; m_guis[instrument][position]->sendProgram(bank, program); } void AudioPluginOSCGUIManager::updatePort(InstrumentId instrument, int position, int port) { RG_DEBUG << "AudioPluginOSCGUIManager::updatePort(" << instrument << "," << position << "," << port << ")" << endl; if (m_guis.find(instrument) == m_guis.end() || m_guis[instrument].find(position) == m_guis[instrument].end()) return ; PluginContainer *container = 0; container = m_studio->getContainerById(instrument); if (!container) return; AudioPluginInstance *pluginInstance = container->getPlugin(position); if (!pluginInstance) return ; PluginPortInstance *porti = pluginInstance->getPort(port); if (!porti) return ; RG_DEBUG << "AudioPluginOSCGUIManager::updatePort(" << instrument << "," << position << "," << port << "): value " << porti->value << endl; m_guis[instrument][position]->sendPortValue(port, porti->value); } void AudioPluginOSCGUIManager::updateConfiguration(InstrumentId instrument, int position, TQString key) { RG_DEBUG << "AudioPluginOSCGUIManager::updateConfiguration(" << instrument << "," << position << "," << key << ")" << endl; if (m_guis.find(instrument) == m_guis.end() || m_guis[instrument].find(position) == m_guis[instrument].end()) return ; PluginContainer *container = m_studio->getContainerById(instrument); if (!container) return; AudioPluginInstance *pluginInstance = container->getPlugin(position); if (!pluginInstance) return; TQString value = strtoqstr(pluginInstance->getConfigurationValue(qstrtostr(key))); RG_DEBUG << "AudioPluginOSCGUIManager::updatePort(" << instrument << "," << position << "," << key << "): value " << value << endl; m_guis[instrument][position]->sendConfiguration(key, value); } TQString AudioPluginOSCGUIManager::getOSCUrl(InstrumentId instrument, int position, TQString identifier) { // OSC URL will be of the form // osc.udp://localhost:54343/plugin/dssi///