annotate audio/AudioCallbackPlaySource.cpp @ 687:e0b0f3e163ca by-id

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