annotate audio/AudioCallbackPlaySource.cpp @ 566:36076ef960fb levelpanwidget

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