1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
|
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
/*
Sonic Visualiser
An audio file viewer and annotation editor.
Centre for Digital Music, Queen Mary, University of London.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version. See the file
COPYING included with this distribution for more information.
*/
/*
Based on trivial_sampler from the DSSI distribution
(by Chris Cannam, public domain).
*/
#include "SamplePlayer.h"
#include "system/System.h"
#include "../api/dssi.h"
#include <cmath>
#include <cstdlib>
#include <QMutexLocker>
#include <QDir>
#include <QFileInfo>
#include <bqaudiostream/AudioReadStreamFactory.h>
#include <bqaudiostream/AudioReadStream.h>
#include <bqresample/Resampler.h>
#include <iostream>
#include <vector>
using std::vector;
//#define DEBUG_SAMPLE_PLAYER 1
const char *const
SamplePlayer::portNames[PortCount] =
{
"Output",
"Tuned (on/off)",
"Base Pitch (MIDI)",
"Tuning of A (Hz)",
"Sustain (on/off)",
"Release time (s)"
};
const LADSPA_PortDescriptor
SamplePlayer::ports[PortCount] =
{
LADSPA_PORT_OUTPUT | LADSPA_PORT_AUDIO,
LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL,
LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL,
LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL,
LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL
};
const LADSPA_PortRangeHint
SamplePlayer::hints[PortCount] =
{
{ 0, 0, 0 },
{ LADSPA_HINT_DEFAULT_MAXIMUM | LADSPA_HINT_INTEGER |
LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE, 0, 1 },
{ LADSPA_HINT_DEFAULT_MIDDLE | LADSPA_HINT_INTEGER |
LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE, 0, 120 },
{ LADSPA_HINT_DEFAULT_440 | LADSPA_HINT_LOGARITHMIC |
LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE, 400, 499 },
{ LADSPA_HINT_DEFAULT_MINIMUM | LADSPA_HINT_INTEGER |
LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE, 0, 1 },
{ LADSPA_HINT_DEFAULT_MINIMUM | LADSPA_HINT_LOGARITHMIC |
LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE, 0.001f, 2.0f }
};
const LADSPA_Properties
SamplePlayer::properties = LADSPA_PROPERTY_HARD_RT_CAPABLE;
const LADSPA_Descriptor
SamplePlayer::ladspaDescriptor =
{
0, // "Unique" ID
"sample_player", // Label
properties,
"Library Sample Player", // Name
"Chris Cannam", // Maker
"GPL", // Copyright
PortCount,
ports,
portNames,
hints,
nullptr, // Implementation data
instantiate,
connectPort,
activate,
run,
nullptr, // Run adding
nullptr, // Set run adding gain
deactivate,
cleanup
};
const DSSI_Descriptor
SamplePlayer::dssiDescriptor =
{
2, // DSSI API version
&ladspaDescriptor,
configure,
getProgram,
selectProgram,
getMidiController,
runSynth,
nullptr, // Run synth adding
nullptr, // Run multiple synths
nullptr, // Run multiple synths adding
receiveHostDescriptor
};
const DSSI_Host_Descriptor *
SamplePlayer::hostDescriptor = nullptr;
const DSSI_Descriptor *
SamplePlayer::getDescriptor(unsigned long index)
{
if (index == 0) return &dssiDescriptor;
return nullptr;
}
SamplePlayer::SamplePlayer(int sampleRate) :
m_output(nullptr),
m_retune(nullptr),
m_basePitch(nullptr),
m_concertA(nullptr),
m_sustain(nullptr),
m_release(nullptr),
m_sampleData(nullptr),
m_sampleCount(0),
m_sampleRate(sampleRate),
m_sampleNo(0),
m_sampleDir("samples"),
m_sampleSearchComplete(false),
m_pendingProgramChange(-1)
{
}
SamplePlayer::~SamplePlayer()
{
delete[] m_sampleData;
}
LADSPA_Handle
SamplePlayer::instantiate(const LADSPA_Descriptor *, unsigned long rate)
{
if (!hostDescriptor || !hostDescriptor->request_non_rt_thread) {
SVDEBUG << "SamplePlayer::instantiate: Host does not provide request_non_rt_thread, not instantiating" << endl;
return nullptr;
}
SamplePlayer *player = new SamplePlayer(int(rate));
// std::cerr << "Instantiated sample player " << std::endl;
if (hostDescriptor->request_non_rt_thread(player, workThreadCallback)) {
SVDEBUG << "SamplePlayer::instantiate: Host rejected request_non_rt_thread call, not instantiating" << endl;
delete player;
return nullptr;
}
return player;
}
void
SamplePlayer::connectPort(LADSPA_Handle handle,
unsigned long port, LADSPA_Data *location)
{
SamplePlayer *player = (SamplePlayer *)handle;
float **ports[PortCount] = {
&player->m_output,
&player->m_retune,
&player->m_basePitch,
&player->m_concertA,
&player->m_sustain,
&player->m_release
};
*ports[port] = (float *)location;
}
void
SamplePlayer::activate(LADSPA_Handle handle)
{
SamplePlayer *player = (SamplePlayer *)handle;
QMutexLocker locker(&player->m_mutex);
player->m_sampleNo = 0;
for (size_t i = 0; i < Polyphony; ++i) {
player->m_ons[i] = -1;
player->m_offs[i] = -1;
player->m_velocities[i] = 0;
}
}
void
SamplePlayer::run(LADSPA_Handle handle, unsigned long samples)
{
runSynth(handle, samples, nullptr, 0);
}
void
SamplePlayer::deactivate(LADSPA_Handle handle)
{
activate(handle); // both functions just reset the plugin
}
void
SamplePlayer::cleanup(LADSPA_Handle handle)
{
delete (SamplePlayer *)handle;
}
char *
SamplePlayer::configure(LADSPA_Handle handle, const char *key, const char *value)
{
if (key && !strcmp(key, "sampledir")) {
SamplePlayer *player = (SamplePlayer *)handle;
QMutexLocker locker(&player->m_mutex);
if (QFileInfo(value).exists() &&
QFileInfo(value).isDir()) {
player->m_sampleDir = value;
if (player->m_sampleSearchComplete) {
player->m_sampleSearchComplete = false;
player->searchSamples();
}
return nullptr;
} else {
size_t len = strlen(value) + 80;
char *buffer = (char *)malloc(len);
snprintf(buffer, len, "Sample directory \"%s\" does not exist, leaving unchanged", value);
return buffer;
}
}
return strdup("Unknown configure key");
}
const DSSI_Program_Descriptor *
SamplePlayer::getProgram(LADSPA_Handle handle, unsigned long program)
{
SamplePlayer *player = (SamplePlayer *)handle;
if (!player->m_sampleSearchComplete) {
QMutexLocker locker(&player->m_mutex);
if (!player->m_sampleSearchComplete) {
player->searchSamples();
}
}
if (program >= player->m_samples.size()) return nullptr;
static DSSI_Program_Descriptor descriptor;
static char name[60];
strncpy(name, player->m_samples[program].first.toLocal8Bit().data(), 59);
name[59] = '\0';
descriptor.Bank = 0;
descriptor.Program = program;
descriptor.Name = name;
return &descriptor;
}
void
SamplePlayer::selectProgram(LADSPA_Handle handle,
unsigned long,
unsigned long program)
{
SamplePlayer *player = (SamplePlayer *)handle;
player->m_pendingProgramChange = (int)program;
}
int
SamplePlayer::getMidiController(LADSPA_Handle, unsigned long port)
{
int controllers[PortCount] = {
DSSI_NONE,
DSSI_CC(12),
DSSI_CC(13),
DSSI_CC(64),
DSSI_CC(72)
};
return controllers[port];
}
void
SamplePlayer::runSynth(LADSPA_Handle handle, unsigned long samples,
snd_seq_event_t *events, unsigned long eventCount)
{
SamplePlayer *player = (SamplePlayer *)handle;
player->runImpl(samples, events, eventCount);
}
void
SamplePlayer::receiveHostDescriptor(const DSSI_Host_Descriptor *descriptor)
{
hostDescriptor = descriptor;
}
void
SamplePlayer::workThreadCallback(LADSPA_Handle handle)
{
SamplePlayer *player = (SamplePlayer *)handle;
if (player->m_pendingProgramChange >= 0) {
#ifdef DEBUG_SAMPLE_PLAYER
SVDEBUG << "SamplePlayer::workThreadCallback: pending program change " << player->m_pendingProgramChange << endl;
#endif
player->m_mutex.lock();
int program = player->m_pendingProgramChange;
player->m_pendingProgramChange = -1;
if (!player->m_sampleSearchComplete) {
player->searchSamples();
}
if (program < int(player->m_samples.size())) {
QString path = player->m_samples[program].second;
QString programName = player->m_samples[program].first;
if (programName != player->m_program) {
player->m_program = programName;
player->m_mutex.unlock();
player->loadSampleData(path);
} else {
player->m_mutex.unlock();
}
}
}
if (!player->m_sampleSearchComplete) {
QMutexLocker locker(&player->m_mutex);
if (!player->m_sampleSearchComplete) {
player->searchSamples();
}
}
}
void
SamplePlayer::searchSamples()
{
if (m_sampleSearchComplete) return;
m_samples.clear();
#ifdef DEBUG_SAMPLE_PLAYER
SVDEBUG << "SamplePlayer::searchSamples: Directory is \""
<< m_sampleDir << "\"" << endl;
#endif
QDir dir(m_sampleDir, "*.wav");
for (unsigned int i = 0; i < dir.count(); ++i) {
QFileInfo file(dir.filePath(dir[i]));
if (file.isReadable()) {
m_samples.push_back(std::pair<QString, QString>
(file.baseName(), file.filePath()));
#ifdef DEBUG_SAMPLE_PLAYER
SVCERR << "Found: " << dir[i] << endl;
#endif
}
}
m_sampleSearchComplete = true;
}
void
SamplePlayer::loadSampleData(QString path)
{
breakfastquay::AudioReadStream *stream = nullptr;
try {
stream = breakfastquay::AudioReadStreamFactory::createReadStream
(path.toStdString());
} catch (const std::exception &e) {
SVCERR << "SamplePlayer::loadSampleData: ERROR: " << e.what() << endl;
return;
}
auto channels = stream->getChannelCount();
auto rate = stream->getSampleRate();
auto frames = stream->getEstimatedFrameCount();
if (!stream->isSeekable() || frames == 0) {
SVCERR << "SamplePlayer::loadSampleData: ERROR: Audio file \""
<< path << "\" must be of a format with known length"
<< endl;
delete stream;
return;
}
// Allow some extra at the end in case of resampler imprecision
size_t padding = 1000;
vector<float> interleaved((frames + padding) * channels, 0.f);
auto obtained = stream->getInterleavedFrames(frames, interleaved.data());
delete stream;
if (obtained < frames) {
SVCERR << "SamplePlayer::loadSampleData: ERROR: Too few frames read from \""
<< path << "\" (expected " << frames << ", got " << obtained
<< ")" << endl;
return;
}
vector<float> resampled;
size_t targetFrames = frames;
if (int(rate) != m_sampleRate) {
double ratio = (double)m_sampleRate / (double)rate;
targetFrames = (size_t)(round(double(frames) * ratio));
resampled = vector<float>(targetFrames * channels, 0.f);
breakfastquay::Resampler::Parameters params;
params.quality = breakfastquay::Resampler::Best;
params.dynamism = breakfastquay::Resampler::RatioMostlyFixed;
params.ratioChange = breakfastquay::Resampler::SuddenRatioChange;
breakfastquay::Resampler resampler(params, channels);
int obtained = resampler.resampleInterleaved
(resampled.data(), targetFrames,
interleaved.data(), frames + padding,
ratio, true);
if (obtained != int(targetFrames)) {
SVDEBUG << "SamplePlayer::loadSampleData: WARNING: Expected "
<< targetFrames << " frames from resampler (input frames = "
<< frames << ", padding = " << padding << ", ratio = "
<< ratio << "), obtained " << obtained << endl;
}
} else {
resampled = interleaved;
}
/* mixdown, adding an extra sample for linear interpolation */
float *tmpSamples = new float[targetFrames + 1];
for (size_t i = 0; i < targetFrames; ++i) {
tmpSamples[i] = 0.0f;
for (size_t j = 0; j < channels; ++j) {
tmpSamples[i] += resampled[i * channels + j];
}
}
tmpSamples[targetFrames] = 0.0f;
QMutexLocker locker(&m_mutex);
float *old = m_sampleData;
m_sampleData = tmpSamples;
m_sampleCount = targetFrames;
for (int i = 0; i < Polyphony; ++i) {
m_ons[i] = -1;
m_offs[i] = -1;
m_velocities[i] = 0;
}
delete[] old;
printf("%s: loaded %s (%ld samples from original %ld channels resampled from %ld frames at %ld Hz)\n", "sampler", path.toStdString().data(), (long)targetFrames, (long)channels, (long)frames, (long)rate);
}
void
SamplePlayer::runImpl(unsigned long sampleCount,
snd_seq_event_t *events,
unsigned long eventCount)
{
unsigned long pos;
unsigned long count;
unsigned long event_pos;
int i;
memset(m_output, 0, sampleCount * sizeof(float));
if (!m_mutex.tryLock()) return;
if (!m_sampleData || !m_sampleCount) {
m_sampleNo += sampleCount;
m_mutex.unlock();
return;
}
for (pos = 0, event_pos = 0; pos < sampleCount; ) {
while (event_pos < eventCount
&& pos >= events[event_pos].time.tick) {
if (events[event_pos].type == SND_SEQ_EVENT_NOTEON) {
#ifdef DEBUG_SAMPLE_PLAYER
SVCERR << "SamplePlayer: found NOTEON at time "
<< events[event_pos].time.tick << endl;
#endif
snd_seq_ev_note_t n = events[event_pos].data.note;
if (n.velocity > 0) {
m_ons[n.note] =
m_sampleNo + events[event_pos].time.tick;
m_offs[n.note] = -1;
m_velocities[n.note] = n.velocity;
} else {
if (!m_sustain || (*m_sustain < 0.001)) {
m_offs[n.note] =
m_sampleNo + events[event_pos].time.tick;
}
}
} else if (events[event_pos].type == SND_SEQ_EVENT_NOTEOFF &&
(!m_sustain || (*m_sustain < 0.001))) {
#ifdef DEBUG_SAMPLE_PLAYER
SVCERR << "SamplePlayer: found NOTEOFF at time "
<< events[event_pos].time.tick << endl;
#endif
snd_seq_ev_note_t n = events[event_pos].data.note;
m_offs[n.note] =
m_sampleNo + events[event_pos].time.tick;
}
++event_pos;
}
count = sampleCount - pos;
if (event_pos < eventCount &&
events[event_pos].time.tick < sampleCount) {
count = events[event_pos].time.tick - pos;
}
int notecount = 0;
for (i = 0; i < Polyphony; ++i) {
if (m_ons[i] >= 0) {
++notecount;
addSample(i, pos, count);
}
}
#ifdef DEBUG_SAMPLE_PLAYER
SVCERR << "SamplePlayer: have " << notecount << " note(s) sounding currently" << endl;
#else
(void)notecount;
#endif
pos += count;
}
m_sampleNo += sampleCount;
m_mutex.unlock();
}
void
SamplePlayer::addSample(int n, unsigned long pos, unsigned long count)
{
float ratio = 1.f;
float gain = 1.f;
unsigned long i, s;
if (m_retune && *m_retune) {
if (m_concertA) {
ratio *= *m_concertA / 440.f;
}
if (m_basePitch && float(n) != *m_basePitch) {
ratio *= powf(1.059463094f, float(n) - *m_basePitch);
}
}
if (long(pos + m_sampleNo) < m_ons[n]) return;
gain = (float)m_velocities[n] / 127.0f;
for (i = 0, s = pos + m_sampleNo - m_ons[n];
i < count;
++i, ++s) {
float lgain = gain;
float rs = float(s) * ratio;
unsigned long rsi = lrintf(floorf(rs));
if (rsi >= m_sampleCount) {
#ifdef DEBUG_SAMPLE_PLAYER
SVCERR << "Note " << n << " has run out of samples (were " << m_sampleCount << " available at ratio " << ratio << "), ending" << endl;
#endif
m_ons[n] = -1;
break;
}
if (m_offs[n] >= 0 &&
long(pos + i + m_sampleNo) > m_offs[n]) {
unsigned long dist =
pos + i + m_sampleNo - m_offs[n];
unsigned long releaseFrames = 200;
if (m_release) {
releaseFrames = long(*m_release * float(m_sampleRate) + 0.0001f);
}
if (dist > releaseFrames) {
#ifdef DEBUG_SAMPLE_PLAYER
SVCERR << "Note " << n << " has expired its release time (" << releaseFrames << " frames), ending" << endl;
#endif
m_ons[n] = -1;
break;
} else {
lgain = lgain * (float)(releaseFrames - dist) /
(float)releaseFrames;
}
}
float sample = m_sampleData[rsi] +
((m_sampleData[rsi + 1] -
m_sampleData[rsi]) *
(rs - (float)rsi));
m_output[pos + i] += lgain * sample;
}
}
|