annotate audioio/AudioCallbackPlaySource.cpp @ 94:9cc9862333bd sv1-v1.2pre4

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