annotate audioio/AudioGenerator.cpp @ 307:6eb15c3aee0a tonioni

More toward using clip mixer
author Chris Cannam
date Tue, 07 Jan 2014 13:10:35 +0000
parents 9716c75499ef
children 289d65722123
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.
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 "AudioGenerator.h"
Chris@43 17
Chris@43 18 #include "base/TempDirectory.h"
Chris@43 19 #include "base/PlayParameters.h"
Chris@43 20 #include "base/PlayParameterRepository.h"
Chris@43 21 #include "base/Pitch.h"
Chris@43 22 #include "base/Exceptions.h"
Chris@43 23
Chris@43 24 #include "data/model/NoteModel.h"
Chris@278 25 #include "data/model/FlexiNoteModel.h"
Chris@43 26 #include "data/model/DenseTimeValueModel.h"
Chris@43 27 #include "data/model/SparseOneDimensionalModel.h"
Chris@299 28 #include "data/model/NoteData.h"
Chris@43 29
Chris@307 30 #include "ClipMixer.h"
Chris@307 31
Chris@43 32 #include <iostream>
Chris@167 33 #include <cmath>
Chris@43 34
Chris@43 35 #include <QDir>
Chris@43 36 #include <QFile>
Chris@43 37
Chris@43 38 const size_t
Chris@305 39 AudioGenerator::m_processingBlockSize = 2048;
Chris@43 40
Chris@43 41 QString
Chris@43 42 AudioGenerator::m_sampleDir = "";
Chris@43 43
Chris@43 44 //#define DEBUG_AUDIO_GENERATOR 1
Chris@43 45
Chris@43 46 AudioGenerator::AudioGenerator() :
Chris@43 47 m_sourceSampleRate(0),
Chris@43 48 m_targetChannelCount(1),
Chris@43 49 m_soloing(false)
Chris@43 50 {
Chris@108 51 initialiseSampleDir();
Chris@43 52
Chris@43 53 connect(PlayParameterRepository::getInstance(),
Chris@305 54 SIGNAL(playSampleIdChanged(const Playable *, QString)),
Chris@43 55 this,
Chris@305 56 SLOT(playSampleIdChanged(const Playable *, QString)));
Chris@43 57 }
Chris@43 58
Chris@43 59 AudioGenerator::~AudioGenerator()
Chris@43 60 {
Chris@177 61 #ifdef DEBUG_AUDIO_GENERATOR
Chris@233 62 SVDEBUG << "AudioGenerator::~AudioGenerator" << endl;
Chris@177 63 #endif
Chris@43 64 }
Chris@43 65
Chris@108 66 void
Chris@108 67 AudioGenerator::initialiseSampleDir()
Chris@43 68 {
Chris@108 69 if (m_sampleDir != "") return;
Chris@108 70
Chris@108 71 try {
Chris@108 72 m_sampleDir = TempDirectory::getInstance()->getSubDirectoryPath("samples");
Chris@108 73 } catch (DirectoryCreationFailed f) {
Chris@293 74 cerr << "WARNING: AudioGenerator::initialiseSampleDir:"
Chris@108 75 << " Failed to create temporary sample directory"
Chris@293 76 << endl;
Chris@108 77 m_sampleDir = "";
Chris@108 78 return;
Chris@108 79 }
Chris@108 80
Chris@108 81 QDir sampleResourceDir(":/samples", "*.wav");
Chris@108 82
Chris@108 83 for (unsigned int i = 0; i < sampleResourceDir.count(); ++i) {
Chris@108 84
Chris@108 85 QString fileName(sampleResourceDir[i]);
Chris@108 86 QFile file(sampleResourceDir.filePath(fileName));
Chris@151 87 QString target = QDir(m_sampleDir).filePath(fileName);
Chris@108 88
Chris@151 89 if (!file.copy(target)) {
Chris@293 90 cerr << "WARNING: AudioGenerator::getSampleDir: "
Chris@294 91 << "Unable to copy " << fileName
Chris@108 92 << " into temporary directory \""
Chris@293 93 << m_sampleDir << "\"" << endl;
Chris@151 94 } else {
Chris@151 95 QFile tf(target);
Chris@151 96 tf.setPermissions(tf.permissions() |
Chris@151 97 QFile::WriteOwner |
Chris@151 98 QFile::WriteUser);
Chris@108 99 }
Chris@43 100 }
Chris@43 101 }
Chris@43 102
Chris@43 103 bool
Chris@43 104 AudioGenerator::addModel(Model *model)
Chris@43 105 {
Chris@43 106 if (m_sourceSampleRate == 0) {
Chris@43 107
Chris@43 108 m_sourceSampleRate = model->getSampleRate();
Chris@43 109
Chris@43 110 } else {
Chris@43 111
Chris@43 112 DenseTimeValueModel *dtvm =
Chris@43 113 dynamic_cast<DenseTimeValueModel *>(model);
Chris@43 114
Chris@43 115 if (dtvm) {
Chris@43 116 m_sourceSampleRate = model->getSampleRate();
Chris@43 117 return true;
Chris@43 118 }
Chris@43 119 }
Chris@307 120
Chris@307 121 ClipMixer *mixer = makeClipMixerFor(model);
Chris@307 122 if (mixer) {
Chris@43 123 QMutexLocker locker(&m_mutex);
Chris@307 124 m_clipMixerMap[model] = mixer;
Chris@43 125 return true;
Chris@43 126 }
Chris@307 127
Chris@43 128 return false;
Chris@43 129 }
Chris@43 130
Chris@43 131 void
Chris@305 132 AudioGenerator::playSampleIdChanged(const Playable *playable, QString)
Chris@43 133 {
Chris@108 134 const Model *model = dynamic_cast<const Model *>(playable);
Chris@108 135 if (!model) {
Chris@305 136 cerr << "WARNING: AudioGenerator::playSampleIdChanged: playable "
Chris@108 137 << playable << " is not a supported model type"
Chris@293 138 << endl;
Chris@108 139 return;
Chris@108 140 }
Chris@108 141
Chris@307 142 if (m_clipMixerMap.find(model) == m_clipMixerMap.end()) return;
Chris@307 143
Chris@307 144 ClipMixer *mixer = makeClipMixerFor(model);
Chris@307 145 if (mixer) {
Chris@43 146 QMutexLocker locker(&m_mutex);
Chris@307 147 m_clipMixerMap[model] = mixer;
Chris@307 148 return true;
Chris@43 149 }
Chris@43 150 }
Chris@305 151 /*!!!
Chris@43 152 void
Chris@108 153 AudioGenerator::playPluginConfigurationChanged(const Playable *playable,
Chris@43 154 QString configurationXml)
Chris@43 155 {
Chris@233 156 // SVDEBUG << "AudioGenerator::playPluginConfigurationChanged" << endl;
Chris@43 157
Chris@108 158 const Model *model = dynamic_cast<const Model *>(playable);
Chris@108 159 if (!model) {
Chris@305 160 cerr << "WARNING: AudioGenerator::playSampleIdChanged: playable "
Chris@108 161 << playable << " is not a supported model type"
Chris@293 162 << endl;
Chris@108 163 return;
Chris@108 164 }
Chris@108 165
Chris@43 166 if (m_synthMap.find(model) == m_synthMap.end()) {
Chris@233 167 SVDEBUG << "AudioGenerator::playPluginConfigurationChanged: We don't know about this plugin" << endl;
Chris@43 168 return;
Chris@43 169 }
Chris@43 170
Chris@43 171 RealTimePluginInstance *plugin = m_synthMap[model];
Chris@43 172 if (plugin) {
Chris@43 173 PluginXml(plugin).setParametersFromXml(configurationXml);
Chris@43 174 }
Chris@43 175 }
Chris@43 176
Chris@43 177 void
Chris@43 178 AudioGenerator::setSampleDir(RealTimePluginInstance *plugin)
Chris@43 179 {
Chris@108 180 if (m_sampleDir != "") {
Chris@108 181 plugin->configure("sampledir", m_sampleDir.toStdString());
Chris@108 182 }
Chris@43 183 }
Chris@307 184 */
Chris@307 185 ClipMixer *
Chris@307 186 AudioGenerator::makeClipMixerFor(const Model *model)
Chris@43 187 {
Chris@307 188 QString sampleId;
Chris@43 189
Chris@108 190 const Playable *playable = model;
Chris@108 191 if (!playable || !playable->canPlay()) return 0;
Chris@108 192
Chris@43 193 PlayParameters *parameters =
Chris@108 194 PlayParameterRepository::getInstance()->getPlayParameters(playable);
Chris@43 195 if (parameters) {
Chris@307 196 sampleId = parameters->getPlaySampleId();
Chris@43 197 }
Chris@43 198
Chris@307 199 std::cerr << "AudioGenerator::loadPluginFor(" << model << "): sample id = " << sampleId << std::endl;
Chris@276 200
Chris@307 201 if (sampleId == "") {
Chris@307 202 SVDEBUG << "AudioGenerator::loadPluginFor(" << model << "): no sample, skipping" << endl;
Chris@276 203 return 0;
Chris@276 204 }
Chris@43 205
Chris@307 206
Chris@307 207
Chris@307 208
Chris@307 209
Chris@307 210
Chris@43 211 RealTimePluginInstance *plugin = loadPlugin(pluginId, "");
Chris@43 212 if (!plugin) return 0;
Chris@43 213
Chris@276 214 std::cerr << "AudioGenerator::loadPluginFor(" << model << "): loaded plugin "
Chris@276 215 << plugin << std::endl;
Chris@276 216
Chris@43 217 if (configurationXml != "") {
Chris@43 218 PluginXml(plugin).setParametersFromXml(configurationXml);
Chris@179 219 setSampleDir(plugin);
Chris@43 220 }
Chris@43 221
Chris@108 222 configurationXml = PluginXml(plugin).toXmlString();
Chris@108 223
Chris@43 224 if (parameters) {
Chris@305 225 parameters->setPlaySampleId(pluginId);
Chris@43 226 parameters->setPlayPluginConfiguration(configurationXml);
Chris@43 227 }
Chris@43 228
Chris@43 229 return plugin;
Chris@43 230 }
Chris@43 231
Chris@43 232 RealTimePluginInstance *
Chris@43 233 AudioGenerator::loadPlugin(QString pluginId, QString program)
Chris@43 234 {
Chris@43 235 RealTimePluginFactory *factory =
Chris@43 236 RealTimePluginFactory::instanceFor(pluginId);
Chris@43 237
Chris@43 238 if (!factory) {
Chris@293 239 cerr << "Failed to get plugin factory" << endl;
Chris@274 240 return 0;
Chris@43 241 }
Chris@43 242
Chris@43 243 RealTimePluginInstance *instance =
Chris@43 244 factory->instantiatePlugin
Chris@305 245 (pluginId, 0, 0, m_sourceSampleRate, m_processingBlockSize, m_targetChannelCount);
Chris@43 246
Chris@43 247 if (!instance) {
Chris@293 248 cerr << "Failed to instantiate plugin " << pluginId << endl;
Chris@43 249 return 0;
Chris@43 250 }
Chris@43 251
Chris@43 252 setSampleDir(instance);
Chris@43 253
Chris@43 254 for (unsigned int i = 0; i < instance->getParameterCount(); ++i) {
Chris@43 255 instance->setParameterValue(i, instance->getParameterDefault(i));
Chris@43 256 }
Chris@43 257 std::string defaultProgram = instance->getProgram(0, 0);
Chris@43 258 if (defaultProgram != "") {
Chris@295 259 cerr << "first selecting default program " << defaultProgram << endl;
Chris@43 260 instance->selectProgram(defaultProgram);
Chris@43 261 }
Chris@43 262 if (program != "") {
Chris@295 263 cerr << "now selecting desired program " << program << endl;
Chris@43 264 instance->selectProgram(program.toStdString());
Chris@43 265 }
Chris@43 266 instance->setIdealChannelCount(m_targetChannelCount); // reset!
Chris@43 267
Chris@43 268 return instance;
Chris@43 269 }
Chris@305 270 */
Chris@43 271 void
Chris@43 272 AudioGenerator::removeModel(Model *model)
Chris@43 273 {
Chris@43 274 SparseOneDimensionalModel *sodm =
Chris@43 275 dynamic_cast<SparseOneDimensionalModel *>(model);
Chris@43 276 if (!sodm) return; // nothing to do
Chris@43 277
Chris@43 278 QMutexLocker locker(&m_mutex);
Chris@43 279
Chris@43 280 if (m_synthMap.find(sodm) == m_synthMap.end()) return;
Chris@43 281
Chris@305 282 //!!! RealTimePluginInstance *instance = m_synthMap[sodm];
Chris@305 283 // m_synthMap.erase(sodm);
Chris@43 284 delete instance;
Chris@43 285 }
Chris@43 286
Chris@43 287 void
Chris@43 288 AudioGenerator::clearModels()
Chris@43 289 {
Chris@43 290 QMutexLocker locker(&m_mutex);
Chris@305 291 /*!!!
Chris@43 292 while (!m_synthMap.empty()) {
Chris@43 293 RealTimePluginInstance *instance = m_synthMap.begin()->second;
Chris@43 294 m_synthMap.erase(m_synthMap.begin());
Chris@43 295 delete instance;
Chris@43 296 }
Chris@305 297 */
Chris@43 298 }
Chris@43 299
Chris@43 300 void
Chris@43 301 AudioGenerator::reset()
Chris@43 302 {
Chris@43 303 QMutexLocker locker(&m_mutex);
Chris@305 304 /*!!!
Chris@43 305 for (PluginMap::iterator i = m_synthMap.begin(); i != m_synthMap.end(); ++i) {
Chris@43 306 if (i->second) {
Chris@43 307 i->second->silence();
Chris@43 308 i->second->discardEvents();
Chris@43 309 }
Chris@43 310 }
Chris@305 311 */
Chris@43 312
Chris@43 313 m_noteOffs.clear();
Chris@43 314 }
Chris@43 315
Chris@43 316 void
Chris@43 317 AudioGenerator::setTargetChannelCount(size_t targetChannelCount)
Chris@43 318 {
Chris@43 319 if (m_targetChannelCount == targetChannelCount) return;
Chris@43 320
Chris@233 321 // SVDEBUG << "AudioGenerator::setTargetChannelCount(" << targetChannelCount << ")" << endl;
Chris@43 322
Chris@43 323 QMutexLocker locker(&m_mutex);
Chris@43 324 m_targetChannelCount = targetChannelCount;
Chris@43 325
Chris@305 326 /*!!!
Chris@43 327 for (PluginMap::iterator i = m_synthMap.begin(); i != m_synthMap.end(); ++i) {
Chris@43 328 if (i->second) i->second->setIdealChannelCount(targetChannelCount);
Chris@43 329 }
Chris@305 330 */
Chris@43 331 }
Chris@43 332
Chris@43 333 size_t
Chris@43 334 AudioGenerator::getBlockSize() const
Chris@43 335 {
Chris@305 336 return m_processingBlockSize;
Chris@43 337 }
Chris@43 338
Chris@43 339 void
Chris@43 340 AudioGenerator::setSoloModelSet(std::set<Model *> s)
Chris@43 341 {
Chris@43 342 QMutexLocker locker(&m_mutex);
Chris@43 343
Chris@43 344 m_soloModelSet = s;
Chris@43 345 m_soloing = true;
Chris@43 346 }
Chris@43 347
Chris@43 348 void
Chris@43 349 AudioGenerator::clearSoloModelSet()
Chris@43 350 {
Chris@43 351 QMutexLocker locker(&m_mutex);
Chris@43 352
Chris@43 353 m_soloModelSet.clear();
Chris@43 354 m_soloing = false;
Chris@43 355 }
Chris@43 356
Chris@43 357 size_t
Chris@43 358 AudioGenerator::mixModel(Model *model, size_t startFrame, size_t frameCount,
Chris@43 359 float **buffer, size_t fadeIn, size_t fadeOut)
Chris@43 360 {
Chris@43 361 if (m_sourceSampleRate == 0) {
Chris@293 362 cerr << "WARNING: AudioGenerator::mixModel: No base source sample rate available" << endl;
Chris@43 363 return frameCount;
Chris@43 364 }
Chris@43 365
Chris@43 366 QMutexLocker locker(&m_mutex);
Chris@43 367
Chris@108 368 Playable *playable = model;
Chris@108 369 if (!playable || !playable->canPlay()) return frameCount;
Chris@108 370
Chris@43 371 PlayParameters *parameters =
Chris@108 372 PlayParameterRepository::getInstance()->getPlayParameters(playable);
Chris@43 373 if (!parameters) return frameCount;
Chris@43 374
Chris@43 375 bool playing = !parameters->isPlayMuted();
Chris@43 376 if (!playing) {
Chris@43 377 #ifdef DEBUG_AUDIO_GENERATOR
Chris@293 378 cout << "AudioGenerator::mixModel(" << model << "): muted" << endl;
Chris@43 379 #endif
Chris@43 380 return frameCount;
Chris@43 381 }
Chris@43 382
Chris@43 383 if (m_soloing) {
Chris@43 384 if (m_soloModelSet.find(model) == m_soloModelSet.end()) {
Chris@43 385 #ifdef DEBUG_AUDIO_GENERATOR
Chris@293 386 cout << "AudioGenerator::mixModel(" << model << "): not one of the solo'd models" << endl;
Chris@43 387 #endif
Chris@43 388 return frameCount;
Chris@43 389 }
Chris@43 390 }
Chris@43 391
Chris@43 392 float gain = parameters->getPlayGain();
Chris@43 393 float pan = parameters->getPlayPan();
Chris@43 394
Chris@43 395 DenseTimeValueModel *dtvm = dynamic_cast<DenseTimeValueModel *>(model);
Chris@43 396 if (dtvm) {
Chris@43 397 return mixDenseTimeValueModel(dtvm, startFrame, frameCount,
Chris@43 398 buffer, gain, pan, fadeIn, fadeOut);
Chris@43 399 }
Chris@43 400
Chris@279 401 bool synthetic =
Chris@279 402 (qobject_cast<SparseOneDimensionalModel *>(model) ||
Chris@278 403 qobject_cast<NoteModel *>(model) ||
Chris@278 404 qobject_cast<FlexiNoteModel *>(model));
Chris@43 405
Chris@279 406 if (synthetic) {
Chris@275 407 return mixSyntheticNoteModel(model, startFrame, frameCount,
Chris@275 408 buffer, gain, pan, fadeIn, fadeOut);
Chris@43 409 }
Chris@43 410
Chris@276 411 std::cerr << "AudioGenerator::mixModel: WARNING: Model " << model << " of type " << model->getTypeName() << " is marked as playable, but I have no mechanism to play it" << std::endl;
Chris@276 412
Chris@43 413 return frameCount;
Chris@43 414 }
Chris@43 415
Chris@43 416 size_t
Chris@43 417 AudioGenerator::mixDenseTimeValueModel(DenseTimeValueModel *dtvm,
Chris@43 418 size_t startFrame, size_t frames,
Chris@43 419 float **buffer, float gain, float pan,
Chris@43 420 size_t fadeIn, size_t fadeOut)
Chris@43 421 {
Chris@80 422 static float **channelBuffer = 0;
Chris@80 423 static size_t channelBufSiz = 0;
Chris@80 424 static size_t channelBufCount = 0;
Chris@43 425
Chris@43 426 size_t totalFrames = frames + fadeIn/2 + fadeOut/2;
Chris@43 427
Chris@80 428 size_t modelChannels = dtvm->getChannelCount();
Chris@80 429
Chris@80 430 if (channelBufSiz < totalFrames || channelBufCount < modelChannels) {
Chris@80 431
Chris@80 432 for (size_t c = 0; c < channelBufCount; ++c) {
Chris@80 433 delete[] channelBuffer[c];
Chris@80 434 }
Chris@80 435
Chris@43 436 delete[] channelBuffer;
Chris@80 437 channelBuffer = new float *[modelChannels];
Chris@80 438
Chris@80 439 for (size_t c = 0; c < modelChannels; ++c) {
Chris@80 440 channelBuffer[c] = new float[totalFrames];
Chris@80 441 }
Chris@80 442
Chris@80 443 channelBufCount = modelChannels;
Chris@43 444 channelBufSiz = totalFrames;
Chris@43 445 }
Chris@80 446
Chris@43 447 size_t got = 0;
Chris@80 448
Chris@80 449 if (startFrame >= fadeIn/2) {
Chris@80 450 got = dtvm->getData(0, modelChannels - 1,
Chris@80 451 startFrame - fadeIn/2,
Chris@80 452 frames + fadeOut/2 + fadeIn/2,
Chris@80 453 channelBuffer);
Chris@80 454 } else {
Chris@80 455 size_t missing = fadeIn/2 - startFrame;
Chris@80 456
Chris@80 457 for (size_t c = 0; c < modelChannels; ++c) {
Chris@80 458 channelBuffer[c] += missing;
Chris@80 459 }
Chris@80 460
Chris@80 461 got = dtvm->getData(0, modelChannels - 1,
Chris@80 462 startFrame,
Chris@80 463 frames + fadeOut/2,
Chris@80 464 channelBuffer);
Chris@80 465
Chris@80 466 for (size_t c = 0; c < modelChannels; ++c) {
Chris@80 467 channelBuffer[c] -= missing;
Chris@80 468 }
Chris@80 469
Chris@80 470 got += missing;
Chris@80 471 }
Chris@43 472
Chris@43 473 for (size_t c = 0; c < m_targetChannelCount; ++c) {
Chris@43 474
Chris@80 475 size_t sourceChannel = (c % modelChannels);
Chris@43 476
Chris@233 477 // SVDEBUG << "mixing channel " << c << " from source channel " << sourceChannel << endl;
Chris@43 478
Chris@43 479 float channelGain = gain;
Chris@43 480 if (pan != 0.0) {
Chris@43 481 if (c == 0) {
Chris@43 482 if (pan > 0.0) channelGain *= 1.0 - pan;
Chris@43 483 } else {
Chris@43 484 if (pan < 0.0) channelGain *= pan + 1.0;
Chris@43 485 }
Chris@43 486 }
Chris@43 487
Chris@43 488 for (size_t i = 0; i < fadeIn/2; ++i) {
Chris@43 489 float *back = buffer[c];
Chris@43 490 back -= fadeIn/2;
Chris@80 491 back[i] += (channelGain * channelBuffer[sourceChannel][i] * i) / fadeIn;
Chris@43 492 }
Chris@43 493
Chris@43 494 for (size_t i = 0; i < frames + fadeOut/2; ++i) {
Chris@43 495 float mult = channelGain;
Chris@43 496 if (i < fadeIn/2) {
Chris@43 497 mult = (mult * i) / fadeIn;
Chris@43 498 }
Chris@43 499 if (i > frames - fadeOut/2) {
Chris@43 500 mult = (mult * ((frames + fadeOut/2) - i)) / fadeOut;
Chris@43 501 }
Chris@80 502 float val = channelBuffer[sourceChannel][i];
Chris@80 503 if (i >= got) val = 0.f;
Chris@80 504 buffer[c][i] += mult * val;
Chris@43 505 }
Chris@43 506 }
Chris@43 507
Chris@43 508 return got;
Chris@43 509 }
Chris@43 510
Chris@43 511 size_t
Chris@275 512 AudioGenerator::mixSyntheticNoteModel(Model *model,
Chris@275 513 size_t startFrame, size_t frames,
Chris@275 514 float **buffer, float gain, float pan,
Chris@275 515 size_t /* fadeIn */,
Chris@275 516 size_t /* fadeOut */)
Chris@43 517 {
Chris@275 518 RealTimePluginInstance *plugin = m_synthMap[model];
Chris@43 519 if (!plugin) return 0;
Chris@43 520
Chris@43 521 size_t latency = plugin->getLatency();
Chris@305 522 size_t blocks = frames / m_processingBlockSize;
Chris@43 523
Chris@43 524 //!!! hang on -- the fact that the audio callback play source's
Chris@43 525 //buffer is a multiple of the plugin's buffer size doesn't mean
Chris@43 526 //that we always get called for a multiple of it here (because it
Chris@43 527 //also depends on the JACK block size). how should we ensure that
Chris@43 528 //all models write the same amount in to the mix, and that we
Chris@43 529 //always have a multiple of the plugin buffer size? I guess this
Chris@43 530 //class has to be queryable for the plugin buffer size & the
Chris@43 531 //callback play source has to use that as a multiple for all the
Chris@43 532 //calls to mixModel
Chris@43 533
Chris@305 534 size_t got = blocks * m_processingBlockSize;
Chris@43 535
Chris@43 536 #ifdef DEBUG_AUDIO_GENERATOR
Chris@293 537 cout << "mixModel [synthetic note]: frames " << frames
Chris@293 538 << ", blocks " << blocks << endl;
Chris@43 539 #endif
Chris@43 540
Chris@43 541 snd_seq_event_t onEv;
Chris@43 542 onEv.type = SND_SEQ_EVENT_NOTEON;
Chris@43 543 onEv.data.note.channel = 0;
Chris@43 544
Chris@43 545 snd_seq_event_t offEv;
Chris@43 546 offEv.type = SND_SEQ_EVENT_NOTEOFF;
Chris@43 547 offEv.data.note.channel = 0;
Chris@43 548 offEv.data.note.velocity = 0;
Chris@43 549
Chris@275 550 NoteOffSet &noteOffs = m_noteOffs[model];
Chris@43 551
Chris@43 552 for (size_t i = 0; i < blocks; ++i) {
Chris@43 553
Chris@305 554 size_t reqStart = startFrame + i * m_processingBlockSize;
Chris@43 555
Chris@299 556 NoteList notes;
Chris@299 557 NoteExportable *exportable = dynamic_cast<NoteExportable *>(model);
Chris@299 558 if (exportable) {
Chris@299 559 notes = exportable->getNotes(reqStart + latency,
Chris@305 560 reqStart + latency + m_processingBlockSize);
Chris@299 561 }
Chris@43 562
Chris@43 563 Vamp::RealTime blockTime = Vamp::RealTime::frame2RealTime
Chris@305 564 (startFrame + i * m_processingBlockSize, m_sourceSampleRate);
Chris@43 565
Chris@275 566 for (NoteList::const_iterator ni = notes.begin();
Chris@275 567 ni != notes.end(); ++ni) {
Chris@43 568
Chris@275 569 size_t noteFrame = ni->start;
Chris@43 570
Chris@275 571 if (noteFrame >= latency) noteFrame -= latency;
Chris@43 572
Chris@275 573 if (noteFrame < reqStart ||
Chris@305 574 noteFrame >= reqStart + m_processingBlockSize) continue;
Chris@43 575
Chris@43 576 while (noteOffs.begin() != noteOffs.end() &&
Chris@275 577 noteOffs.begin()->frame <= noteFrame) {
Chris@43 578
Chris@43 579 Vamp::RealTime eventTime = Vamp::RealTime::frame2RealTime
Chris@43 580 (noteOffs.begin()->frame, m_sourceSampleRate);
Chris@43 581
Chris@43 582 offEv.data.note.note = noteOffs.begin()->pitch;
Chris@43 583
Chris@43 584 #ifdef DEBUG_AUDIO_GENERATOR
Chris@293 585 cerr << "mixModel [synthetic]: sending note-off event at time " << eventTime << " frame " << noteOffs.begin()->frame << " pitch " << noteOffs.begin()->pitch << endl;
Chris@43 586 #endif
Chris@43 587
Chris@43 588 plugin->sendEvent(eventTime, &offEv);
Chris@43 589 noteOffs.erase(noteOffs.begin());
Chris@43 590 }
Chris@43 591
Chris@43 592 Vamp::RealTime eventTime = Vamp::RealTime::frame2RealTime
Chris@275 593 (noteFrame, m_sourceSampleRate);
Chris@43 594
Chris@275 595 if (ni->isMidiPitchQuantized) {
Chris@275 596 onEv.data.note.note = ni->midiPitch;
Chris@275 597 } else {
Chris@275 598 #ifdef DEBUG_AUDIO_GENERATOR
Chris@293 599 cerr << "mixModel [synthetic]: non-pitch-quantized notes are not supported [yet], quantizing" << endl;
Chris@275 600 #endif
Chris@275 601 onEv.data.note.note = Pitch::getPitchForFrequency(ni->frequency);
Chris@275 602 }
Chris@275 603
Chris@275 604 onEv.data.note.velocity = ni->velocity;
Chris@275 605
Chris@43 606 plugin->sendEvent(eventTime, &onEv);
Chris@43 607
Chris@43 608 #ifdef DEBUG_AUDIO_GENERATOR
Chris@305 609 cout << "mixModel [synthetic]: note at frame " << noteFrame << ", block start " << (startFrame + i * m_processingBlockSize) << ", resulting time " << eventTime << endl;
Chris@43 610 #endif
Chris@43 611
Chris@275 612 noteOffs.insert
Chris@275 613 (NoteOff(onEv.data.note.note, noteFrame + ni->duration));
Chris@43 614 }
Chris@43 615
Chris@43 616 while (noteOffs.begin() != noteOffs.end() &&
Chris@43 617 noteOffs.begin()->frame <=
Chris@305 618 startFrame + i * m_processingBlockSize + m_processingBlockSize) {
Chris@43 619
Chris@43 620 Vamp::RealTime eventTime = Vamp::RealTime::frame2RealTime
Chris@43 621 (noteOffs.begin()->frame, m_sourceSampleRate);
Chris@43 622
Chris@43 623 offEv.data.note.note = noteOffs.begin()->pitch;
Chris@43 624
Chris@43 625 #ifdef DEBUG_AUDIO_GENERATOR
Chris@293 626 cerr << "mixModel [synthetic]: sending leftover note-off event at time " << eventTime << " frame " << noteOffs.begin()->frame << " pitch " << noteOffs.begin()->pitch << endl;
Chris@43 627 #endif
Chris@43 628
Chris@43 629 plugin->sendEvent(eventTime, &offEv);
Chris@43 630 noteOffs.erase(noteOffs.begin());
Chris@43 631 }
Chris@43 632
Chris@43 633 plugin->run(blockTime);
Chris@43 634 float **outs = plugin->getAudioOutputBuffers();
Chris@43 635
Chris@43 636 for (size_t c = 0; c < m_targetChannelCount; ++c) {
Chris@43 637 #ifdef DEBUG_AUDIO_GENERATOR
Chris@305 638 cout << "mixModel [synthetic]: adding " << m_processingBlockSize << " samples from plugin output " << c << endl;
Chris@43 639 #endif
Chris@43 640
Chris@43 641 size_t sourceChannel = (c % plugin->getAudioOutputCount());
Chris@43 642
Chris@43 643 float channelGain = gain;
Chris@43 644 if (pan != 0.0) {
Chris@43 645 if (c == 0) {
Chris@43 646 if (pan > 0.0) channelGain *= 1.0 - pan;
Chris@43 647 } else {
Chris@43 648 if (pan < 0.0) channelGain *= pan + 1.0;
Chris@43 649 }
Chris@43 650 }
Chris@43 651
Chris@305 652 for (size_t j = 0; j < m_processingBlockSize; ++j) {
Chris@305 653 buffer[c][i * m_processingBlockSize + j] +=
Chris@43 654 channelGain * outs[sourceChannel][j];
Chris@43 655 }
Chris@43 656 }
Chris@43 657 }
Chris@43 658
Chris@43 659 return got;
Chris@43 660 }