annotate plugin/LADSPAPluginInstance.cpp @ 52:d397ea0a79f5

* Update licensing rubric for GPL
author Chris Cannam
date Mon, 20 Mar 2006 15:10:07 +0000
parents c2913298cf94
children 7439f1696314
rev   line source
Chris@49 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@52 4 Sonic Visualiser
Chris@52 5 An audio file viewer and annotation editor.
Chris@52 6 Centre for Digital Music, Queen Mary, University of London.
Chris@0 7
Chris@52 8 This program is free software; you can redistribute it and/or
Chris@52 9 modify it under the terms of the GNU General Public License as
Chris@52 10 published by the Free Software Foundation; either version 2 of the
Chris@52 11 License, or (at your option) any later version. See the file
Chris@52 12 COPYING included with this distribution for more information.
Chris@0 13 */
Chris@0 14
Chris@0 15 /*
Chris@0 16 This is a modified version of a source file from the
Chris@0 17 Rosegarden MIDI and audio sequencer and notation editor.
Chris@17 18 This file copyright 2000-2006 Chris Cannam and Richard Bown.
Chris@0 19 */
Chris@0 20
Chris@0 21 #include <iostream>
Chris@0 22 #include <cassert>
Chris@0 23
Chris@0 24 #include "LADSPAPluginInstance.h"
Chris@0 25 #include "LADSPAPluginFactory.h"
Chris@0 26
Chris@0 27 //#define DEBUG_LADSPA 1
Chris@0 28
Chris@0 29
Chris@0 30 LADSPAPluginInstance::LADSPAPluginInstance(RealTimePluginFactory *factory,
Chris@0 31 int clientId,
Chris@0 32 QString identifier,
Chris@0 33 int position,
Chris@0 34 unsigned long sampleRate,
Chris@0 35 size_t blockSize,
Chris@0 36 int idealChannelCount,
Chris@0 37 const LADSPA_Descriptor* descriptor) :
Chris@0 38 RealTimePluginInstance(factory, identifier),
Chris@0 39 m_client(clientId),
Chris@0 40 m_position(position),
Chris@0 41 m_instanceCount(0),
Chris@0 42 m_descriptor(descriptor),
Chris@0 43 m_blockSize(blockSize),
Chris@0 44 m_sampleRate(sampleRate),
Chris@0 45 m_latencyPort(0),
Chris@0 46 m_run(false),
Chris@0 47 m_bypassed(false)
Chris@0 48 {
Chris@0 49 init(idealChannelCount);
Chris@0 50
Chris@0 51 m_inputBuffers = new sample_t*[m_instanceCount * m_audioPortsIn.size()];
Chris@0 52 m_outputBuffers = new sample_t*[m_instanceCount * m_audioPortsOut.size()];
Chris@0 53
Chris@0 54 for (size_t i = 0; i < m_instanceCount * m_audioPortsIn.size(); ++i) {
Chris@0 55 m_inputBuffers[i] = new sample_t[blockSize];
Chris@0 56 }
Chris@0 57 for (size_t i = 0; i < m_instanceCount * m_audioPortsOut.size(); ++i) {
Chris@0 58 m_outputBuffers[i] = new sample_t[blockSize];
Chris@0 59 }
Chris@0 60
Chris@0 61 m_ownBuffers = true;
Chris@0 62
Chris@0 63 instantiate(sampleRate);
Chris@0 64 if (isOK()) {
Chris@0 65 connectPorts();
Chris@0 66 activate();
Chris@0 67 }
Chris@0 68 }
Chris@0 69
Chris@51 70 std::string
Chris@51 71 LADSPAPluginInstance::getName() const
Chris@0 72 {
Chris@51 73 return m_descriptor->Label;
Chris@0 74 }
Chris@0 75
Chris@51 76 std::string
Chris@51 77 LADSPAPluginInstance::getDescription() const
Chris@51 78 {
Chris@51 79 return m_descriptor->Name;
Chris@51 80 }
Chris@51 81
Chris@51 82 std::string
Chris@51 83 LADSPAPluginInstance::getMaker() const
Chris@51 84 {
Chris@51 85 return m_descriptor->Maker;
Chris@51 86 }
Chris@51 87
Chris@51 88 int
Chris@51 89 LADSPAPluginInstance::getPluginVersion() const
Chris@51 90 {
Chris@51 91 return 1;
Chris@51 92 }
Chris@51 93
Chris@51 94 std::string
Chris@51 95 LADSPAPluginInstance::getCopyright() const
Chris@51 96 {
Chris@51 97 return m_descriptor->Copyright;
Chris@51 98 }
Chris@51 99
Chris@51 100 LADSPAPluginInstance::ParameterList
Chris@51 101 LADSPAPluginInstance::getParameterDescriptors() const
Chris@51 102 {
Chris@51 103 ParameterList list;
Chris@51 104 LADSPAPluginFactory *f = dynamic_cast<LADSPAPluginFactory *>(m_factory);
Chris@51 105
Chris@51 106 for (unsigned int i = 0; i < m_controlPortsIn.size(); ++i) {
Chris@51 107
Chris@51 108 ParameterDescriptor pd;
Chris@51 109 unsigned int pn = m_controlPortsIn[i].first;
Chris@51 110
Chris@51 111 pd.name = m_descriptor->PortNames[pn];
Chris@51 112 pd.description = pd.name;
Chris@51 113 pd.minValue = f->getPortMinimum(m_descriptor, pn);
Chris@51 114 pd.maxValue = f->getPortMaximum(m_descriptor, pn);
Chris@51 115 pd.defaultValue = f->getPortDefault(m_descriptor, pn);
Chris@51 116 pd.isQuantized = false;
Chris@51 117
Chris@51 118 list.push_back(pd);
Chris@51 119 }
Chris@51 120
Chris@51 121 return list;
Chris@51 122 }
Chris@51 123
Chris@51 124 float
Chris@51 125 LADSPAPluginInstance::getParameter(std::string name) const
Chris@51 126 {
Chris@51 127 for (unsigned int i = 0; i < m_controlPortsIn.size(); ++i) {
Chris@51 128 if (name == m_descriptor->PortNames[m_controlPortsIn[i].first]) {
Chris@51 129 return getParameterValue(i);
Chris@51 130 }
Chris@51 131 }
Chris@51 132
Chris@51 133 return 0.0;
Chris@51 134 }
Chris@51 135
Chris@51 136 void
Chris@51 137 LADSPAPluginInstance::setParameter(std::string name, float value)
Chris@51 138 {
Chris@51 139 for (unsigned int i = 0; i < m_controlPortsIn.size(); ++i) {
Chris@51 140 if (name == m_descriptor->PortNames[m_controlPortsIn[i].first]) {
Chris@51 141 setParameterValue(i, value);
Chris@51 142 break;
Chris@51 143 }
Chris@51 144 }
Chris@51 145 }
Chris@0 146
Chris@0 147 void
Chris@0 148 LADSPAPluginInstance::init(int idealChannelCount)
Chris@0 149 {
Chris@0 150 #ifdef DEBUG_LADSPA
Chris@0 151 std::cerr << "LADSPAPluginInstance::init(" << idealChannelCount << "): plugin has "
Chris@0 152 << m_descriptor->PortCount << " ports" << std::endl;
Chris@0 153 #endif
Chris@0 154
Chris@0 155 // Discover ports numbers and identities
Chris@0 156 //
Chris@0 157 for (unsigned long i = 0; i < m_descriptor->PortCount; ++i)
Chris@0 158 {
Chris@0 159 if (LADSPA_IS_PORT_AUDIO(m_descriptor->PortDescriptors[i]))
Chris@0 160 {
Chris@0 161 if (LADSPA_IS_PORT_INPUT(m_descriptor->PortDescriptors[i])) {
Chris@0 162 #ifdef DEBUG_LADSPA
Chris@0 163 std::cerr << "LADSPAPluginInstance::init: port " << i << " is audio in" << std::endl;
Chris@0 164 #endif
Chris@0 165 m_audioPortsIn.push_back(i);
Chris@0 166 } else {
Chris@0 167 #ifdef DEBUG_LADSPA
Chris@0 168 std::cerr << "LADSPAPluginInstance::init: port " << i << " is audio out" << std::endl;
Chris@0 169 #endif
Chris@0 170 m_audioPortsOut.push_back(i);
Chris@0 171 }
Chris@0 172 }
Chris@0 173 else
Chris@0 174 if (LADSPA_IS_PORT_CONTROL(m_descriptor->PortDescriptors[i]))
Chris@0 175 {
Chris@0 176 if (LADSPA_IS_PORT_INPUT(m_descriptor->PortDescriptors[i])) {
Chris@0 177 #ifdef DEBUG_LADSPA
Chris@0 178 std::cerr << "LADSPAPluginInstance::init: port " << i << " is control in" << std::endl;
Chris@0 179 #endif
Chris@0 180 LADSPA_Data *data = new LADSPA_Data(0.0);
Chris@0 181 m_controlPortsIn.push_back(
Chris@0 182 std::pair<unsigned long, LADSPA_Data*>(i, data));
Chris@0 183 } else {
Chris@0 184 #ifdef DEBUG_LADSPA
Chris@0 185 std::cerr << "LADSPAPluginInstance::init: port " << i << " is control out" << std::endl;
Chris@0 186 #endif
Chris@0 187 LADSPA_Data *data = new LADSPA_Data(0.0);
Chris@0 188 m_controlPortsOut.push_back(
Chris@0 189 std::pair<unsigned long, LADSPA_Data*>(i, data));
Chris@0 190 if (!strcmp(m_descriptor->PortNames[i], "latency") ||
Chris@0 191 !strcmp(m_descriptor->PortNames[i], "_latency")) {
Chris@0 192 #ifdef DEBUG_LADSPA
Chris@0 193 std::cerr << "Wooo! We have a latency port!" << std::endl;
Chris@0 194 #endif
Chris@0 195 m_latencyPort = data;
Chris@0 196 }
Chris@0 197 }
Chris@0 198 }
Chris@0 199 #ifdef DEBUG_LADSPA
Chris@0 200 else
Chris@0 201 std::cerr << "LADSPAPluginInstance::init - "
Chris@0 202 << "unrecognised port type" << std::endl;
Chris@0 203 #endif
Chris@0 204 }
Chris@0 205
Chris@0 206 m_instanceCount = 1;
Chris@0 207
Chris@0 208 if (idealChannelCount > 0) {
Chris@0 209 if (m_audioPortsIn.size() == 1) {
Chris@0 210 // mono plugin: duplicate it if need be
Chris@0 211 m_instanceCount = idealChannelCount;
Chris@0 212 }
Chris@0 213 }
Chris@0 214 }
Chris@0 215
Chris@0 216 size_t
Chris@0 217 LADSPAPluginInstance::getLatency()
Chris@0 218 {
Chris@0 219 if (m_latencyPort) {
Chris@0 220 if (!m_run) run(RealTime::zeroTime);
Chris@0 221 if (*m_latencyPort > 0) return (size_t)*m_latencyPort;
Chris@0 222 }
Chris@0 223 return 0;
Chris@0 224 }
Chris@0 225
Chris@0 226 void
Chris@0 227 LADSPAPluginInstance::silence()
Chris@0 228 {
Chris@0 229 if (isOK()) {
Chris@0 230 deactivate();
Chris@0 231 activate();
Chris@0 232 }
Chris@0 233 }
Chris@0 234
Chris@0 235 void
Chris@0 236 LADSPAPluginInstance::setIdealChannelCount(size_t channels)
Chris@0 237 {
Chris@0 238 if (m_audioPortsIn.size() != 1 || channels == m_instanceCount) {
Chris@0 239 silence();
Chris@0 240 return;
Chris@0 241 }
Chris@0 242
Chris@0 243 if (isOK()) {
Chris@0 244 deactivate();
Chris@0 245 }
Chris@0 246
Chris@0 247 //!!! don't we need to reallocate inputBuffers and outputBuffers?
Chris@0 248
Chris@0 249 cleanup();
Chris@0 250 m_instanceCount = channels;
Chris@0 251 instantiate(m_sampleRate);
Chris@0 252 if (isOK()) {
Chris@0 253 connectPorts();
Chris@0 254 activate();
Chris@0 255 }
Chris@0 256 }
Chris@0 257
Chris@0 258
Chris@0 259 LADSPAPluginInstance::~LADSPAPluginInstance()
Chris@0 260 {
Chris@0 261 #ifdef DEBUG_LADSPA
Chris@0 262 std::cerr << "LADSPAPluginInstance::~LADSPAPluginInstance" << std::endl;
Chris@0 263 #endif
Chris@0 264
Chris@0 265 if (m_instanceHandles.size() != 0) { // "isOK()"
Chris@0 266 deactivate();
Chris@0 267 }
Chris@0 268
Chris@0 269 cleanup();
Chris@0 270
Chris@0 271 for (unsigned int i = 0; i < m_controlPortsIn.size(); ++i)
Chris@0 272 delete m_controlPortsIn[i].second;
Chris@0 273
Chris@0 274 for (unsigned int i = 0; i < m_controlPortsOut.size(); ++i)
Chris@0 275 delete m_controlPortsOut[i].second;
Chris@0 276
Chris@0 277 m_controlPortsIn.clear();
Chris@0 278 m_controlPortsOut.clear();
Chris@0 279
Chris@0 280 if (m_ownBuffers) {
Chris@0 281 for (size_t i = 0; i < m_audioPortsIn.size(); ++i) {
Chris@0 282 delete[] m_inputBuffers[i];
Chris@0 283 }
Chris@0 284 for (size_t i = 0; i < m_audioPortsOut.size(); ++i) {
Chris@0 285 delete[] m_outputBuffers[i];
Chris@0 286 }
Chris@0 287
Chris@0 288 delete[] m_inputBuffers;
Chris@0 289 delete[] m_outputBuffers;
Chris@0 290 }
Chris@0 291
Chris@0 292 m_audioPortsIn.clear();
Chris@0 293 m_audioPortsOut.clear();
Chris@0 294 }
Chris@0 295
Chris@0 296
Chris@0 297 void
Chris@0 298 LADSPAPluginInstance::instantiate(unsigned long sampleRate)
Chris@0 299 {
Chris@0 300 #ifdef DEBUG_LADSPA
Chris@0 301 std::cout << "LADSPAPluginInstance::instantiate - plugin unique id = "
Chris@0 302 << m_descriptor->UniqueID << std::endl;
Chris@0 303 #endif
Chris@0 304 if (!m_descriptor) return;
Chris@0 305
Chris@0 306 if (!m_descriptor->instantiate) {
Chris@0 307 std::cerr << "Bad plugin: plugin id " << m_descriptor->UniqueID
Chris@0 308 << ":" << m_descriptor->Label
Chris@0 309 << " has no instantiate method!" << std::endl;
Chris@0 310 return;
Chris@0 311 }
Chris@0 312
Chris@0 313 for (size_t i = 0; i < m_instanceCount; ++i) {
Chris@0 314 m_instanceHandles.push_back
Chris@0 315 (m_descriptor->instantiate(m_descriptor, sampleRate));
Chris@0 316 }
Chris@0 317 }
Chris@0 318
Chris@0 319 void
Chris@0 320 LADSPAPluginInstance::activate()
Chris@0 321 {
Chris@0 322 if (!m_descriptor || !m_descriptor->activate) return;
Chris@0 323
Chris@0 324 for (std::vector<LADSPA_Handle>::iterator hi = m_instanceHandles.begin();
Chris@0 325 hi != m_instanceHandles.end(); ++hi) {
Chris@0 326 m_descriptor->activate(*hi);
Chris@0 327 }
Chris@0 328 }
Chris@0 329
Chris@0 330 void
Chris@0 331 LADSPAPluginInstance::connectPorts()
Chris@0 332 {
Chris@0 333 if (!m_descriptor || !m_descriptor->connect_port) return;
Chris@0 334
Chris@0 335 assert(sizeof(LADSPA_Data) == sizeof(float));
Chris@0 336 assert(sizeof(sample_t) == sizeof(float));
Chris@0 337
Chris@0 338 int inbuf = 0, outbuf = 0;
Chris@0 339
Chris@0 340 for (std::vector<LADSPA_Handle>::iterator hi = m_instanceHandles.begin();
Chris@0 341 hi != m_instanceHandles.end(); ++hi) {
Chris@0 342
Chris@0 343 for (unsigned int i = 0; i < m_audioPortsIn.size(); ++i) {
Chris@0 344 m_descriptor->connect_port(*hi,
Chris@0 345 m_audioPortsIn[i],
Chris@0 346 (LADSPA_Data *)m_inputBuffers[inbuf]);
Chris@0 347 ++inbuf;
Chris@0 348 }
Chris@0 349
Chris@0 350 for (unsigned int i = 0; i < m_audioPortsOut.size(); ++i) {
Chris@0 351 m_descriptor->connect_port(*hi,
Chris@0 352 m_audioPortsOut[i],
Chris@0 353 (LADSPA_Data *)m_outputBuffers[outbuf]);
Chris@0 354 ++outbuf;
Chris@0 355 }
Chris@0 356
Chris@0 357 // If there is more than one instance, they all share the same
Chris@0 358 // control port ins (and outs, for the moment, because we
Chris@0 359 // don't actually do anything with the outs anyway -- but they
Chris@0 360 // do have to be connected as the plugin can't know if they're
Chris@0 361 // not and will write to them anyway).
Chris@0 362
Chris@0 363 for (unsigned int i = 0; i < m_controlPortsIn.size(); ++i) {
Chris@0 364 m_descriptor->connect_port(*hi,
Chris@0 365 m_controlPortsIn[i].first,
Chris@0 366 m_controlPortsIn[i].second);
Chris@0 367 }
Chris@0 368
Chris@0 369 for (unsigned int i = 0; i < m_controlPortsOut.size(); ++i) {
Chris@0 370 m_descriptor->connect_port(*hi,
Chris@0 371 m_controlPortsOut[i].first,
Chris@0 372 m_controlPortsOut[i].second);
Chris@0 373 }
Chris@0 374 }
Chris@0 375 }
Chris@0 376
Chris@0 377 unsigned int
Chris@0 378 LADSPAPluginInstance::getParameterCount() const
Chris@0 379 {
Chris@0 380 return m_controlPortsIn.size();
Chris@0 381 }
Chris@0 382
Chris@0 383 void
Chris@0 384 LADSPAPluginInstance::setParameterValue(unsigned int parameter, float value)
Chris@0 385 {
Chris@0 386 if (parameter >= m_controlPortsIn.size()) return;
Chris@0 387
Chris@0 388 unsigned int portNumber = m_controlPortsIn[parameter].first;
Chris@0 389
Chris@0 390 LADSPAPluginFactory *f = dynamic_cast<LADSPAPluginFactory *>(m_factory);
Chris@0 391 if (f) {
Chris@0 392 if (value < f->getPortMinimum(m_descriptor, portNumber)) {
Chris@0 393 value = f->getPortMinimum(m_descriptor, portNumber);
Chris@0 394 }
Chris@0 395 if (value > f->getPortMaximum(m_descriptor, portNumber)) {
Chris@0 396 value = f->getPortMaximum(m_descriptor, portNumber);
Chris@0 397 }
Chris@0 398 }
Chris@0 399
Chris@0 400 (*m_controlPortsIn[parameter].second) = value;
Chris@0 401 }
Chris@0 402
Chris@0 403 float
Chris@0 404 LADSPAPluginInstance::getParameterValue(unsigned int parameter) const
Chris@0 405 {
Chris@0 406 if (parameter >= m_controlPortsIn.size()) return 0.0;
Chris@0 407 return (*m_controlPortsIn[parameter].second);
Chris@0 408 }
Chris@0 409
Chris@0 410 float
Chris@0 411 LADSPAPluginInstance::getParameterDefault(unsigned int parameter) const
Chris@0 412 {
Chris@0 413 if (parameter >= m_controlPortsIn.size()) return 0.0;
Chris@0 414
Chris@0 415 LADSPAPluginFactory *f = dynamic_cast<LADSPAPluginFactory *>(m_factory);
Chris@0 416 if (f) {
Chris@0 417 return f->getPortDefault(m_descriptor, m_controlPortsIn[parameter].first);
Chris@0 418 } else {
Chris@0 419 return 0.0f;
Chris@0 420 }
Chris@0 421 }
Chris@0 422
Chris@0 423 void
Chris@0 424 LADSPAPluginInstance::run(const RealTime &)
Chris@0 425 {
Chris@0 426 if (!m_descriptor || !m_descriptor->run) return;
Chris@0 427
Chris@0 428 for (std::vector<LADSPA_Handle>::iterator hi = m_instanceHandles.begin();
Chris@0 429 hi != m_instanceHandles.end(); ++hi) {
Chris@0 430 m_descriptor->run(*hi, m_blockSize);
Chris@0 431 }
Chris@0 432
Chris@0 433 m_run = true;
Chris@0 434 }
Chris@0 435
Chris@0 436 void
Chris@0 437 LADSPAPluginInstance::deactivate()
Chris@0 438 {
Chris@0 439 if (!m_descriptor || !m_descriptor->deactivate) return;
Chris@0 440
Chris@0 441 for (std::vector<LADSPA_Handle>::iterator hi = m_instanceHandles.begin();
Chris@0 442 hi != m_instanceHandles.end(); ++hi) {
Chris@0 443 m_descriptor->deactivate(*hi);
Chris@0 444 }
Chris@0 445 }
Chris@0 446
Chris@0 447 void
Chris@0 448 LADSPAPluginInstance::cleanup()
Chris@0 449 {
Chris@0 450 if (!m_descriptor) return;
Chris@0 451
Chris@0 452 if (!m_descriptor->cleanup) {
Chris@0 453 std::cerr << "Bad plugin: plugin id " << m_descriptor->UniqueID
Chris@0 454 << ":" << m_descriptor->Label
Chris@0 455 << " has no cleanup method!" << std::endl;
Chris@0 456 return;
Chris@0 457 }
Chris@0 458
Chris@0 459 for (std::vector<LADSPA_Handle>::iterator hi = m_instanceHandles.begin();
Chris@0 460 hi != m_instanceHandles.end(); ++hi) {
Chris@0 461 m_descriptor->cleanup(*hi);
Chris@0 462 }
Chris@0 463
Chris@0 464 m_instanceHandles.clear();
Chris@0 465 }
Chris@0 466
Chris@0 467