annotate plugin/DSSIPluginInstance.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.
Chris@0 14 */
Chris@0 15
Chris@0 16 #include <iostream>
Chris@0 17 #include <cassert>
Chris@0 18
Chris@0 19 #include "DSSIPluginInstance.h"
Chris@0 20 #include "PluginIdentifier.h"
Chris@0 21 #include "LADSPAPluginFactory.h"
Chris@0 22
Chris@28 23 //#define DEBUG_DSSI 1
Chris@0 24 //#define DEBUG_DSSI_PROCESS 1
Chris@0 25
Chris@0 26 #define EVENT_BUFFER_SIZE 1023
Chris@0 27
Chris@0 28 #ifdef DEBUG_DSSI
Chris@0 29 static std::ostream &operator<<(std::ostream& o, const QString &s)
Chris@0 30 {
Chris@0 31 o << s.toLocal8Bit().data();
Chris@0 32 return o;
Chris@0 33 }
Chris@0 34 #endif
Chris@0 35
Chris@0 36 DSSIPluginInstance::GroupMap DSSIPluginInstance::m_groupMap;
Chris@0 37 snd_seq_event_t **DSSIPluginInstance::m_groupLocalEventBuffers = 0;
Chris@0 38 size_t DSSIPluginInstance::m_groupLocalEventBufferCount = 0;
Chris@0 39 Scavenger<ScavengerArrayWrapper<snd_seq_event_t *> > DSSIPluginInstance::m_bufferScavenger(2, 10);
Chris@0 40 std::map<LADSPA_Handle, std::set<DSSIPluginInstance::NonRTPluginThread *> > DSSIPluginInstance::m_threads;
Chris@0 41
Chris@0 42
Chris@0 43 DSSIPluginInstance::DSSIPluginInstance(RealTimePluginFactory *factory,
Chris@0 44 int clientId,
Chris@0 45 QString identifier,
Chris@0 46 int position,
Chris@0 47 unsigned long sampleRate,
Chris@0 48 size_t blockSize,
Chris@0 49 int idealChannelCount,
Chris@0 50 const DSSI_Descriptor* descriptor) :
Chris@0 51 RealTimePluginInstance(factory, identifier),
Chris@0 52 m_client(clientId),
Chris@0 53 m_position(position),
Chris@0 54 m_descriptor(descriptor),
Chris@0 55 m_programCacheValid(false),
Chris@0 56 m_eventBuffer(EVENT_BUFFER_SIZE),
Chris@0 57 m_blockSize(blockSize),
Chris@0 58 m_idealChannelCount(idealChannelCount),
Chris@0 59 m_sampleRate(sampleRate),
Chris@0 60 m_latencyPort(0),
Chris@0 61 m_run(false),
Chris@0 62 m_bypassed(false),
Chris@10 63 m_grouped(false),
Chris@10 64 m_haveLastEventSendTime(false)
Chris@0 65 {
Chris@0 66 #ifdef DEBUG_DSSI
Chris@0 67 std::cerr << "DSSIPluginInstance::DSSIPluginInstance(" << identifier << ")"
Chris@0 68 << std::endl;
Chris@0 69 #endif
Chris@0 70
Chris@0 71 init();
Chris@0 72
Chris@0 73 m_inputBuffers = new sample_t*[m_audioPortsIn.size()];
Chris@0 74 m_outputBuffers = new sample_t*[m_outputBufferCount];
Chris@0 75
Chris@0 76 for (size_t i = 0; i < m_audioPortsIn.size(); ++i) {
Chris@0 77 m_inputBuffers[i] = new sample_t[blockSize];
Chris@0 78 }
Chris@0 79 for (size_t i = 0; i < m_outputBufferCount; ++i) {
Chris@0 80 m_outputBuffers[i] = new sample_t[blockSize];
Chris@0 81 }
Chris@0 82
Chris@0 83 m_ownBuffers = true;
Chris@0 84
Chris@0 85 m_pending.lsb = m_pending.msb = m_pending.program = -1;
Chris@0 86
Chris@0 87 instantiate(sampleRate);
Chris@0 88 if (isOK()) {
Chris@0 89 connectPorts();
Chris@0 90 activate();
Chris@0 91 initialiseGroupMembership();
Chris@0 92 }
Chris@0 93 }
Chris@0 94
Chris@51 95 std::string
Chris@51 96 DSSIPluginInstance::getName() const
Chris@0 97 {
Chris@51 98 return m_descriptor->LADSPA_Plugin->Label;
Chris@0 99 }
Chris@0 100
Chris@51 101 std::string
Chris@51 102 DSSIPluginInstance::getDescription() const
Chris@51 103 {
Chris@51 104 return m_descriptor->LADSPA_Plugin->Name;
Chris@51 105 }
Chris@51 106
Chris@51 107 std::string
Chris@51 108 DSSIPluginInstance::getMaker() const
Chris@51 109 {
Chris@51 110 return m_descriptor->LADSPA_Plugin->Maker;
Chris@51 111 }
Chris@51 112
Chris@51 113 int
Chris@51 114 DSSIPluginInstance::getPluginVersion() const
Chris@51 115 {
Chris@51 116 return 1;
Chris@51 117 }
Chris@51 118
Chris@51 119 std::string
Chris@51 120 DSSIPluginInstance::getCopyright() const
Chris@51 121 {
Chris@51 122 return m_descriptor->LADSPA_Plugin->Copyright;
Chris@51 123 }
Chris@51 124
Chris@51 125 DSSIPluginInstance::ParameterList
Chris@51 126 DSSIPluginInstance::getParameterDescriptors() const
Chris@51 127 {
Chris@51 128 ParameterList list;
Chris@51 129 LADSPAPluginFactory *f = dynamic_cast<LADSPAPluginFactory *>(m_factory);
Chris@51 130
Chris@51 131 for (unsigned int i = 0; i < m_controlPortsIn.size(); ++i) {
Chris@51 132
Chris@51 133 ParameterDescriptor pd;
Chris@51 134 unsigned int pn = m_controlPortsIn[i].first;
Chris@51 135
Chris@51 136 pd.name = m_descriptor->LADSPA_Plugin->PortNames[pn];
Chris@51 137 pd.description = pd.name;
Chris@51 138 pd.minValue = f->getPortMinimum(m_descriptor->LADSPA_Plugin, pn);
Chris@51 139 pd.maxValue = f->getPortMaximum(m_descriptor->LADSPA_Plugin, pn);
Chris@51 140 pd.defaultValue = f->getPortDefault(m_descriptor->LADSPA_Plugin, pn);
Chris@51 141 pd.isQuantized = false;
Chris@51 142
Chris@51 143 list.push_back(pd);
Chris@51 144 }
Chris@51 145
Chris@51 146 return list;
Chris@51 147 }
Chris@51 148
Chris@51 149 float
Chris@51 150 DSSIPluginInstance::getParameter(std::string name) const
Chris@51 151 {
Chris@51 152 for (unsigned int i = 0; i < m_controlPortsIn.size(); ++i) {
Chris@51 153 if (name == m_descriptor->LADSPA_Plugin->PortNames[m_controlPortsIn[i].first]) {
Chris@51 154 return getParameterValue(i);
Chris@51 155 }
Chris@51 156 }
Chris@51 157
Chris@51 158 return 0.0;
Chris@51 159 }
Chris@51 160
Chris@51 161 void
Chris@51 162 DSSIPluginInstance::setParameter(std::string name, float value)
Chris@51 163 {
Chris@51 164 for (unsigned int i = 0; i < m_controlPortsIn.size(); ++i) {
Chris@51 165 if (name == m_descriptor->LADSPA_Plugin->PortNames[m_controlPortsIn[i].first]) {
Chris@51 166 setParameterValue(i, value);
Chris@51 167 break;
Chris@51 168 }
Chris@51 169 }
Chris@51 170 }
Chris@0 171
Chris@0 172 void
Chris@0 173 DSSIPluginInstance::init()
Chris@0 174 {
Chris@0 175 #ifdef DEBUG_DSSI
Chris@0 176 std::cerr << "DSSIPluginInstance::init" << std::endl;
Chris@0 177 #endif
Chris@0 178
Chris@0 179 // Discover ports numbers and identities
Chris@0 180 //
Chris@0 181 const LADSPA_Descriptor *descriptor = m_descriptor->LADSPA_Plugin;
Chris@0 182
Chris@0 183 for (unsigned long i = 0; i < descriptor->PortCount; ++i)
Chris@0 184 {
Chris@0 185 if (LADSPA_IS_PORT_AUDIO(descriptor->PortDescriptors[i]))
Chris@0 186 {
Chris@0 187 if (LADSPA_IS_PORT_INPUT(descriptor->PortDescriptors[i])) {
Chris@0 188 m_audioPortsIn.push_back(i);
Chris@0 189 } else {
Chris@0 190 m_audioPortsOut.push_back(i);
Chris@0 191 }
Chris@0 192 }
Chris@0 193 else
Chris@0 194 if (LADSPA_IS_PORT_CONTROL(descriptor->PortDescriptors[i]))
Chris@0 195 {
Chris@0 196 if (LADSPA_IS_PORT_INPUT(descriptor->PortDescriptors[i])) {
Chris@0 197
Chris@0 198 LADSPA_Data *data = new LADSPA_Data(0.0);
Chris@0 199
Chris@0 200 m_controlPortsIn.push_back(std::pair<unsigned long, LADSPA_Data*>
Chris@0 201 (i, data));
Chris@0 202
Chris@0 203 m_backupControlPortsIn.push_back(0.0);
Chris@0 204
Chris@0 205 } else {
Chris@0 206 LADSPA_Data *data = new LADSPA_Data(0.0);
Chris@0 207 m_controlPortsOut.push_back(
Chris@0 208 std::pair<unsigned long, LADSPA_Data*>(i, data));
Chris@0 209 if (!strcmp(descriptor->PortNames[i], "latency") ||
Chris@0 210 !strcmp(descriptor->PortNames[i], "_latency")) {
Chris@0 211 #ifdef DEBUG_DSSI
Chris@0 212 std::cerr << "Wooo! We have a latency port!" << std::endl;
Chris@0 213 #endif
Chris@0 214 m_latencyPort = data;
Chris@0 215 }
Chris@0 216 }
Chris@0 217 }
Chris@0 218 #ifdef DEBUG_DSSI
Chris@0 219 else
Chris@0 220 std::cerr << "DSSIPluginInstance::DSSIPluginInstance - "
Chris@0 221 << "unrecognised port type" << std::endl;
Chris@0 222 #endif
Chris@0 223 }
Chris@0 224
Chris@0 225 m_outputBufferCount = std::max(m_idealChannelCount, m_audioPortsOut.size());
Chris@0 226 }
Chris@0 227
Chris@0 228 size_t
Chris@0 229 DSSIPluginInstance::getLatency()
Chris@0 230 {
Chris@0 231 size_t latency = 0;
Chris@0 232
Chris@0 233 #ifdef DEBUG_DSSI_PROCESS
Chris@0 234 std::cerr << "DSSIPluginInstance::getLatency(): m_latencyPort " << m_latencyPort << ", m_run " << m_run << std::endl;
Chris@0 235 #endif
Chris@0 236
Chris@0 237 if (m_latencyPort) {
Chris@0 238 if (!m_run) run(RealTime::zeroTime);
Chris@0 239 latency = (size_t)(*m_latencyPort + 0.1);
Chris@0 240 }
Chris@0 241
Chris@0 242 #ifdef DEBUG_DSSI_PROCESS
Chris@0 243 std::cerr << "DSSIPluginInstance::getLatency(): latency is " << latency << std::endl;
Chris@0 244 #endif
Chris@0 245
Chris@0 246 return latency;
Chris@0 247 }
Chris@0 248
Chris@0 249 void
Chris@0 250 DSSIPluginInstance::silence()
Chris@0 251 {
Chris@0 252 if (m_instanceHandle != 0) {
Chris@0 253 deactivate();
Chris@0 254 activate();
Chris@0 255 }
Chris@0 256 }
Chris@0 257
Chris@0 258 void
Chris@0 259 DSSIPluginInstance::discardEvents()
Chris@0 260 {
Chris@0 261 m_eventBuffer.reset();
Chris@0 262 }
Chris@0 263
Chris@0 264 void
Chris@0 265 DSSIPluginInstance::setIdealChannelCount(size_t channels)
Chris@0 266 {
Chris@0 267 #ifdef DEBUG_DSSI
Chris@0 268 std::cerr << "DSSIPluginInstance::setIdealChannelCount: channel count "
Chris@0 269 << channels << " (was " << m_idealChannelCount << ")" << std::endl;
Chris@0 270 #endif
Chris@0 271
Chris@0 272 if (channels == m_idealChannelCount) {
Chris@0 273 silence();
Chris@0 274 return;
Chris@0 275 }
Chris@0 276
Chris@0 277 if (m_instanceHandle != 0) {
Chris@0 278 deactivate();
Chris@0 279 }
Chris@0 280
Chris@0 281 m_idealChannelCount = channels;
Chris@0 282
Chris@0 283 if (channels > m_outputBufferCount) {
Chris@0 284
Chris@0 285 for (size_t i = 0; i < m_outputBufferCount; ++i) {
Chris@0 286 delete[] m_outputBuffers[i];
Chris@0 287 }
Chris@0 288
Chris@0 289 delete[] m_outputBuffers;
Chris@0 290
Chris@0 291 m_outputBufferCount = channels;
Chris@0 292
Chris@0 293 m_outputBuffers = new sample_t*[m_outputBufferCount];
Chris@0 294
Chris@0 295 for (size_t i = 0; i < m_outputBufferCount; ++i) {
Chris@0 296 m_outputBuffers[i] = new sample_t[m_blockSize];
Chris@0 297 }
Chris@0 298
Chris@0 299 connectPorts();
Chris@0 300 }
Chris@0 301
Chris@0 302 if (m_instanceHandle != 0) {
Chris@0 303 activate();
Chris@0 304 }
Chris@0 305 }
Chris@0 306
Chris@0 307 void
Chris@0 308 DSSIPluginInstance::detachFromGroup()
Chris@0 309 {
Chris@0 310 if (!m_grouped) return;
Chris@0 311 m_groupMap[m_identifier].erase(this);
Chris@0 312 m_grouped = false;
Chris@0 313 }
Chris@0 314
Chris@0 315 void
Chris@0 316 DSSIPluginInstance::initialiseGroupMembership()
Chris@0 317 {
Chris@0 318 if (!m_descriptor->run_multiple_synths) {
Chris@0 319 m_grouped = false;
Chris@0 320 return;
Chris@0 321 }
Chris@0 322
Chris@0 323 //!!! GroupMap is not actually thread-safe.
Chris@0 324
Chris@0 325 size_t pluginsInGroup = m_groupMap[m_identifier].size();
Chris@0 326
Chris@0 327 if (++pluginsInGroup > m_groupLocalEventBufferCount) {
Chris@0 328
Chris@0 329 size_t nextBufferCount = pluginsInGroup * 2;
Chris@0 330
Chris@0 331 snd_seq_event_t **eventLocalBuffers = new snd_seq_event_t *[nextBufferCount];
Chris@0 332
Chris@0 333 for (size_t i = 0; i < m_groupLocalEventBufferCount; ++i) {
Chris@0 334 eventLocalBuffers[i] = m_groupLocalEventBuffers[i];
Chris@0 335 }
Chris@0 336 for (size_t i = m_groupLocalEventBufferCount; i < nextBufferCount; ++i) {
Chris@0 337 eventLocalBuffers[i] = new snd_seq_event_t[EVENT_BUFFER_SIZE];
Chris@0 338 }
Chris@0 339
Chris@0 340 if (m_groupLocalEventBuffers) {
Chris@0 341 m_bufferScavenger.claim(new ScavengerArrayWrapper<snd_seq_event_t *>
Chris@0 342 (m_groupLocalEventBuffers));
Chris@0 343 }
Chris@0 344
Chris@0 345 m_groupLocalEventBuffers = eventLocalBuffers;
Chris@0 346 m_groupLocalEventBufferCount = nextBufferCount;
Chris@0 347 }
Chris@0 348
Chris@0 349 m_grouped = true;
Chris@0 350 m_groupMap[m_identifier].insert(this);
Chris@0 351 }
Chris@0 352
Chris@0 353 DSSIPluginInstance::~DSSIPluginInstance()
Chris@0 354 {
Chris@0 355 std::cerr << "DSSIPluginInstance::~DSSIPluginInstance" << std::endl;
Chris@0 356
Chris@0 357 if (m_threads.find(m_instanceHandle) != m_threads.end()) {
Chris@0 358
Chris@0 359 for (std::set<NonRTPluginThread *>::iterator i =
Chris@0 360 m_threads[m_instanceHandle].begin();
Chris@0 361 i != m_threads[m_instanceHandle].end(); ++i) {
Chris@0 362
Chris@0 363 (*i)->setExiting();
Chris@0 364 (*i)->wait();
Chris@0 365 delete *i;
Chris@0 366 }
Chris@0 367
Chris@0 368 m_threads.erase(m_instanceHandle);
Chris@0 369 }
Chris@0 370
Chris@0 371 detachFromGroup();
Chris@0 372
Chris@0 373 if (m_instanceHandle != 0) {
Chris@0 374 deactivate();
Chris@0 375 }
Chris@0 376
Chris@0 377 cleanup();
Chris@0 378
Chris@0 379 for (unsigned int i = 0; i < m_controlPortsIn.size(); ++i)
Chris@0 380 delete m_controlPortsIn[i].second;
Chris@0 381
Chris@0 382 for (unsigned int i = 0; i < m_controlPortsOut.size(); ++i)
Chris@0 383 delete m_controlPortsOut[i].second;
Chris@0 384
Chris@0 385 m_controlPortsIn.clear();
Chris@0 386 m_controlPortsOut.clear();
Chris@0 387
Chris@0 388 if (m_ownBuffers) {
Chris@0 389 for (size_t i = 0; i < m_audioPortsIn.size(); ++i) {
Chris@0 390 delete[] m_inputBuffers[i];
Chris@0 391 }
Chris@0 392 for (size_t i = 0; i < m_outputBufferCount; ++i) {
Chris@0 393 delete[] m_outputBuffers[i];
Chris@0 394 }
Chris@0 395
Chris@0 396 delete[] m_inputBuffers;
Chris@0 397 delete[] m_outputBuffers;
Chris@0 398 }
Chris@0 399
Chris@0 400 m_audioPortsIn.clear();
Chris@0 401 m_audioPortsOut.clear();
Chris@0 402 }
Chris@0 403
Chris@0 404
Chris@0 405 void
Chris@0 406 DSSIPluginInstance::instantiate(unsigned long sampleRate)
Chris@0 407 {
Chris@0 408 #ifdef DEBUG_DSSI
Chris@0 409 std::cout << "DSSIPluginInstance::instantiate - plugin \"unique\" id = "
Chris@0 410 << m_descriptor->LADSPA_Plugin->UniqueID << std::endl;
Chris@0 411 #endif
Chris@0 412 if (!m_descriptor) return;
Chris@0 413
Chris@0 414 const LADSPA_Descriptor *descriptor = m_descriptor->LADSPA_Plugin;
Chris@0 415
Chris@0 416 if (!descriptor->instantiate) {
Chris@0 417 std::cerr << "Bad plugin: plugin id " << descriptor->UniqueID
Chris@0 418 << ":" << descriptor->Label
Chris@0 419 << " has no instantiate method!" << std::endl;
Chris@0 420 return;
Chris@0 421 }
Chris@0 422
Chris@0 423 m_instanceHandle = descriptor->instantiate(descriptor, sampleRate);
Chris@0 424
Chris@0 425 if (m_instanceHandle) {
Chris@0 426
Chris@0 427 if (m_descriptor->get_midi_controller_for_port) {
Chris@0 428
Chris@0 429 for (unsigned long i = 0; i < descriptor->PortCount; ++i) {
Chris@0 430
Chris@0 431 if (LADSPA_IS_PORT_CONTROL(descriptor->PortDescriptors[i]) &&
Chris@0 432 LADSPA_IS_PORT_INPUT(descriptor->PortDescriptors[i])) {
Chris@0 433
Chris@0 434 int controller = m_descriptor->get_midi_controller_for_port
Chris@0 435 (m_instanceHandle, i);
Chris@0 436
Chris@0 437 if (controller != 0 && controller != 32 &&
Chris@0 438 DSSI_IS_CC(controller)) {
Chris@0 439
Chris@0 440 m_controllerMap[DSSI_CC_NUMBER(controller)] = i;
Chris@0 441 }
Chris@0 442 }
Chris@0 443 }
Chris@0 444 }
Chris@0 445 }
Chris@0 446 }
Chris@0 447
Chris@0 448 void
Chris@0 449 DSSIPluginInstance::checkProgramCache() const
Chris@0 450 {
Chris@0 451 if (m_programCacheValid) return;
Chris@0 452 m_cachedPrograms.clear();
Chris@0 453
Chris@0 454 #ifdef DEBUG_DSSI
Chris@0 455 std::cerr << "DSSIPluginInstance::checkProgramCache" << std::endl;
Chris@0 456 #endif
Chris@0 457
Chris@0 458 if (!m_descriptor || !m_descriptor->get_program) {
Chris@0 459 m_programCacheValid = true;
Chris@0 460 return;
Chris@0 461 }
Chris@0 462
Chris@0 463 unsigned long index = 0;
Chris@0 464 const DSSI_Program_Descriptor *programDescriptor;
Chris@0 465 while ((programDescriptor = m_descriptor->get_program(m_instanceHandle, index))) {
Chris@0 466 ++index;
Chris@0 467 ProgramDescriptor d;
Chris@0 468 d.bank = programDescriptor->Bank;
Chris@0 469 d.program = programDescriptor->Program;
Chris@0 470 d.name = programDescriptor->Name;
Chris@0 471 m_cachedPrograms.push_back(d);
Chris@0 472 }
Chris@0 473
Chris@0 474 #ifdef DEBUG_DSSI
Chris@0 475 std::cerr << "DSSIPluginInstance::checkProgramCache: have " << m_cachedPrograms.size() << " programs" << std::endl;
Chris@0 476 #endif
Chris@0 477
Chris@0 478 m_programCacheValid = true;
Chris@0 479 }
Chris@0 480
Chris@51 481 DSSIPluginInstance::ProgramList
Chris@0 482 DSSIPluginInstance::getPrograms() const
Chris@0 483 {
Chris@0 484 #ifdef DEBUG_DSSI
Chris@0 485 std::cerr << "DSSIPluginInstance::getPrograms" << std::endl;
Chris@0 486 #endif
Chris@0 487
Chris@51 488 if (!m_descriptor) return ProgramList();
Chris@0 489
Chris@0 490 checkProgramCache();
Chris@0 491
Chris@51 492 ProgramList programs;
Chris@0 493
Chris@0 494 for (std::vector<ProgramDescriptor>::iterator i = m_cachedPrograms.begin();
Chris@0 495 i != m_cachedPrograms.end(); ++i) {
Chris@0 496 programs.push_back(i->name);
Chris@0 497 }
Chris@0 498
Chris@0 499 return programs;
Chris@0 500 }
Chris@0 501
Chris@51 502 std::string
Chris@0 503 DSSIPluginInstance::getProgram(int bank, int program) const
Chris@0 504 {
Chris@0 505 #ifdef DEBUG_DSSI
Chris@0 506 std::cerr << "DSSIPluginInstance::getProgram(" << bank << "," << program << ")" << std::endl;
Chris@0 507 #endif
Chris@0 508
Chris@51 509 if (!m_descriptor) return std::string();
Chris@0 510
Chris@0 511 checkProgramCache();
Chris@0 512
Chris@0 513 for (std::vector<ProgramDescriptor>::iterator i = m_cachedPrograms.begin();
Chris@0 514 i != m_cachedPrograms.end(); ++i) {
Chris@0 515 if (i->bank == bank && i->program == program) return i->name;
Chris@0 516 }
Chris@0 517
Chris@51 518 return std::string();
Chris@0 519 }
Chris@0 520
Chris@0 521 unsigned long
Chris@51 522 DSSIPluginInstance::getProgram(std::string name) const
Chris@0 523 {
Chris@0 524 #ifdef DEBUG_DSSI
Chris@0 525 std::cerr << "DSSIPluginInstance::getProgram(" << name << ")" << std::endl;
Chris@0 526 #endif
Chris@0 527
Chris@0 528 if (!m_descriptor) return 0;
Chris@0 529
Chris@0 530 checkProgramCache();
Chris@0 531
Chris@0 532 unsigned long rv;
Chris@0 533
Chris@0 534 for (std::vector<ProgramDescriptor>::iterator i = m_cachedPrograms.begin();
Chris@0 535 i != m_cachedPrograms.end(); ++i) {
Chris@0 536 if (i->name == name) {
Chris@0 537 rv = i->bank;
Chris@0 538 rv = (rv << 16) + i->program;
Chris@0 539 return rv;
Chris@0 540 }
Chris@0 541 }
Chris@0 542
Chris@0 543 return 0;
Chris@0 544 }
Chris@0 545
Chris@51 546 std::string
Chris@0 547 DSSIPluginInstance::getCurrentProgram() const
Chris@0 548 {
Chris@0 549 return m_program;
Chris@0 550 }
Chris@0 551
Chris@0 552 void
Chris@51 553 DSSIPluginInstance::selectProgram(std::string program)
Chris@0 554 {
Chris@0 555 selectProgramAux(program, true);
Chris@0 556 }
Chris@0 557
Chris@0 558 void
Chris@51 559 DSSIPluginInstance::selectProgramAux(std::string program, bool backupPortValues)
Chris@0 560 {
Chris@0 561 #ifdef DEBUG_DSSI
Chris@0 562 std::cerr << "DSSIPluginInstance::selectProgram(" << program << ")" << std::endl;
Chris@0 563 #endif
Chris@0 564
Chris@0 565 if (!m_descriptor) return;
Chris@0 566
Chris@0 567 checkProgramCache();
Chris@0 568
Chris@0 569 if (!m_descriptor->select_program) return;
Chris@0 570
Chris@0 571 bool found = false;
Chris@0 572 unsigned long bankNo = 0, programNo = 0;
Chris@0 573
Chris@0 574 for (std::vector<ProgramDescriptor>::iterator i = m_cachedPrograms.begin();
Chris@0 575 i != m_cachedPrograms.end(); ++i) {
Chris@0 576
Chris@0 577 if (i->name == program) {
Chris@0 578
Chris@0 579 bankNo = i->bank;
Chris@0 580 programNo = i->program;
Chris@0 581 found = true;
Chris@0 582
Chris@0 583 #ifdef DEBUG_DSSI
Chris@0 584 std::cerr << "DSSIPluginInstance::selectProgram(" << program << "): found at bank " << bankNo << ", program " << programNo << std::endl;
Chris@0 585 #endif
Chris@0 586
Chris@0 587 break;
Chris@0 588 }
Chris@0 589 }
Chris@0 590
Chris@0 591 if (!found) return;
Chris@0 592 m_program = program;
Chris@0 593
Chris@0 594 // DSSI select_program is an audio context call
Chris@0 595 m_processLock.lock();
Chris@0 596 m_descriptor->select_program(m_instanceHandle, bankNo, programNo);
Chris@0 597 m_processLock.unlock();
Chris@0 598
Chris@0 599 #ifdef DEBUG_DSSI
Chris@0 600 std::cerr << "DSSIPluginInstance::selectProgram(" << program << "): made select_program(" << bankNo << "," << programNo << ") call" << std::endl;
Chris@0 601 #endif
Chris@0 602
Chris@0 603 if (backupPortValues) {
Chris@0 604 for (size_t i = 0; i < m_backupControlPortsIn.size(); ++i) {
Chris@0 605 m_backupControlPortsIn[i] = *m_controlPortsIn[i].second;
Chris@0 606 }
Chris@0 607 }
Chris@0 608 }
Chris@0 609
Chris@0 610 void
Chris@0 611 DSSIPluginInstance::activate()
Chris@0 612 {
Chris@0 613 #ifdef DEBUG_DSSI
Chris@0 614 std::cerr << "DSSIPluginInstance::activate" << std::endl;
Chris@0 615 #endif
Chris@0 616
Chris@0 617 if (!m_descriptor || !m_descriptor->LADSPA_Plugin->activate) return;
Chris@0 618 m_descriptor->LADSPA_Plugin->activate(m_instanceHandle);
Chris@0 619
Chris@51 620 if (m_program != "") {
Chris@0 621 #ifdef DEBUG_DSSI
Chris@0 622 std::cerr << "DSSIPluginInstance::activate: restoring program " << m_program << std::endl;
Chris@0 623 #endif
Chris@0 624 selectProgramAux(m_program, false);
Chris@0 625 }
Chris@0 626
Chris@0 627 for (size_t i = 0; i < m_backupControlPortsIn.size(); ++i) {
Chris@0 628 #ifdef DEBUG_DSSI
Chris@0 629 std::cerr << "DSSIPluginInstance::activate: setting port " << m_controlPortsIn[i].first << " to " << m_backupControlPortsIn[i] << std::endl;
Chris@0 630 #endif
Chris@0 631 *m_controlPortsIn[i].second = m_backupControlPortsIn[i];
Chris@0 632 }
Chris@0 633 }
Chris@0 634
Chris@0 635 void
Chris@0 636 DSSIPluginInstance::connectPorts()
Chris@0 637 {
Chris@0 638 if (!m_descriptor || !m_descriptor->LADSPA_Plugin->connect_port) return;
Chris@0 639 std::cerr << "DSSIPluginInstance::connectPorts: " << m_audioPortsIn.size()
Chris@0 640 << " audio ports in, " << m_audioPortsOut.size() << " out, "
Chris@0 641 << m_outputBufferCount << " output buffers" << std::endl;
Chris@0 642
Chris@0 643 assert(sizeof(LADSPA_Data) == sizeof(float));
Chris@0 644 assert(sizeof(sample_t) == sizeof(float));
Chris@0 645
Chris@0 646 int inbuf = 0, outbuf = 0;
Chris@0 647
Chris@0 648 for (unsigned int i = 0; i < m_audioPortsIn.size(); ++i) {
Chris@0 649 m_descriptor->LADSPA_Plugin->connect_port
Chris@0 650 (m_instanceHandle,
Chris@0 651 m_audioPortsIn[i],
Chris@0 652 (LADSPA_Data *)m_inputBuffers[inbuf]);
Chris@0 653 ++inbuf;
Chris@0 654 }
Chris@0 655
Chris@0 656 for (unsigned int i = 0; i < m_audioPortsOut.size(); ++i) {
Chris@0 657 m_descriptor->LADSPA_Plugin->connect_port
Chris@0 658 (m_instanceHandle,
Chris@0 659 m_audioPortsOut[i],
Chris@0 660 (LADSPA_Data *)m_outputBuffers[outbuf]);
Chris@0 661 ++outbuf;
Chris@0 662 }
Chris@0 663
Chris@0 664 for (unsigned int i = 0; i < m_controlPortsIn.size(); ++i) {
Chris@0 665 m_descriptor->LADSPA_Plugin->connect_port
Chris@0 666 (m_instanceHandle,
Chris@0 667 m_controlPortsIn[i].first,
Chris@0 668 m_controlPortsIn[i].second);
Chris@0 669 }
Chris@0 670
Chris@0 671 for (unsigned int i = 0; i < m_controlPortsOut.size(); ++i) {
Chris@0 672 m_descriptor->LADSPA_Plugin->connect_port
Chris@0 673 (m_instanceHandle,
Chris@0 674 m_controlPortsOut[i].first,
Chris@0 675 m_controlPortsOut[i].second);
Chris@0 676 }
Chris@0 677 }
Chris@0 678
Chris@0 679 unsigned int
Chris@0 680 DSSIPluginInstance::getParameterCount() const
Chris@0 681 {
Chris@0 682 return m_controlPortsIn.size();
Chris@0 683 }
Chris@0 684
Chris@0 685 void
Chris@0 686 DSSIPluginInstance::setParameterValue(unsigned int parameter, float value)
Chris@0 687 {
Chris@0 688 #ifdef DEBUG_DSSI
Chris@0 689 std::cerr << "DSSIPluginInstance::setParameterValue(" << parameter << ") to " << value << std::endl;
Chris@0 690 #endif
Chris@0 691 if (parameter >= m_controlPortsIn.size()) return;
Chris@0 692
Chris@0 693 unsigned int portNumber = m_controlPortsIn[parameter].first;
Chris@0 694
Chris@0 695 LADSPAPluginFactory *f = dynamic_cast<LADSPAPluginFactory *>(m_factory);
Chris@0 696 if (f) {
Chris@0 697 if (value < f->getPortMinimum(m_descriptor->LADSPA_Plugin, portNumber)) {
Chris@0 698 value = f->getPortMinimum(m_descriptor->LADSPA_Plugin, portNumber);
Chris@0 699 }
Chris@0 700 if (value > f->getPortMaximum(m_descriptor->LADSPA_Plugin, portNumber)) {
Chris@0 701 value = f->getPortMaximum(m_descriptor->LADSPA_Plugin, portNumber);
Chris@0 702 }
Chris@0 703 }
Chris@0 704
Chris@0 705 (*m_controlPortsIn[parameter].second) = value;
Chris@0 706 m_backupControlPortsIn[parameter] = value;
Chris@0 707 }
Chris@0 708
Chris@0 709 void
Chris@0 710 DSSIPluginInstance::setPortValueFromController(unsigned int port, int cv)
Chris@0 711 {
Chris@0 712 #ifdef DEBUG_DSSI
Chris@0 713 std::cerr << "DSSIPluginInstance::setPortValueFromController(" << port << ") to " << cv << std::endl;
Chris@0 714 #endif
Chris@0 715
Chris@0 716 const LADSPA_Descriptor *p = m_descriptor->LADSPA_Plugin;
Chris@0 717 LADSPA_PortRangeHintDescriptor d = p->PortRangeHints[port].HintDescriptor;
Chris@0 718 LADSPA_Data lb = p->PortRangeHints[port].LowerBound;
Chris@0 719 LADSPA_Data ub = p->PortRangeHints[port].UpperBound;
Chris@0 720
Chris@0 721 float value = (float)cv;
Chris@0 722
Chris@0 723 if (!LADSPA_IS_HINT_BOUNDED_BELOW(d)) {
Chris@0 724 if (!LADSPA_IS_HINT_BOUNDED_ABOVE(d)) {
Chris@0 725 /* unbounded: might as well leave the value alone. */
Chris@0 726 } else {
Chris@0 727 /* bounded above only. just shift the range. */
Chris@0 728 value = ub - 127.0f + value;
Chris@0 729 }
Chris@0 730 } else {
Chris@0 731 if (!LADSPA_IS_HINT_BOUNDED_ABOVE(d)) {
Chris@0 732 /* bounded below only. just shift the range. */
Chris@0 733 value = lb + value;
Chris@0 734 } else {
Chris@0 735 /* bounded both ends. more interesting. */
Chris@0 736 /* XXX !!! todo: fill in logarithmic, sample rate &c */
Chris@0 737 value = lb + ((ub - lb) * value / 127.0f);
Chris@0 738 }
Chris@0 739 }
Chris@0 740
Chris@0 741 for (unsigned int i = 0; i < m_controlPortsIn.size(); ++i) {
Chris@0 742 if (m_controlPortsIn[i].first == port) {
Chris@0 743 setParameterValue(i, value);
Chris@0 744 }
Chris@0 745 }
Chris@0 746 }
Chris@0 747
Chris@0 748 float
Chris@0 749 DSSIPluginInstance::getParameterValue(unsigned int parameter) const
Chris@0 750 {
Chris@0 751 #ifdef DEBUG_DSSI
Chris@0 752 std::cerr << "DSSIPluginInstance::getParameterValue(" << parameter << ")" << std::endl;
Chris@0 753 #endif
Chris@0 754 if (parameter >= m_controlPortsIn.size()) return 0.0;
Chris@0 755 return (*m_controlPortsIn[parameter].second);
Chris@0 756 }
Chris@0 757
Chris@0 758 float
Chris@0 759 DSSIPluginInstance::getParameterDefault(unsigned int parameter) const
Chris@0 760 {
Chris@0 761 if (parameter >= m_controlPortsIn.size()) return 0.0;
Chris@0 762
Chris@0 763 LADSPAPluginFactory *f = dynamic_cast<LADSPAPluginFactory *>(m_factory);
Chris@0 764 if (f) {
Chris@0 765 return f->getPortDefault(m_descriptor->LADSPA_Plugin,
Chris@0 766 m_controlPortsIn[parameter].first);
Chris@0 767 } else {
Chris@0 768 return 0.0f;
Chris@0 769 }
Chris@0 770 }
Chris@0 771
Chris@51 772 std::string
Chris@51 773 DSSIPluginInstance::configure(std::string key,
Chris@51 774 std::string value)
Chris@0 775 {
Chris@51 776 if (!m_descriptor || !m_descriptor->configure) return std::string();
Chris@0 777
Chris@51 778 if (key == PluginIdentifier::RESERVED_PROJECT_DIRECTORY_KEY.toStdString()) {
Chris@0 779 #ifdef DSSI_PROJECT_DIRECTORY_KEY
Chris@0 780 key = DSSI_PROJECT_DIRECTORY_KEY;
Chris@0 781 #else
Chris@51 782 return std::string();
Chris@0 783 #endif
Chris@0 784 }
Chris@0 785
Chris@0 786
Chris@0 787 #ifdef DEBUG_DSSI
Chris@0 788 std::cerr << "DSSIPluginInstance::configure(" << key << "," << value << ")" << std::endl;
Chris@0 789 #endif
Chris@0 790
Chris@0 791 char *message = m_descriptor->configure(m_instanceHandle,
Chris@51 792 key.c_str(),
Chris@51 793 value.c_str());
Chris@0 794
Chris@0 795 m_programCacheValid = false;
Chris@0 796
Chris@51 797 std::string qm;
Chris@0 798
Chris@0 799 // Ignore return values from reserved key configuration calls such
Chris@0 800 // as project directory
Chris@0 801 #ifdef DSSI_RESERVED_CONFIGURE_PREFIX
Chris@51 802 if (QString(key.c_str()).startsWith(DSSI_RESERVED_CONFIGURE_PREFIX)) {
Chris@0 803 return qm;
Chris@0 804 }
Chris@0 805 #endif
Chris@0 806
Chris@0 807 if (message) {
Chris@0 808 if (m_descriptor->LADSPA_Plugin && m_descriptor->LADSPA_Plugin->Label) {
Chris@51 809 qm = std::string(m_descriptor->LADSPA_Plugin->Label) + ": ";
Chris@0 810 }
Chris@0 811 qm = qm + message;
Chris@0 812 free(message);
Chris@0 813 }
Chris@0 814
Chris@0 815 return qm;
Chris@0 816 }
Chris@0 817
Chris@0 818 void
Chris@0 819 DSSIPluginInstance::sendEvent(const RealTime &eventTime,
Chris@0 820 const void *e)
Chris@0 821 {
Chris@11 822 #ifdef DEBUG_DSSI_PROCESS
Chris@11 823 std::cerr << "DSSIPluginInstance::sendEvent: last was " << m_lastEventSendTime << " (valid " << m_haveLastEventSendTime << "), this is " << eventTime << std::endl;
Chris@11 824 #endif
Chris@11 825
Chris@11 826 // The process mechanism only works correctly if the events are
Chris@11 827 // sorted. It's the responsibility of the caller to ensure that:
Chris@11 828 // we will happily drop events here if we find the timeline going
Chris@11 829 // backwards.
Chris@10 830 if (m_haveLastEventSendTime &&
Chris@10 831 m_lastEventSendTime > eventTime) {
Chris@11 832 #ifdef DEBUG_DSSI_PROCESS
Chris@11 833 std::cerr << "... clearing down" << std::endl;
Chris@11 834 #endif
Chris@11 835 m_haveLastEventSendTime = false;
Chris@10 836 clearEvents();
Chris@10 837 }
Chris@10 838
Chris@0 839 snd_seq_event_t *event = (snd_seq_event_t *)e;
Chris@0 840 #ifdef DEBUG_DSSI_PROCESS
Chris@0 841 std::cerr << "DSSIPluginInstance::sendEvent at " << eventTime << std::endl;
Chris@0 842 #endif
Chris@0 843 snd_seq_event_t ev(*event);
Chris@0 844
Chris@0 845 ev.time.time.tv_sec = eventTime.sec;
Chris@0 846 ev.time.time.tv_nsec = eventTime.nsec;
Chris@0 847
Chris@0 848 // DSSI doesn't use MIDI channels, it uses run_multiple_synths instead.
Chris@0 849 ev.data.note.channel = 0;
Chris@0 850
Chris@0 851 m_eventBuffer.write(&ev, 1);
Chris@11 852
Chris@11 853 m_lastEventSendTime = eventTime;
Chris@11 854 m_haveLastEventSendTime = true;
Chris@0 855 }
Chris@0 856
Chris@10 857 void
Chris@10 858 DSSIPluginInstance::clearEvents()
Chris@10 859 {
Chris@10 860 m_haveLastEventSendTime = false;
Chris@10 861 m_eventBuffer.reset();
Chris@10 862 }
Chris@10 863
Chris@0 864 bool
Chris@0 865 DSSIPluginInstance::handleController(snd_seq_event_t *ev)
Chris@0 866 {
Chris@0 867 int controller = ev->data.control.param;
Chris@0 868
Chris@0 869 #ifdef DEBUG_DSSI_PROCESS
Chris@0 870 std::cerr << "DSSIPluginInstance::handleController " << controller << std::endl;
Chris@0 871 #endif
Chris@0 872
Chris@0 873 if (controller == 0) { // bank select MSB
Chris@0 874
Chris@0 875 m_pending.msb = ev->data.control.value;
Chris@0 876
Chris@0 877 } else if (controller == 32) { // bank select LSB
Chris@0 878
Chris@0 879 m_pending.lsb = ev->data.control.value;
Chris@0 880
Chris@0 881 } else if (controller > 0 && controller < 128) {
Chris@0 882
Chris@0 883 if (m_controllerMap.find(controller) != m_controllerMap.end()) {
Chris@0 884 int port = m_controllerMap[controller];
Chris@0 885 setPortValueFromController(port, ev->data.control.value);
Chris@0 886 } else {
Chris@0 887 return true; // pass through to plugin
Chris@0 888 }
Chris@0 889 }
Chris@0 890
Chris@0 891 return false;
Chris@0 892 }
Chris@0 893
Chris@0 894 void
Chris@0 895 DSSIPluginInstance::run(const RealTime &blockTime)
Chris@0 896 {
Chris@0 897 static snd_seq_event_t localEventBuffer[EVENT_BUFFER_SIZE];
Chris@0 898 int evCount = 0;
Chris@0 899
Chris@0 900 bool needLock = false;
Chris@0 901 if (m_descriptor->select_program) needLock = true;
Chris@0 902
Chris@0 903 if (needLock) {
Chris@0 904 if (!m_processLock.tryLock()) {
Chris@0 905 for (size_t ch = 0; ch < m_audioPortsOut.size(); ++ch) {
Chris@0 906 memset(m_outputBuffers[ch], 0, m_blockSize * sizeof(sample_t));
Chris@0 907 }
Chris@0 908 return;
Chris@0 909 }
Chris@0 910 }
Chris@0 911
Chris@0 912 if (m_grouped) {
Chris@0 913 runGrouped(blockTime);
Chris@0 914 goto done;
Chris@0 915 }
Chris@0 916
Chris@0 917 if (!m_descriptor || !m_descriptor->run_synth) {
Chris@0 918 m_eventBuffer.skip(m_eventBuffer.getReadSpace());
Chris@10 919 m_haveLastEventSendTime = false;
Chris@0 920 if (m_descriptor->LADSPA_Plugin->run) {
Chris@0 921 m_descriptor->LADSPA_Plugin->run(m_instanceHandle, m_blockSize);
Chris@0 922 } else {
Chris@0 923 for (size_t ch = 0; ch < m_audioPortsOut.size(); ++ch) {
Chris@0 924 memset(m_outputBuffers[ch], 0, m_blockSize * sizeof(sample_t));
Chris@0 925 }
Chris@0 926 }
Chris@0 927 m_run = true;
Chris@0 928 if (needLock) m_processLock.unlock();
Chris@0 929 return;
Chris@0 930 }
Chris@0 931
Chris@0 932 #ifdef DEBUG_DSSI_PROCESS
Chris@0 933 std::cerr << "DSSIPluginInstance::run(" << blockTime << ")" << std::endl;
Chris@0 934 #endif
Chris@0 935
Chris@0 936 #ifdef DEBUG_DSSI_PROCESS
Chris@0 937 if (m_eventBuffer.getReadSpace() > 0) {
Chris@0 938 std::cerr << "DSSIPluginInstance::run: event buffer has "
Chris@0 939 << m_eventBuffer.getReadSpace() << " event(s) in it" << std::endl;
Chris@0 940 }
Chris@0 941 #endif
Chris@0 942
Chris@0 943 while (m_eventBuffer.getReadSpace() > 0) {
Chris@0 944
Chris@0 945 snd_seq_event_t *ev = localEventBuffer + evCount;
Chris@0 946 *ev = m_eventBuffer.peekOne();
Chris@0 947 bool accept = true;
Chris@0 948
Chris@0 949 RealTime evTime(ev->time.time.tv_sec, ev->time.time.tv_nsec);
Chris@0 950
Chris@0 951 int frameOffset = 0;
Chris@0 952 if (evTime > blockTime) {
Chris@0 953 frameOffset = RealTime::realTime2Frame(evTime - blockTime, m_sampleRate);
Chris@0 954 }
Chris@0 955
Chris@0 956 #ifdef DEBUG_DSSI_PROCESS
Chris@0 957 std::cerr << "DSSIPluginInstance::run: evTime " << evTime << ", blockTime " << blockTime << ", frameOffset " << frameOffset
Chris@0 958 << ", blockSize " << m_blockSize << std::endl;
Chris@0 959 std::cerr << "Type: " << int(ev->type) << ", pitch: " << int(ev->data.note.note) << ", velocity: " << int(ev->data.note.velocity) << std::endl;
Chris@0 960 #endif
Chris@0 961
Chris@0 962 if (frameOffset >= int(m_blockSize)) break;
Chris@10 963 if (frameOffset < 0) {
Chris@10 964 frameOffset = 0;
Chris@10 965 if (ev->type == SND_SEQ_EVENT_NOTEON) {
Chris@10 966 m_eventBuffer.skip(1);
Chris@10 967 continue;
Chris@10 968 }
Chris@10 969 }
Chris@0 970
Chris@0 971 ev->time.tick = frameOffset;
Chris@0 972 m_eventBuffer.skip(1);
Chris@0 973
Chris@0 974 if (ev->type == SND_SEQ_EVENT_CONTROLLER) {
Chris@0 975 accept = handleController(ev);
Chris@0 976 } else if (ev->type == SND_SEQ_EVENT_PGMCHANGE) {
Chris@0 977 m_pending.program = ev->data.control.value;
Chris@0 978 accept = false;
Chris@0 979 }
Chris@0 980
Chris@0 981 if (accept) {
Chris@0 982 if (++evCount >= EVENT_BUFFER_SIZE) break;
Chris@0 983 }
Chris@0 984 }
Chris@0 985
Chris@0 986 if (m_pending.program >= 0 && m_descriptor->select_program) {
Chris@0 987
Chris@0 988 int program = m_pending.program;
Chris@0 989 int bank = m_pending.lsb + 128 * m_pending.msb;
Chris@0 990
Chris@0 991 #ifdef DEBUG_DSSI
Chris@0 992 std::cerr << "DSSIPluginInstance::run: making select_program(" << bank << "," << program << ") call" << std::endl;
Chris@0 993 #endif
Chris@0 994
Chris@0 995 m_pending.lsb = m_pending.msb = m_pending.program = -1;
Chris@0 996 m_descriptor->select_program(m_instanceHandle, bank, program);
Chris@0 997
Chris@0 998 #ifdef DEBUG_DSSI
Chris@0 999 std::cerr << "DSSIPluginInstance::run: made select_program(" << bank << "," << program << ") call" << std::endl;
Chris@0 1000 #endif
Chris@0 1001 }
Chris@0 1002
Chris@0 1003 #ifdef DEBUG_DSSI_PROCESS
Chris@0 1004 std::cerr << "DSSIPluginInstance::run: running with " << evCount << " events"
Chris@0 1005 << std::endl;
Chris@0 1006 #endif
Chris@0 1007
Chris@0 1008 m_descriptor->run_synth(m_instanceHandle, m_blockSize,
Chris@0 1009 localEventBuffer, evCount);
Chris@0 1010
Chris@0 1011 #ifdef DEBUG_DSSI_PROCESS
Chris@0 1012 // for (int i = 0; i < m_blockSize; ++i) {
Chris@0 1013 // std::cout << m_outputBuffers[0][i] << " ";
Chris@0 1014 // if (i % 8 == 0) std::cout << std::endl;
Chris@0 1015 // }
Chris@0 1016 #endif
Chris@0 1017
Chris@0 1018 done:
Chris@0 1019 if (needLock) m_processLock.unlock();
Chris@0 1020
Chris@0 1021 if (m_audioPortsOut.size() == 0) {
Chris@0 1022 // copy inputs to outputs
Chris@0 1023 for (size_t ch = 0; ch < m_idealChannelCount; ++ch) {
Chris@0 1024 size_t sch = ch % m_audioPortsIn.size();
Chris@0 1025 for (size_t i = 0; i < m_blockSize; ++i) {
Chris@0 1026 m_outputBuffers[ch][i] = m_inputBuffers[sch][i];
Chris@0 1027 }
Chris@0 1028 }
Chris@0 1029 } else if (m_idealChannelCount < m_audioPortsOut.size()) {
Chris@0 1030 if (m_idealChannelCount == 1) {
Chris@0 1031 // mix down to mono
Chris@0 1032 for (size_t ch = 1; ch < m_audioPortsOut.size(); ++ch) {
Chris@0 1033 for (size_t i = 0; i < m_blockSize; ++i) {
Chris@0 1034 m_outputBuffers[0][i] += m_outputBuffers[ch][i];
Chris@0 1035 }
Chris@0 1036 }
Chris@0 1037 }
Chris@0 1038 } else if (m_idealChannelCount > m_audioPortsOut.size()) {
Chris@0 1039 // duplicate
Chris@0 1040 for (size_t ch = m_audioPortsOut.size(); ch < m_idealChannelCount; ++ch) {
Chris@0 1041 size_t sch = (ch - m_audioPortsOut.size()) % m_audioPortsOut.size();
Chris@0 1042 for (size_t i = 0; i < m_blockSize; ++i) {
Chris@0 1043 m_outputBuffers[ch][i] = m_outputBuffers[sch][i];
Chris@0 1044 }
Chris@0 1045 }
Chris@0 1046 }
Chris@0 1047
Chris@0 1048 m_lastRunTime = blockTime;
Chris@0 1049 m_run = true;
Chris@0 1050 }
Chris@0 1051
Chris@0 1052 void
Chris@0 1053 DSSIPluginInstance::runGrouped(const RealTime &blockTime)
Chris@0 1054 {
Chris@0 1055 // If something else in our group has just been called for this
Chris@0 1056 // block time (but we haven't) then we should just write out the
Chris@0 1057 // results and return; if we have just been called for this block
Chris@0 1058 // time or nothing else in the group has been, we should run the
Chris@0 1059 // whole group.
Chris@0 1060
Chris@0 1061 bool needRun = true;
Chris@0 1062
Chris@0 1063 PluginSet &s = m_groupMap[m_identifier];
Chris@0 1064
Chris@0 1065 #ifdef DEBUG_DSSI_PROCESS
Chris@0 1066 std::cerr << "DSSIPluginInstance::runGrouped(" << blockTime << "): this is " << this << "; " << s.size() << " elements in m_groupMap[" << m_identifier << "]" << std::endl;
Chris@0 1067 #endif
Chris@0 1068
Chris@0 1069 if (m_lastRunTime != blockTime) {
Chris@0 1070 for (PluginSet::iterator i = s.begin(); i != s.end(); ++i) {
Chris@0 1071 DSSIPluginInstance *instance = *i;
Chris@0 1072 if (instance != this && instance->m_lastRunTime == blockTime) {
Chris@0 1073 #ifdef DEBUG_DSSI_PROCESS
Chris@0 1074 std::cerr << "DSSIPluginInstance::runGrouped(" << blockTime << "): plugin " << instance << " has already been run" << std::endl;
Chris@0 1075 #endif
Chris@0 1076 needRun = false;
Chris@0 1077 }
Chris@0 1078 }
Chris@0 1079 }
Chris@0 1080
Chris@0 1081 if (!needRun) {
Chris@0 1082 #ifdef DEBUG_DSSI_PROCESS
Chris@0 1083 std::cerr << "DSSIPluginInstance::runGrouped(" << blockTime << "): already run, returning" << std::endl;
Chris@0 1084 #endif
Chris@0 1085 return;
Chris@0 1086 }
Chris@0 1087
Chris@0 1088 #ifdef DEBUG_DSSI_PROCESS
Chris@0 1089 std::cerr << "DSSIPluginInstance::runGrouped(" << blockTime << "): I'm the first, running" << std::endl;
Chris@0 1090 #endif
Chris@0 1091
Chris@0 1092 size_t index = 0;
Chris@0 1093 unsigned long *counts = (unsigned long *)
Chris@0 1094 alloca(m_groupLocalEventBufferCount * sizeof(unsigned long));
Chris@0 1095 LADSPA_Handle *instances = (LADSPA_Handle *)
Chris@0 1096 alloca(m_groupLocalEventBufferCount * sizeof(LADSPA_Handle));
Chris@0 1097
Chris@0 1098 for (PluginSet::iterator i = s.begin(); i != s.end(); ++i) {
Chris@0 1099
Chris@0 1100 if (index >= m_groupLocalEventBufferCount) break;
Chris@0 1101
Chris@0 1102 DSSIPluginInstance *instance = *i;
Chris@0 1103 counts[index] = 0;
Chris@0 1104 instances[index] = instance->m_instanceHandle;
Chris@0 1105
Chris@0 1106 #ifdef DEBUG_DSSI_PROCESS
Chris@0 1107 std::cerr << "DSSIPluginInstance::runGrouped(" << blockTime << "): running " << instance << std::endl;
Chris@0 1108 #endif
Chris@0 1109
Chris@0 1110 if (instance->m_pending.program >= 0 &&
Chris@0 1111 instance->m_descriptor->select_program) {
Chris@0 1112 int program = instance->m_pending.program;
Chris@0 1113 int bank = instance->m_pending.lsb + 128 * instance->m_pending.msb;
Chris@0 1114 instance->m_pending.lsb = instance->m_pending.msb = instance->m_pending.program = -1;
Chris@0 1115 instance->m_descriptor->select_program
Chris@0 1116 (instance->m_instanceHandle, bank, program);
Chris@0 1117 }
Chris@0 1118
Chris@0 1119 while (instance->m_eventBuffer.getReadSpace() > 0) {
Chris@0 1120
Chris@0 1121 snd_seq_event_t *ev = m_groupLocalEventBuffers[index] + counts[index];
Chris@0 1122 *ev = instance->m_eventBuffer.peekOne();
Chris@0 1123 bool accept = true;
Chris@0 1124
Chris@0 1125 RealTime evTime(ev->time.time.tv_sec, ev->time.time.tv_nsec);
Chris@0 1126
Chris@0 1127 int frameOffset = 0;
Chris@0 1128 if (evTime > blockTime) {
Chris@0 1129 frameOffset = RealTime::realTime2Frame(evTime - blockTime, m_sampleRate);
Chris@0 1130 }
Chris@0 1131
Chris@0 1132 #ifdef DEBUG_DSSI_PROCESS
Chris@0 1133 std::cerr << "DSSIPluginInstance::runGrouped: evTime " << evTime << ", frameOffset " << frameOffset
Chris@0 1134 << ", block size " << m_blockSize << std::endl;
Chris@0 1135 #endif
Chris@0 1136
Chris@0 1137 if (frameOffset >= int(m_blockSize)) break;
Chris@0 1138 if (frameOffset < 0) frameOffset = 0;
Chris@0 1139
Chris@0 1140 ev->time.tick = frameOffset;
Chris@0 1141 instance->m_eventBuffer.skip(1);
Chris@0 1142
Chris@0 1143 if (ev->type == SND_SEQ_EVENT_CONTROLLER) {
Chris@0 1144 accept = instance->handleController(ev);
Chris@0 1145 } else if (ev->type == SND_SEQ_EVENT_PGMCHANGE) {
Chris@0 1146 instance->m_pending.program = ev->data.control.value;
Chris@0 1147 accept = false;
Chris@0 1148 }
Chris@0 1149
Chris@0 1150 if (accept) {
Chris@0 1151 if (++counts[index] >= EVENT_BUFFER_SIZE) break;
Chris@0 1152 }
Chris@0 1153 }
Chris@0 1154
Chris@0 1155 ++index;
Chris@0 1156 }
Chris@0 1157
Chris@0 1158 m_descriptor->run_multiple_synths(index,
Chris@0 1159 instances,
Chris@0 1160 m_blockSize,
Chris@0 1161 m_groupLocalEventBuffers,
Chris@0 1162 counts);
Chris@0 1163 }
Chris@0 1164
Chris@0 1165 int
Chris@0 1166 DSSIPluginInstance::requestMidiSend(LADSPA_Handle instance,
Chris@0 1167 unsigned char ports,
Chris@0 1168 unsigned char channels)
Chris@0 1169 {
Chris@0 1170 // This is called from a non-RT context (during instantiate)
Chris@0 1171
Chris@0 1172 std::cerr << "DSSIPluginInstance::requestMidiSend" << std::endl;
Chris@0 1173 return 1;
Chris@0 1174 }
Chris@0 1175
Chris@0 1176 void
Chris@0 1177 DSSIPluginInstance::midiSend(LADSPA_Handle instance,
Chris@0 1178 snd_seq_event_t *events,
Chris@0 1179 unsigned long eventCount)
Chris@0 1180 {
Chris@0 1181 // This is likely to be called from an RT context
Chris@0 1182
Chris@0 1183 std::cerr << "DSSIPluginInstance::midiSend" << std::endl;
Chris@0 1184 }
Chris@0 1185
Chris@0 1186 void
Chris@0 1187 DSSIPluginInstance::NonRTPluginThread::run()
Chris@0 1188 {
Chris@0 1189 while (!m_exiting) {
Chris@0 1190 m_runFunction(m_handle);
Chris@0 1191 usleep(100000);
Chris@0 1192 }
Chris@0 1193 }
Chris@0 1194
Chris@0 1195 int
Chris@0 1196 DSSIPluginInstance::requestNonRTThread(LADSPA_Handle instance,
Chris@0 1197 void (*runFunction)(LADSPA_Handle))
Chris@0 1198 {
Chris@0 1199 NonRTPluginThread *thread = new NonRTPluginThread(instance, runFunction);
Chris@0 1200 m_threads[instance].insert(thread);
Chris@0 1201 thread->start();
Chris@0 1202 return 0;
Chris@0 1203 }
Chris@0 1204
Chris@0 1205 void
Chris@0 1206 DSSIPluginInstance::deactivate()
Chris@0 1207 {
Chris@0 1208 #ifdef DEBUG_DSSI
Chris@0 1209 std::cerr << "DSSIPluginInstance::deactivate " << m_identifier << std::endl;
Chris@0 1210 #endif
Chris@0 1211 if (!m_descriptor || !m_descriptor->LADSPA_Plugin->deactivate) return;
Chris@0 1212
Chris@0 1213 for (size_t i = 0; i < m_backupControlPortsIn.size(); ++i) {
Chris@0 1214 m_backupControlPortsIn[i] = *m_controlPortsIn[i].second;
Chris@0 1215 }
Chris@0 1216
Chris@0 1217 m_descriptor->LADSPA_Plugin->deactivate(m_instanceHandle);
Chris@0 1218 #ifdef DEBUG_DSSI
Chris@0 1219 std::cerr << "DSSIPluginInstance::deactivate " << m_identifier << " done" << std::endl;
Chris@0 1220 #endif
Chris@0 1221
Chris@0 1222 m_bufferScavenger.scavenge();
Chris@0 1223 }
Chris@0 1224
Chris@0 1225 void
Chris@0 1226 DSSIPluginInstance::cleanup()
Chris@0 1227 {
Chris@0 1228 #ifdef DEBUG_DSSI
Chris@0 1229 std::cerr << "DSSIPluginInstance::cleanup " << m_identifier << std::endl;
Chris@0 1230 #endif
Chris@0 1231 if (!m_descriptor) return;
Chris@0 1232
Chris@0 1233 if (!m_descriptor->LADSPA_Plugin->cleanup) {
Chris@0 1234 std::cerr << "Bad plugin: plugin id "
Chris@0 1235 << m_descriptor->LADSPA_Plugin->UniqueID
Chris@0 1236 << ":" << m_descriptor->LADSPA_Plugin->Label
Chris@0 1237 << " has no cleanup method!" << std::endl;
Chris@0 1238 return;
Chris@0 1239 }
Chris@0 1240
Chris@0 1241 m_descriptor->LADSPA_Plugin->cleanup(m_instanceHandle);
Chris@0 1242 m_instanceHandle = 0;
Chris@0 1243 #ifdef DEBUG_DSSI
Chris@0 1244 std::cerr << "DSSIPluginInstance::cleanup " << m_identifier << " done" << std::endl;
Chris@0 1245 #endif
Chris@0 1246 }
Chris@0 1247