comparison plugin/plugins/SamplePlayer.cpp @ 601:5eb5449432f5

* debug only
author Chris Cannam
date Tue, 18 Aug 2009 11:08:29 +0000
parents bdc9bb371a9f
children 9a6b50d3cc50
comparison
equal deleted inserted replaced
600:3f6d9930bc28 601:5eb5449432f5
29 #include <QFileInfo> 29 #include <QFileInfo>
30 30
31 #include <sndfile.h> 31 #include <sndfile.h>
32 #include <samplerate.h> 32 #include <samplerate.h>
33 #include <iostream> 33 #include <iostream>
34
35 //#define DEBUG_SAMPLE_PLAYER 1
34 36
35 const char *const 37 const char *const
36 SamplePlayer::portNames[PortCount] = 38 SamplePlayer::portNames[PortCount] =
37 { 39 {
38 "Output", 40 "Output",
315 { 317 {
316 SamplePlayer *player = (SamplePlayer *)handle; 318 SamplePlayer *player = (SamplePlayer *)handle;
317 319
318 if (player->m_pendingProgramChange >= 0) { 320 if (player->m_pendingProgramChange >= 0) {
319 321
320 // std::cerr << "SamplePlayer::workThreadCallback: pending program change " << player->m_pendingProgramChange << std::endl; 322 #ifdef DEBUG_SAMPLE_PLAYER
323 std::cerr << "SamplePlayer::workThreadCallback: pending program change " << player->m_pendingProgramChange << std::endl;
324 #endif
321 325
322 player->m_mutex.lock(); 326 player->m_mutex.lock();
323 327
324 int program = player->m_pendingProgramChange; 328 int program = player->m_pendingProgramChange;
325 player->m_pendingProgramChange = -1; 329 player->m_pendingProgramChange = -1;
356 { 360 {
357 if (m_sampleSearchComplete) return; 361 if (m_sampleSearchComplete) return;
358 362
359 m_samples.clear(); 363 m_samples.clear();
360 364
365 #ifdef DEBUG_SAMPLE_PLAYER
361 std::cerr << "SamplePlayer::searchSamples: Directory is \"" 366 std::cerr << "SamplePlayer::searchSamples: Directory is \""
362 << m_sampleDir.toLocal8Bit().data() << "\"" << std::endl; 367 << m_sampleDir.toLocal8Bit().data() << "\"" << std::endl;
368 #endif
363 369
364 QDir dir(m_sampleDir, "*.wav"); 370 QDir dir(m_sampleDir, "*.wav");
365 371
366 for (unsigned int i = 0; i < dir.count(); ++i) { 372 for (unsigned int i = 0; i < dir.count(); ++i) {
367 QFileInfo file(dir.filePath(dir[i])); 373 QFileInfo file(dir.filePath(dir[i]));
368 if (file.isReadable()) { 374 if (file.isReadable()) {
369 m_samples.push_back(std::pair<QString, QString> 375 m_samples.push_back(std::pair<QString, QString>
370 (file.baseName(), file.filePath())); 376 (file.baseName(), file.filePath()));
371 // std::cerr << "Found: " << dir[i].toLocal8Bit().data() << std::endl; 377 #ifdef DEBUG_SAMPLE_PLAYER
378 std::cerr << "Found: " << dir[i].toLocal8Bit().data() << std::endl;
379 #endif
372 } 380 }
373 } 381 }
374 382
375 m_sampleSearchComplete = true; 383 m_sampleSearchComplete = true;
376 } 384 }
492 500
493 while (event_pos < eventCount 501 while (event_pos < eventCount
494 && pos >= events[event_pos].time.tick) { 502 && pos >= events[event_pos].time.tick) {
495 503
496 if (events[event_pos].type == SND_SEQ_EVENT_NOTEON) { 504 if (events[event_pos].type == SND_SEQ_EVENT_NOTEON) {
505 #ifdef DEBUG_SAMPLE_PLAYER
506 std::cerr << "SamplePlayer: found NOTEON at time "
507 << events[event_pos].time.tick << std::endl;
508 #endif
497 snd_seq_ev_note_t n = events[event_pos].data.note; 509 snd_seq_ev_note_t n = events[event_pos].data.note;
498 if (n.velocity > 0) { 510 if (n.velocity > 0) {
499 m_ons[n.note] = 511 m_ons[n.note] =
500 m_sampleNo + events[event_pos].time.tick; 512 m_sampleNo + events[event_pos].time.tick;
501 m_offs[n.note] = -1; 513 m_offs[n.note] = -1;
506 m_sampleNo + events[event_pos].time.tick; 518 m_sampleNo + events[event_pos].time.tick;
507 } 519 }
508 } 520 }
509 } else if (events[event_pos].type == SND_SEQ_EVENT_NOTEOFF && 521 } else if (events[event_pos].type == SND_SEQ_EVENT_NOTEOFF &&
510 (!m_sustain || (*m_sustain < 0.001))) { 522 (!m_sustain || (*m_sustain < 0.001))) {
523 #ifdef DEBUG_SAMPLE_PLAYER
524 std::cerr << "SamplePlayer: found NOTEOFF at time "
525 << events[event_pos].time.tick << std::endl;
526 #endif
511 snd_seq_ev_note_t n = events[event_pos].data.note; 527 snd_seq_ev_note_t n = events[event_pos].data.note;
512 m_offs[n.note] = 528 m_offs[n.note] =
513 m_sampleNo + events[event_pos].time.tick; 529 m_sampleNo + events[event_pos].time.tick;
514 } 530 }
515 531
520 if (event_pos < eventCount && 536 if (event_pos < eventCount &&
521 events[event_pos].time.tick < sampleCount) { 537 events[event_pos].time.tick < sampleCount) {
522 count = events[event_pos].time.tick - pos; 538 count = events[event_pos].time.tick - pos;
523 } 539 }
524 540
541 int notecount = 0;
542
525 for (i = 0; i < Polyphony; ++i) { 543 for (i = 0; i < Polyphony; ++i) {
526 if (m_ons[i] >= 0) { 544 if (m_ons[i] >= 0) {
545 ++notecount;
527 addSample(i, pos, count); 546 addSample(i, pos, count);
528 } 547 }
529 } 548 }
549
550 #ifdef DEBUG_SAMPLE_PLAYER
551 std::cerr << "SamplePlayer: have " << notecount << " note(s) sounding currently" << std::endl;
552 #endif
530 553
531 pos += count; 554 pos += count;
532 } 555 }
533 556
534 m_sampleNo += sampleCount; 557 m_sampleNo += sampleCount;
562 float lgain = gain; 585 float lgain = gain;
563 float rs = s * ratio; 586 float rs = s * ratio;
564 unsigned long rsi = lrintf(floor(rs)); 587 unsigned long rsi = lrintf(floor(rs));
565 588
566 if (rsi >= m_sampleCount) { 589 if (rsi >= m_sampleCount) {
590 #ifdef DEBUG_SAMPLE_PLAYER
591 std::cerr << "Note " << n << " has run out of samples (were " << m_sampleCount << " available at ratio " << ratio << "), ending" << std::endl;
592 #endif
567 m_ons[n] = -1; 593 m_ons[n] = -1;
568 break; 594 break;
569 } 595 }
570 596
571 if (m_offs[n] >= 0 && 597 if (m_offs[n] >= 0 &&
578 if (m_release) { 604 if (m_release) {
579 releaseFrames = long(*m_release * m_sampleRate + 0.0001); 605 releaseFrames = long(*m_release * m_sampleRate + 0.0001);
580 } 606 }
581 607
582 if (dist > releaseFrames) { 608 if (dist > releaseFrames) {
609 #ifdef DEBUG_SAMPLE_PLAYER
610 std::cerr << "Note " << n << " has expired its release time (" << releaseFrames << " frames), ending" << std::endl;
611 #endif
583 m_ons[n] = -1; 612 m_ons[n] = -1;
584 break; 613 break;
585 } else { 614 } else {
586 lgain = lgain * (float)(releaseFrames - dist) / 615 lgain = lgain * (float)(releaseFrames - dist) /
587 (float)releaseFrames; 616 (float)releaseFrames;