comparison plugin/LADSPAPluginInstance.cpp @ 1429:48e9f538e6e9

Untabify
author Chris Cannam
date Thu, 01 Mar 2018 18:02:22 +0000
parents a1cd5abcb38b
children 70e172e6cc59
comparison
equal deleted inserted replaced
1428:87ae75da6527 1429:48e9f538e6e9
32 32
33 #include <cmath> 33 #include <cmath>
34 34
35 35
36 LADSPAPluginInstance::LADSPAPluginInstance(RealTimePluginFactory *factory, 36 LADSPAPluginInstance::LADSPAPluginInstance(RealTimePluginFactory *factory,
37 int clientId, 37 int clientId,
38 QString identifier, 38 QString identifier,
39 int position, 39 int position,
40 sv_samplerate_t sampleRate, 40 sv_samplerate_t sampleRate,
41 int blockSize, 41 int blockSize,
42 int idealChannelCount, 42 int idealChannelCount,
43 const LADSPA_Descriptor* descriptor) : 43 const LADSPA_Descriptor* descriptor) :
44 RealTimePluginInstance(factory, identifier), 44 RealTimePluginInstance(factory, identifier),
45 m_client(clientId), 45 m_client(clientId),
46 m_position(position), 46 m_position(position),
47 m_instanceCount(0), 47 m_instanceCount(0),
65 } else { 65 } else {
66 m_outputBuffers = new sample_t*[m_instanceCount * m_audioPortsOut.size()]; 66 m_outputBuffers = new sample_t*[m_instanceCount * m_audioPortsOut.size()];
67 } 67 }
68 68
69 for (size_t i = 0; i < m_instanceCount * m_audioPortsIn.size(); ++i) { 69 for (size_t i = 0; i < m_instanceCount * m_audioPortsIn.size(); ++i) {
70 m_inputBuffers[i] = new sample_t[blockSize]; 70 m_inputBuffers[i] = new sample_t[blockSize];
71 } 71 }
72 for (size_t i = 0; i < m_instanceCount * m_audioPortsOut.size(); ++i) { 72 for (size_t i = 0; i < m_instanceCount * m_audioPortsOut.size(); ++i) {
73 m_outputBuffers[i] = new sample_t[blockSize]; 73 m_outputBuffers[i] = new sample_t[blockSize];
74 } 74 }
75 75
76 m_ownBuffers = true; 76 m_ownBuffers = true;
77 77
78 instantiate(sampleRate); 78 instantiate(sampleRate);
79 if (isOK()) { 79 if (isOK()) {
80 connectPorts(); 80 connectPorts();
81 activate(); 81 activate();
82 } 82 }
83 } 83 }
84 84
85 std::string 85 std::string
86 LADSPAPluginInstance::getIdentifier() const 86 LADSPAPluginInstance::getIdentifier() const
220 void 220 void
221 LADSPAPluginInstance::init(int idealChannelCount) 221 LADSPAPluginInstance::init(int idealChannelCount)
222 { 222 {
223 #ifdef DEBUG_LADSPA 223 #ifdef DEBUG_LADSPA
224 SVDEBUG << "LADSPAPluginInstance::init(" << idealChannelCount << "): plugin has " 224 SVDEBUG << "LADSPAPluginInstance::init(" << idealChannelCount << "): plugin has "
225 << m_descriptor->PortCount << " ports" << endl; 225 << m_descriptor->PortCount << " ports" << endl;
226 #endif 226 #endif
227 227
228 // Discover ports numbers and identities 228 // Discover ports numbers and identities
229 // 229 //
230 for (int i = 0; i < (int)m_descriptor->PortCount; ++i) { 230 for (int i = 0; i < (int)m_descriptor->PortCount; ++i) {
231 231
232 if (LADSPA_IS_PORT_AUDIO(m_descriptor->PortDescriptors[i])) { 232 if (LADSPA_IS_PORT_AUDIO(m_descriptor->PortDescriptors[i])) {
233 233
234 if (LADSPA_IS_PORT_INPUT(m_descriptor->PortDescriptors[i])) { 234 if (LADSPA_IS_PORT_INPUT(m_descriptor->PortDescriptors[i])) {
235 #ifdef DEBUG_LADSPA 235 #ifdef DEBUG_LADSPA
236 SVDEBUG << "LADSPAPluginInstance::init: port " << i << " is audio in" << endl; 236 SVDEBUG << "LADSPAPluginInstance::init: port " << i << " is audio in" << endl;
237 #endif 237 #endif
238 m_audioPortsIn.push_back(i); 238 m_audioPortsIn.push_back(i);
239 } else { 239 } else {
240 #ifdef DEBUG_LADSPA 240 #ifdef DEBUG_LADSPA
241 SVDEBUG << "LADSPAPluginInstance::init: port " << i << " is audio out" << endl; 241 SVDEBUG << "LADSPAPluginInstance::init: port " << i << " is audio out" << endl;
242 #endif 242 #endif
243 m_audioPortsOut.push_back(i); 243 m_audioPortsOut.push_back(i);
244 } 244 }
245 245
246 } else if (LADSPA_IS_PORT_CONTROL(m_descriptor->PortDescriptors[i])) { 246 } else if (LADSPA_IS_PORT_CONTROL(m_descriptor->PortDescriptors[i])) {
247 247
248 if (LADSPA_IS_PORT_INPUT(m_descriptor->PortDescriptors[i])) { 248 if (LADSPA_IS_PORT_INPUT(m_descriptor->PortDescriptors[i])) {
249 249
250 #ifdef DEBUG_LADSPA 250 #ifdef DEBUG_LADSPA
251 SVDEBUG << "LADSPAPluginInstance::init: port " << i << " is control in" << endl; 251 SVDEBUG << "LADSPAPluginInstance::init: port " << i << " is control in" << endl;
252 #endif 252 #endif
253 LADSPA_Data *data = new LADSPA_Data(0.0); 253 LADSPA_Data *data = new LADSPA_Data(0.0);
254 m_controlPortsIn.push_back( 254 m_controlPortsIn.push_back(
255 std::pair<unsigned long, LADSPA_Data*>(i, data)); 255 std::pair<unsigned long, LADSPA_Data*>(i, data));
256 256
257 } else { 257 } else {
258 258
259 #ifdef DEBUG_LADSPA 259 #ifdef DEBUG_LADSPA
260 SVDEBUG << "LADSPAPluginInstance::init: port " << i << " is control out" << endl; 260 SVDEBUG << "LADSPAPluginInstance::init: port " << i << " is control out" << endl;
261 #endif 261 #endif
262 LADSPA_Data *data = new LADSPA_Data(0.0); 262 LADSPA_Data *data = new LADSPA_Data(0.0);
263 m_controlPortsOut.push_back( 263 m_controlPortsOut.push_back(
264 std::pair<unsigned long, LADSPA_Data*>(i, data)); 264 std::pair<unsigned long, LADSPA_Data*>(i, data));
265 if (!strcmp(m_descriptor->PortNames[i], "latency") || 265 if (!strcmp(m_descriptor->PortNames[i], "latency") ||
266 !strcmp(m_descriptor->PortNames[i], "_latency")) { 266 !strcmp(m_descriptor->PortNames[i], "_latency")) {
267 #ifdef DEBUG_LADSPA 267 #ifdef DEBUG_LADSPA
268 cerr << "Wooo! We have a latency port!" << endl; 268 cerr << "Wooo! We have a latency port!" << endl;
269 #endif 269 #endif
270 m_latencyPort = data; 270 m_latencyPort = data;
271 } 271 }
272 272
273 } 273 }
274 } 274 }
275 #ifdef DEBUG_LADSPA 275 #ifdef DEBUG_LADSPA
276 else 276 else
277 SVDEBUG << "LADSPAPluginInstance::init - " 277 SVDEBUG << "LADSPAPluginInstance::init - "
278 << "unrecognised port type" << endl; 278 << "unrecognised port type" << endl;
280 } 280 }
281 281
282 m_instanceCount = 1; 282 m_instanceCount = 1;
283 283
284 if (idealChannelCount > 0) { 284 if (idealChannelCount > 0) {
285 if (m_audioPortsIn.size() == 1) { 285 if (m_audioPortsIn.size() == 1) {
286 // mono plugin: duplicate it if need be 286 // mono plugin: duplicate it if need be
287 m_instanceCount = idealChannelCount; 287 m_instanceCount = idealChannelCount;
288 } 288 }
289 } 289 }
290 } 290 }
291 291
292 sv_frame_t 292 sv_frame_t
293 LADSPAPluginInstance::getLatency() 293 LADSPAPluginInstance::getLatency()
294 { 294 {
295 if (m_latencyPort) { 295 if (m_latencyPort) {
296 if (!m_run) { 296 if (!m_run) {
297 for (int i = 0; i < getAudioInputCount(); ++i) { 297 for (int i = 0; i < getAudioInputCount(); ++i) {
298 for (int j = 0; j < m_blockSize; ++j) { 298 for (int j = 0; j < m_blockSize; ++j) {
299 m_inputBuffers[i][j] = 0.f; 299 m_inputBuffers[i][j] = 0.f;
300 } 300 }
301 } 301 }
302 run(Vamp::RealTime::zeroTime); 302 run(Vamp::RealTime::zeroTime);
303 } 303 }
304 if (*m_latencyPort > 0) return (sv_frame_t)*m_latencyPort; 304 if (*m_latencyPort > 0) return (sv_frame_t)*m_latencyPort;
305 } 305 }
306 return 0; 306 return 0;
307 } 307 }
308 308
309 void 309 void
310 LADSPAPluginInstance::silence() 310 LADSPAPluginInstance::silence()
311 { 311 {
312 if (isOK()) { 312 if (isOK()) {
313 deactivate(); 313 deactivate();
314 activate(); 314 activate();
315 } 315 }
316 } 316 }
317 317
318 void 318 void
319 LADSPAPluginInstance::setIdealChannelCount(int channels) 319 LADSPAPluginInstance::setIdealChannelCount(int channels)
320 { 320 {
321 if (m_audioPortsIn.size() != 1 || channels == m_instanceCount) { 321 if (m_audioPortsIn.size() != 1 || channels == m_instanceCount) {
322 silence(); 322 silence();
323 return; 323 return;
324 } 324 }
325 325
326 if (isOK()) { 326 if (isOK()) {
327 deactivate(); 327 deactivate();
328 } 328 }
329 329
330 //!!! don't we need to reallocate inputBuffers and outputBuffers? 330 //!!! don't we need to reallocate inputBuffers and outputBuffers?
331 331
332 cleanup(); 332 cleanup();
333 m_instanceCount = channels; 333 m_instanceCount = channels;
334 instantiate(m_sampleRate); 334 instantiate(m_sampleRate);
335 if (isOK()) { 335 if (isOK()) {
336 connectPorts(); 336 connectPorts();
337 activate(); 337 activate();
338 } 338 }
339 } 339 }
340 340
341 341
342 LADSPAPluginInstance::~LADSPAPluginInstance() 342 LADSPAPluginInstance::~LADSPAPluginInstance()
344 #ifdef DEBUG_LADSPA 344 #ifdef DEBUG_LADSPA
345 SVDEBUG << "LADSPAPluginInstance::~LADSPAPluginInstance" << endl; 345 SVDEBUG << "LADSPAPluginInstance::~LADSPAPluginInstance" << endl;
346 #endif 346 #endif
347 347
348 if (m_instanceHandles.size() != 0) { // "isOK()" 348 if (m_instanceHandles.size() != 0) { // "isOK()"
349 deactivate(); 349 deactivate();
350 } 350 }
351 351
352 cleanup(); 352 cleanup();
353 353
354 for (unsigned int i = 0; i < m_controlPortsIn.size(); ++i) 354 for (unsigned int i = 0; i < m_controlPortsIn.size(); ++i)
359 359
360 m_controlPortsIn.clear(); 360 m_controlPortsIn.clear();
361 m_controlPortsOut.clear(); 361 m_controlPortsOut.clear();
362 362
363 if (m_ownBuffers) { 363 if (m_ownBuffers) {
364 for (size_t i = 0; i < m_instanceCount * m_audioPortsIn.size(); ++i) { 364 for (size_t i = 0; i < m_instanceCount * m_audioPortsIn.size(); ++i) {
365 delete[] m_inputBuffers[i]; 365 delete[] m_inputBuffers[i];
366 } 366 }
367 for (size_t i = 0; i < m_instanceCount * m_audioPortsOut.size(); ++i) { 367 for (size_t i = 0; i < m_instanceCount * m_audioPortsOut.size(); ++i) {
368 delete[] m_outputBuffers[i]; 368 delete[] m_outputBuffers[i];
369 } 369 }
370 370
371 delete[] m_inputBuffers; 371 delete[] m_inputBuffers;
372 delete[] m_outputBuffers; 372 delete[] m_outputBuffers;
373 } 373 }
374 374
375 m_audioPortsIn.clear(); 375 m_audioPortsIn.clear();
376 m_audioPortsOut.clear(); 376 m_audioPortsOut.clear();
377 } 377 }
386 cout << "LADSPAPluginInstance::instantiate - plugin unique id = " 386 cout << "LADSPAPluginInstance::instantiate - plugin unique id = "
387 << m_descriptor->UniqueID << endl; 387 << m_descriptor->UniqueID << endl;
388 #endif 388 #endif
389 389
390 if (!m_descriptor->instantiate) { 390 if (!m_descriptor->instantiate) {
391 cerr << "Bad plugin: plugin id " << m_descriptor->UniqueID 391 cerr << "Bad plugin: plugin id " << m_descriptor->UniqueID
392 << ":" << m_descriptor->Label 392 << ":" << m_descriptor->Label
393 << " has no instantiate method!" << endl; 393 << " has no instantiate method!" << endl;
394 return; 394 return;
395 } 395 }
396 396
397 unsigned long pluginRate = (unsigned long)(sampleRate); 397 unsigned long pluginRate = (unsigned long)(sampleRate);
398 if (sampleRate != sv_samplerate_t(pluginRate)) { 398 if (sampleRate != sv_samplerate_t(pluginRate)) {
399 cerr << "LADSPAPluginInstance: WARNING: Non-integer sample rate " 399 cerr << "LADSPAPluginInstance: WARNING: Non-integer sample rate "
400 << sampleRate << " presented, rounding to " << pluginRate 400 << sampleRate << " presented, rounding to " << pluginRate
401 << endl; 401 << endl;
402 } 402 }
403 403
404 for (int i = 0; i < m_instanceCount; ++i) { 404 for (int i = 0; i < m_instanceCount; ++i) {
405 m_instanceHandles.push_back 405 m_instanceHandles.push_back
406 (m_descriptor->instantiate(m_descriptor, pluginRate)); 406 (m_descriptor->instantiate(m_descriptor, pluginRate));
407 } 407 }
408 } 408 }
409 409
410 void 410 void
411 LADSPAPluginInstance::activate() 411 LADSPAPluginInstance::activate()
412 { 412 {
413 if (!m_descriptor || !m_descriptor->activate) return; 413 if (!m_descriptor || !m_descriptor->activate) return;
414 414
415 for (std::vector<LADSPA_Handle>::iterator hi = m_instanceHandles.begin(); 415 for (std::vector<LADSPA_Handle>::iterator hi = m_instanceHandles.begin();
416 hi != m_instanceHandles.end(); ++hi) { 416 hi != m_instanceHandles.end(); ++hi) {
417 m_descriptor->activate(*hi); 417 m_descriptor->activate(*hi);
418 } 418 }
419 } 419 }
420 420
421 void 421 void
422 LADSPAPluginInstance::connectPorts() 422 LADSPAPluginInstance::connectPorts()
428 428
429 LADSPAPluginFactory *f = dynamic_cast<LADSPAPluginFactory *>(m_factory); 429 LADSPAPluginFactory *f = dynamic_cast<LADSPAPluginFactory *>(m_factory);
430 int inbuf = 0, outbuf = 0; 430 int inbuf = 0, outbuf = 0;
431 431
432 for (std::vector<LADSPA_Handle>::iterator hi = m_instanceHandles.begin(); 432 for (std::vector<LADSPA_Handle>::iterator hi = m_instanceHandles.begin();
433 hi != m_instanceHandles.end(); ++hi) { 433 hi != m_instanceHandles.end(); ++hi) {
434 434
435 for (unsigned int i = 0; i < m_audioPortsIn.size(); ++i) { 435 for (unsigned int i = 0; i < m_audioPortsIn.size(); ++i) {
436 m_descriptor->connect_port(*hi, 436 m_descriptor->connect_port(*hi,
437 m_audioPortsIn[i], 437 m_audioPortsIn[i],
438 (LADSPA_Data *)m_inputBuffers[inbuf]); 438 (LADSPA_Data *)m_inputBuffers[inbuf]);
439 ++inbuf; 439 ++inbuf;
440 } 440 }
441 441
442 for (unsigned int i = 0; i < m_audioPortsOut.size(); ++i) { 442 for (unsigned int i = 0; i < m_audioPortsOut.size(); ++i) {
443 m_descriptor->connect_port(*hi, 443 m_descriptor->connect_port(*hi,
444 m_audioPortsOut[i], 444 m_audioPortsOut[i],
445 (LADSPA_Data *)m_outputBuffers[outbuf]); 445 (LADSPA_Data *)m_outputBuffers[outbuf]);
446 ++outbuf; 446 ++outbuf;
447 } 447 }
448 448
449 // If there is more than one instance, they all share the same 449 // If there is more than one instance, they all share the same
450 // control port ins (and outs, for the moment, because we 450 // control port ins (and outs, for the moment, because we
451 // don't actually do anything with the outs anyway -- but they 451 // don't actually do anything with the outs anyway -- but they
452 // do have to be connected as the plugin can't know if they're 452 // do have to be connected as the plugin can't know if they're
453 // not and will write to them anyway). 453 // not and will write to them anyway).
454 454
455 for (unsigned int i = 0; i < m_controlPortsIn.size(); ++i) { 455 for (unsigned int i = 0; i < m_controlPortsIn.size(); ++i) {
456 m_descriptor->connect_port(*hi, 456 m_descriptor->connect_port(*hi,
457 m_controlPortsIn[i].first, 457 m_controlPortsIn[i].first,
458 m_controlPortsIn[i].second); 458 m_controlPortsIn[i].second);
459 if (f) { 459 if (f) {
460 float defaultValue = f->getPortDefault 460 float defaultValue = f->getPortDefault
461 (m_descriptor, m_controlPortsIn[i].first); 461 (m_descriptor, m_controlPortsIn[i].first);
462 *m_controlPortsIn[i].second = defaultValue; 462 *m_controlPortsIn[i].second = defaultValue;
463 } 463 }
464 } 464 }
465 465
466 for (unsigned int i = 0; i < m_controlPortsOut.size(); ++i) { 466 for (unsigned int i = 0; i < m_controlPortsOut.size(); ++i) {
467 m_descriptor->connect_port(*hi, 467 m_descriptor->connect_port(*hi,
468 m_controlPortsOut[i].first, 468 m_controlPortsOut[i].first,
469 m_controlPortsOut[i].second); 469 m_controlPortsOut[i].second);
470 } 470 }
471 } 471 }
472 } 472 }
473 473
474 int 474 int
475 LADSPAPluginInstance::getParameterCount() const 475 LADSPAPluginInstance::getParameterCount() const
484 484
485 unsigned int portNumber = m_controlPortsIn[parameter].first; 485 unsigned int portNumber = m_controlPortsIn[parameter].first;
486 486
487 LADSPAPluginFactory *f = dynamic_cast<LADSPAPluginFactory *>(m_factory); 487 LADSPAPluginFactory *f = dynamic_cast<LADSPAPluginFactory *>(m_factory);
488 if (f) { 488 if (f) {
489 if (value < f->getPortMinimum(m_descriptor, portNumber)) { 489 if (value < f->getPortMinimum(m_descriptor, portNumber)) {
490 value = f->getPortMinimum(m_descriptor, portNumber); 490 value = f->getPortMinimum(m_descriptor, portNumber);
491 } 491 }
492 if (value > f->getPortMaximum(m_descriptor, portNumber)) { 492 if (value > f->getPortMaximum(m_descriptor, portNumber)) {
493 value = f->getPortMaximum(m_descriptor, portNumber); 493 value = f->getPortMaximum(m_descriptor, portNumber);
494 } 494 }
495 } 495 }
496 496
497 (*m_controlPortsIn[parameter].second) = value; 497 (*m_controlPortsIn[parameter].second) = value;
498 } 498 }
499 499
516 { 516 {
517 if (!in_range_for(m_controlPortsIn, parameter)) return 0.0; 517 if (!in_range_for(m_controlPortsIn, parameter)) return 0.0;
518 518
519 LADSPAPluginFactory *f = dynamic_cast<LADSPAPluginFactory *>(m_factory); 519 LADSPAPluginFactory *f = dynamic_cast<LADSPAPluginFactory *>(m_factory);
520 if (f) { 520 if (f) {
521 return f->getPortDefault(m_descriptor, m_controlPortsIn[parameter].first); 521 return f->getPortDefault(m_descriptor, m_controlPortsIn[parameter].first);
522 } else { 522 } else {
523 return 0.0f; 523 return 0.0f;
524 } 524 }
525 } 525 }
526 526
527 int 527 int
528 LADSPAPluginInstance::getParameterDisplayHint(int parameter) const 528 LADSPAPluginInstance::getParameterDisplayHint(int parameter) const
529 { 529 {
530 if (!in_range_for(m_controlPortsIn, parameter)) return 0.0; 530 if (!in_range_for(m_controlPortsIn, parameter)) return 0.0;
531 531
532 LADSPAPluginFactory *f = dynamic_cast<LADSPAPluginFactory *>(m_factory); 532 LADSPAPluginFactory *f = dynamic_cast<LADSPAPluginFactory *>(m_factory);
533 if (f) { 533 if (f) {
534 return f->getPortDisplayHint(m_descriptor, m_controlPortsIn[parameter].first); 534 return f->getPortDisplayHint(m_descriptor, m_controlPortsIn[parameter].first);
535 } else { 535 } else {
536 return PortHint::NoHint; 536 return PortHint::NoHint;
537 } 537 }
538 } 538 }
539 539
540 void 540 void
541 LADSPAPluginInstance::run(const RealTime &, int count) 541 LADSPAPluginInstance::run(const RealTime &, int count)
543 if (!m_descriptor || !m_descriptor->run) return; 543 if (!m_descriptor || !m_descriptor->run) return;
544 544
545 if (count == 0) count = m_blockSize; 545 if (count == 0) count = m_blockSize;
546 546
547 for (std::vector<LADSPA_Handle>::iterator hi = m_instanceHandles.begin(); 547 for (std::vector<LADSPA_Handle>::iterator hi = m_instanceHandles.begin();
548 hi != m_instanceHandles.end(); ++hi) { 548 hi != m_instanceHandles.end(); ++hi) {
549 549
550 m_descriptor->run(*hi, count); 550 m_descriptor->run(*hi, count);
551 } 551 }
552 552
553 m_run = true; 553 m_run = true;
557 LADSPAPluginInstance::deactivate() 557 LADSPAPluginInstance::deactivate()
558 { 558 {
559 if (!m_descriptor || !m_descriptor->deactivate) return; 559 if (!m_descriptor || !m_descriptor->deactivate) return;
560 560
561 for (std::vector<LADSPA_Handle>::iterator hi = m_instanceHandles.begin(); 561 for (std::vector<LADSPA_Handle>::iterator hi = m_instanceHandles.begin();
562 hi != m_instanceHandles.end(); ++hi) { 562 hi != m_instanceHandles.end(); ++hi) {
563 m_descriptor->deactivate(*hi); 563 m_descriptor->deactivate(*hi);
564 } 564 }
565 } 565 }
566 566
567 void 567 void
568 LADSPAPluginInstance::cleanup() 568 LADSPAPluginInstance::cleanup()
569 { 569 {
570 if (!m_descriptor) return; 570 if (!m_descriptor) return;
571 571
572 if (!m_descriptor->cleanup) { 572 if (!m_descriptor->cleanup) {
573 cerr << "Bad plugin: plugin id " << m_descriptor->UniqueID 573 cerr << "Bad plugin: plugin id " << m_descriptor->UniqueID
574 << ":" << m_descriptor->Label 574 << ":" << m_descriptor->Label
575 << " has no cleanup method!" << endl; 575 << " has no cleanup method!" << endl;
576 return; 576 return;
577 } 577 }
578 578
579 for (std::vector<LADSPA_Handle>::iterator hi = m_instanceHandles.begin(); 579 for (std::vector<LADSPA_Handle>::iterator hi = m_instanceHandles.begin();
580 hi != m_instanceHandles.end(); ++hi) { 580 hi != m_instanceHandles.end(); ++hi) {
581 m_descriptor->cleanup(*hi); 581 m_descriptor->cleanup(*hi);
582 } 582 }
583 583
584 m_instanceHandles.clear(); 584 m_instanceHandles.clear();
585 } 585 }
586 586