annotate audioio/AudioCallbackPlaySource.cpp @ 438:7fa74786dec5 cxx11

Register new types
author Chris Cannam
date Tue, 10 Mar 2015 17:40:38 +0000
parents 72c662fe7ea3
children aa6fb3516e28
rev   line source
Chris@43 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@43 2
Chris@43 3 /*
Chris@43 4 Sonic Visualiser
Chris@43 5 An audio file viewer and annotation editor.
Chris@43 6 Centre for Digital Music, Queen Mary, University of London.
Chris@43 7 This file copyright 2006 Chris Cannam and QMUL.
Chris@43 8
Chris@43 9 This program is free software; you can redistribute it and/or
Chris@43 10 modify it under the terms of the GNU General Public License as
Chris@43 11 published by the Free Software Foundation; either version 2 of the
Chris@43 12 License, or (at your option) any later version. See the file
Chris@43 13 COPYING included with this distribution for more information.
Chris@43 14 */
Chris@43 15
Chris@43 16 #include "AudioCallbackPlaySource.h"
Chris@43 17
Chris@43 18 #include "AudioGenerator.h"
Chris@43 19
Chris@43 20 #include "data/model/Model.h"
Chris@105 21 #include "base/ViewManagerBase.h"
Chris@43 22 #include "base/PlayParameterRepository.h"
Chris@43 23 #include "base/Preferences.h"
Chris@43 24 #include "data/model/DenseTimeValueModel.h"
Chris@43 25 #include "data/model/WaveFileModel.h"
Chris@43 26 #include "data/model/SparseOneDimensionalModel.h"
Chris@43 27 #include "plugin/RealTimePluginInstance.h"
Chris@62 28
Chris@91 29 #include "AudioCallbackPlayTarget.h"
Chris@91 30
Chris@62 31 #include <rubberband/RubberBandStretcher.h>
Chris@62 32 using namespace RubberBand;
Chris@43 33
Chris@43 34 #include <iostream>
Chris@43 35 #include <cassert>
Chris@43 36
Chris@174 37 //#define DEBUG_AUDIO_PLAY_SOURCE 1
Chris@43 38 //#define DEBUG_AUDIO_PLAY_SOURCE_PLAYING 1
Chris@43 39
Chris@366 40 static const int DEFAULT_RING_BUFFER_SIZE = 131071;
Chris@43 41
Chris@105 42 AudioCallbackPlaySource::AudioCallbackPlaySource(ViewManagerBase *manager,
Chris@57 43 QString clientName) :
Chris@43 44 m_viewManager(manager),
Chris@43 45 m_audioGenerator(new AudioGenerator()),
Chris@57 46 m_clientName(clientName),
Chris@43 47 m_readBuffers(0),
Chris@43 48 m_writeBuffers(0),
Chris@43 49 m_readBufferFill(0),
Chris@43 50 m_writeBufferFill(0),
Chris@43 51 m_bufferScavenger(1),
Chris@43 52 m_sourceChannelCount(0),
Chris@43 53 m_blockSize(1024),
Chris@43 54 m_sourceSampleRate(0),
Chris@43 55 m_targetSampleRate(0),
Chris@43 56 m_playLatency(0),
Chris@91 57 m_target(0),
Chris@91 58 m_lastRetrievalTimestamp(0.0),
Chris@91 59 m_lastRetrievedBlockSize(0),
Chris@102 60 m_trustworthyTimestamps(true),
Chris@102 61 m_lastCurrentFrame(0),
Chris@43 62 m_playing(false),
Chris@43 63 m_exiting(false),
Chris@43 64 m_lastModelEndFrame(0),
Chris@193 65 m_ringBufferSize(DEFAULT_RING_BUFFER_SIZE),
Chris@43 66 m_outputLeft(0.0),
Chris@43 67 m_outputRight(0.0),
Chris@43 68 m_auditioningPlugin(0),
Chris@43 69 m_auditioningPluginBypassed(false),
Chris@94 70 m_playStartFrame(0),
Chris@94 71 m_playStartFramePassed(false),
Chris@43 72 m_timeStretcher(0),
Chris@130 73 m_monoStretcher(0),
Chris@91 74 m_stretchRatio(1.0),
Chris@405 75 m_stretchMono(false),
Chris@91 76 m_stretcherInputCount(0),
Chris@91 77 m_stretcherInputs(0),
Chris@91 78 m_stretcherInputSizes(0),
Chris@43 79 m_fillThread(0),
Chris@43 80 m_converter(0),
Chris@43 81 m_crapConverter(0),
Chris@43 82 m_resampleQuality(Preferences::getInstance()->getResampleQuality())
Chris@43 83 {
Chris@43 84 m_viewManager->setAudioPlaySource(this);
Chris@43 85
Chris@43 86 connect(m_viewManager, SIGNAL(selectionChanged()),
Chris@43 87 this, SLOT(selectionChanged()));
Chris@43 88 connect(m_viewManager, SIGNAL(playLoopModeChanged()),
Chris@43 89 this, SLOT(playLoopModeChanged()));
Chris@43 90 connect(m_viewManager, SIGNAL(playSelectionModeChanged()),
Chris@43 91 this, SLOT(playSelectionModeChanged()));
Chris@43 92
Chris@300 93 connect(this, SIGNAL(playStatusChanged(bool)),
Chris@300 94 m_viewManager, SLOT(playStatusChanged(bool)));
Chris@300 95
Chris@43 96 connect(PlayParameterRepository::getInstance(),
Chris@43 97 SIGNAL(playParametersChanged(PlayParameters *)),
Chris@43 98 this, SLOT(playParametersChanged(PlayParameters *)));
Chris@43 99
Chris@43 100 connect(Preferences::getInstance(),
Chris@43 101 SIGNAL(propertyChanged(PropertyContainer::PropertyName)),
Chris@43 102 this, SLOT(preferenceChanged(PropertyContainer::PropertyName)));
Chris@43 103 }
Chris@43 104
Chris@43 105 AudioCallbackPlaySource::~AudioCallbackPlaySource()
Chris@43 106 {
Chris@177 107 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@233 108 SVDEBUG << "AudioCallbackPlaySource::~AudioCallbackPlaySource entering" << endl;
Chris@177 109 #endif
Chris@43 110 m_exiting = true;
Chris@43 111
Chris@43 112 if (m_fillThread) {
Chris@212 113 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@293 114 cout << "AudioCallbackPlaySource dtor: awakening thread" << endl;
Chris@212 115 #endif
Chris@212 116 m_condition.wakeAll();
Chris@43 117 m_fillThread->wait();
Chris@43 118 delete m_fillThread;
Chris@43 119 }
Chris@43 120
Chris@43 121 clearModels();
Chris@43 122
Chris@43 123 if (m_readBuffers != m_writeBuffers) {
Chris@43 124 delete m_readBuffers;
Chris@43 125 }
Chris@43 126
Chris@43 127 delete m_writeBuffers;
Chris@43 128
Chris@43 129 delete m_audioGenerator;
Chris@43 130
Chris@366 131 for (int i = 0; i < m_stretcherInputCount; ++i) {
Chris@91 132 delete[] m_stretcherInputs[i];
Chris@91 133 }
Chris@91 134 delete[] m_stretcherInputSizes;
Chris@91 135 delete[] m_stretcherInputs;
Chris@91 136
Chris@130 137 delete m_timeStretcher;
Chris@130 138 delete m_monoStretcher;
Chris@130 139
Chris@43 140 m_bufferScavenger.scavenge(true);
Chris@43 141 m_pluginScavenger.scavenge(true);
Chris@177 142 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@233 143 SVDEBUG << "AudioCallbackPlaySource::~AudioCallbackPlaySource finishing" << endl;
Chris@177 144 #endif
Chris@43 145 }
Chris@43 146
Chris@43 147 void
Chris@43 148 AudioCallbackPlaySource::addModel(Model *model)
Chris@43 149 {
Chris@43 150 if (m_models.find(model) != m_models.end()) return;
Chris@43 151
Chris@43 152 bool canPlay = m_audioGenerator->addModel(model);
Chris@43 153
Chris@43 154 m_mutex.lock();
Chris@43 155
Chris@43 156 m_models.insert(model);
Chris@43 157 if (model->getEndFrame() > m_lastModelEndFrame) {
Chris@43 158 m_lastModelEndFrame = model->getEndFrame();
Chris@43 159 }
Chris@43 160
Chris@43 161 bool buffersChanged = false, srChanged = false;
Chris@43 162
Chris@366 163 int modelChannels = 1;
Chris@43 164 DenseTimeValueModel *dtvm = dynamic_cast<DenseTimeValueModel *>(model);
Chris@43 165 if (dtvm) modelChannels = dtvm->getChannelCount();
Chris@43 166 if (modelChannels > m_sourceChannelCount) {
Chris@43 167 m_sourceChannelCount = modelChannels;
Chris@43 168 }
Chris@43 169
Chris@43 170 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@295 171 cout << "AudioCallbackPlaySource: Adding model with " << modelChannels << " channels at rate " << model->getSampleRate() << endl;
Chris@43 172 #endif
Chris@43 173
Chris@43 174 if (m_sourceSampleRate == 0) {
Chris@43 175
Chris@43 176 m_sourceSampleRate = model->getSampleRate();
Chris@43 177 srChanged = true;
Chris@43 178
Chris@43 179 } else if (model->getSampleRate() != m_sourceSampleRate) {
Chris@43 180
Chris@43 181 // If this is a dense time-value model and we have no other, we
Chris@43 182 // can just switch to this model's sample rate
Chris@43 183
Chris@43 184 if (dtvm) {
Chris@43 185
Chris@43 186 bool conflicting = false;
Chris@43 187
Chris@43 188 for (std::set<Model *>::const_iterator i = m_models.begin();
Chris@43 189 i != m_models.end(); ++i) {
Chris@43 190 // Only wave file models can be considered conflicting --
Chris@43 191 // writable wave file models are derived and we shouldn't
Chris@43 192 // take their rates into account. Also, don't give any
Chris@43 193 // particular weight to a file that's already playing at
Chris@43 194 // the wrong rate anyway
Chris@43 195 WaveFileModel *wfm = dynamic_cast<WaveFileModel *>(*i);
Chris@43 196 if (wfm && wfm != dtvm &&
Chris@43 197 wfm->getSampleRate() != model->getSampleRate() &&
Chris@43 198 wfm->getSampleRate() == m_sourceSampleRate) {
Chris@233 199 SVDEBUG << "AudioCallbackPlaySource::addModel: Conflicting wave file model " << *i << " found" << endl;
Chris@43 200 conflicting = true;
Chris@43 201 break;
Chris@43 202 }
Chris@43 203 }
Chris@43 204
Chris@43 205 if (conflicting) {
Chris@43 206
Chris@233 207 SVDEBUG << "AudioCallbackPlaySource::addModel: ERROR: "
Chris@229 208 << "New model sample rate does not match" << endl
Chris@43 209 << "existing model(s) (new " << model->getSampleRate()
Chris@43 210 << " vs " << m_sourceSampleRate
Chris@43 211 << "), playback will be wrong"
Chris@229 212 << endl;
Chris@43 213
Chris@43 214 emit sampleRateMismatch(model->getSampleRate(),
Chris@43 215 m_sourceSampleRate,
Chris@43 216 false);
Chris@43 217 } else {
Chris@43 218 m_sourceSampleRate = model->getSampleRate();
Chris@43 219 srChanged = true;
Chris@43 220 }
Chris@43 221 }
Chris@43 222 }
Chris@43 223
Chris@366 224 if (!m_writeBuffers || (int)m_writeBuffers->size() < getTargetChannelCount()) {
Chris@43 225 clearRingBuffers(true, getTargetChannelCount());
Chris@43 226 buffersChanged = true;
Chris@43 227 } else {
Chris@43 228 if (canPlay) clearRingBuffers(true);
Chris@43 229 }
Chris@43 230
Chris@43 231 if (buffersChanged || srChanged) {
Chris@43 232 if (m_converter) {
Chris@43 233 src_delete(m_converter);
Chris@43 234 src_delete(m_crapConverter);
Chris@43 235 m_converter = 0;
Chris@43 236 m_crapConverter = 0;
Chris@43 237 }
Chris@43 238 }
Chris@43 239
Chris@164 240 rebuildRangeLists();
Chris@164 241
Chris@43 242 m_mutex.unlock();
Chris@43 243
Chris@43 244 m_audioGenerator->setTargetChannelCount(getTargetChannelCount());
Chris@43 245
Chris@43 246 if (!m_fillThread) {
Chris@43 247 m_fillThread = new FillThread(*this);
Chris@43 248 m_fillThread->start();
Chris@43 249 }
Chris@43 250
Chris@43 251 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@293 252 cout << "AudioCallbackPlaySource::addModel: now have " << m_models.size() << " model(s) -- emitting modelReplaced" << endl;
Chris@43 253 #endif
Chris@43 254
Chris@43 255 if (buffersChanged || srChanged) {
Chris@43 256 emit modelReplaced();
Chris@43 257 }
Chris@43 258
Chris@435 259 connect(model, SIGNAL(modelChangedWithin(sv_frame_t, sv_frame_t)),
Chris@435 260 this, SLOT(modelChangedWithin(sv_frame_t, sv_frame_t)));
Chris@43 261
Chris@212 262 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@293 263 cout << "AudioCallbackPlaySource::addModel: awakening thread" << endl;
Chris@212 264 #endif
Chris@212 265
Chris@43 266 m_condition.wakeAll();
Chris@43 267 }
Chris@43 268
Chris@43 269 void
Chris@435 270 AudioCallbackPlaySource::modelChangedWithin(sv_frame_t
Chris@367 271 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@367 272 startFrame
Chris@367 273 #endif
Chris@435 274 , sv_frame_t endFrame)
Chris@43 275 {
Chris@43 276 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@367 277 SVDEBUG << "AudioCallbackPlaySource::modelChangedWithin(" << startFrame << "," << endFrame << ")" << endl;
Chris@43 278 #endif
Chris@93 279 if (endFrame > m_lastModelEndFrame) {
Chris@93 280 m_lastModelEndFrame = endFrame;
Chris@99 281 rebuildRangeLists();
Chris@93 282 }
Chris@43 283 }
Chris@43 284
Chris@43 285 void
Chris@43 286 AudioCallbackPlaySource::removeModel(Model *model)
Chris@43 287 {
Chris@43 288 m_mutex.lock();
Chris@43 289
Chris@43 290 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@293 291 cout << "AudioCallbackPlaySource::removeModel(" << model << ")" << endl;
Chris@43 292 #endif
Chris@43 293
Chris@435 294 disconnect(model, SIGNAL(modelChangedWithin(sv_frame_t, sv_frame_t)),
Chris@435 295 this, SLOT(modelChangedWithin(sv_frame_t, sv_frame_t)));
Chris@43 296
Chris@43 297 m_models.erase(model);
Chris@43 298
Chris@43 299 if (m_models.empty()) {
Chris@43 300 if (m_converter) {
Chris@43 301 src_delete(m_converter);
Chris@43 302 src_delete(m_crapConverter);
Chris@43 303 m_converter = 0;
Chris@43 304 m_crapConverter = 0;
Chris@43 305 }
Chris@43 306 m_sourceSampleRate = 0;
Chris@43 307 }
Chris@43 308
Chris@436 309 sv_frame_t lastEnd = 0;
Chris@43 310 for (std::set<Model *>::const_iterator i = m_models.begin();
Chris@43 311 i != m_models.end(); ++i) {
Chris@164 312 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@293 313 cout << "AudioCallbackPlaySource::removeModel(" << model << "): checking end frame on model " << *i << endl;
Chris@164 314 #endif
Chris@367 315 if ((*i)->getEndFrame() > lastEnd) {
Chris@367 316 lastEnd = (*i)->getEndFrame();
Chris@367 317 }
Chris@164 318 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@293 319 cout << "(done, lastEnd now " << lastEnd << ")" << endl;
Chris@164 320 #endif
Chris@43 321 }
Chris@43 322 m_lastModelEndFrame = lastEnd;
Chris@43 323
Chris@212 324 m_audioGenerator->removeModel(model);
Chris@212 325
Chris@43 326 m_mutex.unlock();
Chris@43 327
Chris@43 328 clearRingBuffers();
Chris@43 329 }
Chris@43 330
Chris@43 331 void
Chris@43 332 AudioCallbackPlaySource::clearModels()
Chris@43 333 {
Chris@43 334 m_mutex.lock();
Chris@43 335
Chris@43 336 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@293 337 cout << "AudioCallbackPlaySource::clearModels()" << endl;
Chris@43 338 #endif
Chris@43 339
Chris@43 340 m_models.clear();
Chris@43 341
Chris@43 342 if (m_converter) {
Chris@43 343 src_delete(m_converter);
Chris@43 344 src_delete(m_crapConverter);
Chris@43 345 m_converter = 0;
Chris@43 346 m_crapConverter = 0;
Chris@43 347 }
Chris@43 348
Chris@43 349 m_lastModelEndFrame = 0;
Chris@43 350
Chris@43 351 m_sourceSampleRate = 0;
Chris@43 352
Chris@43 353 m_mutex.unlock();
Chris@43 354
Chris@43 355 m_audioGenerator->clearModels();
Chris@93 356
Chris@93 357 clearRingBuffers();
Chris@43 358 }
Chris@43 359
Chris@43 360 void
Chris@366 361 AudioCallbackPlaySource::clearRingBuffers(bool haveLock, int count)
Chris@43 362 {
Chris@43 363 if (!haveLock) m_mutex.lock();
Chris@43 364
Chris@397 365 cerr << "clearRingBuffers" << endl;
Chris@397 366
Chris@93 367 rebuildRangeLists();
Chris@93 368
Chris@43 369 if (count == 0) {
Chris@436 370 if (m_writeBuffers) count = int(m_writeBuffers->size());
Chris@43 371 }
Chris@43 372
Chris@397 373 cerr << "current playing frame = " << getCurrentPlayingFrame() << endl;
Chris@397 374
Chris@397 375 cerr << "write buffer fill (before) = " << m_writeBufferFill << endl;
Chris@397 376
Chris@93 377 m_writeBufferFill = getCurrentBufferedFrame();
Chris@43 378
Chris@397 379 cerr << "current buffered frame = " << m_writeBufferFill << endl;
Chris@397 380
Chris@43 381 if (m_readBuffers != m_writeBuffers) {
Chris@43 382 delete m_writeBuffers;
Chris@43 383 }
Chris@43 384
Chris@43 385 m_writeBuffers = new RingBufferVector;
Chris@43 386
Chris@366 387 for (int i = 0; i < count; ++i) {
Chris@43 388 m_writeBuffers->push_back(new RingBuffer<float>(m_ringBufferSize));
Chris@43 389 }
Chris@43 390
Chris@293 391 // cout << "AudioCallbackPlaySource::clearRingBuffers: Created "
Chris@293 392 // << count << " write buffers" << endl;
Chris@43 393
Chris@43 394 if (!haveLock) {
Chris@43 395 m_mutex.unlock();
Chris@43 396 }
Chris@43 397 }
Chris@43 398
Chris@43 399 void
Chris@434 400 AudioCallbackPlaySource::play(sv_frame_t startFrame)
Chris@43 401 {
Chris@414 402 if (!m_sourceSampleRate) {
Chris@414 403 cerr << "AudioCallbackPlaySource::play: No source sample rate available, not playing" << endl;
Chris@414 404 return;
Chris@414 405 }
Chris@414 406
Chris@43 407 if (m_viewManager->getPlaySelectionMode() &&
Chris@43 408 !m_viewManager->getSelections().empty()) {
Chris@60 409
Chris@233 410 SVDEBUG << "AudioCallbackPlaySource::play: constraining frame " << startFrame << " to selection = ";
Chris@94 411
Chris@60 412 startFrame = m_viewManager->constrainFrameToSelection(startFrame);
Chris@60 413
Chris@233 414 SVDEBUG << startFrame << endl;
Chris@94 415
Chris@43 416 } else {
Chris@43 417 if (startFrame >= m_lastModelEndFrame) {
Chris@43 418 startFrame = 0;
Chris@43 419 }
Chris@43 420 }
Chris@43 421
Chris@132 422 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@293 423 cerr << "play(" << startFrame << ") -> playback model ";
Chris@132 424 #endif
Chris@60 425
Chris@60 426 startFrame = m_viewManager->alignReferenceToPlaybackFrame(startFrame);
Chris@60 427
Chris@189 428 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@293 429 cerr << startFrame << endl;
Chris@189 430 #endif
Chris@60 431
Chris@43 432 // The fill thread will automatically empty its buffers before
Chris@43 433 // starting again if we have not so far been playing, but not if
Chris@43 434 // we're just re-seeking.
Chris@102 435 // NO -- we can end up playing some first -- always reset here
Chris@43 436
Chris@43 437 m_mutex.lock();
Chris@102 438
Chris@91 439 if (m_timeStretcher) {
Chris@91 440 m_timeStretcher->reset();
Chris@91 441 }
Chris@130 442 if (m_monoStretcher) {
Chris@130 443 m_monoStretcher->reset();
Chris@130 444 }
Chris@102 445
Chris@102 446 m_readBufferFill = m_writeBufferFill = startFrame;
Chris@102 447 if (m_readBuffers) {
Chris@366 448 for (int c = 0; c < getTargetChannelCount(); ++c) {
Chris@102 449 RingBuffer<float> *rb = getReadRingBuffer(c);
Chris@132 450 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@293 451 cerr << "reset ring buffer for channel " << c << endl;
Chris@132 452 #endif
Chris@102 453 if (rb) rb->reset();
Chris@102 454 }
Chris@43 455 }
Chris@102 456 if (m_converter) src_reset(m_converter);
Chris@102 457 if (m_crapConverter) src_reset(m_crapConverter);
Chris@102 458
Chris@43 459 m_mutex.unlock();
Chris@43 460
Chris@43 461 m_audioGenerator->reset();
Chris@43 462
Chris@94 463 m_playStartFrame = startFrame;
Chris@94 464 m_playStartFramePassed = false;
Chris@94 465 m_playStartedAt = RealTime::zeroTime;
Chris@94 466 if (m_target) {
Chris@94 467 m_playStartedAt = RealTime::fromSeconds(m_target->getCurrentTime());
Chris@94 468 }
Chris@94 469
Chris@43 470 bool changed = !m_playing;
Chris@91 471 m_lastRetrievalTimestamp = 0;
Chris@102 472 m_lastCurrentFrame = 0;
Chris@43 473 m_playing = true;
Chris@212 474
Chris@212 475 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@293 476 cout << "AudioCallbackPlaySource::play: awakening thread" << endl;
Chris@212 477 #endif
Chris@212 478
Chris@43 479 m_condition.wakeAll();
Chris@158 480 if (changed) {
Chris@158 481 emit playStatusChanged(m_playing);
Chris@158 482 emit activity(tr("Play from %1").arg
Chris@158 483 (RealTime::frame2RealTime
Chris@158 484 (m_playStartFrame, m_sourceSampleRate).toText().c_str()));
Chris@158 485 }
Chris@43 486 }
Chris@43 487
Chris@43 488 void
Chris@43 489 AudioCallbackPlaySource::stop()
Chris@43 490 {
Chris@212 491 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@233 492 SVDEBUG << "AudioCallbackPlaySource::stop()" << endl;
Chris@212 493 #endif
Chris@43 494 bool changed = m_playing;
Chris@43 495 m_playing = false;
Chris@212 496
Chris@212 497 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@293 498 cout << "AudioCallbackPlaySource::stop: awakening thread" << endl;
Chris@212 499 #endif
Chris@212 500
Chris@43 501 m_condition.wakeAll();
Chris@91 502 m_lastRetrievalTimestamp = 0;
Chris@158 503 if (changed) {
Chris@158 504 emit playStatusChanged(m_playing);
Chris@158 505 emit activity(tr("Stop at %1").arg
Chris@158 506 (RealTime::frame2RealTime
Chris@158 507 (m_lastCurrentFrame, m_sourceSampleRate).toText().c_str()));
Chris@158 508 }
Chris@102 509 m_lastCurrentFrame = 0;
Chris@43 510 }
Chris@43 511
Chris@43 512 void
Chris@43 513 AudioCallbackPlaySource::selectionChanged()
Chris@43 514 {
Chris@43 515 if (m_viewManager->getPlaySelectionMode()) {
Chris@43 516 clearRingBuffers();
Chris@43 517 }
Chris@43 518 }
Chris@43 519
Chris@43 520 void
Chris@43 521 AudioCallbackPlaySource::playLoopModeChanged()
Chris@43 522 {
Chris@43 523 clearRingBuffers();
Chris@43 524 }
Chris@43 525
Chris@43 526 void
Chris@43 527 AudioCallbackPlaySource::playSelectionModeChanged()
Chris@43 528 {
Chris@43 529 if (!m_viewManager->getSelections().empty()) {
Chris@43 530 clearRingBuffers();
Chris@43 531 }
Chris@43 532 }
Chris@43 533
Chris@43 534 void
Chris@43 535 AudioCallbackPlaySource::playParametersChanged(PlayParameters *)
Chris@43 536 {
Chris@43 537 clearRingBuffers();
Chris@43 538 }
Chris@43 539
Chris@43 540 void
Chris@43 541 AudioCallbackPlaySource::preferenceChanged(PropertyContainer::PropertyName n)
Chris@43 542 {
Chris@43 543 if (n == "Resample Quality") {
Chris@43 544 setResampleQuality(Preferences::getInstance()->getResampleQuality());
Chris@43 545 }
Chris@43 546 }
Chris@43 547
Chris@43 548 void
Chris@43 549 AudioCallbackPlaySource::audioProcessingOverload()
Chris@43 550 {
Chris@293 551 cerr << "Audio processing overload!" << endl;
Chris@130 552
Chris@130 553 if (!m_playing) return;
Chris@130 554
Chris@43 555 RealTimePluginInstance *ap = m_auditioningPlugin;
Chris@130 556 if (ap && !m_auditioningPluginBypassed) {
Chris@43 557 m_auditioningPluginBypassed = true;
Chris@43 558 emit audioOverloadPluginDisabled();
Chris@130 559 return;
Chris@130 560 }
Chris@130 561
Chris@130 562 if (m_timeStretcher &&
Chris@130 563 m_timeStretcher->getTimeRatio() < 1.0 &&
Chris@130 564 m_stretcherInputCount > 1 &&
Chris@130 565 m_monoStretcher && !m_stretchMono) {
Chris@130 566 m_stretchMono = true;
Chris@130 567 emit audioTimeStretchMultiChannelDisabled();
Chris@130 568 return;
Chris@43 569 }
Chris@43 570 }
Chris@43 571
Chris@43 572 void
Chris@366 573 AudioCallbackPlaySource::setTarget(AudioCallbackPlayTarget *target, int size)
Chris@43 574 {
Chris@91 575 m_target = target;
Chris@293 576 cout << "AudioCallbackPlaySource::setTarget: Block size -> " << size << endl;
Chris@193 577 if (size != 0) {
Chris@193 578 m_blockSize = size;
Chris@193 579 }
Chris@193 580 if (size * 4 > m_ringBufferSize) {
Chris@233 581 SVDEBUG << "AudioCallbackPlaySource::setTarget: Buffer size "
Chris@193 582 << size << " > a quarter of ring buffer size "
Chris@193 583 << m_ringBufferSize << ", calling for more ring buffer"
Chris@229 584 << endl;
Chris@193 585 m_ringBufferSize = size * 4;
Chris@193 586 if (m_writeBuffers && !m_writeBuffers->empty()) {
Chris@193 587 clearRingBuffers();
Chris@193 588 }
Chris@193 589 }
Chris@43 590 }
Chris@43 591
Chris@366 592 int
Chris@43 593 AudioCallbackPlaySource::getTargetBlockSize() const
Chris@43 594 {
Chris@293 595 // cout << "AudioCallbackPlaySource::getTargetBlockSize() -> " << m_blockSize << endl;
Chris@436 596 return int(m_blockSize);
Chris@43 597 }
Chris@43 598
Chris@43 599 void
Chris@434 600 AudioCallbackPlaySource::setTargetPlayLatency(sv_frame_t latency)
Chris@43 601 {
Chris@43 602 m_playLatency = latency;
Chris@43 603 }
Chris@43 604
Chris@434 605 sv_frame_t
Chris@43 606 AudioCallbackPlaySource::getTargetPlayLatency() const
Chris@43 607 {
Chris@43 608 return m_playLatency;
Chris@43 609 }
Chris@43 610
Chris@434 611 sv_frame_t
Chris@43 612 AudioCallbackPlaySource::getCurrentPlayingFrame()
Chris@43 613 {
Chris@91 614 // This method attempts to estimate which audio sample frame is
Chris@91 615 // "currently coming through the speakers".
Chris@91 616
Chris@436 617 sv_samplerate_t targetRate = getTargetSampleRate();
Chris@436 618 sv_frame_t latency = m_playLatency; // at target rate
Chris@402 619 RealTime latency_t = RealTime::zeroTime;
Chris@402 620
Chris@402 621 if (targetRate != 0) {
Chris@402 622 latency_t = RealTime::frame2RealTime(latency, targetRate);
Chris@402 623 }
Chris@93 624
Chris@93 625 return getCurrentFrame(latency_t);
Chris@93 626 }
Chris@93 627
Chris@434 628 sv_frame_t
Chris@93 629 AudioCallbackPlaySource::getCurrentBufferedFrame()
Chris@93 630 {
Chris@93 631 return getCurrentFrame(RealTime::zeroTime);
Chris@93 632 }
Chris@93 633
Chris@434 634 sv_frame_t
Chris@93 635 AudioCallbackPlaySource::getCurrentFrame(RealTime latency_t)
Chris@93 636 {
Chris@91 637 // We resample when filling the ring buffer, and time-stretch when
Chris@91 638 // draining it. The buffer contains data at the "target rate" and
Chris@91 639 // the latency provided by the target is also at the target rate.
Chris@91 640 // Because of the multiple rates involved, we do the actual
Chris@91 641 // calculation using RealTime instead.
Chris@43 642
Chris@434 643 sv_samplerate_t sourceRate = getSourceSampleRate();
Chris@434 644 sv_samplerate_t targetRate = getTargetSampleRate();
Chris@91 645
Chris@91 646 if (sourceRate == 0 || targetRate == 0) return 0;
Chris@91 647
Chris@366 648 int inbuffer = 0; // at target rate
Chris@91 649
Chris@366 650 for (int c = 0; c < getTargetChannelCount(); ++c) {
Chris@43 651 RingBuffer<float> *rb = getReadRingBuffer(c);
Chris@43 652 if (rb) {
Chris@366 653 int here = rb->getReadSpace();
Chris@91 654 if (c == 0 || here < inbuffer) inbuffer = here;
Chris@43 655 }
Chris@43 656 }
Chris@43 657
Chris@436 658 sv_frame_t readBufferFill = m_readBufferFill;
Chris@436 659 sv_frame_t lastRetrievedBlockSize = m_lastRetrievedBlockSize;
Chris@91 660 double lastRetrievalTimestamp = m_lastRetrievalTimestamp;
Chris@91 661 double currentTime = 0.0;
Chris@91 662 if (m_target) currentTime = m_target->getCurrentTime();
Chris@91 663
Chris@102 664 bool looping = m_viewManager->getPlayLoopMode();
Chris@102 665
Chris@91 666 RealTime inbuffer_t = RealTime::frame2RealTime(inbuffer, targetRate);
Chris@91 667
Chris@436 668 sv_frame_t stretchlat = 0;
Chris@91 669 double timeRatio = 1.0;
Chris@91 670
Chris@91 671 if (m_timeStretcher) {
Chris@91 672 stretchlat = m_timeStretcher->getLatency();
Chris@91 673 timeRatio = m_timeStretcher->getTimeRatio();
Chris@43 674 }
Chris@43 675
Chris@91 676 RealTime stretchlat_t = RealTime::frame2RealTime(stretchlat, targetRate);
Chris@43 677
Chris@91 678 // When the target has just requested a block from us, the last
Chris@91 679 // sample it obtained was our buffer fill frame count minus the
Chris@91 680 // amount of read space (converted back to source sample rate)
Chris@91 681 // remaining now. That sample is not expected to be played until
Chris@91 682 // the target's play latency has elapsed. By the time the
Chris@91 683 // following block is requested, that sample will be at the
Chris@91 684 // target's play latency minus the last requested block size away
Chris@91 685 // from being played.
Chris@91 686
Chris@91 687 RealTime sincerequest_t = RealTime::zeroTime;
Chris@91 688 RealTime lastretrieved_t = RealTime::zeroTime;
Chris@91 689
Chris@102 690 if (m_target &&
Chris@102 691 m_trustworthyTimestamps &&
Chris@102 692 lastRetrievalTimestamp != 0.0) {
Chris@91 693
Chris@91 694 lastretrieved_t = RealTime::frame2RealTime
Chris@91 695 (lastRetrievedBlockSize, targetRate);
Chris@91 696
Chris@91 697 // calculate number of frames at target rate that have elapsed
Chris@91 698 // since the end of the last call to getSourceSamples
Chris@91 699
Chris@102 700 if (m_trustworthyTimestamps && !looping) {
Chris@91 701
Chris@102 702 // this adjustment seems to cause more problems when looping
Chris@102 703 double elapsed = currentTime - lastRetrievalTimestamp;
Chris@102 704
Chris@102 705 if (elapsed > 0.0) {
Chris@102 706 sincerequest_t = RealTime::fromSeconds(elapsed);
Chris@102 707 }
Chris@91 708 }
Chris@91 709
Chris@91 710 } else {
Chris@91 711
Chris@91 712 lastretrieved_t = RealTime::frame2RealTime
Chris@91 713 (getTargetBlockSize(), targetRate);
Chris@62 714 }
Chris@91 715
Chris@91 716 RealTime bufferedto_t = RealTime::frame2RealTime(readBufferFill, sourceRate);
Chris@91 717
Chris@91 718 if (timeRatio != 1.0) {
Chris@91 719 lastretrieved_t = lastretrieved_t / timeRatio;
Chris@91 720 sincerequest_t = sincerequest_t / timeRatio;
Chris@163 721 latency_t = latency_t / timeRatio;
Chris@43 722 }
Chris@43 723
Chris@91 724 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
Chris@293 725 cerr << "\nbuffered to: " << bufferedto_t << ", in buffer: " << inbuffer_t << ", time ratio " << timeRatio << "\n stretcher latency: " << stretchlat_t << ", device latency: " << latency_t << "\n since request: " << sincerequest_t << ", last retrieved quantity: " << lastretrieved_t << endl;
Chris@91 726 #endif
Chris@43 727
Chris@93 728 // Normally the range lists should contain at least one item each
Chris@93 729 // -- if playback is unconstrained, that item should report the
Chris@93 730 // entire source audio duration.
Chris@43 731
Chris@93 732 if (m_rangeStarts.empty()) {
Chris@93 733 rebuildRangeLists();
Chris@93 734 }
Chris@92 735
Chris@93 736 if (m_rangeStarts.empty()) {
Chris@93 737 // this code is only used in case of error in rebuildRangeLists
Chris@93 738 RealTime playing_t = bufferedto_t
Chris@93 739 - latency_t - stretchlat_t - lastretrieved_t - inbuffer_t
Chris@93 740 + sincerequest_t;
Chris@193 741 if (playing_t < RealTime::zeroTime) playing_t = RealTime::zeroTime;
Chris@434 742 sv_frame_t frame = RealTime::realTime2Frame(playing_t, sourceRate);
Chris@93 743 return m_viewManager->alignPlaybackFrameToReference(frame);
Chris@93 744 }
Chris@43 745
Chris@91 746 int inRange = 0;
Chris@91 747 int index = 0;
Chris@91 748
Chris@366 749 for (int i = 0; i < (int)m_rangeStarts.size(); ++i) {
Chris@93 750 if (bufferedto_t >= m_rangeStarts[i]) {
Chris@93 751 inRange = index;
Chris@93 752 } else {
Chris@93 753 break;
Chris@93 754 }
Chris@93 755 ++index;
Chris@93 756 }
Chris@93 757
Chris@436 758 if (inRange >= int(m_rangeStarts.size())) {
Chris@436 759 inRange = int(m_rangeStarts.size())-1;
Chris@436 760 }
Chris@93 761
Chris@94 762 RealTime playing_t = bufferedto_t;
Chris@93 763
Chris@93 764 playing_t = playing_t
Chris@93 765 - latency_t - stretchlat_t - lastretrieved_t - inbuffer_t
Chris@93 766 + sincerequest_t;
Chris@94 767
Chris@94 768 // This rather gross little hack is used to ensure that latency
Chris@94 769 // compensation doesn't result in the playback pointer appearing
Chris@94 770 // to start earlier than the actual playback does. It doesn't
Chris@94 771 // work properly (hence the bail-out in the middle) because if we
Chris@94 772 // are playing a relatively short looped region, the playing time
Chris@94 773 // estimated from the buffer fill frame may have wrapped around
Chris@94 774 // the region boundary and end up being much smaller than the
Chris@94 775 // theoretical play start frame, perhaps even for the entire
Chris@94 776 // duration of playback!
Chris@94 777
Chris@94 778 if (!m_playStartFramePassed) {
Chris@94 779 RealTime playstart_t = RealTime::frame2RealTime(m_playStartFrame,
Chris@94 780 sourceRate);
Chris@94 781 if (playing_t < playstart_t) {
Chris@293 782 // cerr << "playing_t " << playing_t << " < playstart_t "
Chris@293 783 // << playstart_t << endl;
Chris@122 784 if (/*!!! sincerequest_t > RealTime::zeroTime && */
Chris@94 785 m_playStartedAt + latency_t + stretchlat_t <
Chris@94 786 RealTime::fromSeconds(currentTime)) {
Chris@293 787 // cerr << "but we've been playing for long enough that I think we should disregard it (it probably results from loop wrapping)" << endl;
Chris@94 788 m_playStartFramePassed = true;
Chris@94 789 } else {
Chris@94 790 playing_t = playstart_t;
Chris@94 791 }
Chris@94 792 } else {
Chris@94 793 m_playStartFramePassed = true;
Chris@94 794 }
Chris@94 795 }
Chris@163 796
Chris@163 797 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
Chris@293 798 cerr << "playing_t " << playing_t;
Chris@163 799 #endif
Chris@94 800
Chris@94 801 playing_t = playing_t - m_rangeStarts[inRange];
Chris@93 802
Chris@93 803 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
Chris@293 804 cerr << " as offset into range " << inRange << " (start =" << m_rangeStarts[inRange] << " duration =" << m_rangeDurations[inRange] << ") = " << playing_t << endl;
Chris@93 805 #endif
Chris@93 806
Chris@93 807 while (playing_t < RealTime::zeroTime) {
Chris@93 808
Chris@93 809 if (inRange == 0) {
Chris@93 810 if (looping) {
Chris@436 811 inRange = int(m_rangeStarts.size()) - 1;
Chris@93 812 } else {
Chris@93 813 break;
Chris@93 814 }
Chris@93 815 } else {
Chris@93 816 --inRange;
Chris@93 817 }
Chris@93 818
Chris@93 819 playing_t = playing_t + m_rangeDurations[inRange];
Chris@93 820 }
Chris@93 821
Chris@93 822 playing_t = playing_t + m_rangeStarts[inRange];
Chris@93 823
Chris@93 824 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
Chris@293 825 cerr << " playing time: " << playing_t << endl;
Chris@93 826 #endif
Chris@93 827
Chris@93 828 if (!looping) {
Chris@366 829 if (inRange == (int)m_rangeStarts.size()-1 &&
Chris@93 830 playing_t >= m_rangeStarts[inRange] + m_rangeDurations[inRange]) {
Chris@293 831 cerr << "Not looping, inRange " << inRange << " == rangeStarts.size()-1, playing_t " << playing_t << " >= m_rangeStarts[inRange] " << m_rangeStarts[inRange] << " + m_rangeDurations[inRange] " << m_rangeDurations[inRange] << " -- stopping" << endl;
Chris@93 832 stop();
Chris@93 833 }
Chris@93 834 }
Chris@93 835
Chris@93 836 if (playing_t < RealTime::zeroTime) playing_t = RealTime::zeroTime;
Chris@93 837
Chris@434 838 sv_frame_t frame = RealTime::realTime2Frame(playing_t, sourceRate);
Chris@102 839
Chris@102 840 if (m_lastCurrentFrame > 0 && !looping) {
Chris@102 841 if (frame < m_lastCurrentFrame) {
Chris@102 842 frame = m_lastCurrentFrame;
Chris@102 843 }
Chris@102 844 }
Chris@102 845
Chris@102 846 m_lastCurrentFrame = frame;
Chris@102 847
Chris@93 848 return m_viewManager->alignPlaybackFrameToReference(frame);
Chris@93 849 }
Chris@93 850
Chris@93 851 void
Chris@93 852 AudioCallbackPlaySource::rebuildRangeLists()
Chris@93 853 {
Chris@93 854 bool constrained = (m_viewManager->getPlaySelectionMode());
Chris@93 855
Chris@93 856 m_rangeStarts.clear();
Chris@93 857 m_rangeDurations.clear();
Chris@93 858
Chris@436 859 sv_samplerate_t sourceRate = getSourceSampleRate();
Chris@93 860 if (sourceRate == 0) return;
Chris@93 861
Chris@93 862 RealTime end = RealTime::frame2RealTime(m_lastModelEndFrame, sourceRate);
Chris@93 863 if (end == RealTime::zeroTime) return;
Chris@93 864
Chris@93 865 if (!constrained) {
Chris@93 866 m_rangeStarts.push_back(RealTime::zeroTime);
Chris@93 867 m_rangeDurations.push_back(end);
Chris@93 868 return;
Chris@93 869 }
Chris@93 870
Chris@93 871 MultiSelection::SelectionList selections = m_viewManager->getSelections();
Chris@93 872 MultiSelection::SelectionList::const_iterator i;
Chris@93 873
Chris@93 874 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@233 875 SVDEBUG << "AudioCallbackPlaySource::rebuildRangeLists" << endl;
Chris@93 876 #endif
Chris@93 877
Chris@93 878 if (!selections.empty()) {
Chris@91 879
Chris@91 880 for (i = selections.begin(); i != selections.end(); ++i) {
Chris@91 881
Chris@91 882 RealTime start =
Chris@91 883 (RealTime::frame2RealTime
Chris@91 884 (m_viewManager->alignReferenceToPlaybackFrame(i->getStartFrame()),
Chris@91 885 sourceRate));
Chris@91 886 RealTime duration =
Chris@91 887 (RealTime::frame2RealTime
Chris@91 888 (m_viewManager->alignReferenceToPlaybackFrame(i->getEndFrame()) -
Chris@91 889 m_viewManager->alignReferenceToPlaybackFrame(i->getStartFrame()),
Chris@91 890 sourceRate));
Chris@91 891
Chris@93 892 m_rangeStarts.push_back(start);
Chris@93 893 m_rangeDurations.push_back(duration);
Chris@91 894 }
Chris@93 895 } else {
Chris@93 896 m_rangeStarts.push_back(RealTime::zeroTime);
Chris@93 897 m_rangeDurations.push_back(end);
Chris@43 898 }
Chris@43 899
Chris@93 900 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@293 901 cerr << "Now have " << m_rangeStarts.size() << " play ranges" << endl;
Chris@91 902 #endif
Chris@43 903 }
Chris@43 904
Chris@43 905 void
Chris@43 906 AudioCallbackPlaySource::setOutputLevels(float left, float right)
Chris@43 907 {
Chris@43 908 m_outputLeft = left;
Chris@43 909 m_outputRight = right;
Chris@43 910 }
Chris@43 911
Chris@43 912 bool
Chris@43 913 AudioCallbackPlaySource::getOutputLevels(float &left, float &right)
Chris@43 914 {
Chris@43 915 left = m_outputLeft;
Chris@43 916 right = m_outputRight;
Chris@43 917 return true;
Chris@43 918 }
Chris@43 919
Chris@43 920 void
Chris@434 921 AudioCallbackPlaySource::setTargetSampleRate(sv_samplerate_t sr)
Chris@43 922 {
Chris@244 923 bool first = (m_targetSampleRate == 0);
Chris@244 924
Chris@43 925 m_targetSampleRate = sr;
Chris@43 926 initialiseConverter();
Chris@244 927
Chris@244 928 if (first && (m_stretchRatio != 1.f)) {
Chris@244 929 // couldn't create a stretcher before because we had no sample
Chris@244 930 // rate: make one now
Chris@244 931 setTimeStretch(m_stretchRatio);
Chris@244 932 }
Chris@43 933 }
Chris@43 934
Chris@43 935 void
Chris@43 936 AudioCallbackPlaySource::initialiseConverter()
Chris@43 937 {
Chris@43 938 m_mutex.lock();
Chris@43 939
Chris@43 940 if (m_converter) {
Chris@43 941 src_delete(m_converter);
Chris@43 942 src_delete(m_crapConverter);
Chris@43 943 m_converter = 0;
Chris@43 944 m_crapConverter = 0;
Chris@43 945 }
Chris@43 946
Chris@43 947 if (getSourceSampleRate() != getTargetSampleRate()) {
Chris@43 948
Chris@43 949 int err = 0;
Chris@43 950
Chris@43 951 m_converter = src_new(m_resampleQuality == 2 ? SRC_SINC_BEST_QUALITY :
Chris@43 952 m_resampleQuality == 1 ? SRC_SINC_MEDIUM_QUALITY :
Chris@43 953 m_resampleQuality == 0 ? SRC_SINC_FASTEST :
Chris@43 954 SRC_SINC_MEDIUM_QUALITY,
Chris@43 955 getTargetChannelCount(), &err);
Chris@43 956
Chris@43 957 if (m_converter) {
Chris@43 958 m_crapConverter = src_new(SRC_LINEAR,
Chris@43 959 getTargetChannelCount(),
Chris@43 960 &err);
Chris@43 961 }
Chris@43 962
Chris@43 963 if (!m_converter || !m_crapConverter) {
Chris@293 964 cerr
Chris@43 965 << "AudioCallbackPlaySource::setModel: ERROR in creating samplerate converter: "
Chris@293 966 << src_strerror(err) << endl;
Chris@43 967
Chris@43 968 if (m_converter) {
Chris@43 969 src_delete(m_converter);
Chris@43 970 m_converter = 0;
Chris@43 971 }
Chris@43 972
Chris@43 973 if (m_crapConverter) {
Chris@43 974 src_delete(m_crapConverter);
Chris@43 975 m_crapConverter = 0;
Chris@43 976 }
Chris@43 977
Chris@43 978 m_mutex.unlock();
Chris@43 979
Chris@43 980 emit sampleRateMismatch(getSourceSampleRate(),
Chris@43 981 getTargetSampleRate(),
Chris@43 982 false);
Chris@43 983 } else {
Chris@43 984
Chris@43 985 m_mutex.unlock();
Chris@43 986
Chris@43 987 emit sampleRateMismatch(getSourceSampleRate(),
Chris@43 988 getTargetSampleRate(),
Chris@43 989 true);
Chris@43 990 }
Chris@43 991 } else {
Chris@43 992 m_mutex.unlock();
Chris@43 993 }
Chris@43 994 }
Chris@43 995
Chris@43 996 void
Chris@43 997 AudioCallbackPlaySource::setResampleQuality(int q)
Chris@43 998 {
Chris@43 999 if (q == m_resampleQuality) return;
Chris@43 1000 m_resampleQuality = q;
Chris@43 1001
Chris@43 1002 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@233 1003 SVDEBUG << "AudioCallbackPlaySource::setResampleQuality: setting to "
Chris@229 1004 << m_resampleQuality << endl;
Chris@43 1005 #endif
Chris@43 1006
Chris@43 1007 initialiseConverter();
Chris@43 1008 }
Chris@43 1009
Chris@43 1010 void
Chris@107 1011 AudioCallbackPlaySource::setAuditioningEffect(Auditionable *a)
Chris@43 1012 {
Chris@107 1013 RealTimePluginInstance *plugin = dynamic_cast<RealTimePluginInstance *>(a);
Chris@107 1014 if (a && !plugin) {
Chris@293 1015 cerr << "WARNING: AudioCallbackPlaySource::setAuditioningEffect: auditionable object " << a << " is not a real-time plugin instance" << endl;
Chris@107 1016 }
Chris@204 1017
Chris@204 1018 m_mutex.lock();
Chris@43 1019 m_auditioningPlugin = plugin;
Chris@43 1020 m_auditioningPluginBypassed = false;
Chris@204 1021 m_mutex.unlock();
Chris@43 1022 }
Chris@43 1023
Chris@43 1024 void
Chris@43 1025 AudioCallbackPlaySource::setSoloModelSet(std::set<Model *> s)
Chris@43 1026 {
Chris@43 1027 m_audioGenerator->setSoloModelSet(s);
Chris@43 1028 clearRingBuffers();
Chris@43 1029 }
Chris@43 1030
Chris@43 1031 void
Chris@43 1032 AudioCallbackPlaySource::clearSoloModelSet()
Chris@43 1033 {
Chris@43 1034 m_audioGenerator->clearSoloModelSet();
Chris@43 1035 clearRingBuffers();
Chris@43 1036 }
Chris@43 1037
Chris@434 1038 sv_samplerate_t
Chris@43 1039 AudioCallbackPlaySource::getTargetSampleRate() const
Chris@43 1040 {
Chris@43 1041 if (m_targetSampleRate) return m_targetSampleRate;
Chris@43 1042 else return getSourceSampleRate();
Chris@43 1043 }
Chris@43 1044
Chris@366 1045 int
Chris@43 1046 AudioCallbackPlaySource::getSourceChannelCount() const
Chris@43 1047 {
Chris@43 1048 return m_sourceChannelCount;
Chris@43 1049 }
Chris@43 1050
Chris@366 1051 int
Chris@43 1052 AudioCallbackPlaySource::getTargetChannelCount() const
Chris@43 1053 {
Chris@43 1054 if (m_sourceChannelCount < 2) return 2;
Chris@43 1055 return m_sourceChannelCount;
Chris@43 1056 }
Chris@43 1057
Chris@434 1058 sv_samplerate_t
Chris@43 1059 AudioCallbackPlaySource::getSourceSampleRate() const
Chris@43 1060 {
Chris@43 1061 return m_sourceSampleRate;
Chris@43 1062 }
Chris@43 1063
Chris@43 1064 void
Chris@436 1065 AudioCallbackPlaySource::setTimeStretch(double factor)
Chris@43 1066 {
Chris@91 1067 m_stretchRatio = factor;
Chris@91 1068
Chris@244 1069 if (!getTargetSampleRate()) return; // have to make our stretcher later
Chris@244 1070
Chris@436 1071 if (m_timeStretcher || (factor == 1.0)) {
Chris@91 1072 // stretch ratio will be set in next process call if appropriate
Chris@62 1073 } else {
Chris@91 1074 m_stretcherInputCount = getTargetChannelCount();
Chris@62 1075 RubberBandStretcher *stretcher = new RubberBandStretcher
Chris@436 1076 (int(getTargetSampleRate()),
Chris@91 1077 m_stretcherInputCount,
Chris@62 1078 RubberBandStretcher::OptionProcessRealTime,
Chris@62 1079 factor);
Chris@130 1080 RubberBandStretcher *monoStretcher = new RubberBandStretcher
Chris@436 1081 (int(getTargetSampleRate()),
Chris@130 1082 1,
Chris@130 1083 RubberBandStretcher::OptionProcessRealTime,
Chris@130 1084 factor);
Chris@91 1085 m_stretcherInputs = new float *[m_stretcherInputCount];
Chris@436 1086 m_stretcherInputSizes = new sv_frame_t[m_stretcherInputCount];
Chris@366 1087 for (int c = 0; c < m_stretcherInputCount; ++c) {
Chris@91 1088 m_stretcherInputSizes[c] = 16384;
Chris@91 1089 m_stretcherInputs[c] = new float[m_stretcherInputSizes[c]];
Chris@91 1090 }
Chris@130 1091 m_monoStretcher = monoStretcher;
Chris@62 1092 m_timeStretcher = stretcher;
Chris@62 1093 }
Chris@158 1094
Chris@158 1095 emit activity(tr("Change time-stretch factor to %1").arg(factor));
Chris@43 1096 }
Chris@43 1097
Chris@434 1098 sv_frame_t
Chris@434 1099 AudioCallbackPlaySource::getSourceSamples(sv_frame_t count, float **buffer)
Chris@43 1100 {
Chris@43 1101 if (!m_playing) {
Chris@193 1102 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
Chris@233 1103 SVDEBUG << "AudioCallbackPlaySource::getSourceSamples: Not playing" << endl;
Chris@193 1104 #endif
Chris@366 1105 for (int ch = 0; ch < getTargetChannelCount(); ++ch) {
Chris@130 1106 for (int i = 0; i < count; ++i) {
Chris@43 1107 buffer[ch][i] = 0.0;
Chris@43 1108 }
Chris@43 1109 }
Chris@43 1110 return 0;
Chris@43 1111 }
Chris@43 1112
Chris@212 1113 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
Chris@233 1114 SVDEBUG << "AudioCallbackPlaySource::getSourceSamples: Playing" << endl;
Chris@212 1115 #endif
Chris@212 1116
Chris@43 1117 // Ensure that all buffers have at least the amount of data we
Chris@43 1118 // need -- else reduce the size of our requests correspondingly
Chris@43 1119
Chris@366 1120 for (int ch = 0; ch < getTargetChannelCount(); ++ch) {
Chris@43 1121
Chris@43 1122 RingBuffer<float> *rb = getReadRingBuffer(ch);
Chris@43 1123
Chris@43 1124 if (!rb) {
Chris@293 1125 cerr << "WARNING: AudioCallbackPlaySource::getSourceSamples: "
Chris@43 1126 << "No ring buffer available for channel " << ch
Chris@293 1127 << ", returning no data here" << endl;
Chris@43 1128 count = 0;
Chris@43 1129 break;
Chris@43 1130 }
Chris@43 1131
Chris@366 1132 int rs = rb->getReadSpace();
Chris@43 1133 if (rs < count) {
Chris@43 1134 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@293 1135 cerr << "WARNING: AudioCallbackPlaySource::getSourceSamples: "
Chris@43 1136 << "Ring buffer for channel " << ch << " has only "
Chris@193 1137 << rs << " (of " << count << ") samples available ("
Chris@193 1138 << "ring buffer size is " << rb->getSize() << ", write "
Chris@193 1139 << "space " << rb->getWriteSpace() << "), "
Chris@293 1140 << "reducing request size" << endl;
Chris@43 1141 #endif
Chris@43 1142 count = rs;
Chris@43 1143 }
Chris@43 1144 }
Chris@43 1145
Chris@43 1146 if (count == 0) return 0;
Chris@43 1147
Chris@62 1148 RubberBandStretcher *ts = m_timeStretcher;
Chris@130 1149 RubberBandStretcher *ms = m_monoStretcher;
Chris@130 1150
Chris@436 1151 double ratio = ts ? ts->getTimeRatio() : 1.0;
Chris@91 1152
Chris@91 1153 if (ratio != m_stretchRatio) {
Chris@91 1154 if (!ts) {
Chris@293 1155 cerr << "WARNING: AudioCallbackPlaySource::getSourceSamples: Time ratio change to " << m_stretchRatio << " is pending, but no stretcher is set" << endl;
Chris@436 1156 m_stretchRatio = 1.0;
Chris@91 1157 } else {
Chris@91 1158 ts->setTimeRatio(m_stretchRatio);
Chris@130 1159 if (ms) ms->setTimeRatio(m_stretchRatio);
Chris@130 1160 if (m_stretchRatio >= 1.0) m_stretchMono = false;
Chris@130 1161 }
Chris@130 1162 }
Chris@130 1163
Chris@130 1164 int stretchChannels = m_stretcherInputCount;
Chris@130 1165 if (m_stretchMono) {
Chris@130 1166 if (ms) {
Chris@130 1167 ts = ms;
Chris@130 1168 stretchChannels = 1;
Chris@130 1169 } else {
Chris@130 1170 m_stretchMono = false;
Chris@91 1171 }
Chris@91 1172 }
Chris@91 1173
Chris@91 1174 if (m_target) {
Chris@91 1175 m_lastRetrievedBlockSize = count;
Chris@91 1176 m_lastRetrievalTimestamp = m_target->getCurrentTime();
Chris@91 1177 }
Chris@43 1178
Chris@62 1179 if (!ts || ratio == 1.f) {
Chris@43 1180
Chris@130 1181 int got = 0;
Chris@43 1182
Chris@366 1183 for (int ch = 0; ch < getTargetChannelCount(); ++ch) {
Chris@43 1184
Chris@43 1185 RingBuffer<float> *rb = getReadRingBuffer(ch);
Chris@43 1186
Chris@43 1187 if (rb) {
Chris@43 1188
Chris@43 1189 // this is marginally more likely to leave our channels in
Chris@43 1190 // sync after a processing failure than just passing "count":
Chris@436 1191 sv_frame_t request = count;
Chris@43 1192 if (ch > 0) request = got;
Chris@43 1193
Chris@436 1194 got = rb->read(buffer[ch], int(request));
Chris@43 1195
Chris@43 1196 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
Chris@293 1197 cout << "AudioCallbackPlaySource::getSamples: got " << got << " (of " << count << ") samples on channel " << ch << ", signalling for more (possibly)" << endl;
Chris@43 1198 #endif
Chris@43 1199 }
Chris@43 1200
Chris@366 1201 for (int ch = 0; ch < getTargetChannelCount(); ++ch) {
Chris@130 1202 for (int i = got; i < count; ++i) {
Chris@43 1203 buffer[ch][i] = 0.0;
Chris@43 1204 }
Chris@43 1205 }
Chris@43 1206 }
Chris@43 1207
Chris@43 1208 applyAuditioningEffect(count, buffer);
Chris@43 1209
Chris@212 1210 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@293 1211 cout << "AudioCallbackPlaySource::getSamples: awakening thread" << endl;
Chris@212 1212 #endif
Chris@212 1213
Chris@43 1214 m_condition.wakeAll();
Chris@91 1215
Chris@43 1216 return got;
Chris@43 1217 }
Chris@43 1218
Chris@366 1219 int channels = getTargetChannelCount();
Chris@436 1220 sv_frame_t available;
Chris@436 1221 sv_frame_t fedToStretcher = 0;
Chris@91 1222 int warned = 0;
Chris@43 1223
Chris@91 1224 // The input block for a given output is approx output / ratio,
Chris@91 1225 // but we can't predict it exactly, for an adaptive timestretcher.
Chris@91 1226
Chris@91 1227 while ((available = ts->available()) < count) {
Chris@91 1228
Chris@436 1229 sv_frame_t reqd = lrint(double(count - available) / ratio);
Chris@436 1230 reqd = std::max(reqd, sv_frame_t(ts->getSamplesRequired()));
Chris@91 1231 if (reqd == 0) reqd = 1;
Chris@91 1232
Chris@436 1233 sv_frame_t got = reqd;
Chris@91 1234
Chris@91 1235 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
Chris@293 1236 cerr << "reqd = " <<reqd << ", channels = " << channels << ", ic = " << m_stretcherInputCount << endl;
Chris@62 1237 #endif
Chris@43 1238
Chris@366 1239 for (int c = 0; c < channels; ++c) {
Chris@131 1240 if (c >= m_stretcherInputCount) continue;
Chris@91 1241 if (reqd > m_stretcherInputSizes[c]) {
Chris@91 1242 if (c == 0) {
Chris@293 1243 cerr << "WARNING: resizing stretcher input buffer from " << m_stretcherInputSizes[c] << " to " << (reqd * 2) << endl;
Chris@91 1244 }
Chris@91 1245 delete[] m_stretcherInputs[c];
Chris@91 1246 m_stretcherInputSizes[c] = reqd * 2;
Chris@91 1247 m_stretcherInputs[c] = new float[m_stretcherInputSizes[c]];
Chris@91 1248 }
Chris@91 1249 }
Chris@43 1250
Chris@366 1251 for (int c = 0; c < channels; ++c) {
Chris@131 1252 if (c >= m_stretcherInputCount) continue;
Chris@91 1253 RingBuffer<float> *rb = getReadRingBuffer(c);
Chris@91 1254 if (rb) {
Chris@436 1255 sv_frame_t gotHere;
Chris@130 1256 if (stretchChannels == 1 && c > 0) {
Chris@436 1257 gotHere = rb->readAdding(m_stretcherInputs[0], int(got));
Chris@130 1258 } else {
Chris@436 1259 gotHere = rb->read(m_stretcherInputs[c], int(got));
Chris@130 1260 }
Chris@91 1261 if (gotHere < got) got = gotHere;
Chris@91 1262
Chris@91 1263 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
Chris@91 1264 if (c == 0) {
Chris@233 1265 SVDEBUG << "feeding stretcher: got " << gotHere
Chris@229 1266 << ", " << rb->getReadSpace() << " remain" << endl;
Chris@91 1267 }
Chris@62 1268 #endif
Chris@43 1269
Chris@91 1270 } else {
Chris@293 1271 cerr << "WARNING: No ring buffer available for channel " << c << " in stretcher input block" << endl;
Chris@43 1272 }
Chris@43 1273 }
Chris@43 1274
Chris@43 1275 if (got < reqd) {
Chris@293 1276 cerr << "WARNING: Read underrun in playback ("
Chris@293 1277 << got << " < " << reqd << ")" << endl;
Chris@43 1278 }
Chris@43 1279
Chris@91 1280 ts->process(m_stretcherInputs, got, false);
Chris@91 1281
Chris@91 1282 fedToStretcher += got;
Chris@43 1283
Chris@43 1284 if (got == 0) break;
Chris@43 1285
Chris@62 1286 if (ts->available() == available) {
Chris@293 1287 cerr << "WARNING: AudioCallbackPlaySource::getSamples: Added " << got << " samples to time stretcher, created no new available output samples (warned = " << warned << ")" << endl;
Chris@43 1288 if (++warned == 5) break;
Chris@43 1289 }
Chris@43 1290 }
Chris@43 1291
Chris@62 1292 ts->retrieve(buffer, count);
Chris@43 1293
Chris@130 1294 for (int c = stretchChannels; c < getTargetChannelCount(); ++c) {
Chris@130 1295 for (int i = 0; i < count; ++i) {
Chris@130 1296 buffer[c][i] = buffer[0][i];
Chris@130 1297 }
Chris@130 1298 }
Chris@130 1299
Chris@43 1300 applyAuditioningEffect(count, buffer);
Chris@43 1301
Chris@212 1302 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@293 1303 cout << "AudioCallbackPlaySource::getSamples [stretched]: awakening thread" << endl;
Chris@212 1304 #endif
Chris@212 1305
Chris@43 1306 m_condition.wakeAll();
Chris@43 1307
Chris@43 1308 return count;
Chris@43 1309 }
Chris@43 1310
Chris@43 1311 void
Chris@434 1312 AudioCallbackPlaySource::applyAuditioningEffect(sv_frame_t count, float **buffers)
Chris@43 1313 {
Chris@43 1314 if (m_auditioningPluginBypassed) return;
Chris@43 1315 RealTimePluginInstance *plugin = m_auditioningPlugin;
Chris@43 1316 if (!plugin) return;
Chris@204 1317
Chris@366 1318 if ((int)plugin->getAudioInputCount() != getTargetChannelCount()) {
Chris@293 1319 // cerr << "plugin input count " << plugin->getAudioInputCount()
Chris@43 1320 // << " != our channel count " << getTargetChannelCount()
Chris@293 1321 // << endl;
Chris@43 1322 return;
Chris@43 1323 }
Chris@366 1324 if ((int)plugin->getAudioOutputCount() != getTargetChannelCount()) {
Chris@293 1325 // cerr << "plugin output count " << plugin->getAudioOutputCount()
Chris@43 1326 // << " != our channel count " << getTargetChannelCount()
Chris@293 1327 // << endl;
Chris@43 1328 return;
Chris@43 1329 }
Chris@366 1330 if ((int)plugin->getBufferSize() < count) {
Chris@293 1331 // cerr << "plugin buffer size " << plugin->getBufferSize()
Chris@102 1332 // << " < our block size " << count
Chris@293 1333 // << endl;
Chris@43 1334 return;
Chris@43 1335 }
Chris@43 1336
Chris@43 1337 float **ib = plugin->getAudioInputBuffers();
Chris@43 1338 float **ob = plugin->getAudioOutputBuffers();
Chris@43 1339
Chris@366 1340 for (int c = 0; c < getTargetChannelCount(); ++c) {
Chris@366 1341 for (int i = 0; i < count; ++i) {
Chris@43 1342 ib[c][i] = buffers[c][i];
Chris@43 1343 }
Chris@43 1344 }
Chris@43 1345
Chris@436 1346 plugin->run(Vamp::RealTime::zeroTime, int(count));
Chris@43 1347
Chris@366 1348 for (int c = 0; c < getTargetChannelCount(); ++c) {
Chris@366 1349 for (int i = 0; i < count; ++i) {
Chris@43 1350 buffers[c][i] = ob[c][i];
Chris@43 1351 }
Chris@43 1352 }
Chris@43 1353 }
Chris@43 1354
Chris@43 1355 // Called from fill thread, m_playing true, mutex held
Chris@43 1356 bool
Chris@43 1357 AudioCallbackPlaySource::fillBuffers()
Chris@43 1358 {
Chris@43 1359 static float *tmp = 0;
Chris@436 1360 static sv_frame_t tmpSize = 0;
Chris@43 1361
Chris@434 1362 sv_frame_t space = 0;
Chris@366 1363 for (int c = 0; c < getTargetChannelCount(); ++c) {
Chris@43 1364 RingBuffer<float> *wb = getWriteRingBuffer(c);
Chris@43 1365 if (wb) {
Chris@434 1366 sv_frame_t spaceHere = wb->getWriteSpace();
Chris@43 1367 if (c == 0 || spaceHere < space) space = spaceHere;
Chris@43 1368 }
Chris@43 1369 }
Chris@43 1370
Chris@103 1371 if (space == 0) {
Chris@103 1372 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@293 1373 cout << "AudioCallbackPlaySourceFillThread: no space to fill" << endl;
Chris@103 1374 #endif
Chris@103 1375 return false;
Chris@103 1376 }
Chris@43 1377
Chris@434 1378 sv_frame_t f = m_writeBufferFill;
Chris@43 1379
Chris@43 1380 bool readWriteEqual = (m_readBuffers == m_writeBuffers);
Chris@43 1381
Chris@43 1382 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@193 1383 if (!readWriteEqual) {
Chris@293 1384 cout << "AudioCallbackPlaySourceFillThread: note read buffers != write buffers" << endl;
Chris@193 1385 }
Chris@293 1386 cout << "AudioCallbackPlaySourceFillThread: filling " << space << " frames" << endl;
Chris@43 1387 #endif
Chris@43 1388
Chris@43 1389 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@293 1390 cout << "buffered to " << f << " already" << endl;
Chris@43 1391 #endif
Chris@43 1392
Chris@43 1393 bool resample = (getSourceSampleRate() != getTargetSampleRate());
Chris@43 1394
Chris@43 1395 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@293 1396 cout << (resample ? "" : "not ") << "resampling (source " << getSourceSampleRate() << ", target " << getTargetSampleRate() << ")" << endl;
Chris@43 1397 #endif
Chris@43 1398
Chris@366 1399 int channels = getTargetChannelCount();
Chris@43 1400
Chris@434 1401 sv_frame_t orig = space;
Chris@434 1402 sv_frame_t got = 0;
Chris@43 1403
Chris@43 1404 static float **bufferPtrs = 0;
Chris@366 1405 static int bufferPtrCount = 0;
Chris@43 1406
Chris@43 1407 if (bufferPtrCount < channels) {
Chris@43 1408 if (bufferPtrs) delete[] bufferPtrs;
Chris@43 1409 bufferPtrs = new float *[channels];
Chris@43 1410 bufferPtrCount = channels;
Chris@43 1411 }
Chris@43 1412
Chris@436 1413 sv_frame_t generatorBlockSize = m_audioGenerator->getBlockSize();
Chris@43 1414
Chris@43 1415 if (resample && !m_converter) {
Chris@43 1416 static bool warned = false;
Chris@43 1417 if (!warned) {
Chris@293 1418 cerr << "WARNING: sample rates differ, but no converter available!" << endl;
Chris@43 1419 warned = true;
Chris@43 1420 }
Chris@43 1421 }
Chris@43 1422
Chris@43 1423 if (resample && m_converter) {
Chris@43 1424
Chris@43 1425 double ratio =
Chris@43 1426 double(getTargetSampleRate()) / double(getSourceSampleRate());
Chris@436 1427 orig = sv_frame_t(double(orig) / ratio + 0.1);
Chris@43 1428
Chris@43 1429 // orig must be a multiple of generatorBlockSize
Chris@43 1430 orig = (orig / generatorBlockSize) * generatorBlockSize;
Chris@43 1431 if (orig == 0) return false;
Chris@43 1432
Chris@436 1433 sv_frame_t work = std::max(orig, space);
Chris@43 1434
Chris@43 1435 // We only allocate one buffer, but we use it in two halves.
Chris@43 1436 // We place the non-interleaved values in the second half of
Chris@43 1437 // the buffer (orig samples for channel 0, orig samples for
Chris@43 1438 // channel 1 etc), and then interleave them into the first
Chris@43 1439 // half of the buffer. Then we resample back into the second
Chris@43 1440 // half (interleaved) and de-interleave the results back to
Chris@43 1441 // the start of the buffer for insertion into the ringbuffers.
Chris@43 1442 // What a faff -- especially as we've already de-interleaved
Chris@43 1443 // the audio data from the source file elsewhere before we
Chris@43 1444 // even reach this point.
Chris@43 1445
Chris@43 1446 if (tmpSize < channels * work * 2) {
Chris@43 1447 delete[] tmp;
Chris@43 1448 tmp = new float[channels * work * 2];
Chris@43 1449 tmpSize = channels * work * 2;
Chris@43 1450 }
Chris@43 1451
Chris@43 1452 float *nonintlv = tmp + channels * work;
Chris@43 1453 float *intlv = tmp;
Chris@43 1454 float *srcout = tmp + channels * work;
Chris@43 1455
Chris@366 1456 for (int c = 0; c < channels; ++c) {
Chris@366 1457 for (int i = 0; i < orig; ++i) {
Chris@43 1458 nonintlv[channels * i + c] = 0.0f;
Chris@43 1459 }
Chris@43 1460 }
Chris@43 1461
Chris@366 1462 for (int c = 0; c < channels; ++c) {
Chris@43 1463 bufferPtrs[c] = nonintlv + c * orig;
Chris@43 1464 }
Chris@43 1465
Chris@163 1466 got = mixModels(f, orig, bufferPtrs); // also modifies f
Chris@43 1467
Chris@43 1468 // and interleave into first half
Chris@366 1469 for (int c = 0; c < channels; ++c) {
Chris@366 1470 for (int i = 0; i < got; ++i) {
Chris@43 1471 float sample = nonintlv[c * got + i];
Chris@43 1472 intlv[channels * i + c] = sample;
Chris@43 1473 }
Chris@43 1474 }
Chris@43 1475
Chris@43 1476 SRC_DATA data;
Chris@43 1477 data.data_in = intlv;
Chris@43 1478 data.data_out = srcout;
Chris@43 1479 data.input_frames = got;
Chris@43 1480 data.output_frames = work;
Chris@43 1481 data.src_ratio = ratio;
Chris@43 1482 data.end_of_input = 0;
Chris@43 1483
Chris@43 1484 int err = 0;
Chris@43 1485
Chris@62 1486 if (m_timeStretcher && m_timeStretcher->getTimeRatio() < 0.4) {
Chris@43 1487 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@293 1488 cout << "Using crappy converter" << endl;
Chris@43 1489 #endif
Chris@43 1490 err = src_process(m_crapConverter, &data);
Chris@43 1491 } else {
Chris@43 1492 err = src_process(m_converter, &data);
Chris@43 1493 }
Chris@43 1494
Chris@436 1495 sv_frame_t toCopy = sv_frame_t(double(got) * ratio + 0.1);
Chris@43 1496
Chris@43 1497 if (err) {
Chris@293 1498 cerr
Chris@43 1499 << "AudioCallbackPlaySourceFillThread: ERROR in samplerate conversion: "
Chris@293 1500 << src_strerror(err) << endl;
Chris@43 1501 //!!! Then what?
Chris@43 1502 } else {
Chris@43 1503 got = data.input_frames_used;
Chris@43 1504 toCopy = data.output_frames_gen;
Chris@43 1505 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@293 1506 cout << "Resampled " << got << " frames to " << toCopy << " frames" << endl;
Chris@43 1507 #endif
Chris@43 1508 }
Chris@43 1509
Chris@366 1510 for (int c = 0; c < channels; ++c) {
Chris@366 1511 for (int i = 0; i < toCopy; ++i) {
Chris@43 1512 tmp[i] = srcout[channels * i + c];
Chris@43 1513 }
Chris@43 1514 RingBuffer<float> *wb = getWriteRingBuffer(c);
Chris@436 1515 if (wb) wb->write(tmp, int(toCopy));
Chris@43 1516 }
Chris@43 1517
Chris@43 1518 m_writeBufferFill = f;
Chris@43 1519 if (readWriteEqual) m_readBufferFill = f;
Chris@43 1520
Chris@43 1521 } else {
Chris@43 1522
Chris@43 1523 // space must be a multiple of generatorBlockSize
Chris@436 1524 sv_frame_t reqSpace = space;
Chris@195 1525 space = (reqSpace / generatorBlockSize) * generatorBlockSize;
Chris@91 1526 if (space == 0) {
Chris@91 1527 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@293 1528 cout << "requested fill of " << reqSpace
Chris@195 1529 << " is less than generator block size of "
Chris@293 1530 << generatorBlockSize << ", leaving it" << endl;
Chris@91 1531 #endif
Chris@91 1532 return false;
Chris@91 1533 }
Chris@43 1534
Chris@43 1535 if (tmpSize < channels * space) {
Chris@43 1536 delete[] tmp;
Chris@43 1537 tmp = new float[channels * space];
Chris@43 1538 tmpSize = channels * space;
Chris@43 1539 }
Chris@43 1540
Chris@366 1541 for (int c = 0; c < channels; ++c) {
Chris@43 1542
Chris@43 1543 bufferPtrs[c] = tmp + c * space;
Chris@43 1544
Chris@366 1545 for (int i = 0; i < space; ++i) {
Chris@43 1546 tmp[c * space + i] = 0.0f;
Chris@43 1547 }
Chris@43 1548 }
Chris@43 1549
Chris@436 1550 sv_frame_t got = mixModels(f, space, bufferPtrs); // also modifies f
Chris@43 1551
Chris@366 1552 for (int c = 0; c < channels; ++c) {
Chris@43 1553
Chris@43 1554 RingBuffer<float> *wb = getWriteRingBuffer(c);
Chris@43 1555 if (wb) {
Chris@436 1556 int actual = wb->write(bufferPtrs[c], int(got));
Chris@43 1557 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@293 1558 cout << "Wrote " << actual << " samples for ch " << c << ", now "
Chris@43 1559 << wb->getReadSpace() << " to read"
Chris@293 1560 << endl;
Chris@43 1561 #endif
Chris@43 1562 if (actual < got) {
Chris@293 1563 cerr << "WARNING: Buffer overrun in channel " << c
Chris@43 1564 << ": wrote " << actual << " of " << got
Chris@293 1565 << " samples" << endl;
Chris@43 1566 }
Chris@43 1567 }
Chris@43 1568 }
Chris@43 1569
Chris@43 1570 m_writeBufferFill = f;
Chris@43 1571 if (readWriteEqual) m_readBufferFill = f;
Chris@43 1572
Chris@163 1573 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@293 1574 cout << "Read buffer fill is now " << m_readBufferFill << endl;
Chris@163 1575 #endif
Chris@163 1576
Chris@43 1577 //!!! how do we know when ended? need to mark up a fully-buffered flag and check this if we find the buffers empty in getSourceSamples
Chris@43 1578 }
Chris@43 1579
Chris@43 1580 return true;
Chris@43 1581 }
Chris@43 1582
Chris@434 1583 sv_frame_t
Chris@434 1584 AudioCallbackPlaySource::mixModels(sv_frame_t &frame, sv_frame_t count, float **buffers)
Chris@43 1585 {
Chris@434 1586 sv_frame_t processed = 0;
Chris@434 1587 sv_frame_t chunkStart = frame;
Chris@434 1588 sv_frame_t chunkSize = count;
Chris@434 1589 sv_frame_t selectionSize = 0;
Chris@434 1590 sv_frame_t nextChunkStart = chunkStart + chunkSize;
Chris@43 1591
Chris@43 1592 bool looping = m_viewManager->getPlayLoopMode();
Chris@43 1593 bool constrained = (m_viewManager->getPlaySelectionMode() &&
Chris@43 1594 !m_viewManager->getSelections().empty());
Chris@43 1595
Chris@43 1596 static float **chunkBufferPtrs = 0;
Chris@366 1597 static int chunkBufferPtrCount = 0;
Chris@366 1598 int channels = getTargetChannelCount();
Chris@43 1599
Chris@43 1600 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@293 1601 cout << "Selection playback: start " << frame << ", size " << count <<", channels " << channels << endl;
Chris@43 1602 #endif
Chris@43 1603
Chris@43 1604 if (chunkBufferPtrCount < channels) {
Chris@43 1605 if (chunkBufferPtrs) delete[] chunkBufferPtrs;
Chris@43 1606 chunkBufferPtrs = new float *[channels];
Chris@43 1607 chunkBufferPtrCount = channels;
Chris@43 1608 }
Chris@43 1609
Chris@366 1610 for (int c = 0; c < channels; ++c) {
Chris@43 1611 chunkBufferPtrs[c] = buffers[c];
Chris@43 1612 }
Chris@43 1613
Chris@43 1614 while (processed < count) {
Chris@43 1615
Chris@43 1616 chunkSize = count - processed;
Chris@43 1617 nextChunkStart = chunkStart + chunkSize;
Chris@43 1618 selectionSize = 0;
Chris@43 1619
Chris@434 1620 sv_frame_t fadeIn = 0, fadeOut = 0;
Chris@43 1621
Chris@43 1622 if (constrained) {
Chris@60 1623
Chris@434 1624 sv_frame_t rChunkStart =
Chris@60 1625 m_viewManager->alignPlaybackFrameToReference(chunkStart);
Chris@43 1626
Chris@43 1627 Selection selection =
Chris@60 1628 m_viewManager->getContainingSelection(rChunkStart, true);
Chris@43 1629
Chris@43 1630 if (selection.isEmpty()) {
Chris@43 1631 if (looping) {
Chris@43 1632 selection = *m_viewManager->getSelections().begin();
Chris@60 1633 chunkStart = m_viewManager->alignReferenceToPlaybackFrame
Chris@60 1634 (selection.getStartFrame());
Chris@43 1635 fadeIn = 50;
Chris@43 1636 }
Chris@43 1637 }
Chris@43 1638
Chris@43 1639 if (selection.isEmpty()) {
Chris@43 1640
Chris@43 1641 chunkSize = 0;
Chris@43 1642 nextChunkStart = chunkStart;
Chris@43 1643
Chris@43 1644 } else {
Chris@43 1645
Chris@434 1646 sv_frame_t sf = m_viewManager->alignReferenceToPlaybackFrame
Chris@60 1647 (selection.getStartFrame());
Chris@434 1648 sv_frame_t ef = m_viewManager->alignReferenceToPlaybackFrame
Chris@60 1649 (selection.getEndFrame());
Chris@43 1650
Chris@60 1651 selectionSize = ef - sf;
Chris@60 1652
Chris@60 1653 if (chunkStart < sf) {
Chris@60 1654 chunkStart = sf;
Chris@43 1655 fadeIn = 50;
Chris@43 1656 }
Chris@43 1657
Chris@43 1658 nextChunkStart = chunkStart + chunkSize;
Chris@43 1659
Chris@60 1660 if (nextChunkStart >= ef) {
Chris@60 1661 nextChunkStart = ef;
Chris@43 1662 fadeOut = 50;
Chris@43 1663 }
Chris@43 1664
Chris@43 1665 chunkSize = nextChunkStart - chunkStart;
Chris@43 1666 }
Chris@43 1667
Chris@43 1668 } else if (looping && m_lastModelEndFrame > 0) {
Chris@43 1669
Chris@43 1670 if (chunkStart >= m_lastModelEndFrame) {
Chris@43 1671 chunkStart = 0;
Chris@43 1672 }
Chris@43 1673 if (chunkSize > m_lastModelEndFrame - chunkStart) {
Chris@43 1674 chunkSize = m_lastModelEndFrame - chunkStart;
Chris@43 1675 }
Chris@43 1676 nextChunkStart = chunkStart + chunkSize;
Chris@43 1677 }
Chris@43 1678
Chris@293 1679 // cout << "chunkStart " << chunkStart << ", chunkSize " << chunkSize << ", nextChunkStart " << nextChunkStart << ", frame " << frame << ", count " << count << ", processed " << processed << endl;
Chris@43 1680
Chris@43 1681 if (!chunkSize) {
Chris@43 1682 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@293 1683 cout << "Ending selection playback at " << nextChunkStart << endl;
Chris@43 1684 #endif
Chris@43 1685 // We need to maintain full buffers so that the other
Chris@43 1686 // thread can tell where it's got to in the playback -- so
Chris@43 1687 // return the full amount here
Chris@43 1688 frame = frame + count;
Chris@43 1689 return count;
Chris@43 1690 }
Chris@43 1691
Chris@43 1692 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@293 1693 cout << "Selection playback: chunk at " << chunkStart << " -> " << nextChunkStart << " (size " << chunkSize << ")" << endl;
Chris@43 1694 #endif
Chris@43 1695
Chris@43 1696 if (selectionSize < 100) {
Chris@43 1697 fadeIn = 0;
Chris@43 1698 fadeOut = 0;
Chris@43 1699 } else if (selectionSize < 300) {
Chris@43 1700 if (fadeIn > 0) fadeIn = 10;
Chris@43 1701 if (fadeOut > 0) fadeOut = 10;
Chris@43 1702 }
Chris@43 1703
Chris@43 1704 if (fadeIn > 0) {
Chris@43 1705 if (processed * 2 < fadeIn) {
Chris@43 1706 fadeIn = processed * 2;
Chris@43 1707 }
Chris@43 1708 }
Chris@43 1709
Chris@43 1710 if (fadeOut > 0) {
Chris@43 1711 if ((count - processed - chunkSize) * 2 < fadeOut) {
Chris@43 1712 fadeOut = (count - processed - chunkSize) * 2;
Chris@43 1713 }
Chris@43 1714 }
Chris@43 1715
Chris@43 1716 for (std::set<Model *>::iterator mi = m_models.begin();
Chris@43 1717 mi != m_models.end(); ++mi) {
Chris@43 1718
Chris@366 1719 (void) m_audioGenerator->mixModel(*mi, chunkStart,
Chris@366 1720 chunkSize, chunkBufferPtrs,
Chris@366 1721 fadeIn, fadeOut);
Chris@43 1722 }
Chris@43 1723
Chris@366 1724 for (int c = 0; c < channels; ++c) {
Chris@43 1725 chunkBufferPtrs[c] += chunkSize;
Chris@43 1726 }
Chris@43 1727
Chris@43 1728 processed += chunkSize;
Chris@43 1729 chunkStart = nextChunkStart;
Chris@43 1730 }
Chris@43 1731
Chris@43 1732 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@293 1733 cout << "Returning selection playback " << processed << " frames to " << nextChunkStart << endl;
Chris@43 1734 #endif
Chris@43 1735
Chris@43 1736 frame = nextChunkStart;
Chris@43 1737 return processed;
Chris@43 1738 }
Chris@43 1739
Chris@43 1740 void
Chris@43 1741 AudioCallbackPlaySource::unifyRingBuffers()
Chris@43 1742 {
Chris@43 1743 if (m_readBuffers == m_writeBuffers) return;
Chris@43 1744
Chris@43 1745 // only unify if there will be something to read
Chris@366 1746 for (int c = 0; c < getTargetChannelCount(); ++c) {
Chris@43 1747 RingBuffer<float> *wb = getWriteRingBuffer(c);
Chris@43 1748 if (wb) {
Chris@43 1749 if (wb->getReadSpace() < m_blockSize * 2) {
Chris@43 1750 if ((m_writeBufferFill + m_blockSize * 2) <
Chris@43 1751 m_lastModelEndFrame) {
Chris@43 1752 // OK, we don't have enough and there's more to
Chris@43 1753 // read -- don't unify until we can do better
Chris@193 1754 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
Chris@233 1755 SVDEBUG << "AudioCallbackPlaySource::unifyRingBuffers: Not unifying: write buffer has less (" << wb->getReadSpace() << ") than " << m_blockSize*2 << " to read and write buffer fill (" << m_writeBufferFill << ") is not close to end frame (" << m_lastModelEndFrame << ")" << endl;
Chris@193 1756 #endif
Chris@43 1757 return;
Chris@43 1758 }
Chris@43 1759 }
Chris@43 1760 break;
Chris@43 1761 }
Chris@43 1762 }
Chris@43 1763
Chris@436 1764 sv_frame_t rf = m_readBufferFill;
Chris@43 1765 RingBuffer<float> *rb = getReadRingBuffer(0);
Chris@43 1766 if (rb) {
Chris@366 1767 int rs = rb->getReadSpace();
Chris@43 1768 //!!! incorrect when in non-contiguous selection, see comments elsewhere
Chris@293 1769 // cout << "rs = " << rs << endl;
Chris@43 1770 if (rs < rf) rf -= rs;
Chris@43 1771 else rf = 0;
Chris@43 1772 }
Chris@43 1773
Chris@193 1774 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
Chris@233 1775 SVDEBUG << "AudioCallbackPlaySource::unifyRingBuffers: m_readBufferFill = " << m_readBufferFill << ", rf = " << rf << ", m_writeBufferFill = " << m_writeBufferFill << endl;
Chris@193 1776 #endif
Chris@43 1777
Chris@436 1778 sv_frame_t wf = m_writeBufferFill;
Chris@436 1779 sv_frame_t skip = 0;
Chris@366 1780 for (int c = 0; c < getTargetChannelCount(); ++c) {
Chris@43 1781 RingBuffer<float> *wb = getWriteRingBuffer(c);
Chris@43 1782 if (wb) {
Chris@43 1783 if (c == 0) {
Chris@43 1784
Chris@366 1785 int wrs = wb->getReadSpace();
Chris@293 1786 // cout << "wrs = " << wrs << endl;
Chris@43 1787
Chris@43 1788 if (wrs < wf) wf -= wrs;
Chris@43 1789 else wf = 0;
Chris@293 1790 // cout << "wf = " << wf << endl;
Chris@43 1791
Chris@43 1792 if (wf < rf) skip = rf - wf;
Chris@43 1793 if (skip == 0) break;
Chris@43 1794 }
Chris@43 1795
Chris@293 1796 // cout << "skipping " << skip << endl;
Chris@436 1797 wb->skip(int(skip));
Chris@43 1798 }
Chris@43 1799 }
Chris@43 1800
Chris@43 1801 m_bufferScavenger.claim(m_readBuffers);
Chris@43 1802 m_readBuffers = m_writeBuffers;
Chris@43 1803 m_readBufferFill = m_writeBufferFill;
Chris@193 1804 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
Chris@293 1805 cerr << "unified" << endl;
Chris@193 1806 #endif
Chris@43 1807 }
Chris@43 1808
Chris@43 1809 void
Chris@43 1810 AudioCallbackPlaySource::FillThread::run()
Chris@43 1811 {
Chris@43 1812 AudioCallbackPlaySource &s(m_source);
Chris@43 1813
Chris@43 1814 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@293 1815 cout << "AudioCallbackPlaySourceFillThread starting" << endl;
Chris@43 1816 #endif
Chris@43 1817
Chris@43 1818 s.m_mutex.lock();
Chris@43 1819
Chris@43 1820 bool previouslyPlaying = s.m_playing;
Chris@43 1821 bool work = false;
Chris@43 1822
Chris@43 1823 while (!s.m_exiting) {
Chris@43 1824
Chris@43 1825 s.unifyRingBuffers();
Chris@43 1826 s.m_bufferScavenger.scavenge();
Chris@43 1827 s.m_pluginScavenger.scavenge();
Chris@43 1828
Chris@43 1829 if (work && s.m_playing && s.getSourceSampleRate()) {
Chris@43 1830
Chris@43 1831 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@293 1832 cout << "AudioCallbackPlaySourceFillThread: not waiting" << endl;
Chris@43 1833 #endif
Chris@43 1834
Chris@43 1835 s.m_mutex.unlock();
Chris@43 1836 s.m_mutex.lock();
Chris@43 1837
Chris@43 1838 } else {
Chris@43 1839
Chris@436 1840 double ms = 100;
Chris@43 1841 if (s.getSourceSampleRate() > 0) {
Chris@436 1842 ms = double(s.m_ringBufferSize) / s.getSourceSampleRate() * 1000.0;
Chris@43 1843 }
Chris@43 1844
Chris@43 1845 if (s.m_playing) ms /= 10;
Chris@43 1846
Chris@43 1847 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@293 1848 if (!s.m_playing) cout << endl;
Chris@293 1849 cout << "AudioCallbackPlaySourceFillThread: waiting for " << ms << "ms..." << endl;
Chris@43 1850 #endif
Chris@43 1851
Chris@366 1852 s.m_condition.wait(&s.m_mutex, int(ms));
Chris@43 1853 }
Chris@43 1854
Chris@43 1855 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@293 1856 cout << "AudioCallbackPlaySourceFillThread: awoken" << endl;
Chris@43 1857 #endif
Chris@43 1858
Chris@43 1859 work = false;
Chris@43 1860
Chris@103 1861 if (!s.getSourceSampleRate()) {
Chris@103 1862 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@293 1863 cout << "AudioCallbackPlaySourceFillThread: source sample rate is zero" << endl;
Chris@103 1864 #endif
Chris@103 1865 continue;
Chris@103 1866 }
Chris@43 1867
Chris@43 1868 bool playing = s.m_playing;
Chris@43 1869
Chris@43 1870 if (playing && !previouslyPlaying) {
Chris@43 1871 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@293 1872 cout << "AudioCallbackPlaySourceFillThread: playback state changed, resetting" << endl;
Chris@43 1873 #endif
Chris@366 1874 for (int c = 0; c < s.getTargetChannelCount(); ++c) {
Chris@43 1875 RingBuffer<float> *rb = s.getReadRingBuffer(c);
Chris@43 1876 if (rb) rb->reset();
Chris@43 1877 }
Chris@43 1878 }
Chris@43 1879 previouslyPlaying = playing;
Chris@43 1880
Chris@43 1881 work = s.fillBuffers();
Chris@43 1882 }
Chris@43 1883
Chris@43 1884 s.m_mutex.unlock();
Chris@43 1885 }
Chris@43 1886