annotate plugin/plugins/SamplePlayer.cpp @ 185:d5052b5fea9c

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