annotate audioio/AudioCallbackPlaySource.cpp @ 138:834ff910e3d2

* More icons, distinguish between icons for open session/audio and general open
author Chris Cannam
date Mon, 30 Apr 2007 14:07:21 +0000
parents 006c90387f40
children ee977f93f66c
rev   line source
Chris@0 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@0 4 Sonic Visualiser
Chris@0 5 An audio file viewer and annotation editor.
Chris@0 6 Centre for Digital Music, Queen Mary, University of London.
Chris@77 7 This file copyright 2006 Chris Cannam and QMUL.
Chris@0 8
Chris@0 9 This program is free software; you can redistribute it and/or
Chris@0 10 modify it under the terms of the GNU General Public License as
Chris@0 11 published by the Free Software Foundation; either version 2 of the
Chris@0 12 License, or (at your option) any later version. See the file
Chris@0 13 COPYING included with this distribution for more information.
Chris@0 14 */
Chris@0 15
Chris@0 16 #include "AudioCallbackPlaySource.h"
Chris@0 17
Chris@0 18 #include "AudioGenerator.h"
Chris@0 19
Chris@1 20 #include "data/model/Model.h"
Chris@1 21 #include "view/ViewManager.h"
Chris@0 22 #include "base/PlayParameterRepository.h"
Chris@32 23 #include "base/Preferences.h"
Chris@1 24 #include "data/model/DenseTimeValueModel.h"
Chris@1 25 #include "data/model/SparseOneDimensionalModel.h"
Chris@41 26 #include "plugin/RealTimePluginInstance.h"
Chris@14 27 #include "PhaseVocoderTimeStretcher.h"
Chris@0 28
Chris@0 29 #include <iostream>
Chris@0 30 #include <cassert>
Chris@0 31
Chris@0 32 //#define DEBUG_AUDIO_PLAY_SOURCE 1
Chris@14 33 //#define DEBUG_AUDIO_PLAY_SOURCE_PLAYING 1
Chris@0 34
Chris@0 35 const size_t AudioCallbackPlaySource::m_ringBufferSize = 131071;
Chris@0 36
Chris@0 37 AudioCallbackPlaySource::AudioCallbackPlaySource(ViewManager *manager) :
Chris@0 38 m_viewManager(manager),
Chris@0 39 m_audioGenerator(new AudioGenerator()),
Chris@0 40 m_readBuffers(0),
Chris@0 41 m_writeBuffers(0),
Chris@0 42 m_readBufferFill(0),
Chris@0 43 m_writeBufferFill(0),
Chris@0 44 m_bufferScavenger(1),
Chris@0 45 m_sourceChannelCount(0),
Chris@0 46 m_blockSize(1024),
Chris@0 47 m_sourceSampleRate(0),
Chris@0 48 m_targetSampleRate(0),
Chris@0 49 m_playLatency(0),
Chris@0 50 m_playing(false),
Chris@0 51 m_exiting(false),
Chris@0 52 m_lastModelEndFrame(0),
Chris@0 53 m_outputLeft(0.0),
Chris@0 54 m_outputRight(0.0),
Chris@41 55 m_auditioningPlugin(0),
Chris@42 56 m_auditioningPluginBypassed(false),
Chris@0 57 m_timeStretcher(0),
Chris@0 58 m_fillThread(0),
Chris@32 59 m_converter(0),
Chris@32 60 m_crapConverter(0),
Chris@32 61 m_resampleQuality(Preferences::getInstance()->getResampleQuality())
Chris@0 62 {
Chris@0 63 m_viewManager->setAudioPlaySource(this);
Chris@0 64
Chris@0 65 connect(m_viewManager, SIGNAL(selectionChanged()),
Chris@0 66 this, SLOT(selectionChanged()));
Chris@0 67 connect(m_viewManager, SIGNAL(playLoopModeChanged()),
Chris@0 68 this, SLOT(playLoopModeChanged()));
Chris@0 69 connect(m_viewManager, SIGNAL(playSelectionModeChanged()),
Chris@0 70 this, SLOT(playSelectionModeChanged()));
Chris@0 71
Chris@0 72 connect(PlayParameterRepository::getInstance(),
Chris@0 73 SIGNAL(playParametersChanged(PlayParameters *)),
Chris@0 74 this, SLOT(playParametersChanged(PlayParameters *)));
Chris@32 75
Chris@32 76 connect(Preferences::getInstance(),
Chris@32 77 SIGNAL(propertyChanged(PropertyContainer::PropertyName)),
Chris@32 78 this, SLOT(preferenceChanged(PropertyContainer::PropertyName)));
Chris@0 79 }
Chris@0 80
Chris@0 81 AudioCallbackPlaySource::~AudioCallbackPlaySource()
Chris@0 82 {
Chris@0 83 m_exiting = true;
Chris@0 84
Chris@0 85 if (m_fillThread) {
Chris@0 86 m_condition.wakeAll();
Chris@0 87 m_fillThread->wait();
Chris@0 88 delete m_fillThread;
Chris@0 89 }
Chris@0 90
Chris@0 91 clearModels();
Chris@0 92
Chris@0 93 if (m_readBuffers != m_writeBuffers) {
Chris@0 94 delete m_readBuffers;
Chris@0 95 }
Chris@0 96
Chris@0 97 delete m_writeBuffers;
Chris@0 98
Chris@0 99 delete m_audioGenerator;
Chris@0 100
Chris@0 101 m_bufferScavenger.scavenge(true);
Chris@41 102 m_pluginScavenger.scavenge(true);
Chris@41 103 m_timeStretcherScavenger.scavenge(true);
Chris@0 104 }
Chris@0 105
Chris@0 106 void
Chris@0 107 AudioCallbackPlaySource::addModel(Model *model)
Chris@0 108 {
Chris@0 109 if (m_models.find(model) != m_models.end()) return;
Chris@0 110
Chris@0 111 bool canPlay = m_audioGenerator->addModel(model);
Chris@0 112
Chris@0 113 m_mutex.lock();
Chris@0 114
Chris@0 115 m_models.insert(model);
Chris@0 116 if (model->getEndFrame() > m_lastModelEndFrame) {
Chris@0 117 m_lastModelEndFrame = model->getEndFrame();
Chris@0 118 }
Chris@0 119
Chris@0 120 bool buffersChanged = false, srChanged = false;
Chris@0 121
Chris@0 122 size_t modelChannels = 1;
Chris@0 123 DenseTimeValueModel *dtvm = dynamic_cast<DenseTimeValueModel *>(model);
Chris@0 124 if (dtvm) modelChannels = dtvm->getChannelCount();
Chris@0 125 if (modelChannels > m_sourceChannelCount) {
Chris@0 126 m_sourceChannelCount = modelChannels;
Chris@0 127 }
Chris@0 128
Chris@118 129 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@118 130 std::cout << "Adding model with " << modelChannels << " channels " << std::endl;
Chris@118 131 #endif
Chris@0 132
Chris@0 133 if (m_sourceSampleRate == 0) {
Chris@0 134
Chris@0 135 m_sourceSampleRate = model->getSampleRate();
Chris@0 136 srChanged = true;
Chris@0 137
Chris@0 138 } else if (model->getSampleRate() != m_sourceSampleRate) {
Chris@0 139
Chris@0 140 // If this is a dense time-value model and we have no other, we
Chris@0 141 // can just switch to this model's sample rate
Chris@0 142
Chris@0 143 if (dtvm) {
Chris@0 144
Chris@0 145 bool conflicting = false;
Chris@0 146
Chris@0 147 for (std::set<Model *>::const_iterator i = m_models.begin();
Chris@0 148 i != m_models.end(); ++i) {
Chris@118 149 DenseTimeValueModel *dtvm2 =
Chris@118 150 dynamic_cast<DenseTimeValueModel *>(*i);
Chris@118 151 if (dtvm2 && dtvm2 != dtvm &&
Chris@118 152 dtvm2->getSampleRate() != model->getSampleRate()) {
Chris@0 153 std::cerr << "AudioCallbackPlaySource::addModel: Conflicting dense time-value model " << *i << " found" << std::endl;
Chris@0 154 conflicting = true;
Chris@0 155 break;
Chris@0 156 }
Chris@0 157 }
Chris@0 158
Chris@0 159 if (conflicting) {
Chris@0 160
Chris@0 161 std::cerr << "AudioCallbackPlaySource::addModel: ERROR: "
Chris@0 162 << "New model sample rate does not match" << std::endl
Chris@0 163 << "existing model(s) (new " << model->getSampleRate()
Chris@0 164 << " vs " << m_sourceSampleRate
Chris@0 165 << "), playback will be wrong"
Chris@0 166 << std::endl;
Chris@0 167
Chris@0 168 emit sampleRateMismatch(model->getSampleRate(), m_sourceSampleRate,
Chris@0 169 false);
Chris@0 170 } else {
Chris@0 171 m_sourceSampleRate = model->getSampleRate();
Chris@0 172 srChanged = true;
Chris@0 173 }
Chris@0 174 }
Chris@0 175 }
Chris@0 176
Chris@0 177 if (!m_writeBuffers || (m_writeBuffers->size() < getTargetChannelCount())) {
Chris@0 178 clearRingBuffers(true, getTargetChannelCount());
Chris@0 179 buffersChanged = true;
Chris@0 180 } else {
Chris@0 181 if (canPlay) clearRingBuffers(true);
Chris@0 182 }
Chris@0 183
Chris@0 184 if (buffersChanged || srChanged) {
Chris@0 185 if (m_converter) {
Chris@0 186 src_delete(m_converter);
Chris@32 187 src_delete(m_crapConverter);
Chris@0 188 m_converter = 0;
Chris@32 189 m_crapConverter = 0;
Chris@0 190 }
Chris@0 191 }
Chris@0 192
Chris@0 193 m_mutex.unlock();
Chris@0 194
Chris@0 195 m_audioGenerator->setTargetChannelCount(getTargetChannelCount());
Chris@0 196
Chris@0 197 if (!m_fillThread) {
Chris@127 198 m_fillThread = new FillThread(*this);
Chris@0 199 m_fillThread->start();
Chris@0 200 }
Chris@0 201
Chris@0 202 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@118 203 std::cout << "AudioCallbackPlaySource::addModel: now have " << m_models.size() << " model(s) -- emitting modelReplaced" << std::endl;
Chris@0 204 #endif
Chris@0 205
Chris@0 206 if (buffersChanged || srChanged) {
Chris@0 207 emit modelReplaced();
Chris@0 208 }
Chris@0 209
Chris@0 210 m_condition.wakeAll();
Chris@0 211 }
Chris@0 212
Chris@0 213 void
Chris@0 214 AudioCallbackPlaySource::removeModel(Model *model)
Chris@0 215 {
Chris@0 216 m_mutex.lock();
Chris@0 217
Chris@118 218 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@118 219 std::cout << "AudioCallbackPlaySource::removeModel(" << model << ")" << std::endl;
Chris@118 220 #endif
Chris@118 221
Chris@0 222 m_models.erase(model);
Chris@0 223
Chris@0 224 if (m_models.empty()) {
Chris@0 225 if (m_converter) {
Chris@0 226 src_delete(m_converter);
Chris@32 227 src_delete(m_crapConverter);
Chris@0 228 m_converter = 0;
Chris@32 229 m_crapConverter = 0;
Chris@0 230 }
Chris@0 231 m_sourceSampleRate = 0;
Chris@0 232 }
Chris@0 233
Chris@0 234 size_t lastEnd = 0;
Chris@0 235 for (std::set<Model *>::const_iterator i = m_models.begin();
Chris@0 236 i != m_models.end(); ++i) {
Chris@106 237 // std::cout << "AudioCallbackPlaySource::removeModel(" << model << "): checking end frame on model " << *i << std::endl;
Chris@0 238 if ((*i)->getEndFrame() > lastEnd) lastEnd = (*i)->getEndFrame();
Chris@106 239 // std::cout << "(done, lastEnd now " << lastEnd << ")" << std::endl;
Chris@0 240 }
Chris@0 241 m_lastModelEndFrame = lastEnd;
Chris@0 242
Chris@0 243 m_mutex.unlock();
Chris@0 244
Chris@0 245 m_audioGenerator->removeModel(model);
Chris@0 246
Chris@0 247 clearRingBuffers();
Chris@0 248 }
Chris@0 249
Chris@0 250 void
Chris@0 251 AudioCallbackPlaySource::clearModels()
Chris@0 252 {
Chris@0 253 m_mutex.lock();
Chris@0 254
Chris@118 255 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@118 256 std::cout << "AudioCallbackPlaySource::clearModels()" << std::endl;
Chris@118 257 #endif
Chris@118 258
Chris@0 259 m_models.clear();
Chris@0 260
Chris@0 261 if (m_converter) {
Chris@0 262 src_delete(m_converter);
Chris@32 263 src_delete(m_crapConverter);
Chris@0 264 m_converter = 0;
Chris@32 265 m_crapConverter = 0;
Chris@0 266 }
Chris@0 267
Chris@0 268 m_lastModelEndFrame = 0;
Chris@0 269
Chris@0 270 m_sourceSampleRate = 0;
Chris@0 271
Chris@0 272 m_mutex.unlock();
Chris@0 273
Chris@0 274 m_audioGenerator->clearModels();
Chris@0 275 }
Chris@0 276
Chris@0 277 void
Chris@0 278 AudioCallbackPlaySource::clearRingBuffers(bool haveLock, size_t count)
Chris@0 279 {
Chris@0 280 if (!haveLock) m_mutex.lock();
Chris@0 281
Chris@0 282 if (count == 0) {
Chris@0 283 if (m_writeBuffers) count = m_writeBuffers->size();
Chris@0 284 }
Chris@0 285
Chris@0 286 size_t sf = m_readBufferFill;
Chris@0 287 RingBuffer<float> *rb = getReadRingBuffer(0);
Chris@0 288 if (rb) {
Chris@0 289 //!!! This is incorrect if we're in a non-contiguous selection
Chris@0 290 //Same goes for all related code (subtracting the read space
Chris@0 291 //from the fill frame to try to establish where the effective
Chris@0 292 //pre-resample/timestretch read pointer is)
Chris@0 293 size_t rs = rb->getReadSpace();
Chris@0 294 if (rs < sf) sf -= rs;
Chris@0 295 else sf = 0;
Chris@0 296 }
Chris@0 297 m_writeBufferFill = sf;
Chris@0 298
Chris@0 299 if (m_readBuffers != m_writeBuffers) {
Chris@0 300 delete m_writeBuffers;
Chris@0 301 }
Chris@0 302
Chris@0 303 m_writeBuffers = new RingBufferVector;
Chris@0 304
Chris@0 305 for (size_t i = 0; i < count; ++i) {
Chris@0 306 m_writeBuffers->push_back(new RingBuffer<float>(m_ringBufferSize));
Chris@0 307 }
Chris@0 308
Chris@106 309 // std::cout << "AudioCallbackPlaySource::clearRingBuffers: Created "
Chris@0 310 // << count << " write buffers" << std::endl;
Chris@0 311
Chris@0 312 if (!haveLock) {
Chris@0 313 m_mutex.unlock();
Chris@0 314 }
Chris@0 315 }
Chris@0 316
Chris@0 317 void
Chris@0 318 AudioCallbackPlaySource::play(size_t startFrame)
Chris@0 319 {
Chris@0 320 if (m_viewManager->getPlaySelectionMode() &&
Chris@0 321 !m_viewManager->getSelections().empty()) {
Chris@0 322 MultiSelection::SelectionList selections = m_viewManager->getSelections();
Chris@0 323 MultiSelection::SelectionList::iterator i = selections.begin();
Chris@0 324 if (i != selections.end()) {
Chris@0 325 if (startFrame < i->getStartFrame()) {
Chris@0 326 startFrame = i->getStartFrame();
Chris@0 327 } else {
Chris@0 328 MultiSelection::SelectionList::iterator j = selections.end();
Chris@0 329 --j;
Chris@0 330 if (startFrame >= j->getEndFrame()) {
Chris@0 331 startFrame = i->getStartFrame();
Chris@0 332 }
Chris@0 333 }
Chris@0 334 }
Chris@0 335 } else {
Chris@0 336 if (startFrame >= m_lastModelEndFrame) {
Chris@0 337 startFrame = 0;
Chris@0 338 }
Chris@0 339 }
Chris@0 340
Chris@0 341 // The fill thread will automatically empty its buffers before
Chris@0 342 // starting again if we have not so far been playing, but not if
Chris@0 343 // we're just re-seeking.
Chris@0 344
Chris@0 345 m_mutex.lock();
Chris@0 346 if (m_playing) {
Chris@0 347 m_readBufferFill = m_writeBufferFill = startFrame;
Chris@0 348 if (m_readBuffers) {
Chris@0 349 for (size_t c = 0; c < getTargetChannelCount(); ++c) {
Chris@0 350 RingBuffer<float> *rb = getReadRingBuffer(c);
Chris@0 351 if (rb) rb->reset();
Chris@0 352 }
Chris@0 353 }
Chris@0 354 if (m_converter) src_reset(m_converter);
Chris@32 355 if (m_crapConverter) src_reset(m_crapConverter);
Chris@0 356 } else {
Chris@0 357 if (m_converter) src_reset(m_converter);
Chris@32 358 if (m_crapConverter) src_reset(m_crapConverter);
Chris@0 359 m_readBufferFill = m_writeBufferFill = startFrame;
Chris@0 360 }
Chris@0 361 m_mutex.unlock();
Chris@0 362
Chris@0 363 m_audioGenerator->reset();
Chris@0 364
Chris@0 365 bool changed = !m_playing;
Chris@0 366 m_playing = true;
Chris@0 367 m_condition.wakeAll();
Chris@0 368 if (changed) emit playStatusChanged(m_playing);
Chris@0 369 }
Chris@0 370
Chris@0 371 void
Chris@0 372 AudioCallbackPlaySource::stop()
Chris@0 373 {
Chris@0 374 bool changed = m_playing;
Chris@0 375 m_playing = false;
Chris@0 376 m_condition.wakeAll();
Chris@0 377 if (changed) emit playStatusChanged(m_playing);
Chris@0 378 }
Chris@0 379
Chris@0 380 void
Chris@0 381 AudioCallbackPlaySource::selectionChanged()
Chris@0 382 {
Chris@0 383 if (m_viewManager->getPlaySelectionMode()) {
Chris@0 384 clearRingBuffers();
Chris@0 385 }
Chris@0 386 }
Chris@0 387
Chris@0 388 void
Chris@0 389 AudioCallbackPlaySource::playLoopModeChanged()
Chris@0 390 {
Chris@0 391 clearRingBuffers();
Chris@0 392 }
Chris@0 393
Chris@0 394 void
Chris@0 395 AudioCallbackPlaySource::playSelectionModeChanged()
Chris@0 396 {
Chris@0 397 if (!m_viewManager->getSelections().empty()) {
Chris@0 398 clearRingBuffers();
Chris@0 399 }
Chris@0 400 }
Chris@0 401
Chris@0 402 void
Chris@137 403 AudioCallbackPlaySource::playParametersChanged(PlayParameters *)
Chris@0 404 {
Chris@0 405 clearRingBuffers();
Chris@0 406 }
Chris@0 407
Chris@0 408 void
Chris@32 409 AudioCallbackPlaySource::preferenceChanged(PropertyContainer::PropertyName n)
Chris@32 410 {
Chris@32 411 if (n == "Resample Quality") {
Chris@32 412 setResampleQuality(Preferences::getInstance()->getResampleQuality());
Chris@32 413 }
Chris@32 414 }
Chris@32 415
Chris@32 416 void
Chris@42 417 AudioCallbackPlaySource::audioProcessingOverload()
Chris@42 418 {
Chris@42 419 RealTimePluginInstance *ap = m_auditioningPlugin;
Chris@42 420 if (ap && m_playing && !m_auditioningPluginBypassed) {
Chris@42 421 m_auditioningPluginBypassed = true;
Chris@42 422 emit audioOverloadPluginDisabled();
Chris@42 423 }
Chris@42 424 }
Chris@42 425
Chris@42 426 void
Chris@0 427 AudioCallbackPlaySource::setTargetBlockSize(size_t size)
Chris@0 428 {
Chris@106 429 // std::cout << "AudioCallbackPlaySource::setTargetBlockSize() -> " << size << std::endl;
Chris@0 430 assert(size < m_ringBufferSize);
Chris@0 431 m_blockSize = size;
Chris@0 432 }
Chris@0 433
Chris@0 434 size_t
Chris@0 435 AudioCallbackPlaySource::getTargetBlockSize() const
Chris@0 436 {
Chris@106 437 // std::cout << "AudioCallbackPlaySource::getTargetBlockSize() -> " << m_blockSize << std::endl;
Chris@0 438 return m_blockSize;
Chris@0 439 }
Chris@0 440
Chris@0 441 void
Chris@0 442 AudioCallbackPlaySource::setTargetPlayLatency(size_t latency)
Chris@0 443 {
Chris@0 444 m_playLatency = latency;
Chris@0 445 }
Chris@0 446
Chris@0 447 size_t
Chris@0 448 AudioCallbackPlaySource::getTargetPlayLatency() const
Chris@0 449 {
Chris@0 450 return m_playLatency;
Chris@0 451 }
Chris@0 452
Chris@0 453 size_t
Chris@0 454 AudioCallbackPlaySource::getCurrentPlayingFrame()
Chris@0 455 {
Chris@0 456 bool resample = false;
Chris@0 457 double ratio = 1.0;
Chris@0 458
Chris@0 459 if (getSourceSampleRate() != getTargetSampleRate()) {
Chris@0 460 resample = true;
Chris@0 461 ratio = double(getSourceSampleRate()) / double(getTargetSampleRate());
Chris@0 462 }
Chris@0 463
Chris@0 464 size_t readSpace = 0;
Chris@0 465 for (size_t c = 0; c < getTargetChannelCount(); ++c) {
Chris@0 466 RingBuffer<float> *rb = getReadRingBuffer(c);
Chris@0 467 if (rb) {
Chris@0 468 size_t spaceHere = rb->getReadSpace();
Chris@0 469 if (c == 0 || spaceHere < readSpace) readSpace = spaceHere;
Chris@0 470 }
Chris@0 471 }
Chris@0 472
Chris@0 473 if (resample) {
Chris@0 474 readSpace = size_t(readSpace * ratio + 0.1);
Chris@0 475 }
Chris@0 476
Chris@0 477 size_t latency = m_playLatency;
Chris@0 478 if (resample) latency = size_t(m_playLatency * ratio + 0.1);
Chris@16 479
Chris@16 480 PhaseVocoderTimeStretcher *timeStretcher = m_timeStretcher;
Chris@0 481 if (timeStretcher) {
Chris@16 482 latency += timeStretcher->getProcessingLatency();
Chris@0 483 }
Chris@0 484
Chris@0 485 latency += readSpace;
Chris@0 486 size_t bufferedFrame = m_readBufferFill;
Chris@0 487
Chris@0 488 bool looping = m_viewManager->getPlayLoopMode();
Chris@0 489 bool constrained = (m_viewManager->getPlaySelectionMode() &&
Chris@0 490 !m_viewManager->getSelections().empty());
Chris@0 491
Chris@0 492 size_t framePlaying = bufferedFrame;
Chris@0 493
Chris@0 494 if (looping && !constrained) {
Chris@0 495 while (framePlaying < latency) framePlaying += m_lastModelEndFrame;
Chris@0 496 }
Chris@0 497
Chris@0 498 if (framePlaying > latency) framePlaying -= latency;
Chris@0 499 else framePlaying = 0;
Chris@0 500
Chris@0 501 if (!constrained) {
Chris@0 502 if (!looping && framePlaying > m_lastModelEndFrame) {
Chris@0 503 framePlaying = m_lastModelEndFrame;
Chris@0 504 stop();
Chris@0 505 }
Chris@0 506 return framePlaying;
Chris@0 507 }
Chris@0 508
Chris@0 509 MultiSelection::SelectionList selections = m_viewManager->getSelections();
Chris@0 510 MultiSelection::SelectionList::const_iterator i;
Chris@0 511
Chris@137 512 // i = selections.begin();
Chris@137 513 // size_t rangeStart = i->getStartFrame();
Chris@0 514
Chris@0 515 i = selections.end();
Chris@0 516 --i;
Chris@0 517 size_t rangeEnd = i->getEndFrame();
Chris@0 518
Chris@0 519 for (i = selections.begin(); i != selections.end(); ++i) {
Chris@0 520 if (i->contains(bufferedFrame)) break;
Chris@0 521 }
Chris@0 522
Chris@0 523 size_t f = bufferedFrame;
Chris@0 524
Chris@106 525 // std::cout << "getCurrentPlayingFrame: f=" << f << ", latency=" << latency << ", rangeEnd=" << rangeEnd << std::endl;
Chris@0 526
Chris@0 527 if (i == selections.end()) {
Chris@0 528 --i;
Chris@0 529 if (i->getEndFrame() + latency < f) {
Chris@106 530 // std::cout << "framePlaying = " << framePlaying << ", rangeEnd = " << rangeEnd << std::endl;
Chris@0 531
Chris@0 532 if (!looping && (framePlaying > rangeEnd)) {
Chris@106 533 // std::cout << "STOPPING" << std::endl;
Chris@0 534 stop();
Chris@0 535 return rangeEnd;
Chris@0 536 } else {
Chris@0 537 return framePlaying;
Chris@0 538 }
Chris@0 539 } else {
Chris@106 540 // std::cout << "latency <- " << latency << "-(" << f << "-" << i->getEndFrame() << ")" << std::endl;
Chris@0 541 latency -= (f - i->getEndFrame());
Chris@0 542 f = i->getEndFrame();
Chris@0 543 }
Chris@0 544 }
Chris@0 545
Chris@106 546 // std::cout << "i=(" << i->getStartFrame() << "," << i->getEndFrame() << ") f=" << f << ", latency=" << latency << std::endl;
Chris@0 547
Chris@0 548 while (latency > 0) {
Chris@0 549 size_t offset = f - i->getStartFrame();
Chris@0 550 if (offset >= latency) {
Chris@0 551 if (f > latency) {
Chris@0 552 framePlaying = f - latency;
Chris@0 553 } else {
Chris@0 554 framePlaying = 0;
Chris@0 555 }
Chris@0 556 break;
Chris@0 557 } else {
Chris@0 558 if (i == selections.begin()) {
Chris@0 559 if (looping) {
Chris@0 560 i = selections.end();
Chris@0 561 }
Chris@0 562 }
Chris@0 563 latency -= offset;
Chris@0 564 --i;
Chris@0 565 f = i->getEndFrame();
Chris@0 566 }
Chris@0 567 }
Chris@0 568
Chris@0 569 return framePlaying;
Chris@0 570 }
Chris@0 571
Chris@0 572 void
Chris@0 573 AudioCallbackPlaySource::setOutputLevels(float left, float right)
Chris@0 574 {
Chris@0 575 m_outputLeft = left;
Chris@0 576 m_outputRight = right;
Chris@0 577 }
Chris@0 578
Chris@0 579 bool
Chris@0 580 AudioCallbackPlaySource::getOutputLevels(float &left, float &right)
Chris@0 581 {
Chris@0 582 left = m_outputLeft;
Chris@0 583 right = m_outputRight;
Chris@0 584 return true;
Chris@0 585 }
Chris@0 586
Chris@0 587 void
Chris@0 588 AudioCallbackPlaySource::setTargetSampleRate(size_t sr)
Chris@0 589 {
Chris@0 590 m_targetSampleRate = sr;
Chris@32 591 initialiseConverter();
Chris@32 592 }
Chris@32 593
Chris@32 594 void
Chris@32 595 AudioCallbackPlaySource::initialiseConverter()
Chris@32 596 {
Chris@32 597 m_mutex.lock();
Chris@32 598
Chris@32 599 if (m_converter) {
Chris@32 600 src_delete(m_converter);
Chris@32 601 src_delete(m_crapConverter);
Chris@32 602 m_converter = 0;
Chris@32 603 m_crapConverter = 0;
Chris@32 604 }
Chris@0 605
Chris@0 606 if (getSourceSampleRate() != getTargetSampleRate()) {
Chris@0 607
Chris@0 608 int err = 0;
Chris@32 609
Chris@32 610 m_converter = src_new(m_resampleQuality == 2 ? SRC_SINC_BEST_QUALITY :
Chris@32 611 m_resampleQuality == 1 ? SRC_SINC_MEDIUM_QUALITY :
Chris@32 612 m_resampleQuality == 0 ? SRC_SINC_FASTEST :
Chris@32 613 SRC_SINC_MEDIUM_QUALITY,
Chris@0 614 getTargetChannelCount(), &err);
Chris@32 615
Chris@32 616 if (m_converter) {
Chris@32 617 m_crapConverter = src_new(SRC_LINEAR,
Chris@32 618 getTargetChannelCount(),
Chris@32 619 &err);
Chris@32 620 }
Chris@32 621
Chris@32 622 if (!m_converter || !m_crapConverter) {
Chris@0 623 std::cerr
Chris@0 624 << "AudioCallbackPlaySource::setModel: ERROR in creating samplerate converter: "
Chris@0 625 << src_strerror(err) << std::endl;
Chris@0 626
Chris@32 627 if (m_converter) {
Chris@32 628 src_delete(m_converter);
Chris@32 629 m_converter = 0;
Chris@32 630 }
Chris@32 631
Chris@32 632 if (m_crapConverter) {
Chris@32 633 src_delete(m_crapConverter);
Chris@32 634 m_crapConverter = 0;
Chris@32 635 }
Chris@32 636
Chris@32 637 m_mutex.unlock();
Chris@32 638
Chris@0 639 emit sampleRateMismatch(getSourceSampleRate(),
Chris@0 640 getTargetSampleRate(),
Chris@0 641 false);
Chris@0 642 } else {
Chris@0 643
Chris@32 644 m_mutex.unlock();
Chris@32 645
Chris@0 646 emit sampleRateMismatch(getSourceSampleRate(),
Chris@0 647 getTargetSampleRate(),
Chris@0 648 true);
Chris@0 649 }
Chris@32 650 } else {
Chris@32 651 m_mutex.unlock();
Chris@0 652 }
Chris@0 653 }
Chris@0 654
Chris@32 655 void
Chris@32 656 AudioCallbackPlaySource::setResampleQuality(int q)
Chris@32 657 {
Chris@32 658 if (q == m_resampleQuality) return;
Chris@32 659 m_resampleQuality = q;
Chris@32 660
Chris@32 661 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@32 662 std::cerr << "AudioCallbackPlaySource::setResampleQuality: setting to "
Chris@32 663 << m_resampleQuality << std::endl;
Chris@32 664 #endif
Chris@32 665
Chris@32 666 initialiseConverter();
Chris@32 667 }
Chris@32 668
Chris@41 669 void
Chris@41 670 AudioCallbackPlaySource::setAuditioningPlugin(RealTimePluginInstance *plugin)
Chris@41 671 {
Chris@41 672 RealTimePluginInstance *formerPlugin = m_auditioningPlugin;
Chris@41 673 m_auditioningPlugin = plugin;
Chris@42 674 m_auditioningPluginBypassed = false;
Chris@41 675 if (formerPlugin) m_pluginScavenger.claim(formerPlugin);
Chris@41 676 }
Chris@41 677
Chris@0 678 size_t
Chris@0 679 AudioCallbackPlaySource::getTargetSampleRate() const
Chris@0 680 {
Chris@0 681 if (m_targetSampleRate) return m_targetSampleRate;
Chris@0 682 else return getSourceSampleRate();
Chris@0 683 }
Chris@0 684
Chris@0 685 size_t
Chris@0 686 AudioCallbackPlaySource::getSourceChannelCount() const
Chris@0 687 {
Chris@0 688 return m_sourceChannelCount;
Chris@0 689 }
Chris@0 690
Chris@0 691 size_t
Chris@0 692 AudioCallbackPlaySource::getTargetChannelCount() const
Chris@0 693 {
Chris@0 694 if (m_sourceChannelCount < 2) return 2;
Chris@0 695 return m_sourceChannelCount;
Chris@0 696 }
Chris@0 697
Chris@0 698 size_t
Chris@0 699 AudioCallbackPlaySource::getSourceSampleRate() const
Chris@0 700 {
Chris@0 701 return m_sourceSampleRate;
Chris@0 702 }
Chris@0 703
Chris@0 704 void
Chris@26 705 AudioCallbackPlaySource::setTimeStretch(float factor, bool sharpen, bool mono)
Chris@0 706 {
Chris@0 707 // Avoid locks -- create, assign, mark old one for scavenging
Chris@0 708 // later (as a call to getSourceSamples may still be using it)
Chris@0 709
Chris@16 710 PhaseVocoderTimeStretcher *existingStretcher = m_timeStretcher;
Chris@0 711
Chris@26 712 size_t channels = getTargetChannelCount();
Chris@26 713 if (mono) channels = 1;
Chris@26 714
Chris@16 715 if (existingStretcher &&
Chris@16 716 existingStretcher->getRatio() == factor &&
Chris@26 717 existingStretcher->getSharpening() == sharpen &&
Chris@26 718 existingStretcher->getChannelCount() == channels) {
Chris@0 719 return;
Chris@0 720 }
Chris@0 721
Chris@12 722 if (factor != 1) {
Chris@25 723
Chris@25 724 if (existingStretcher &&
Chris@26 725 existingStretcher->getSharpening() == sharpen &&
Chris@26 726 existingStretcher->getChannelCount() == channels) {
Chris@25 727 existingStretcher->setRatio(factor);
Chris@25 728 return;
Chris@25 729 }
Chris@25 730
Chris@16 731 PhaseVocoderTimeStretcher *newStretcher = new PhaseVocoderTimeStretcher
Chris@22 732 (getTargetSampleRate(),
Chris@26 733 channels,
Chris@16 734 factor,
Chris@16 735 sharpen,
Chris@31 736 getTargetBlockSize());
Chris@26 737
Chris@0 738 m_timeStretcher = newStretcher;
Chris@26 739
Chris@0 740 } else {
Chris@0 741 m_timeStretcher = 0;
Chris@0 742 }
Chris@0 743
Chris@0 744 if (existingStretcher) {
Chris@0 745 m_timeStretcherScavenger.claim(existingStretcher);
Chris@0 746 }
Chris@0 747 }
Chris@26 748
Chris@0 749 size_t
Chris@0 750 AudioCallbackPlaySource::getSourceSamples(size_t count, float **buffer)
Chris@0 751 {
Chris@0 752 if (!m_playing) {
Chris@0 753 for (size_t ch = 0; ch < getTargetChannelCount(); ++ch) {
Chris@0 754 for (size_t i = 0; i < count; ++i) {
Chris@0 755 buffer[ch][i] = 0.0;
Chris@0 756 }
Chris@0 757 }
Chris@0 758 return 0;
Chris@0 759 }
Chris@0 760
Chris@106 761 // Ensure that all buffers have at least the amount of data we
Chris@106 762 // need -- else reduce the size of our requests correspondingly
Chris@106 763
Chris@106 764 for (size_t ch = 0; ch < getTargetChannelCount(); ++ch) {
Chris@106 765
Chris@106 766 RingBuffer<float> *rb = getReadRingBuffer(ch);
Chris@106 767
Chris@106 768 if (!rb) {
Chris@106 769 std::cerr << "WARNING: AudioCallbackPlaySource::getSourceSamples: "
Chris@106 770 << "No ring buffer available for channel " << ch
Chris@106 771 << ", returning no data here" << std::endl;
Chris@106 772 count = 0;
Chris@106 773 break;
Chris@106 774 }
Chris@106 775
Chris@106 776 size_t rs = rb->getReadSpace();
Chris@106 777 if (rs < count) {
Chris@106 778 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@106 779 std::cerr << "WARNING: AudioCallbackPlaySource::getSourceSamples: "
Chris@106 780 << "Ring buffer for channel " << ch << " has only "
Chris@106 781 << rs << " (of " << count << ") samples available, "
Chris@106 782 << "reducing request size" << std::endl;
Chris@106 783 #endif
Chris@106 784 count = rs;
Chris@106 785 }
Chris@106 786 }
Chris@106 787
Chris@106 788 if (count == 0) return 0;
Chris@106 789
Chris@16 790 PhaseVocoderTimeStretcher *ts = m_timeStretcher;
Chris@0 791
Chris@16 792 if (!ts || ts->getRatio() == 1) {
Chris@0 793
Chris@0 794 size_t got = 0;
Chris@0 795
Chris@0 796 for (size_t ch = 0; ch < getTargetChannelCount(); ++ch) {
Chris@0 797
Chris@0 798 RingBuffer<float> *rb = getReadRingBuffer(ch);
Chris@0 799
Chris@0 800 if (rb) {
Chris@0 801
Chris@0 802 // this is marginally more likely to leave our channels in
Chris@0 803 // sync after a processing failure than just passing "count":
Chris@0 804 size_t request = count;
Chris@0 805 if (ch > 0) request = got;
Chris@0 806
Chris@0 807 got = rb->read(buffer[ch], request);
Chris@0 808
Chris@0 809 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
Chris@106 810 std::cout << "AudioCallbackPlaySource::getSamples: got " << got << " (of " << count << ") samples on channel " << ch << ", signalling for more (possibly)" << std::endl;
Chris@0 811 #endif
Chris@0 812 }
Chris@0 813
Chris@0 814 for (size_t ch = 0; ch < getTargetChannelCount(); ++ch) {
Chris@0 815 for (size_t i = got; i < count; ++i) {
Chris@0 816 buffer[ch][i] = 0.0;
Chris@0 817 }
Chris@0 818 }
Chris@0 819 }
Chris@0 820
Chris@41 821 applyAuditioningEffect(count, buffer);
Chris@41 822
Chris@0 823 m_condition.wakeAll();
Chris@0 824 return got;
Chris@0 825 }
Chris@0 826
Chris@16 827 float ratio = ts->getRatio();
Chris@0 828
Chris@16 829 // std::cout << "ratio = " << ratio << std::endl;
Chris@0 830
Chris@26 831 size_t channels = getTargetChannelCount();
Chris@26 832 bool mix = (channels > 1 && ts->getChannelCount() == 1);
Chris@26 833
Chris@16 834 size_t available;
Chris@0 835
Chris@31 836 int warned = 0;
Chris@31 837
Chris@31 838 // We want output blocks of e.g. 1024 (probably fixed, certainly
Chris@31 839 // bounded). We can provide input blocks of any size (unbounded)
Chris@31 840 // at the timestretcher's request. The input block for a given
Chris@31 841 // output is approx output / ratio, but we can't predict it
Chris@31 842 // exactly, for an adaptive timestretcher. The stretcher will
Chris@56 843 // need some additional buffer space. See the time stretcher code
Chris@56 844 // and comments.
Chris@31 845
Chris@16 846 while ((available = ts->getAvailableOutputSamples()) < count) {
Chris@0 847
Chris@16 848 size_t reqd = lrintf((count - available) / ratio);
Chris@16 849 reqd = std::max(reqd, ts->getRequiredInputSamples());
Chris@16 850 if (reqd == 0) reqd = 1;
Chris@16 851
Chris@16 852 float *ib[channels];
Chris@0 853
Chris@16 854 size_t got = reqd;
Chris@0 855
Chris@26 856 if (mix) {
Chris@26 857 for (size_t c = 0; c < channels; ++c) {
Chris@26 858 if (c == 0) ib[c] = new float[reqd]; //!!! fix -- this is a rt function
Chris@26 859 else ib[c] = 0;
Chris@26 860 RingBuffer<float> *rb = getReadRingBuffer(c);
Chris@26 861 if (rb) {
Chris@26 862 size_t gotHere;
Chris@26 863 if (c > 0) gotHere = rb->readAdding(ib[0], got);
Chris@26 864 else gotHere = rb->read(ib[0], got);
Chris@26 865 if (gotHere < got) got = gotHere;
Chris@26 866 }
Chris@26 867 }
Chris@26 868 } else {
Chris@26 869 for (size_t c = 0; c < channels; ++c) {
Chris@26 870 ib[c] = new float[reqd]; //!!! fix -- this is a rt function
Chris@26 871 RingBuffer<float> *rb = getReadRingBuffer(c);
Chris@26 872 if (rb) {
Chris@26 873 size_t gotHere = rb->read(ib[c], got);
Chris@26 874 if (gotHere < got) got = gotHere;
Chris@26 875 }
Chris@16 876 }
Chris@16 877 }
Chris@0 878
Chris@16 879 if (got < reqd) {
Chris@16 880 std::cerr << "WARNING: Read underrun in playback ("
Chris@16 881 << got << " < " << reqd << ")" << std::endl;
Chris@16 882 }
Chris@16 883
Chris@16 884 ts->putInput(ib, got);
Chris@16 885
Chris@16 886 for (size_t c = 0; c < channels; ++c) {
Chris@16 887 delete[] ib[c];
Chris@16 888 }
Chris@16 889
Chris@16 890 if (got == 0) break;
Chris@16 891
Chris@16 892 if (ts->getAvailableOutputSamples() == available) {
Chris@31 893 std::cerr << "WARNING: AudioCallbackPlaySource::getSamples: Added " << got << " samples to time stretcher, created no new available output samples (warned = " << warned << ")" << std::endl;
Chris@31 894 if (++warned == 5) break;
Chris@16 895 }
Chris@0 896 }
Chris@0 897
Chris@16 898 ts->getOutput(buffer, count);
Chris@0 899
Chris@26 900 if (mix) {
Chris@26 901 for (size_t c = 1; c < channels; ++c) {
Chris@26 902 for (size_t i = 0; i < count; ++i) {
Chris@26 903 buffer[c][i] = buffer[0][i] / channels;
Chris@26 904 }
Chris@26 905 }
Chris@26 906 for (size_t i = 0; i < count; ++i) {
Chris@26 907 buffer[0][i] /= channels;
Chris@26 908 }
Chris@26 909 }
Chris@26 910
Chris@41 911 applyAuditioningEffect(count, buffer);
Chris@41 912
Chris@16 913 m_condition.wakeAll();
Chris@12 914
Chris@0 915 return count;
Chris@0 916 }
Chris@0 917
Chris@41 918 void
Chris@41 919 AudioCallbackPlaySource::applyAuditioningEffect(size_t count, float **buffers)
Chris@41 920 {
Chris@42 921 if (m_auditioningPluginBypassed) return;
Chris@41 922 RealTimePluginInstance *plugin = m_auditioningPlugin;
Chris@41 923 if (!plugin) return;
Chris@41 924
Chris@41 925 if (plugin->getAudioInputCount() != getTargetChannelCount()) {
Chris@43 926 // std::cerr << "plugin input count " << plugin->getAudioInputCount()
Chris@43 927 // << " != our channel count " << getTargetChannelCount()
Chris@43 928 // << std::endl;
Chris@41 929 return;
Chris@41 930 }
Chris@41 931 if (plugin->getAudioOutputCount() != getTargetChannelCount()) {
Chris@43 932 // std::cerr << "plugin output count " << plugin->getAudioOutputCount()
Chris@43 933 // << " != our channel count " << getTargetChannelCount()
Chris@43 934 // << std::endl;
Chris@41 935 return;
Chris@41 936 }
Chris@41 937 if (plugin->getBufferSize() != count) {
Chris@43 938 // std::cerr << "plugin buffer size " << plugin->getBufferSize()
Chris@43 939 // << " != our block size " << count
Chris@43 940 // << std::endl;
Chris@41 941 return;
Chris@41 942 }
Chris@41 943
Chris@41 944 float **ib = plugin->getAudioInputBuffers();
Chris@41 945 float **ob = plugin->getAudioOutputBuffers();
Chris@41 946
Chris@41 947 for (size_t c = 0; c < getTargetChannelCount(); ++c) {
Chris@41 948 for (size_t i = 0; i < count; ++i) {
Chris@41 949 ib[c][i] = buffers[c][i];
Chris@41 950 }
Chris@41 951 }
Chris@41 952
Chris@41 953 plugin->run(Vamp::RealTime::zeroTime);
Chris@41 954
Chris@41 955 for (size_t c = 0; c < getTargetChannelCount(); ++c) {
Chris@41 956 for (size_t i = 0; i < count; ++i) {
Chris@41 957 buffers[c][i] = ob[c][i];
Chris@41 958 }
Chris@41 959 }
Chris@41 960 }
Chris@41 961
Chris@0 962 // Called from fill thread, m_playing true, mutex held
Chris@0 963 bool
Chris@0 964 AudioCallbackPlaySource::fillBuffers()
Chris@0 965 {
Chris@0 966 static float *tmp = 0;
Chris@0 967 static size_t tmpSize = 0;
Chris@0 968
Chris@0 969 size_t space = 0;
Chris@0 970 for (size_t c = 0; c < getTargetChannelCount(); ++c) {
Chris@0 971 RingBuffer<float> *wb = getWriteRingBuffer(c);
Chris@0 972 if (wb) {
Chris@0 973 size_t spaceHere = wb->getWriteSpace();
Chris@0 974 if (c == 0 || spaceHere < space) space = spaceHere;
Chris@0 975 }
Chris@0 976 }
Chris@0 977
Chris@0 978 if (space == 0) return false;
Chris@0 979
Chris@0 980 size_t f = m_writeBufferFill;
Chris@0 981
Chris@0 982 bool readWriteEqual = (m_readBuffers == m_writeBuffers);
Chris@0 983
Chris@0 984 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@0 985 std::cout << "AudioCallbackPlaySourceFillThread: filling " << space << " frames" << std::endl;
Chris@0 986 #endif
Chris@0 987
Chris@0 988 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@0 989 std::cout << "buffered to " << f << " already" << std::endl;
Chris@0 990 #endif
Chris@0 991
Chris@0 992 bool resample = (getSourceSampleRate() != getTargetSampleRate());
Chris@0 993
Chris@0 994 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@0 995 std::cout << (resample ? "" : "not ") << "resampling (source " << getSourceSampleRate() << ", target " << getTargetSampleRate() << ")" << std::endl;
Chris@0 996 #endif
Chris@0 997
Chris@0 998 size_t channels = getTargetChannelCount();
Chris@0 999
Chris@0 1000 size_t orig = space;
Chris@0 1001 size_t got = 0;
Chris@0 1002
Chris@0 1003 static float **bufferPtrs = 0;
Chris@0 1004 static size_t bufferPtrCount = 0;
Chris@0 1005
Chris@0 1006 if (bufferPtrCount < channels) {
Chris@0 1007 if (bufferPtrs) delete[] bufferPtrs;
Chris@0 1008 bufferPtrs = new float *[channels];
Chris@0 1009 bufferPtrCount = channels;
Chris@0 1010 }
Chris@0 1011
Chris@0 1012 size_t generatorBlockSize = m_audioGenerator->getBlockSize();
Chris@0 1013
Chris@0 1014 if (resample && !m_converter) {
Chris@0 1015 static bool warned = false;
Chris@0 1016 if (!warned) {
Chris@0 1017 std::cerr << "WARNING: sample rates differ, but no converter available!" << std::endl;
Chris@0 1018 warned = true;
Chris@0 1019 }
Chris@0 1020 }
Chris@0 1021
Chris@0 1022 if (resample && m_converter) {
Chris@0 1023
Chris@0 1024 double ratio =
Chris@0 1025 double(getTargetSampleRate()) / double(getSourceSampleRate());
Chris@0 1026 orig = size_t(orig / ratio + 0.1);
Chris@0 1027
Chris@0 1028 // orig must be a multiple of generatorBlockSize
Chris@0 1029 orig = (orig / generatorBlockSize) * generatorBlockSize;
Chris@0 1030 if (orig == 0) return false;
Chris@0 1031
Chris@0 1032 size_t work = std::max(orig, space);
Chris@0 1033
Chris@0 1034 // We only allocate one buffer, but we use it in two halves.
Chris@0 1035 // We place the non-interleaved values in the second half of
Chris@0 1036 // the buffer (orig samples for channel 0, orig samples for
Chris@0 1037 // channel 1 etc), and then interleave them into the first
Chris@0 1038 // half of the buffer. Then we resample back into the second
Chris@0 1039 // half (interleaved) and de-interleave the results back to
Chris@0 1040 // the start of the buffer for insertion into the ringbuffers.
Chris@0 1041 // What a faff -- especially as we've already de-interleaved
Chris@0 1042 // the audio data from the source file elsewhere before we
Chris@0 1043 // even reach this point.
Chris@0 1044
Chris@0 1045 if (tmpSize < channels * work * 2) {
Chris@0 1046 delete[] tmp;
Chris@0 1047 tmp = new float[channels * work * 2];
Chris@0 1048 tmpSize = channels * work * 2;
Chris@0 1049 }
Chris@0 1050
Chris@0 1051 float *nonintlv = tmp + channels * work;
Chris@0 1052 float *intlv = tmp;
Chris@0 1053 float *srcout = tmp + channels * work;
Chris@0 1054
Chris@0 1055 for (size_t c = 0; c < channels; ++c) {
Chris@0 1056 for (size_t i = 0; i < orig; ++i) {
Chris@0 1057 nonintlv[channels * i + c] = 0.0f;
Chris@0 1058 }
Chris@0 1059 }
Chris@0 1060
Chris@0 1061 for (size_t c = 0; c < channels; ++c) {
Chris@0 1062 bufferPtrs[c] = nonintlv + c * orig;
Chris@0 1063 }
Chris@0 1064
Chris@0 1065 got = mixModels(f, orig, bufferPtrs);
Chris@0 1066
Chris@0 1067 // and interleave into first half
Chris@0 1068 for (size_t c = 0; c < channels; ++c) {
Chris@0 1069 for (size_t i = 0; i < got; ++i) {
Chris@0 1070 float sample = nonintlv[c * got + i];
Chris@0 1071 intlv[channels * i + c] = sample;
Chris@0 1072 }
Chris@0 1073 }
Chris@0 1074
Chris@0 1075 SRC_DATA data;
Chris@0 1076 data.data_in = intlv;
Chris@0 1077 data.data_out = srcout;
Chris@0 1078 data.input_frames = got;
Chris@0 1079 data.output_frames = work;
Chris@0 1080 data.src_ratio = ratio;
Chris@0 1081 data.end_of_input = 0;
Chris@0 1082
Chris@32 1083 int err = 0;
Chris@32 1084
Chris@32 1085 if (m_timeStretcher && m_timeStretcher->getRatio() < 0.4) {
Chris@32 1086 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@106 1087 std::cout << "Using crappy converter" << std::endl;
Chris@32 1088 #endif
Chris@32 1089 src_process(m_crapConverter, &data);
Chris@32 1090 } else {
Chris@32 1091 src_process(m_converter, &data);
Chris@32 1092 }
Chris@32 1093
Chris@0 1094 size_t toCopy = size_t(got * ratio + 0.1);
Chris@0 1095
Chris@0 1096 if (err) {
Chris@0 1097 std::cerr
Chris@0 1098 << "AudioCallbackPlaySourceFillThread: ERROR in samplerate conversion: "
Chris@0 1099 << src_strerror(err) << std::endl;
Chris@0 1100 //!!! Then what?
Chris@0 1101 } else {
Chris@0 1102 got = data.input_frames_used;
Chris@0 1103 toCopy = data.output_frames_gen;
Chris@0 1104 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@106 1105 std::cout << "Resampled " << got << " frames to " << toCopy << " frames" << std::endl;
Chris@0 1106 #endif
Chris@0 1107 }
Chris@0 1108
Chris@0 1109 for (size_t c = 0; c < channels; ++c) {
Chris@0 1110 for (size_t i = 0; i < toCopy; ++i) {
Chris@0 1111 tmp[i] = srcout[channels * i + c];
Chris@0 1112 }
Chris@0 1113 RingBuffer<float> *wb = getWriteRingBuffer(c);
Chris@0 1114 if (wb) wb->write(tmp, toCopy);
Chris@0 1115 }
Chris@0 1116
Chris@0 1117 m_writeBufferFill = f;
Chris@0 1118 if (readWriteEqual) m_readBufferFill = f;
Chris@0 1119
Chris@0 1120 } else {
Chris@0 1121
Chris@0 1122 // space must be a multiple of generatorBlockSize
Chris@0 1123 space = (space / generatorBlockSize) * generatorBlockSize;
Chris@0 1124 if (space == 0) return false;
Chris@0 1125
Chris@0 1126 if (tmpSize < channels * space) {
Chris@0 1127 delete[] tmp;
Chris@0 1128 tmp = new float[channels * space];
Chris@0 1129 tmpSize = channels * space;
Chris@0 1130 }
Chris@0 1131
Chris@0 1132 for (size_t c = 0; c < channels; ++c) {
Chris@0 1133
Chris@0 1134 bufferPtrs[c] = tmp + c * space;
Chris@0 1135
Chris@0 1136 for (size_t i = 0; i < space; ++i) {
Chris@0 1137 tmp[c * space + i] = 0.0f;
Chris@0 1138 }
Chris@0 1139 }
Chris@0 1140
Chris@0 1141 size_t got = mixModels(f, space, bufferPtrs);
Chris@0 1142
Chris@0 1143 for (size_t c = 0; c < channels; ++c) {
Chris@0 1144
Chris@0 1145 RingBuffer<float> *wb = getWriteRingBuffer(c);
Chris@106 1146 if (wb) {
Chris@106 1147 size_t actual = wb->write(bufferPtrs[c], got);
Chris@0 1148 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@106 1149 std::cout << "Wrote " << actual << " samples for ch " << c << ", now "
Chris@0 1150 << wb->getReadSpace() << " to read"
Chris@0 1151 << std::endl;
Chris@0 1152 #endif
Chris@106 1153 if (actual < got) {
Chris@106 1154 std::cerr << "WARNING: Buffer overrun in channel " << c
Chris@106 1155 << ": wrote " << actual << " of " << got
Chris@106 1156 << " samples" << std::endl;
Chris@106 1157 }
Chris@106 1158 }
Chris@0 1159 }
Chris@0 1160
Chris@0 1161 m_writeBufferFill = f;
Chris@0 1162 if (readWriteEqual) m_readBufferFill = f;
Chris@0 1163
Chris@0 1164 //!!! 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@0 1165 }
Chris@0 1166
Chris@0 1167 return true;
Chris@0 1168 }
Chris@0 1169
Chris@0 1170 size_t
Chris@0 1171 AudioCallbackPlaySource::mixModels(size_t &frame, size_t count, float **buffers)
Chris@0 1172 {
Chris@0 1173 size_t processed = 0;
Chris@0 1174 size_t chunkStart = frame;
Chris@0 1175 size_t chunkSize = count;
Chris@0 1176 size_t selectionSize = 0;
Chris@0 1177 size_t nextChunkStart = chunkStart + chunkSize;
Chris@0 1178
Chris@0 1179 bool looping = m_viewManager->getPlayLoopMode();
Chris@0 1180 bool constrained = (m_viewManager->getPlaySelectionMode() &&
Chris@0 1181 !m_viewManager->getSelections().empty());
Chris@0 1182
Chris@0 1183 static float **chunkBufferPtrs = 0;
Chris@0 1184 static size_t chunkBufferPtrCount = 0;
Chris@0 1185 size_t channels = getTargetChannelCount();
Chris@0 1186
Chris@0 1187 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@106 1188 std::cout << "Selection playback: start " << frame << ", size " << count <<", channels " << channels << std::endl;
Chris@0 1189 #endif
Chris@0 1190
Chris@0 1191 if (chunkBufferPtrCount < channels) {
Chris@0 1192 if (chunkBufferPtrs) delete[] chunkBufferPtrs;
Chris@0 1193 chunkBufferPtrs = new float *[channels];
Chris@0 1194 chunkBufferPtrCount = channels;
Chris@0 1195 }
Chris@0 1196
Chris@0 1197 for (size_t c = 0; c < channels; ++c) {
Chris@0 1198 chunkBufferPtrs[c] = buffers[c];
Chris@0 1199 }
Chris@0 1200
Chris@0 1201 while (processed < count) {
Chris@0 1202
Chris@0 1203 chunkSize = count - processed;
Chris@0 1204 nextChunkStart = chunkStart + chunkSize;
Chris@0 1205 selectionSize = 0;
Chris@0 1206
Chris@0 1207 size_t fadeIn = 0, fadeOut = 0;
Chris@0 1208
Chris@0 1209 if (constrained) {
Chris@0 1210
Chris@0 1211 Selection selection =
Chris@0 1212 m_viewManager->getContainingSelection(chunkStart, true);
Chris@0 1213
Chris@0 1214 if (selection.isEmpty()) {
Chris@0 1215 if (looping) {
Chris@0 1216 selection = *m_viewManager->getSelections().begin();
Chris@0 1217 chunkStart = selection.getStartFrame();
Chris@0 1218 fadeIn = 50;
Chris@0 1219 }
Chris@0 1220 }
Chris@0 1221
Chris@0 1222 if (selection.isEmpty()) {
Chris@0 1223
Chris@0 1224 chunkSize = 0;
Chris@0 1225 nextChunkStart = chunkStart;
Chris@0 1226
Chris@0 1227 } else {
Chris@0 1228
Chris@0 1229 selectionSize =
Chris@0 1230 selection.getEndFrame() -
Chris@0 1231 selection.getStartFrame();
Chris@0 1232
Chris@0 1233 if (chunkStart < selection.getStartFrame()) {
Chris@0 1234 chunkStart = selection.getStartFrame();
Chris@0 1235 fadeIn = 50;
Chris@0 1236 }
Chris@0 1237
Chris@0 1238 nextChunkStart = chunkStart + chunkSize;
Chris@0 1239
Chris@0 1240 if (nextChunkStart >= selection.getEndFrame()) {
Chris@0 1241 nextChunkStart = selection.getEndFrame();
Chris@0 1242 fadeOut = 50;
Chris@0 1243 }
Chris@0 1244
Chris@0 1245 chunkSize = nextChunkStart - chunkStart;
Chris@0 1246 }
Chris@0 1247
Chris@0 1248 } else if (looping && m_lastModelEndFrame > 0) {
Chris@0 1249
Chris@0 1250 if (chunkStart >= m_lastModelEndFrame) {
Chris@0 1251 chunkStart = 0;
Chris@0 1252 }
Chris@0 1253 if (chunkSize > m_lastModelEndFrame - chunkStart) {
Chris@0 1254 chunkSize = m_lastModelEndFrame - chunkStart;
Chris@0 1255 }
Chris@0 1256 nextChunkStart = chunkStart + chunkSize;
Chris@0 1257 }
Chris@0 1258
Chris@106 1259 // std::cout << "chunkStart " << chunkStart << ", chunkSize " << chunkSize << ", nextChunkStart " << nextChunkStart << ", frame " << frame << ", count " << count << ", processed " << processed << std::endl;
Chris@0 1260
Chris@0 1261 if (!chunkSize) {
Chris@0 1262 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@106 1263 std::cout << "Ending selection playback at " << nextChunkStart << std::endl;
Chris@0 1264 #endif
Chris@0 1265 // We need to maintain full buffers so that the other
Chris@0 1266 // thread can tell where it's got to in the playback -- so
Chris@0 1267 // return the full amount here
Chris@0 1268 frame = frame + count;
Chris@0 1269 return count;
Chris@0 1270 }
Chris@0 1271
Chris@0 1272 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@106 1273 std::cout << "Selection playback: chunk at " << chunkStart << " -> " << nextChunkStart << " (size " << chunkSize << ")" << std::endl;
Chris@0 1274 #endif
Chris@0 1275
Chris@0 1276 size_t got = 0;
Chris@0 1277
Chris@0 1278 if (selectionSize < 100) {
Chris@0 1279 fadeIn = 0;
Chris@0 1280 fadeOut = 0;
Chris@0 1281 } else if (selectionSize < 300) {
Chris@0 1282 if (fadeIn > 0) fadeIn = 10;
Chris@0 1283 if (fadeOut > 0) fadeOut = 10;
Chris@0 1284 }
Chris@0 1285
Chris@0 1286 if (fadeIn > 0) {
Chris@0 1287 if (processed * 2 < fadeIn) {
Chris@0 1288 fadeIn = processed * 2;
Chris@0 1289 }
Chris@0 1290 }
Chris@0 1291
Chris@0 1292 if (fadeOut > 0) {
Chris@0 1293 if ((count - processed - chunkSize) * 2 < fadeOut) {
Chris@0 1294 fadeOut = (count - processed - chunkSize) * 2;
Chris@0 1295 }
Chris@0 1296 }
Chris@0 1297
Chris@0 1298 for (std::set<Model *>::iterator mi = m_models.begin();
Chris@0 1299 mi != m_models.end(); ++mi) {
Chris@0 1300
Chris@0 1301 got = m_audioGenerator->mixModel(*mi, chunkStart,
Chris@0 1302 chunkSize, chunkBufferPtrs,
Chris@0 1303 fadeIn, fadeOut);
Chris@0 1304 }
Chris@0 1305
Chris@0 1306 for (size_t c = 0; c < channels; ++c) {
Chris@0 1307 chunkBufferPtrs[c] += chunkSize;
Chris@0 1308 }
Chris@0 1309
Chris@0 1310 processed += chunkSize;
Chris@0 1311 chunkStart = nextChunkStart;
Chris@0 1312 }
Chris@0 1313
Chris@0 1314 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@106 1315 std::cout << "Returning selection playback " << processed << " frames to " << nextChunkStart << std::endl;
Chris@0 1316 #endif
Chris@0 1317
Chris@0 1318 frame = nextChunkStart;
Chris@0 1319 return processed;
Chris@0 1320 }
Chris@0 1321
Chris@0 1322 void
Chris@0 1323 AudioCallbackPlaySource::unifyRingBuffers()
Chris@0 1324 {
Chris@0 1325 if (m_readBuffers == m_writeBuffers) return;
Chris@0 1326
Chris@0 1327 // only unify if there will be something to read
Chris@0 1328 for (size_t c = 0; c < getTargetChannelCount(); ++c) {
Chris@0 1329 RingBuffer<float> *wb = getWriteRingBuffer(c);
Chris@0 1330 if (wb) {
Chris@0 1331 if (wb->getReadSpace() < m_blockSize * 2) {
Chris@0 1332 if ((m_writeBufferFill + m_blockSize * 2) <
Chris@0 1333 m_lastModelEndFrame) {
Chris@0 1334 // OK, we don't have enough and there's more to
Chris@0 1335 // read -- don't unify until we can do better
Chris@0 1336 return;
Chris@0 1337 }
Chris@0 1338 }
Chris@0 1339 break;
Chris@0 1340 }
Chris@0 1341 }
Chris@0 1342
Chris@0 1343 size_t rf = m_readBufferFill;
Chris@0 1344 RingBuffer<float> *rb = getReadRingBuffer(0);
Chris@0 1345 if (rb) {
Chris@0 1346 size_t rs = rb->getReadSpace();
Chris@0 1347 //!!! incorrect when in non-contiguous selection, see comments elsewhere
Chris@106 1348 // std::cout << "rs = " << rs << std::endl;
Chris@0 1349 if (rs < rf) rf -= rs;
Chris@0 1350 else rf = 0;
Chris@0 1351 }
Chris@0 1352
Chris@106 1353 //std::cout << "m_readBufferFill = " << m_readBufferFill << ", rf = " << rf << ", m_writeBufferFill = " << m_writeBufferFill << std::endl;
Chris@0 1354
Chris@0 1355 size_t wf = m_writeBufferFill;
Chris@0 1356 size_t skip = 0;
Chris@0 1357 for (size_t c = 0; c < getTargetChannelCount(); ++c) {
Chris@0 1358 RingBuffer<float> *wb = getWriteRingBuffer(c);
Chris@0 1359 if (wb) {
Chris@0 1360 if (c == 0) {
Chris@0 1361
Chris@0 1362 size_t wrs = wb->getReadSpace();
Chris@106 1363 // std::cout << "wrs = " << wrs << std::endl;
Chris@0 1364
Chris@0 1365 if (wrs < wf) wf -= wrs;
Chris@0 1366 else wf = 0;
Chris@106 1367 // std::cout << "wf = " << wf << std::endl;
Chris@0 1368
Chris@0 1369 if (wf < rf) skip = rf - wf;
Chris@0 1370 if (skip == 0) break;
Chris@0 1371 }
Chris@0 1372
Chris@106 1373 // std::cout << "skipping " << skip << std::endl;
Chris@0 1374 wb->skip(skip);
Chris@0 1375 }
Chris@0 1376 }
Chris@0 1377
Chris@0 1378 m_bufferScavenger.claim(m_readBuffers);
Chris@0 1379 m_readBuffers = m_writeBuffers;
Chris@0 1380 m_readBufferFill = m_writeBufferFill;
Chris@106 1381 // std::cout << "unified" << std::endl;
Chris@0 1382 }
Chris@0 1383
Chris@0 1384 void
Chris@127 1385 AudioCallbackPlaySource::FillThread::run()
Chris@0 1386 {
Chris@0 1387 AudioCallbackPlaySource &s(m_source);
Chris@0 1388
Chris@0 1389 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@106 1390 std::cout << "AudioCallbackPlaySourceFillThread starting" << std::endl;
Chris@0 1391 #endif
Chris@0 1392
Chris@0 1393 s.m_mutex.lock();
Chris@0 1394
Chris@0 1395 bool previouslyPlaying = s.m_playing;
Chris@0 1396 bool work = false;
Chris@0 1397
Chris@0 1398 while (!s.m_exiting) {
Chris@0 1399
Chris@0 1400 s.unifyRingBuffers();
Chris@0 1401 s.m_bufferScavenger.scavenge();
Chris@41 1402 s.m_pluginScavenger.scavenge();
Chris@0 1403 s.m_timeStretcherScavenger.scavenge();
Chris@0 1404
Chris@0 1405 if (work && s.m_playing && s.getSourceSampleRate()) {
Chris@0 1406
Chris@0 1407 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@0 1408 std::cout << "AudioCallbackPlaySourceFillThread: not waiting" << std::endl;
Chris@0 1409 #endif
Chris@0 1410
Chris@0 1411 s.m_mutex.unlock();
Chris@0 1412 s.m_mutex.lock();
Chris@0 1413
Chris@0 1414 } else {
Chris@0 1415
Chris@0 1416 float ms = 100;
Chris@0 1417 if (s.getSourceSampleRate() > 0) {
Chris@0 1418 ms = float(m_ringBufferSize) / float(s.getSourceSampleRate()) * 1000.0;
Chris@0 1419 }
Chris@0 1420
Chris@0 1421 if (s.m_playing) ms /= 10;
Chris@106 1422
Chris@0 1423 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@106 1424 if (!s.m_playing) std::cout << std::endl;
Chris@0 1425 std::cout << "AudioCallbackPlaySourceFillThread: waiting for " << ms << "ms..." << std::endl;
Chris@0 1426 #endif
Chris@0 1427
Chris@0 1428 s.m_condition.wait(&s.m_mutex, size_t(ms));
Chris@0 1429 }
Chris@0 1430
Chris@0 1431 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@0 1432 std::cout << "AudioCallbackPlaySourceFillThread: awoken" << std::endl;
Chris@0 1433 #endif
Chris@0 1434
Chris@0 1435 work = false;
Chris@0 1436
Chris@0 1437 if (!s.getSourceSampleRate()) continue;
Chris@0 1438
Chris@0 1439 bool playing = s.m_playing;
Chris@0 1440
Chris@0 1441 if (playing && !previouslyPlaying) {
Chris@0 1442 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@0 1443 std::cout << "AudioCallbackPlaySourceFillThread: playback state changed, resetting" << std::endl;
Chris@0 1444 #endif
Chris@0 1445 for (size_t c = 0; c < s.getTargetChannelCount(); ++c) {
Chris@0 1446 RingBuffer<float> *rb = s.getReadRingBuffer(c);
Chris@0 1447 if (rb) rb->reset();
Chris@0 1448 }
Chris@0 1449 }
Chris@0 1450 previouslyPlaying = playing;
Chris@0 1451
Chris@0 1452 work = s.fillBuffers();
Chris@0 1453 }
Chris@0 1454
Chris@0 1455 s.m_mutex.unlock();
Chris@0 1456 }
Chris@0 1457