annotate plugin/LADSPAPluginInstance.cpp @ 51:c2913298cf94

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