comparison plugin/DSSIPluginInstance.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
52 Scavenger<ScavengerArrayWrapper<snd_seq_event_t *> > DSSIPluginInstance::m_bufferScavenger(2, 10); 52 Scavenger<ScavengerArrayWrapper<snd_seq_event_t *> > DSSIPluginInstance::m_bufferScavenger(2, 10);
53 std::map<LADSPA_Handle, std::set<DSSIPluginInstance::NonRTPluginThread *> > DSSIPluginInstance::m_threads; 53 std::map<LADSPA_Handle, std::set<DSSIPluginInstance::NonRTPluginThread *> > DSSIPluginInstance::m_threads;
54 54
55 55
56 DSSIPluginInstance::DSSIPluginInstance(RealTimePluginFactory *factory, 56 DSSIPluginInstance::DSSIPluginInstance(RealTimePluginFactory *factory,
57 int clientId, 57 int clientId,
58 QString identifier, 58 QString identifier,
59 int position, 59 int position,
60 sv_samplerate_t sampleRate, 60 sv_samplerate_t sampleRate,
61 int blockSize, 61 int blockSize,
62 int idealChannelCount, 62 int idealChannelCount,
63 const DSSI_Descriptor* descriptor) : 63 const DSSI_Descriptor* descriptor) :
64 RealTimePluginInstance(factory, identifier), 64 RealTimePluginInstance(factory, identifier),
65 m_client(clientId), 65 m_client(clientId),
66 m_position(position), 66 m_position(position),
67 m_instanceHandle(0), 67 m_instanceHandle(0),
68 m_descriptor(descriptor), 68 m_descriptor(descriptor),
77 m_grouped(false), 77 m_grouped(false),
78 m_haveLastEventSendTime(false) 78 m_haveLastEventSendTime(false)
79 { 79 {
80 #ifdef DEBUG_DSSI 80 #ifdef DEBUG_DSSI
81 SVDEBUG << "DSSIPluginInstance::DSSIPluginInstance(" << identifier << ")" 81 SVDEBUG << "DSSIPluginInstance::DSSIPluginInstance(" << identifier << ")"
82 << endl; 82 << endl;
83 #endif 83 #endif
84 84
85 init(); 85 init();
86 86
87 m_inputBuffers = new sample_t*[m_audioPortsIn.size()]; 87 m_inputBuffers = new sample_t*[m_audioPortsIn.size()];
88 m_outputBuffers = new sample_t*[m_outputBufferCount]; 88 m_outputBuffers = new sample_t*[m_outputBufferCount];
89 89
90 for (size_t i = 0; i < m_audioPortsIn.size(); ++i) { 90 for (size_t i = 0; i < m_audioPortsIn.size(); ++i) {
91 m_inputBuffers[i] = new sample_t[blockSize]; 91 m_inputBuffers[i] = new sample_t[blockSize];
92 } 92 }
93 for (int i = 0; i < m_outputBufferCount; ++i) { 93 for (int i = 0; i < m_outputBufferCount; ++i) {
94 m_outputBuffers[i] = new sample_t[blockSize]; 94 m_outputBuffers[i] = new sample_t[blockSize];
95 } 95 }
96 96
97 m_ownBuffers = true; 97 m_ownBuffers = true;
98 98
99 m_pending.lsb = m_pending.msb = m_pending.program = -1; 99 m_pending.lsb = m_pending.msb = m_pending.program = -1;
100 100
101 instantiate(sampleRate); 101 instantiate(sampleRate);
102 if (isOK()) { 102 if (isOK()) {
103 connectPorts(); 103 connectPorts();
104 activate(); 104 activate();
105 initialiseGroupMembership(); 105 initialiseGroupMembership();
106 } 106 }
107 } 107 }
108 108
109 std::string 109 std::string
110 DSSIPluginInstance::getIdentifier() const 110 DSSIPluginInstance::getIdentifier() const
227 { 227 {
228 if (LADSPA_IS_PORT_AUDIO(descriptor->PortDescriptors[i])) 228 if (LADSPA_IS_PORT_AUDIO(descriptor->PortDescriptors[i]))
229 { 229 {
230 if (LADSPA_IS_PORT_INPUT(descriptor->PortDescriptors[i])) { 230 if (LADSPA_IS_PORT_INPUT(descriptor->PortDescriptors[i])) {
231 m_audioPortsIn.push_back(i); 231 m_audioPortsIn.push_back(i);
232 } else { 232 } else {
233 m_audioPortsOut.push_back(i); 233 m_audioPortsOut.push_back(i);
234 } 234 }
235 } 235 }
236 else 236 else
237 if (LADSPA_IS_PORT_CONTROL(descriptor->PortDescriptors[i])) 237 if (LADSPA_IS_PORT_CONTROL(descriptor->PortDescriptors[i]))
238 { 238 {
239 if (LADSPA_IS_PORT_INPUT(descriptor->PortDescriptors[i])) { 239 if (LADSPA_IS_PORT_INPUT(descriptor->PortDescriptors[i])) {
240 240
241 LADSPA_Data *data = new LADSPA_Data(0.0); 241 LADSPA_Data *data = new LADSPA_Data(0.0);
242 242
243 m_controlPortsIn.push_back(std::pair<long, LADSPA_Data*> 243 m_controlPortsIn.push_back(std::pair<long, LADSPA_Data*>
244 (i, data)); 244 (i, data));
245 245
246 m_backupControlPortsIn.push_back(0.0); 246 m_backupControlPortsIn.push_back(0.0);
247 247
248 } else { 248 } else {
249 LADSPA_Data *data = new LADSPA_Data(0.0); 249 LADSPA_Data *data = new LADSPA_Data(0.0);
250 m_controlPortsOut.push_back( 250 m_controlPortsOut.push_back(
251 std::pair<long, LADSPA_Data*>(i, data)); 251 std::pair<long, LADSPA_Data*>(i, data));
252 if (!strcmp(descriptor->PortNames[i], "latency") || 252 if (!strcmp(descriptor->PortNames[i], "latency") ||
253 !strcmp(descriptor->PortNames[i], "_latency")) { 253 !strcmp(descriptor->PortNames[i], "_latency")) {
254 #ifdef DEBUG_DSSI 254 #ifdef DEBUG_DSSI
255 cerr << "Wooo! We have a latency port!" << endl; 255 cerr << "Wooo! We have a latency port!" << endl;
256 #endif 256 #endif
257 m_latencyPort = data; 257 m_latencyPort = data;
258 } 258 }
259 } 259 }
260 } 260 }
261 #ifdef DEBUG_DSSI 261 #ifdef DEBUG_DSSI
262 else 262 else
263 SVDEBUG << "DSSIPluginInstance::DSSIPluginInstance - " 263 SVDEBUG << "DSSIPluginInstance::DSSIPluginInstance - "
264 << "unrecognised port type" << endl; 264 << "unrecognised port type" << endl;
277 #ifdef DEBUG_DSSI_PROCESS 277 #ifdef DEBUG_DSSI_PROCESS
278 SVDEBUG << "DSSIPluginInstance::getLatency(): m_latencyPort " << m_latencyPort << ", m_run " << m_run << endl; 278 SVDEBUG << "DSSIPluginInstance::getLatency(): m_latencyPort " << m_latencyPort << ", m_run " << m_run << endl;
279 #endif 279 #endif
280 280
281 if (m_latencyPort) { 281 if (m_latencyPort) {
282 if (!m_run) { 282 if (!m_run) {
283 for (int i = 0; i < getAudioInputCount(); ++i) { 283 for (int i = 0; i < getAudioInputCount(); ++i) {
284 for (int j = 0; j < m_blockSize; ++j) { 284 for (int j = 0; j < m_blockSize; ++j) {
285 m_inputBuffers[i][j] = 0.f; 285 m_inputBuffers[i][j] = 0.f;
286 } 286 }
287 } 287 }
288 run(Vamp::RealTime::zeroTime); 288 run(Vamp::RealTime::zeroTime);
289 } 289 }
290 latency = (sv_frame_t)(*m_latencyPort + 0.1); 290 latency = (sv_frame_t)(*m_latencyPort + 0.1);
291 } 291 }
292 292
293 #ifdef DEBUG_DSSI_PROCESS 293 #ifdef DEBUG_DSSI_PROCESS
294 SVDEBUG << "DSSIPluginInstance::getLatency(): latency is " << latency << endl; 294 SVDEBUG << "DSSIPluginInstance::getLatency(): latency is " << latency << endl;
295 #endif 295 #endif
299 299
300 void 300 void
301 DSSIPluginInstance::silence() 301 DSSIPluginInstance::silence()
302 { 302 {
303 if (m_instanceHandle != 0) { 303 if (m_instanceHandle != 0) {
304 deactivate(); 304 deactivate();
305 activate(); 305 activate();
306 } 306 }
307 } 307 }
308 308
309 void 309 void
310 DSSIPluginInstance::discardEvents() 310 DSSIPluginInstance::discardEvents()
315 void 315 void
316 DSSIPluginInstance::setIdealChannelCount(int channels) 316 DSSIPluginInstance::setIdealChannelCount(int channels)
317 { 317 {
318 #ifdef DEBUG_DSSI 318 #ifdef DEBUG_DSSI
319 SVDEBUG << "DSSIPluginInstance::setIdealChannelCount: channel count " 319 SVDEBUG << "DSSIPluginInstance::setIdealChannelCount: channel count "
320 << channels << " (was " << m_idealChannelCount << ")" << endl; 320 << channels << " (was " << m_idealChannelCount << ")" << endl;
321 #endif 321 #endif
322 322
323 if (channels == m_idealChannelCount) { 323 if (channels == m_idealChannelCount) {
324 silence(); 324 silence();
325 return; 325 return;
326 } 326 }
327 327
328 if (m_instanceHandle != 0) { 328 if (m_instanceHandle != 0) {
329 deactivate(); 329 deactivate();
330 } 330 }
331 331
332 m_idealChannelCount = channels; 332 m_idealChannelCount = channels;
333 333
334 if (channels > m_outputBufferCount) { 334 if (channels > m_outputBufferCount) {
335 335
336 for (int i = 0; i < m_outputBufferCount; ++i) { 336 for (int i = 0; i < m_outputBufferCount; ++i) {
337 delete[] m_outputBuffers[i]; 337 delete[] m_outputBuffers[i];
338 } 338 }
339 339
340 delete[] m_outputBuffers; 340 delete[] m_outputBuffers;
341 341
342 m_outputBufferCount = channels; 342 m_outputBufferCount = channels;
343 343
344 m_outputBuffers = new sample_t*[m_outputBufferCount]; 344 m_outputBuffers = new sample_t*[m_outputBufferCount];
345 345
346 for (int i = 0; i < m_outputBufferCount; ++i) { 346 for (int i = 0; i < m_outputBufferCount; ++i) {
347 m_outputBuffers[i] = new sample_t[m_blockSize]; 347 m_outputBuffers[i] = new sample_t[m_blockSize];
348 } 348 }
349 349
350 connectPorts(); 350 connectPorts();
351 } 351 }
352 352
353 if (m_instanceHandle != 0) { 353 if (m_instanceHandle != 0) {
354 activate(); 354 activate();
355 } 355 }
356 } 356 }
357 357
358 void 358 void
359 DSSIPluginInstance::detachFromGroup() 359 DSSIPluginInstance::detachFromGroup()
365 365
366 void 366 void
367 DSSIPluginInstance::initialiseGroupMembership() 367 DSSIPluginInstance::initialiseGroupMembership()
368 { 368 {
369 if (!m_descriptor->run_multiple_synths) { 369 if (!m_descriptor->run_multiple_synths) {
370 m_grouped = false; 370 m_grouped = false;
371 return; 371 return;
372 } 372 }
373 373
374 //!!! GroupMap is not actually thread-safe. 374 //!!! GroupMap is not actually thread-safe.
375 375
376 size_t pluginsInGroup = m_groupMap[m_identifier].size(); 376 size_t pluginsInGroup = m_groupMap[m_identifier].size();
377 377
378 if (++pluginsInGroup > m_groupLocalEventBufferCount) { 378 if (++pluginsInGroup > m_groupLocalEventBufferCount) {
379 379
380 size_t nextBufferCount = pluginsInGroup * 2; 380 size_t nextBufferCount = pluginsInGroup * 2;
381 381
382 snd_seq_event_t **eventLocalBuffers = new snd_seq_event_t *[nextBufferCount]; 382 snd_seq_event_t **eventLocalBuffers = new snd_seq_event_t *[nextBufferCount];
383 383
384 for (size_t i = 0; i < m_groupLocalEventBufferCount; ++i) { 384 for (size_t i = 0; i < m_groupLocalEventBufferCount; ++i) {
385 eventLocalBuffers[i] = m_groupLocalEventBuffers[i]; 385 eventLocalBuffers[i] = m_groupLocalEventBuffers[i];
386 } 386 }
387 for (size_t i = m_groupLocalEventBufferCount; i < nextBufferCount; ++i) { 387 for (size_t i = m_groupLocalEventBufferCount; i < nextBufferCount; ++i) {
388 eventLocalBuffers[i] = new snd_seq_event_t[EVENT_BUFFER_SIZE]; 388 eventLocalBuffers[i] = new snd_seq_event_t[EVENT_BUFFER_SIZE];
389 } 389 }
390 390
391 if (m_groupLocalEventBuffers) { 391 if (m_groupLocalEventBuffers) {
392 m_bufferScavenger.claim(new ScavengerArrayWrapper<snd_seq_event_t *> 392 m_bufferScavenger.claim(new ScavengerArrayWrapper<snd_seq_event_t *>
393 (m_groupLocalEventBuffers)); 393 (m_groupLocalEventBuffers));
394 } 394 }
395 395
396 m_groupLocalEventBuffers = eventLocalBuffers; 396 m_groupLocalEventBuffers = eventLocalBuffers;
397 m_groupLocalEventBufferCount = nextBufferCount; 397 m_groupLocalEventBufferCount = nextBufferCount;
398 } 398 }
399 399
400 m_grouped = true; 400 m_grouped = true;
401 m_groupMap[m_identifier].insert(this); 401 m_groupMap[m_identifier].insert(this);
402 } 402 }
407 SVDEBUG << "DSSIPluginInstance::~DSSIPluginInstance" << endl; 407 SVDEBUG << "DSSIPluginInstance::~DSSIPluginInstance" << endl;
408 #endif 408 #endif
409 409
410 if (m_threads.find(m_instanceHandle) != m_threads.end()) { 410 if (m_threads.find(m_instanceHandle) != m_threads.end()) {
411 411
412 for (std::set<NonRTPluginThread *>::iterator i = 412 for (std::set<NonRTPluginThread *>::iterator i =
413 m_threads[m_instanceHandle].begin(); 413 m_threads[m_instanceHandle].begin();
414 i != m_threads[m_instanceHandle].end(); ++i) { 414 i != m_threads[m_instanceHandle].end(); ++i) {
415 415
416 (*i)->setExiting(); 416 (*i)->setExiting();
417 (*i)->wait(); 417 (*i)->wait();
418 delete *i; 418 delete *i;
419 } 419 }
420 420
421 m_threads.erase(m_instanceHandle); 421 m_threads.erase(m_instanceHandle);
422 } 422 }
423 423
424 detachFromGroup(); 424 detachFromGroup();
425 425
426 if (m_instanceHandle != 0) { 426 if (m_instanceHandle != 0) {
427 deactivate(); 427 deactivate();
428 } 428 }
429 429
430 cleanup(); 430 cleanup();
431 431
432 for (int i = 0; in_range_for(m_controlPortsIn, i); ++i) 432 for (int i = 0; in_range_for(m_controlPortsIn, i); ++i)
437 437
438 m_controlPortsIn.clear(); 438 m_controlPortsIn.clear();
439 m_controlPortsOut.clear(); 439 m_controlPortsOut.clear();
440 440
441 if (m_ownBuffers) { 441 if (m_ownBuffers) {
442 for (int i = 0; i < getAudioInputCount(); ++i) { 442 for (int i = 0; i < getAudioInputCount(); ++i) {
443 delete[] m_inputBuffers[i]; 443 delete[] m_inputBuffers[i];
444 } 444 }
445 for (int i = 0; i < m_outputBufferCount; ++i) { 445 for (int i = 0; i < m_outputBufferCount; ++i) {
446 delete[] m_outputBuffers[i]; 446 delete[] m_outputBuffers[i];
447 } 447 }
448 448
449 delete[] m_inputBuffers; 449 delete[] m_inputBuffers;
450 delete[] m_outputBuffers; 450 delete[] m_outputBuffers;
451 } 451 }
452 452
453 m_audioPortsIn.clear(); 453 m_audioPortsIn.clear();
454 m_audioPortsOut.clear(); 454 m_audioPortsOut.clear();
455 } 455 }
465 << m_descriptor->LADSPA_Plugin->UniqueID << endl; 465 << m_descriptor->LADSPA_Plugin->UniqueID << endl;
466 #endif 466 #endif
467 const LADSPA_Descriptor *descriptor = m_descriptor->LADSPA_Plugin; 467 const LADSPA_Descriptor *descriptor = m_descriptor->LADSPA_Plugin;
468 468
469 if (!descriptor->instantiate) { 469 if (!descriptor->instantiate) {
470 cerr << "Bad plugin: plugin id " << descriptor->UniqueID 470 cerr << "Bad plugin: plugin id " << descriptor->UniqueID
471 << ":" << descriptor->Label 471 << ":" << descriptor->Label
472 << " has no instantiate method!" << endl; 472 << " has no instantiate method!" << endl;
473 return; 473 return;
474 } 474 }
475 475
476 unsigned long pluginRate = (unsigned long)(sampleRate); 476 unsigned long pluginRate = (unsigned long)(sampleRate);
477 if (sampleRate != sv_samplerate_t(pluginRate)) { 477 if (sampleRate != sv_samplerate_t(pluginRate)) {
478 cerr << "DSSIPluginInstance: WARNING: Non-integer sample rate " 478 cerr << "DSSIPluginInstance: WARNING: Non-integer sample rate "
481 } 481 }
482 m_instanceHandle = descriptor->instantiate(descriptor, pluginRate); 482 m_instanceHandle = descriptor->instantiate(descriptor, pluginRate);
483 483
484 if (m_instanceHandle) { 484 if (m_instanceHandle) {
485 485
486 if (m_descriptor->get_midi_controller_for_port) { 486 if (m_descriptor->get_midi_controller_for_port) {
487 487
488 for (int i = 0; i < (int)descriptor->PortCount; ++i) { 488 for (int i = 0; i < (int)descriptor->PortCount; ++i) {
489 489
490 if (LADSPA_IS_PORT_CONTROL(descriptor->PortDescriptors[i]) && 490 if (LADSPA_IS_PORT_CONTROL(descriptor->PortDescriptors[i]) &&
491 LADSPA_IS_PORT_INPUT(descriptor->PortDescriptors[i])) { 491 LADSPA_IS_PORT_INPUT(descriptor->PortDescriptors[i])) {
492 492
493 int controller = m_descriptor->get_midi_controller_for_port 493 int controller = m_descriptor->get_midi_controller_for_port
494 (m_instanceHandle, i); 494 (m_instanceHandle, i);
495 495
496 if (controller != 0 && controller != 32 && 496 if (controller != 0 && controller != 32 &&
497 DSSI_IS_CC(controller)) { 497 DSSI_IS_CC(controller)) {
498 498
499 m_controllerMap[DSSI_CC_NUMBER(controller)] = i; 499 m_controllerMap[DSSI_CC_NUMBER(controller)] = i;
500 } 500 }
501 } 501 }
502 } 502 }
503 } 503 }
504 } 504 }
505 } 505 }
506 506
507 void 507 void
508 DSSIPluginInstance::checkProgramCache() const 508 DSSIPluginInstance::checkProgramCache() const
513 #ifdef DEBUG_DSSI 513 #ifdef DEBUG_DSSI
514 SVDEBUG << "DSSIPluginInstance::checkProgramCache" << endl; 514 SVDEBUG << "DSSIPluginInstance::checkProgramCache" << endl;
515 #endif 515 #endif
516 516
517 if (!m_descriptor || !m_descriptor->get_program) { 517 if (!m_descriptor || !m_descriptor->get_program) {
518 m_programCacheValid = true; 518 m_programCacheValid = true;
519 return; 519 return;
520 } 520 }
521 521
522 int index = 0; 522 int index = 0;
523 const DSSI_Program_Descriptor *programDescriptor; 523 const DSSI_Program_Descriptor *programDescriptor;
524 while ((programDescriptor = m_descriptor->get_program(m_instanceHandle, index))) { 524 while ((programDescriptor = m_descriptor->get_program(m_instanceHandle, index))) {
525 ++index; 525 ++index;
526 ProgramDescriptor d; 526 ProgramDescriptor d;
527 d.bank = (int)programDescriptor->Bank; 527 d.bank = (int)programDescriptor->Bank;
528 d.program = (int)programDescriptor->Program; 528 d.program = (int)programDescriptor->Program;
529 d.name = programDescriptor->Name; 529 d.name = programDescriptor->Name;
530 m_cachedPrograms.push_back(d); 530 m_cachedPrograms.push_back(d);
531 } 531 }
532 532
533 #ifdef DEBUG_DSSI 533 #ifdef DEBUG_DSSI
534 SVDEBUG << "DSSIPluginInstance::checkProgramCache: have " << m_cachedPrograms.size() << " programs" << endl; 534 SVDEBUG << "DSSIPluginInstance::checkProgramCache: have " << m_cachedPrograms.size() << " programs" << endl;
535 #endif 535 #endif
549 checkProgramCache(); 549 checkProgramCache();
550 550
551 ProgramList programs; 551 ProgramList programs;
552 552
553 for (std::vector<ProgramDescriptor>::iterator i = m_cachedPrograms.begin(); 553 for (std::vector<ProgramDescriptor>::iterator i = m_cachedPrograms.begin();
554 i != m_cachedPrograms.end(); ++i) { 554 i != m_cachedPrograms.end(); ++i) {
555 programs.push_back(i->name); 555 programs.push_back(i->name);
556 } 556 }
557 557
558 return programs; 558 return programs;
559 } 559 }
560 560
568 if (!m_descriptor) return std::string(); 568 if (!m_descriptor) return std::string();
569 569
570 checkProgramCache(); 570 checkProgramCache();
571 571
572 for (std::vector<ProgramDescriptor>::iterator i = m_cachedPrograms.begin(); 572 for (std::vector<ProgramDescriptor>::iterator i = m_cachedPrograms.begin();
573 i != m_cachedPrograms.end(); ++i) { 573 i != m_cachedPrograms.end(); ++i) {
574 if (i->bank == bank && i->program == program) return i->name; 574 if (i->bank == bank && i->program == program) return i->name;
575 } 575 }
576 576
577 return std::string(); 577 return std::string();
578 } 578 }
579 579
589 checkProgramCache(); 589 checkProgramCache();
590 590
591 int rv; 591 int rv;
592 592
593 for (std::vector<ProgramDescriptor>::iterator i = m_cachedPrograms.begin(); 593 for (std::vector<ProgramDescriptor>::iterator i = m_cachedPrograms.begin();
594 i != m_cachedPrograms.end(); ++i) { 594 i != m_cachedPrograms.end(); ++i) {
595 if (i->name == name) { 595 if (i->name == name) {
596 rv = i->bank; 596 rv = i->bank;
597 rv = (rv << 16) + i->program; 597 rv = (rv << 16) + i->program;
598 return rv; 598 return rv;
599 } 599 }
600 } 600 }
601 601
602 return 0; 602 return 0;
603 } 603 }
604 604
629 629
630 bool found = false; 630 bool found = false;
631 int bankNo = 0, programNo = 0; 631 int bankNo = 0, programNo = 0;
632 632
633 for (std::vector<ProgramDescriptor>::iterator i = m_cachedPrograms.begin(); 633 for (std::vector<ProgramDescriptor>::iterator i = m_cachedPrograms.begin();
634 i != m_cachedPrograms.end(); ++i) { 634 i != m_cachedPrograms.end(); ++i) {
635 635
636 if (i->name == program) { 636 if (i->name == program) {
637 637
638 bankNo = i->bank; 638 bankNo = i->bank;
639 programNo = i->program; 639 programNo = i->program;
640 found = true; 640 found = true;
641 641
642 #ifdef DEBUG_DSSI 642 #ifdef DEBUG_DSSI
643 SVDEBUG << "DSSIPluginInstance::selectProgram(" << program << "): found at bank " << bankNo << ", program " << programNo << endl; 643 SVDEBUG << "DSSIPluginInstance::selectProgram(" << program << "): found at bank " << bankNo << ", program " << programNo << endl;
644 #endif 644 #endif
645 645
646 break; 646 break;
647 } 647 }
648 } 648 }
649 649
650 if (!found) return; 650 if (!found) return;
651 m_program = program; 651 m_program = program;
652 652
658 #ifdef DEBUG_DSSI 658 #ifdef DEBUG_DSSI
659 SVDEBUG << "DSSIPluginInstance::selectProgram(" << program << "): made select_program(" << bankNo << "," << programNo << ") call" << endl; 659 SVDEBUG << "DSSIPluginInstance::selectProgram(" << program << "): made select_program(" << bankNo << "," << programNo << ") call" << endl;
660 #endif 660 #endif
661 661
662 if (backupPortValues) { 662 if (backupPortValues) {
663 for (size_t i = 0; i < m_backupControlPortsIn.size(); ++i) { 663 for (size_t i = 0; i < m_backupControlPortsIn.size(); ++i) {
664 m_backupControlPortsIn[i] = *m_controlPortsIn[i].second; 664 m_backupControlPortsIn[i] = *m_controlPortsIn[i].second;
665 } 665 }
666 } 666 }
667 } 667 }
668 668
669 void 669 void
670 DSSIPluginInstance::activate() 670 DSSIPluginInstance::activate()
676 if (!m_descriptor || !m_descriptor->LADSPA_Plugin->activate) return; 676 if (!m_descriptor || !m_descriptor->LADSPA_Plugin->activate) return;
677 m_descriptor->LADSPA_Plugin->activate(m_instanceHandle); 677 m_descriptor->LADSPA_Plugin->activate(m_instanceHandle);
678 678
679 if (m_program != "") { 679 if (m_program != "") {
680 #ifdef DEBUG_DSSI 680 #ifdef DEBUG_DSSI
681 SVDEBUG << "DSSIPluginInstance::activate: restoring program " << m_program << endl; 681 SVDEBUG << "DSSIPluginInstance::activate: restoring program " << m_program << endl;
682 #endif 682 #endif
683 selectProgramAux(m_program, false); 683 selectProgramAux(m_program, false);
684 } 684 }
685 685
686 for (size_t i = 0; i < m_backupControlPortsIn.size(); ++i) { 686 for (size_t i = 0; i < m_backupControlPortsIn.size(); ++i) {
687 #ifdef DEBUG_DSSI 687 #ifdef DEBUG_DSSI
688 SVDEBUG << "DSSIPluginInstance::activate: setting port " << m_controlPortsIn[i].first << " to " << m_backupControlPortsIn[i] << endl; 688 SVDEBUG << "DSSIPluginInstance::activate: setting port " << m_controlPortsIn[i].first << " to " << m_backupControlPortsIn[i] << endl;
689 #endif 689 #endif
690 *m_controlPortsIn[i].second = m_backupControlPortsIn[i]; 690 *m_controlPortsIn[i].second = m_backupControlPortsIn[i];
691 } 691 }
692 } 692 }
693 693
694 void 694 void
695 DSSIPluginInstance::connectPorts() 695 DSSIPluginInstance::connectPorts()
696 { 696 {
697 if (!m_descriptor || !m_descriptor->LADSPA_Plugin->connect_port) return; 697 if (!m_descriptor || !m_descriptor->LADSPA_Plugin->connect_port) return;
698 #ifdef DEBUG_DSSI 698 #ifdef DEBUG_DSSI
699 SVDEBUG << "DSSIPluginInstance::connectPorts: " << getAudioInputCount() 699 SVDEBUG << "DSSIPluginInstance::connectPorts: " << getAudioInputCount()
700 << " audio ports in, " << m_audioPortsOut.size() << " out, " 700 << " audio ports in, " << m_audioPortsOut.size() << " out, "
701 << m_outputBufferCount << " output buffers" << endl; 701 << m_outputBufferCount << " output buffers" << endl;
702 #endif 702 #endif
703 703
704 assert(sizeof(LADSPA_Data) == sizeof(float)); 704 assert(sizeof(LADSPA_Data) == sizeof(float));
705 assert(sizeof(sample_t) == sizeof(float)); 705 assert(sizeof(sample_t) == sizeof(float));
706 706
707 LADSPAPluginFactory *f = dynamic_cast<LADSPAPluginFactory *>(m_factory); 707 LADSPAPluginFactory *f = dynamic_cast<LADSPAPluginFactory *>(m_factory);
708 int inbuf = 0, outbuf = 0; 708 int inbuf = 0, outbuf = 0;
709 709
710 for (int i = 0; i < getAudioInputCount(); ++i) { 710 for (int i = 0; i < getAudioInputCount(); ++i) {
711 m_descriptor->LADSPA_Plugin->connect_port 711 m_descriptor->LADSPA_Plugin->connect_port
712 (m_instanceHandle, 712 (m_instanceHandle,
713 m_audioPortsIn[i], 713 m_audioPortsIn[i],
714 (LADSPA_Data *)m_inputBuffers[inbuf]); 714 (LADSPA_Data *)m_inputBuffers[inbuf]);
715 ++inbuf; 715 ++inbuf;
716 } 716 }
717 717
718 for (size_t i = 0; i < m_audioPortsOut.size(); ++i) { 718 for (size_t i = 0; i < m_audioPortsOut.size(); ++i) {
719 m_descriptor->LADSPA_Plugin->connect_port 719 m_descriptor->LADSPA_Plugin->connect_port
720 (m_instanceHandle, 720 (m_instanceHandle,
721 m_audioPortsOut[i], 721 m_audioPortsOut[i],
722 (LADSPA_Data *)m_outputBuffers[outbuf]); 722 (LADSPA_Data *)m_outputBuffers[outbuf]);
723 ++outbuf; 723 ++outbuf;
724 } 724 }
725 725
726 for (size_t i = 0; i < m_controlPortsIn.size(); ++i) { 726 for (size_t i = 0; i < m_controlPortsIn.size(); ++i) {
727 m_descriptor->LADSPA_Plugin->connect_port 727 m_descriptor->LADSPA_Plugin->connect_port
728 (m_instanceHandle, 728 (m_instanceHandle,
729 m_controlPortsIn[i].first, 729 m_controlPortsIn[i].first,
730 m_controlPortsIn[i].second); 730 m_controlPortsIn[i].second);
731 731
732 if (f) { 732 if (f) {
733 float defaultValue = f->getPortDefault 733 float defaultValue = f->getPortDefault
734 (m_descriptor->LADSPA_Plugin, (int)m_controlPortsIn[i].first); 734 (m_descriptor->LADSPA_Plugin, (int)m_controlPortsIn[i].first);
735 *m_controlPortsIn[i].second = defaultValue; 735 *m_controlPortsIn[i].second = defaultValue;
739 #endif 739 #endif
740 } 740 }
741 } 741 }
742 742
743 for (size_t i = 0; i < m_controlPortsOut.size(); ++i) { 743 for (size_t i = 0; i < m_controlPortsOut.size(); ++i) {
744 m_descriptor->LADSPA_Plugin->connect_port 744 m_descriptor->LADSPA_Plugin->connect_port
745 (m_instanceHandle, 745 (m_instanceHandle,
746 m_controlPortsOut[i].first, 746 m_controlPortsOut[i].first,
747 m_controlPortsOut[i].second); 747 m_controlPortsOut[i].second);
748 } 748 }
749 } 749 }
750 750
751 int 751 int
752 DSSIPluginInstance::getParameterCount() const 752 DSSIPluginInstance::getParameterCount() const
764 764
765 int portNumber = m_controlPortsIn[parameter].first; 765 int portNumber = m_controlPortsIn[parameter].first;
766 766
767 LADSPAPluginFactory *f = dynamic_cast<LADSPAPluginFactory *>(m_factory); 767 LADSPAPluginFactory *f = dynamic_cast<LADSPAPluginFactory *>(m_factory);
768 if (f) { 768 if (f) {
769 if (value < f->getPortMinimum(m_descriptor->LADSPA_Plugin, portNumber)) { 769 if (value < f->getPortMinimum(m_descriptor->LADSPA_Plugin, portNumber)) {
770 value = f->getPortMinimum(m_descriptor->LADSPA_Plugin, portNumber); 770 value = f->getPortMinimum(m_descriptor->LADSPA_Plugin, portNumber);
771 } 771 }
772 if (value > f->getPortMaximum(m_descriptor->LADSPA_Plugin, portNumber)) { 772 if (value > f->getPortMaximum(m_descriptor->LADSPA_Plugin, portNumber)) {
773 value = f->getPortMaximum(m_descriptor->LADSPA_Plugin, portNumber); 773 value = f->getPortMaximum(m_descriptor->LADSPA_Plugin, portNumber);
774 } 774 }
775 } 775 }
776 776
777 (*m_controlPortsIn[parameter].second) = value; 777 (*m_controlPortsIn[parameter].second) = value;
778 m_backupControlPortsIn[parameter] = value; 778 m_backupControlPortsIn[parameter] = value;
779 } 779 }
791 LADSPA_Data ub = p->PortRangeHints[port].UpperBound; 791 LADSPA_Data ub = p->PortRangeHints[port].UpperBound;
792 792
793 float value = (float)cv; 793 float value = (float)cv;
794 794
795 if (!LADSPA_IS_HINT_BOUNDED_BELOW(d)) { 795 if (!LADSPA_IS_HINT_BOUNDED_BELOW(d)) {
796 if (!LADSPA_IS_HINT_BOUNDED_ABOVE(d)) { 796 if (!LADSPA_IS_HINT_BOUNDED_ABOVE(d)) {
797 /* unbounded: might as well leave the value alone. */ 797 /* unbounded: might as well leave the value alone. */
798 } else { 798 } else {
799 /* bounded above only. just shift the range. */ 799 /* bounded above only. just shift the range. */
800 value = ub - 127.0f + value; 800 value = ub - 127.0f + value;
801 } 801 }
802 } else { 802 } else {
803 if (!LADSPA_IS_HINT_BOUNDED_ABOVE(d)) { 803 if (!LADSPA_IS_HINT_BOUNDED_ABOVE(d)) {
804 /* bounded below only. just shift the range. */ 804 /* bounded below only. just shift the range. */
805 value = lb + value; 805 value = lb + value;
806 } else { 806 } else {
807 /* bounded both ends. more interesting. */ 807 /* bounded both ends. more interesting. */
808 /* XXX !!! todo: fill in logarithmic, sample rate &c */ 808 /* XXX !!! todo: fill in logarithmic, sample rate &c */
809 value = lb + ((ub - lb) * value / 127.0f); 809 value = lb + ((ub - lb) * value / 127.0f);
810 } 810 }
811 } 811 }
812 812
813 for (int i = 0; in_range_for(m_controlPortsIn, i); ++i) { 813 for (int i = 0; in_range_for(m_controlPortsIn, i); ++i) {
814 if (m_controlPortsIn[i].first == port) { 814 if (m_controlPortsIn[i].first == port) {
815 setParameterValue(i, value); 815 setParameterValue(i, value);
816 } 816 }
817 } 817 }
818 } 818 }
819 819
820 float 820 float
821 DSSIPluginInstance::getControlOutputValue(int output) const 821 DSSIPluginInstance::getControlOutputValue(int output) const
839 { 839 {
840 if (!in_range_for(m_controlPortsIn, parameter)) return 0.0; 840 if (!in_range_for(m_controlPortsIn, parameter)) return 0.0;
841 841
842 LADSPAPluginFactory *f = dynamic_cast<LADSPAPluginFactory *>(m_factory); 842 LADSPAPluginFactory *f = dynamic_cast<LADSPAPluginFactory *>(m_factory);
843 if (f) { 843 if (f) {
844 return f->getPortDefault(m_descriptor->LADSPA_Plugin, 844 return f->getPortDefault(m_descriptor->LADSPA_Plugin,
845 m_controlPortsIn[parameter].first); 845 m_controlPortsIn[parameter].first);
846 } else { 846 } else {
847 return 0.0f; 847 return 0.0f;
848 } 848 }
849 } 849 }
850 850
851 int 851 int
852 DSSIPluginInstance::getParameterDisplayHint(int parameter) const 852 DSSIPluginInstance::getParameterDisplayHint(int parameter) const
853 { 853 {
854 if (!in_range_for(m_controlPortsIn, parameter)) return 0.0; 854 if (!in_range_for(m_controlPortsIn, parameter)) return 0.0;
855 855
856 LADSPAPluginFactory *f = dynamic_cast<LADSPAPluginFactory *>(m_factory); 856 LADSPAPluginFactory *f = dynamic_cast<LADSPAPluginFactory *>(m_factory);
857 if (f) { 857 if (f) {
858 return f->getPortDisplayHint(m_descriptor->LADSPA_Plugin, 858 return f->getPortDisplayHint(m_descriptor->LADSPA_Plugin,
859 m_controlPortsIn[parameter].first); 859 m_controlPortsIn[parameter].first);
860 } else { 860 } else {
861 return PortHint::NoHint; 861 return PortHint::NoHint;
862 } 862 }
863 } 863 }
864 864
865 std::string 865 std::string
866 DSSIPluginInstance::configure(std::string key, 866 DSSIPluginInstance::configure(std::string key,
867 std::string value) 867 std::string value)
868 { 868 {
869 if (!m_descriptor || !m_descriptor->configure) return std::string(); 869 if (!m_descriptor || !m_descriptor->configure) return std::string();
870 870
871 if (key == PluginIdentifier::RESERVED_PROJECT_DIRECTORY_KEY.toStdString()) { 871 if (key == PluginIdentifier::RESERVED_PROJECT_DIRECTORY_KEY.toStdString()) {
872 #ifdef DSSI_PROJECT_DIRECTORY_KEY 872 #ifdef DSSI_PROJECT_DIRECTORY_KEY
873 key = DSSI_PROJECT_DIRECTORY_KEY; 873 key = DSSI_PROJECT_DIRECTORY_KEY;
874 #else 874 #else
875 return std::string(); 875 return std::string();
876 #endif 876 #endif
877 } 877 }
878 878
879 879
880 #ifdef DEBUG_DSSI 880 #ifdef DEBUG_DSSI
881 SVDEBUG << "DSSIPluginInstance::configure(" << key << "," << value << ")" << endl; 881 SVDEBUG << "DSSIPluginInstance::configure(" << key << "," << value << ")" << endl;
882 #endif 882 #endif
883 883
884 char *message = m_descriptor->configure(m_instanceHandle, 884 char *message = m_descriptor->configure(m_instanceHandle,
885 key.c_str(), 885 key.c_str(),
886 value.c_str()); 886 value.c_str());
887 887
888 m_programCacheValid = false; 888 m_programCacheValid = false;
889 889
890 m_configurationData[key] = value; 890 m_configurationData[key] = value;
891 891
893 893
894 // Ignore return values from reserved key configuration calls such 894 // Ignore return values from reserved key configuration calls such
895 // as project directory 895 // as project directory
896 #ifdef DSSI_RESERVED_CONFIGURE_PREFIX 896 #ifdef DSSI_RESERVED_CONFIGURE_PREFIX
897 if (QString(key.c_str()).startsWith(DSSI_RESERVED_CONFIGURE_PREFIX)) { 897 if (QString(key.c_str()).startsWith(DSSI_RESERVED_CONFIGURE_PREFIX)) {
898 return qm; 898 return qm;
899 } 899 }
900 #endif 900 #endif
901 901
902 if (message) { 902 if (message) {
903 if (m_descriptor->LADSPA_Plugin && m_descriptor->LADSPA_Plugin->Label) { 903 if (m_descriptor->LADSPA_Plugin && m_descriptor->LADSPA_Plugin->Label) {
904 qm = std::string(m_descriptor->LADSPA_Plugin->Label) + ": "; 904 qm = std::string(m_descriptor->LADSPA_Plugin->Label) + ": ";
905 } 905 }
906 qm = qm + message; 906 qm = qm + message;
907 free(message); 907 free(message);
908 908
909 cerr << "DSSIPluginInstance::configure: warning: configure returned message: \"" << qm << "\"" << endl; 909 cerr << "DSSIPluginInstance::configure: warning: configure returned message: \"" << qm << "\"" << endl;
910 } 910 }
911 911
912 return qm; 912 return qm;
913 } 913 }
914 914
915 void 915 void
916 DSSIPluginInstance::sendEvent(const RealTime &eventTime, 916 DSSIPluginInstance::sendEvent(const RealTime &eventTime,
917 const void *e) 917 const void *e)
918 { 918 {
919 #ifdef DEBUG_DSSI_PROCESS 919 #ifdef DEBUG_DSSI_PROCESS
920 SVDEBUG << "DSSIPluginInstance::sendEvent: last was " << m_lastEventSendTime << " (valid " << m_haveLastEventSendTime << "), this is " << eventTime << endl; 920 SVDEBUG << "DSSIPluginInstance::sendEvent: last was " << m_lastEventSendTime << " (valid " << m_haveLastEventSendTime << "), this is " << eventTime << endl;
921 #endif 921 #endif
922 922
923 // The process mechanism only works correctly if the events are 923 // The process mechanism only works correctly if the events are
924 // sorted. It's the responsibility of the caller to ensure that: 924 // sorted. It's the responsibility of the caller to ensure that:
925 // we will happily drop events here if we find the timeline going 925 // we will happily drop events here if we find the timeline going
926 // backwards. 926 // backwards.
927 if (m_haveLastEventSendTime && 927 if (m_haveLastEventSendTime &&
928 m_lastEventSendTime > eventTime) { 928 m_lastEventSendTime > eventTime) {
929 #ifdef DEBUG_DSSI_PROCESS 929 #ifdef DEBUG_DSSI_PROCESS
930 cerr << "... clearing down" << endl; 930 cerr << "... clearing down" << endl;
931 #endif 931 #endif
932 m_haveLastEventSendTime = false; 932 m_haveLastEventSendTime = false;
933 clearEvents(); 933 clearEvents();
934 } 934 }
935 935
936 snd_seq_event_t *event = (snd_seq_event_t *)e; 936 snd_seq_event_t *event = (snd_seq_event_t *)e;
937 #ifdef DEBUG_DSSI_PROCESS 937 #ifdef DEBUG_DSSI_PROCESS
938 SVDEBUG << "DSSIPluginInstance::sendEvent at " << eventTime << endl; 938 SVDEBUG << "DSSIPluginInstance::sendEvent at " << eventTime << endl;
966 #ifdef DEBUG_DSSI_PROCESS 966 #ifdef DEBUG_DSSI_PROCESS
967 SVDEBUG << "DSSIPluginInstance::handleController " << controller << endl; 967 SVDEBUG << "DSSIPluginInstance::handleController " << controller << endl;
968 #endif 968 #endif
969 969
970 if (controller == 0) { // bank select MSB 970 if (controller == 0) { // bank select MSB
971 971
972 m_pending.msb = ev->data.control.value; 972 m_pending.msb = ev->data.control.value;
973 973
974 } else if (controller == 32) { // bank select LSB 974 } else if (controller == 32) { // bank select LSB
975 975
976 m_pending.lsb = ev->data.control.value; 976 m_pending.lsb = ev->data.control.value;
977 977
978 } else if (controller > 0 && controller < 128) { 978 } else if (controller > 0 && controller < 128) {
979 979
980 if (m_controllerMap.find(controller) != m_controllerMap.end()) { 980 if (m_controllerMap.find(controller) != m_controllerMap.end()) {
981 int port = m_controllerMap[controller]; 981 int port = m_controllerMap[controller];
982 setPortValueFromController(port, ev->data.control.value); 982 setPortValueFromController(port, ev->data.control.value);
983 } else { 983 } else {
984 return true; // pass through to plugin 984 return true; // pass through to plugin
985 } 985 }
986 } 986 }
987 987
988 return false; 988 return false;
989 } 989 }
990 990
998 998
999 bool needLock = false; 999 bool needLock = false;
1000 if (m_descriptor && m_descriptor->select_program) needLock = true; 1000 if (m_descriptor && m_descriptor->select_program) needLock = true;
1001 1001
1002 if (needLock) { 1002 if (needLock) {
1003 if (!m_processLock.tryLock()) { 1003 if (!m_processLock.tryLock()) {
1004 for (size_t ch = 0; ch < m_audioPortsOut.size(); ++ch) { 1004 for (size_t ch = 0; ch < m_audioPortsOut.size(); ++ch) {
1005 memset(m_outputBuffers[ch], 0, m_blockSize * sizeof(sample_t)); 1005 memset(m_outputBuffers[ch], 0, m_blockSize * sizeof(sample_t));
1006 } 1006 }
1007 return; 1007 return;
1008 } 1008 }
1009 } 1009 }
1010 1010
1011 if (m_grouped) { 1011 if (m_grouped) {
1012 runGrouped(blockTime); 1012 runGrouped(blockTime);
1013 goto done; 1013 goto done;
1014 } 1014 }
1015 1015
1016 if (!m_descriptor || !m_descriptor->run_synth) { 1016 if (!m_descriptor || !m_descriptor->run_synth) {
1017 m_eventBuffer.skip(m_eventBuffer.getReadSpace()); 1017 m_eventBuffer.skip(m_eventBuffer.getReadSpace());
1018 m_haveLastEventSendTime = false; 1018 m_haveLastEventSendTime = false;
1019 if (m_descriptor && m_descriptor->LADSPA_Plugin->run) { 1019 if (m_descriptor && m_descriptor->LADSPA_Plugin->run) {
1020 m_descriptor->LADSPA_Plugin->run(m_instanceHandle, count); 1020 m_descriptor->LADSPA_Plugin->run(m_instanceHandle, count);
1021 } else { 1021 } else {
1022 for (size_t ch = 0; ch < m_audioPortsOut.size(); ++ch) { 1022 for (size_t ch = 0; ch < m_audioPortsOut.size(); ++ch) {
1023 memset(m_outputBuffers[ch], 0, m_blockSize * sizeof(sample_t)); 1023 memset(m_outputBuffers[ch], 0, m_blockSize * sizeof(sample_t));
1024 } 1024 }
1025 } 1025 }
1026 m_run = true; 1026 m_run = true;
1027 if (needLock) m_processLock.unlock(); 1027 if (needLock) m_processLock.unlock();
1028 return; 1028 return;
1029 } 1029 }
1030 1030
1031 #ifdef DEBUG_DSSI_PROCESS 1031 #ifdef DEBUG_DSSI_PROCESS
1032 SVDEBUG << "DSSIPluginInstance::run(" << blockTime << ")" << endl; 1032 SVDEBUG << "DSSIPluginInstance::run(" << blockTime << ")" << endl;
1033 #endif 1033 #endif
1034 1034
1035 #ifdef DEBUG_DSSI_PROCESS 1035 #ifdef DEBUG_DSSI_PROCESS
1036 if (m_eventBuffer.getReadSpace() > 0) { 1036 if (m_eventBuffer.getReadSpace() > 0) {
1037 SVDEBUG << "DSSIPluginInstance::run: event buffer has " 1037 SVDEBUG << "DSSIPluginInstance::run: event buffer has "
1038 << m_eventBuffer.getReadSpace() << " event(s) in it" << endl; 1038 << m_eventBuffer.getReadSpace() << " event(s) in it" << endl;
1039 } 1039 }
1040 #endif 1040 #endif
1041 1041
1042 while (m_eventBuffer.getReadSpace() > 0) { 1042 while (m_eventBuffer.getReadSpace() > 0) {
1043 1043
1044 snd_seq_event_t *ev = localEventBuffer + evCount; 1044 snd_seq_event_t *ev = localEventBuffer + evCount;
1045 *ev = m_eventBuffer.peekOne(); 1045 *ev = m_eventBuffer.peekOne();
1046 bool accept = true; 1046 bool accept = true;
1047 1047
1048 RealTime evTime(ev->time.time.tv_sec, ev->time.time.tv_nsec); 1048 RealTime evTime(ev->time.time.tv_sec, ev->time.time.tv_nsec);
1049 1049
1050 sv_frame_t frameOffset = 0; 1050 sv_frame_t frameOffset = 0;
1051 if (evTime > blockTime) { 1051 if (evTime > blockTime) {
1052 frameOffset = RealTime::realTime2Frame(evTime - blockTime, m_sampleRate); 1052 frameOffset = RealTime::realTime2Frame(evTime - blockTime, m_sampleRate);
1053 } 1053 }
1054 1054
1055 #ifdef DEBUG_DSSI_PROCESS 1055 #ifdef DEBUG_DSSI_PROCESS
1056 SVDEBUG << "DSSIPluginInstance::run: evTime " << evTime << ", blockTime " << blockTime << ", frameOffset " << frameOffset 1056 SVDEBUG << "DSSIPluginInstance::run: evTime " << evTime << ", blockTime " << blockTime << ", frameOffset " << frameOffset
1057 << ", blockSize " << m_blockSize << endl; 1057 << ", blockSize " << m_blockSize << endl;
1058 cerr << "Type: " << int(ev->type) << ", pitch: " << int(ev->data.note.note) << ", velocity: " << int(ev->data.note.velocity) << endl; 1058 cerr << "Type: " << int(ev->type) << ", pitch: " << int(ev->data.note.note) << ", velocity: " << int(ev->data.note.velocity) << endl;
1059 #endif 1059 #endif
1060 1060
1061 if (frameOffset >= (long)count) break; 1061 if (frameOffset >= (long)count) break;
1062 if (frameOffset < 0) { 1062 if (frameOffset < 0) {
1063 frameOffset = 0; 1063 frameOffset = 0;
1064 if (ev->type == SND_SEQ_EVENT_NOTEON) { 1064 if (ev->type == SND_SEQ_EVENT_NOTEON) {
1065 m_eventBuffer.skip(1); 1065 m_eventBuffer.skip(1);
1066 continue; 1066 continue;
1067 } 1067 }
1068 } 1068 }
1069 1069
1070 ev->time.tick = (snd_seq_tick_time_t)frameOffset; 1070 ev->time.tick = (snd_seq_tick_time_t)frameOffset;
1071 m_eventBuffer.skip(1); 1071 m_eventBuffer.skip(1);
1072 1072
1073 if (ev->type == SND_SEQ_EVENT_CONTROLLER) { 1073 if (ev->type == SND_SEQ_EVENT_CONTROLLER) {
1074 accept = handleController(ev); 1074 accept = handleController(ev);
1075 } else if (ev->type == SND_SEQ_EVENT_PGMCHANGE) { 1075 } else if (ev->type == SND_SEQ_EVENT_PGMCHANGE) {
1076 m_pending.program = ev->data.control.value; 1076 m_pending.program = ev->data.control.value;
1077 accept = false; 1077 accept = false;
1078 } 1078 }
1079 1079
1080 if (accept) { 1080 if (accept) {
1081 if (++evCount >= EVENT_BUFFER_SIZE) break; 1081 if (++evCount >= EVENT_BUFFER_SIZE) break;
1082 } 1082 }
1083 } 1083 }
1084 1084
1085 if (m_pending.program >= 0 && m_descriptor->select_program) { 1085 if (m_pending.program >= 0 && m_descriptor->select_program) {
1086 1086
1087 int program = m_pending.program; 1087 int program = m_pending.program;
1088 int bank = m_pending.lsb + 128 * m_pending.msb; 1088 int bank = m_pending.lsb + 128 * m_pending.msb;
1089 1089
1090 #ifdef DEBUG_DSSI 1090 #ifdef DEBUG_DSSI
1091 SVDEBUG << "DSSIPluginInstance::run: making select_program(" << bank << "," << program << ") call" << endl; 1091 SVDEBUG << "DSSIPluginInstance::run: making select_program(" << bank << "," << program << ") call" << endl;
1092 #endif 1092 #endif
1093 1093
1094 m_pending.lsb = m_pending.msb = m_pending.program = -1; 1094 m_pending.lsb = m_pending.msb = m_pending.program = -1;
1095 m_descriptor->select_program(m_instanceHandle, bank, program); 1095 m_descriptor->select_program(m_instanceHandle, bank, program);
1096 1096
1097 #ifdef DEBUG_DSSI 1097 #ifdef DEBUG_DSSI
1098 SVDEBUG << "DSSIPluginInstance::run: made select_program(" << bank << "," << program << ") call" << endl; 1098 SVDEBUG << "DSSIPluginInstance::run: made select_program(" << bank << "," << program << ") call" << endl;
1099 #endif 1099 #endif
1100 } 1100 }
1101 1101
1102 #ifdef DEBUG_DSSI_PROCESS 1102 #ifdef DEBUG_DSSI_PROCESS
1103 SVDEBUG << "DSSIPluginInstance::run: running with " << evCount << " events" 1103 SVDEBUG << "DSSIPluginInstance::run: running with " << evCount << " events"
1104 << endl; 1104 << endl;
1105 #endif 1105 #endif
1106 1106
1107 m_descriptor->run_synth(m_instanceHandle, count, 1107 m_descriptor->run_synth(m_instanceHandle, count,
1108 localEventBuffer, evCount); 1108 localEventBuffer, evCount);
1109 1109
1110 #ifdef DEBUG_DSSI_PROCESS 1110 #ifdef DEBUG_DSSI_PROCESS
1111 // for (int i = 0; i < count; ++i) { 1111 // for (int i = 0; i < count; ++i) {
1112 // cout << m_outputBuffers[0][i] << " "; 1112 // cout << m_outputBuffers[0][i] << " ";
1113 // if (i % 8 == 0) cout << endl; 1113 // if (i % 8 == 0) cout << endl;
1114 // } 1114 // }
1115 #endif 1115 #endif
1116 1116
1117 done: 1117 done:
1118 if (needLock) m_processLock.unlock(); 1118 if (needLock) m_processLock.unlock();
1119 1119
1120 int numAudioOuts = int(m_audioPortsOut.size()); 1120 int numAudioOuts = int(m_audioPortsOut.size());
1121 1121
1122 if (numAudioOuts == 0) { 1122 if (numAudioOuts == 0) {
1123 // copy inputs to outputs 1123 // copy inputs to outputs
1124 for (int ch = 0; ch < m_idealChannelCount; ++ch) { 1124 for (int ch = 0; ch < m_idealChannelCount; ++ch) {
1125 int sch = ch % getAudioInputCount(); 1125 int sch = ch % getAudioInputCount();
1126 for (int i = 0; i < m_blockSize; ++i) { 1126 for (int i = 0; i < m_blockSize; ++i) {
1127 m_outputBuffers[ch][i] = m_inputBuffers[sch][i]; 1127 m_outputBuffers[ch][i] = m_inputBuffers[sch][i];
1128 } 1128 }
1129 } 1129 }
1130 } else if (m_idealChannelCount < numAudioOuts) { 1130 } else if (m_idealChannelCount < numAudioOuts) {
1131 if (m_idealChannelCount == 1) { 1131 if (m_idealChannelCount == 1) {
1132 // mix down to mono 1132 // mix down to mono
1133 for (int ch = 1; ch < numAudioOuts; ++ch) { 1133 for (int ch = 1; ch < numAudioOuts; ++ch) {
1134 for (int i = 0; i < m_blockSize; ++i) { 1134 for (int i = 0; i < m_blockSize; ++i) {
1135 m_outputBuffers[0][i] += m_outputBuffers[ch][i]; 1135 m_outputBuffers[0][i] += m_outputBuffers[ch][i];
1136 } 1136 }
1137 } 1137 }
1138 } 1138 }
1139 } else if (m_idealChannelCount > numAudioOuts) { 1139 } else if (m_idealChannelCount > numAudioOuts) {
1140 // duplicate 1140 // duplicate
1141 for (int ch = numAudioOuts; ch < m_idealChannelCount; ++ch) { 1141 for (int ch = numAudioOuts; ch < m_idealChannelCount; ++ch) {
1142 int sch = (ch - numAudioOuts) % numAudioOuts; 1142 int sch = (ch - numAudioOuts) % numAudioOuts;
1143 for (int i = 0; i < m_blockSize; ++i) { 1143 for (int i = 0; i < m_blockSize; ++i) {
1144 m_outputBuffers[ch][i] = m_outputBuffers[sch][i]; 1144 m_outputBuffers[ch][i] = m_outputBuffers[sch][i];
1145 } 1145 }
1146 } 1146 }
1147 } 1147 }
1148 1148
1149 m_lastRunTime = blockTime; 1149 m_lastRunTime = blockTime;
1150 m_run = true; 1150 m_run = true;
1151 } 1151 }
1152 1152
1166 #ifdef DEBUG_DSSI_PROCESS 1166 #ifdef DEBUG_DSSI_PROCESS
1167 SVDEBUG << "DSSIPluginInstance::runGrouped(" << blockTime << "): this is " << this << "; " << s.size() << " elements in m_groupMap[" << m_identifier << "]" << endl; 1167 SVDEBUG << "DSSIPluginInstance::runGrouped(" << blockTime << "): this is " << this << "; " << s.size() << " elements in m_groupMap[" << m_identifier << "]" << endl;
1168 #endif 1168 #endif
1169 1169
1170 if (m_lastRunTime != blockTime) { 1170 if (m_lastRunTime != blockTime) {
1171 for (PluginSet::iterator i = s.begin(); i != s.end(); ++i) { 1171 for (PluginSet::iterator i = s.begin(); i != s.end(); ++i) {
1172 DSSIPluginInstance *instance = *i; 1172 DSSIPluginInstance *instance = *i;
1173 if (instance != this && instance->m_lastRunTime == blockTime) { 1173 if (instance != this && instance->m_lastRunTime == blockTime) {
1174 #ifdef DEBUG_DSSI_PROCESS 1174 #ifdef DEBUG_DSSI_PROCESS
1175 SVDEBUG << "DSSIPluginInstance::runGrouped(" << blockTime << "): plugin " << instance << " has already been run" << endl; 1175 SVDEBUG << "DSSIPluginInstance::runGrouped(" << blockTime << "): plugin " << instance << " has already been run" << endl;
1176 #endif 1176 #endif
1177 needRun = false; 1177 needRun = false;
1178 } 1178 }
1179 } 1179 }
1180 } 1180 }
1181 1181
1182 if (!needRun) { 1182 if (!needRun) {
1183 #ifdef DEBUG_DSSI_PROCESS 1183 #ifdef DEBUG_DSSI_PROCESS
1184 SVDEBUG << "DSSIPluginInstance::runGrouped(" << blockTime << "): already run, returning" << endl; 1184 SVDEBUG << "DSSIPluginInstance::runGrouped(" << blockTime << "): already run, returning" << endl;
1185 #endif 1185 #endif
1186 return; 1186 return;
1187 } 1187 }
1188 1188
1189 #ifdef DEBUG_DSSI_PROCESS 1189 #ifdef DEBUG_DSSI_PROCESS
1190 SVDEBUG << "DSSIPluginInstance::runGrouped(" << blockTime << "): I'm the first, running" << endl; 1190 SVDEBUG << "DSSIPluginInstance::runGrouped(" << blockTime << "): I'm the first, running" << endl;
1191 #endif 1191 #endif
1192 1192
1193 size_t index = 0; 1193 size_t index = 0;
1194 unsigned long *counts = (unsigned long *) 1194 unsigned long *counts = (unsigned long *)
1195 alloca(m_groupLocalEventBufferCount * sizeof(unsigned long)); 1195 alloca(m_groupLocalEventBufferCount * sizeof(unsigned long));
1196 LADSPA_Handle *instances = (LADSPA_Handle *) 1196 LADSPA_Handle *instances = (LADSPA_Handle *)
1197 alloca(m_groupLocalEventBufferCount * sizeof(LADSPA_Handle)); 1197 alloca(m_groupLocalEventBufferCount * sizeof(LADSPA_Handle));
1198 1198
1199 for (PluginSet::iterator i = s.begin(); i != s.end(); ++i) { 1199 for (PluginSet::iterator i = s.begin(); i != s.end(); ++i) {
1200 1200
1201 if (index >= m_groupLocalEventBufferCount) break; 1201 if (index >= m_groupLocalEventBufferCount) break;
1202 1202
1203 DSSIPluginInstance *instance = *i; 1203 DSSIPluginInstance *instance = *i;
1204 counts[index] = 0; 1204 counts[index] = 0;
1205 instances[index] = instance->m_instanceHandle; 1205 instances[index] = instance->m_instanceHandle;
1206 1206
1207 #ifdef DEBUG_DSSI_PROCESS 1207 #ifdef DEBUG_DSSI_PROCESS
1208 SVDEBUG << "DSSIPluginInstance::runGrouped(" << blockTime << "): running " << instance << endl; 1208 SVDEBUG << "DSSIPluginInstance::runGrouped(" << blockTime << "): running " << instance << endl;
1209 #endif 1209 #endif
1210 1210
1211 if (instance->m_pending.program >= 0 && 1211 if (instance->m_pending.program >= 0 &&
1212 instance->m_descriptor->select_program) { 1212 instance->m_descriptor->select_program) {
1213 int program = instance->m_pending.program; 1213 int program = instance->m_pending.program;
1214 int bank = instance->m_pending.lsb + 128 * instance->m_pending.msb; 1214 int bank = instance->m_pending.lsb + 128 * instance->m_pending.msb;
1215 instance->m_pending.lsb = instance->m_pending.msb = instance->m_pending.program = -1; 1215 instance->m_pending.lsb = instance->m_pending.msb = instance->m_pending.program = -1;
1216 instance->m_descriptor->select_program 1216 instance->m_descriptor->select_program
1217 (instance->m_instanceHandle, bank, program); 1217 (instance->m_instanceHandle, bank, program);
1218 } 1218 }
1219 1219
1220 while (instance->m_eventBuffer.getReadSpace() > 0) { 1220 while (instance->m_eventBuffer.getReadSpace() > 0) {
1221 1221
1222 snd_seq_event_t *ev = m_groupLocalEventBuffers[index] + counts[index]; 1222 snd_seq_event_t *ev = m_groupLocalEventBuffers[index] + counts[index];
1223 *ev = instance->m_eventBuffer.peekOne(); 1223 *ev = instance->m_eventBuffer.peekOne();
1224 bool accept = true; 1224 bool accept = true;
1225 1225
1226 RealTime evTime(ev->time.time.tv_sec, ev->time.time.tv_nsec); 1226 RealTime evTime(ev->time.time.tv_sec, ev->time.time.tv_nsec);
1227 1227
1228 sv_frame_t frameOffset = 0; 1228 sv_frame_t frameOffset = 0;
1229 if (evTime > blockTime) { 1229 if (evTime > blockTime) {
1230 frameOffset = RealTime::realTime2Frame(evTime - blockTime, m_sampleRate); 1230 frameOffset = RealTime::realTime2Frame(evTime - blockTime, m_sampleRate);
1231 } 1231 }
1232 1232
1233 #ifdef DEBUG_DSSI_PROCESS 1233 #ifdef DEBUG_DSSI_PROCESS
1234 SVDEBUG << "DSSIPluginInstance::runGrouped: evTime " << evTime << ", frameOffset " << frameOffset 1234 SVDEBUG << "DSSIPluginInstance::runGrouped: evTime " << evTime << ", frameOffset " << frameOffset
1235 << ", block size " << m_blockSize << endl; 1235 << ", block size " << m_blockSize << endl;
1236 #endif 1236 #endif
1237 1237
1238 if (frameOffset >= int(m_blockSize)) break; 1238 if (frameOffset >= int(m_blockSize)) break;
1239 if (frameOffset < 0) frameOffset = 0; 1239 if (frameOffset < 0) frameOffset = 0;
1240 1240
1241 ev->time.tick = snd_seq_tick_time_t(frameOffset); 1241 ev->time.tick = snd_seq_tick_time_t(frameOffset);
1242 instance->m_eventBuffer.skip(1); 1242 instance->m_eventBuffer.skip(1);
1243 1243
1244 if (ev->type == SND_SEQ_EVENT_CONTROLLER) { 1244 if (ev->type == SND_SEQ_EVENT_CONTROLLER) {
1245 accept = instance->handleController(ev); 1245 accept = instance->handleController(ev);
1246 } else if (ev->type == SND_SEQ_EVENT_PGMCHANGE) { 1246 } else if (ev->type == SND_SEQ_EVENT_PGMCHANGE) {
1247 instance->m_pending.program = ev->data.control.value; 1247 instance->m_pending.program = ev->data.control.value;
1248 accept = false; 1248 accept = false;
1249 } 1249 }
1250 1250
1251 if (accept) { 1251 if (accept) {
1252 if (++counts[index] >= EVENT_BUFFER_SIZE) break; 1252 if (++counts[index] >= EVENT_BUFFER_SIZE) break;
1253 } 1253 }
1254 } 1254 }
1255 1255
1256 ++index; 1256 ++index;
1257 } 1257 }
1258 1258
1259 m_descriptor->run_multiple_synths(index, 1259 m_descriptor->run_multiple_synths(index,
1260 instances, 1260 instances,
1261 m_blockSize, 1261 m_blockSize,
1262 m_groupLocalEventBuffers, 1262 m_groupLocalEventBuffers,
1263 counts); 1263 counts);
1264 } 1264 }
1265 1265
1266 int 1266 int
1267 DSSIPluginInstance::requestMidiSend(LADSPA_Handle /* instance */, 1267 DSSIPluginInstance::requestMidiSend(LADSPA_Handle /* instance */,
1268 unsigned char /* ports */, 1268 unsigned char /* ports */,
1269 unsigned char /* channels */) 1269 unsigned char /* channels */)
1270 { 1270 {
1271 // This is called from a non-RT context (during instantiate) 1271 // This is called from a non-RT context (during instantiate)
1272 1272
1273 SVDEBUG << "DSSIPluginInstance::requestMidiSend" << endl; 1273 SVDEBUG << "DSSIPluginInstance::requestMidiSend" << endl;
1274 return 1; 1274 return 1;
1275 } 1275 }
1276 1276
1277 void 1277 void
1278 DSSIPluginInstance::midiSend(LADSPA_Handle /* instance */, 1278 DSSIPluginInstance::midiSend(LADSPA_Handle /* instance */,
1279 snd_seq_event_t * /* events */, 1279 snd_seq_event_t * /* events */,
1280 unsigned long /* eventCount */) 1280 unsigned long /* eventCount */)
1281 { 1281 {
1282 // This is likely to be called from an RT context 1282 // This is likely to be called from an RT context
1283 1283
1284 SVDEBUG << "DSSIPluginInstance::midiSend" << endl; 1284 SVDEBUG << "DSSIPluginInstance::midiSend" << endl;
1285 } 1285 }
1286 1286
1287 void 1287 void
1288 DSSIPluginInstance::NonRTPluginThread::run() 1288 DSSIPluginInstance::NonRTPluginThread::run()
1289 { 1289 {
1290 while (!m_exiting) { 1290 while (!m_exiting) {
1291 m_runFunction(m_handle); 1291 m_runFunction(m_handle);
1292 usleep(100000); 1292 usleep(100000);
1293 } 1293 }
1294 } 1294 }
1295 1295
1296 int 1296 int
1297 DSSIPluginInstance::requestNonRTThread(LADSPA_Handle instance, 1297 DSSIPluginInstance::requestNonRTThread(LADSPA_Handle instance,
1298 void (*runFunction)(LADSPA_Handle)) 1298 void (*runFunction)(LADSPA_Handle))
1299 { 1299 {
1300 NonRTPluginThread *thread = new NonRTPluginThread(instance, runFunction); 1300 NonRTPluginThread *thread = new NonRTPluginThread(instance, runFunction);
1301 m_threads[instance].insert(thread); 1301 m_threads[instance].insert(thread);
1302 thread->start(); 1302 thread->start();
1303 return 0; 1303 return 0;
1310 SVDEBUG << "DSSIPluginInstance::deactivate " << m_identifier << endl; 1310 SVDEBUG << "DSSIPluginInstance::deactivate " << m_identifier << endl;
1311 #endif 1311 #endif
1312 if (!m_descriptor || !m_descriptor->LADSPA_Plugin->deactivate) return; 1312 if (!m_descriptor || !m_descriptor->LADSPA_Plugin->deactivate) return;
1313 1313
1314 for (size_t i = 0; i < m_backupControlPortsIn.size(); ++i) { 1314 for (size_t i = 0; i < m_backupControlPortsIn.size(); ++i) {
1315 m_backupControlPortsIn[i] = *m_controlPortsIn[i].second; 1315 m_backupControlPortsIn[i] = *m_controlPortsIn[i].second;
1316 } 1316 }
1317 1317
1318 m_descriptor->LADSPA_Plugin->deactivate(m_instanceHandle); 1318 m_descriptor->LADSPA_Plugin->deactivate(m_instanceHandle);
1319 #ifdef DEBUG_DSSI 1319 #ifdef DEBUG_DSSI
1320 SVDEBUG << "DSSIPluginInstance::deactivate " << m_identifier << " done" << endl; 1320 SVDEBUG << "DSSIPluginInstance::deactivate " << m_identifier << " done" << endl;
1330 SVDEBUG << "DSSIPluginInstance::cleanup " << m_identifier << endl; 1330 SVDEBUG << "DSSIPluginInstance::cleanup " << m_identifier << endl;
1331 #endif 1331 #endif
1332 if (!m_descriptor) return; 1332 if (!m_descriptor) return;
1333 1333
1334 if (!m_descriptor->LADSPA_Plugin->cleanup) { 1334 if (!m_descriptor->LADSPA_Plugin->cleanup) {
1335 cerr << "Bad plugin: plugin id " 1335 cerr << "Bad plugin: plugin id "
1336 << m_descriptor->LADSPA_Plugin->UniqueID 1336 << m_descriptor->LADSPA_Plugin->UniqueID
1337 << ":" << m_descriptor->LADSPA_Plugin->Label 1337 << ":" << m_descriptor->LADSPA_Plugin->Label
1338 << " has no cleanup method!" << endl; 1338 << " has no cleanup method!" << endl;
1339 return; 1339 return;
1340 } 1340 }
1341 1341
1342 m_descriptor->LADSPA_Plugin->cleanup(m_instanceHandle); 1342 m_descriptor->LADSPA_Plugin->cleanup(m_instanceHandle);
1343 m_instanceHandle = 0; 1343 m_instanceHandle = 0;
1344 #ifdef DEBUG_DSSI 1344 #ifdef DEBUG_DSSI