annotate audioio/AudioCallbackPlaySource.cpp @ 101:89a689720ee9 spectrogram-cache-rejig

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