annotate audio/AudioCallbackPlaySource.cpp @ 737:497d80d3b9c4

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