annotate audioio/AudioGenerator.cpp @ 30:f38b8abd3540

* Pull out temporary directory management into its own class * Make sure playback plugins get a default sample path in their original play parameters configuration * Save play parameters to .sv file (we aren't reloading yet though)
author Chris Cannam
date Tue, 25 Apr 2006 22:14:43 +0000
parents dd2084d32652
children b1112948724f
rev   line source
Chris@19 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@21 4 Sonic Visualiser
Chris@21 5 An audio file viewer and annotation editor.
Chris@21 6 Centre for Digital Music, Queen Mary, University of London.
Chris@21 7 This file copyright 2006 Chris Cannam.
Chris@0 8
Chris@21 9 This program is free software; you can redistribute it and/or
Chris@21 10 modify it under the terms of the GNU General Public License as
Chris@21 11 published by the Free Software Foundation; either version 2 of the
Chris@21 12 License, or (at your option) any later version. See the file
Chris@21 13 COPYING included with this distribution for more information.
Chris@0 14 */
Chris@0 15
Chris@0 16 #include "AudioGenerator.h"
Chris@0 17
Chris@30 18 #include "base/TempDirectory.h"
Chris@0 19 #include "base/PlayParameters.h"
Chris@11 20 #include "base/PlayParameterRepository.h"
Chris@0 21
Chris@11 22 #include "model/NoteModel.h"
Chris@0 23 #include "model/DenseTimeValueModel.h"
Chris@0 24 #include "model/SparseOneDimensionalModel.h"
Chris@0 25
Chris@0 26 #include "plugin/RealTimePluginFactory.h"
Chris@0 27 #include "plugin/RealTimePluginInstance.h"
Chris@0 28 #include "plugin/PluginIdentifier.h"
Chris@26 29 #include "plugin/PluginXml.h"
Chris@0 30 #include "plugin/api/alsa/seq_event.h"
Chris@0 31
Chris@0 32 #include <iostream>
Martin@14 33 #include <math.h>
Chris@0 34
Chris@29 35 #include <QDir>
Chris@29 36 #include <QFile>
Chris@29 37
Chris@0 38 const size_t
Chris@0 39 AudioGenerator::m_pluginBlockSize = 2048;
Chris@0 40
Chris@30 41 QString
Chris@30 42 AudioGenerator::m_sampleDir = "";
Chris@30 43
Chris@5 44 //#define DEBUG_AUDIO_GENERATOR 1
Chris@0 45
Chris@30 46 AudioGenerator::AudioGenerator() :
Chris@0 47 m_sourceSampleRate(0),
Chris@0 48 m_targetChannelCount(1)
Chris@0 49 {
Chris@22 50 connect(PlayParameterRepository::instance(),
Chris@22 51 SIGNAL(playPluginIdChanged(const Model *, QString)),
Chris@22 52 this,
Chris@22 53 SLOT(playPluginIdChanged(const Model *, QString)));
Chris@22 54
Chris@22 55 connect(PlayParameterRepository::instance(),
Chris@22 56 SIGNAL(playPluginConfigurationChanged(const Model *, QString)),
Chris@22 57 this,
Chris@22 58 SLOT(playPluginConfigurationChanged(const Model *, QString)));
Chris@0 59 }
Chris@0 60
Chris@0 61 AudioGenerator::~AudioGenerator()
Chris@0 62 {
Chris@0 63 }
Chris@0 64
Chris@10 65 bool
Chris@13 66 AudioGenerator::canPlay(const Model *model)
Chris@13 67 {
Chris@13 68 if (dynamic_cast<const DenseTimeValueModel *>(model) ||
Chris@13 69 dynamic_cast<const SparseOneDimensionalModel *>(model) ||
Chris@13 70 dynamic_cast<const NoteModel *>(model)) {
Chris@13 71 return true;
Chris@13 72 } else {
Chris@13 73 return false;
Chris@13 74 }
Chris@13 75 }
Chris@13 76
Chris@13 77 bool
Chris@0 78 AudioGenerator::addModel(Model *model)
Chris@0 79 {
Chris@0 80 if (m_sourceSampleRate == 0) {
Chris@0 81
Chris@0 82 m_sourceSampleRate = model->getSampleRate();
Chris@0 83
Chris@0 84 } else {
Chris@0 85
Chris@0 86 DenseTimeValueModel *dtvm =
Chris@0 87 dynamic_cast<DenseTimeValueModel *>(model);
Chris@0 88
Chris@0 89 if (dtvm) {
Chris@0 90 m_sourceSampleRate = model->getSampleRate();
Chris@10 91 return true;
Chris@0 92 }
Chris@0 93 }
Chris@0 94
Chris@22 95 RealTimePluginInstance *plugin = loadPluginFor(model);
Chris@22 96 if (plugin) {
Chris@22 97 QMutexLocker locker(&m_mutex);
Chris@22 98 m_synthMap[model] = plugin;
Chris@22 99 return true;
Chris@11 100 }
Chris@0 101
Chris@22 102 return false;
Chris@22 103 }
Chris@22 104
Chris@22 105 void
Chris@22 106 AudioGenerator::playPluginIdChanged(const Model *model, QString)
Chris@22 107 {
Chris@22 108 if (m_synthMap.find(model) == m_synthMap.end()) return;
Chris@22 109
Chris@22 110 RealTimePluginInstance *plugin = loadPluginFor(model);
Chris@22 111 if (plugin) {
Chris@22 112 QMutexLocker locker(&m_mutex);
Chris@22 113 delete m_synthMap[model];
Chris@22 114 m_synthMap[model] = plugin;
Chris@22 115 }
Chris@22 116 }
Chris@22 117
Chris@22 118 void
Chris@22 119 AudioGenerator::playPluginConfigurationChanged(const Model *model,
Chris@22 120 QString configurationXml)
Chris@22 121 {
Chris@22 122 if (m_synthMap.find(model) == m_synthMap.end()) return;
Chris@22 123
Chris@22 124 RealTimePluginInstance *plugin = m_synthMap[model];
Chris@22 125 if (plugin) {
Chris@26 126 PluginXml(plugin).setParametersFromXml(configurationXml);
Chris@22 127 }
Chris@22 128 }
Chris@22 129
Chris@22 130 QString
Chris@22 131 AudioGenerator::getDefaultPlayPluginId(const Model *model)
Chris@22 132 {
Chris@22 133 const SparseOneDimensionalModel *sodm =
Chris@22 134 dynamic_cast<const SparseOneDimensionalModel *>(model);
Chris@22 135 if (sodm) {
Chris@22 136 return QString("dssi:%1:sample_player").
Chris@22 137 arg(PluginIdentifier::BUILTIN_PLUGIN_SONAME);
Chris@22 138 }
Chris@22 139
Chris@22 140 const NoteModel *nm = dynamic_cast<const NoteModel *>(model);
Chris@11 141 if (nm) {
Chris@22 142 return QString("dssi:%1:sample_player").
Chris@22 143 arg(PluginIdentifier::BUILTIN_PLUGIN_SONAME);
Chris@22 144 }
Chris@22 145
Chris@22 146 return "";
Chris@22 147 }
Chris@22 148
Chris@22 149 QString
Chris@22 150 AudioGenerator::getDefaultPlayPluginConfiguration(const Model *model)
Chris@22 151 {
Chris@30 152 QString program = "";
Chris@30 153
Chris@22 154 const SparseOneDimensionalModel *sodm =
Chris@22 155 dynamic_cast<const SparseOneDimensionalModel *>(model);
Chris@22 156 if (sodm) {
Chris@30 157 program = "cowbell";
Chris@11 158 }
Chris@11 159
Chris@22 160 const NoteModel *nm = dynamic_cast<const NoteModel *>(model);
Chris@22 161 if (nm) {
Chris@30 162 program = "piano";
Chris@30 163 }
Chris@30 164
Chris@30 165 if (program == "") return "";
Chris@30 166
Chris@30 167 return
Chris@30 168 QString("<plugin configuration=\"%1\" program=\"%2\"/>")
Chris@30 169 .arg(XmlExportable::encodeEntities
Chris@30 170 (QString("samplepath=%1")
Chris@30 171 .arg(PluginXml::encodeConfigurationChars(getSamplePath()))))
Chris@30 172 .arg(XmlExportable::encodeEntities(program));
Chris@22 173 }
Chris@22 174
Chris@29 175 QString
Chris@29 176 AudioGenerator::getSampleDir()
Chris@29 177 {
Chris@29 178 if (m_sampleDir != "") return m_sampleDir;
Chris@29 179
Chris@30 180 try {
Chris@30 181 m_sampleDir = TempDirectory::instance()->getSubDirectoryPath("samples");
Chris@30 182 } catch (TempDirectory::DirectoryCreationFailed f) {
Chris@29 183 std::cerr << "WARNING: AudioGenerator::getSampleDir: Failed to create "
Chris@30 184 << "temporary sample directory" << std::endl;
Chris@30 185 m_sampleDir = "";
Chris@30 186 return "";
Chris@29 187 }
Chris@29 188
Chris@29 189 QDir sampleResourceDir(":/samples", "*.wav");
Chris@29 190
Chris@29 191 for (unsigned int i = 0; i < sampleResourceDir.count(); ++i) {
Chris@29 192
Chris@29 193 QString fileName(sampleResourceDir[i]);
Chris@29 194 QFile file(sampleResourceDir.filePath(fileName));
Chris@29 195
Chris@29 196 if (!file.copy(QDir(m_sampleDir).filePath(fileName))) {
Chris@29 197 std::cerr << "WARNING: AudioGenerator::getSampleDir: "
Chris@29 198 << "Unable to copy " << fileName.toStdString()
Chris@29 199 << " into temporary directory \""
Chris@29 200 << m_sampleDir.toStdString() << "\"" << std::endl;
Chris@29 201 }
Chris@29 202 }
Chris@29 203
Chris@29 204 return m_sampleDir;
Chris@29 205 }
Chris@29 206
Chris@30 207 QString
Chris@30 208 AudioGenerator::getSamplePath()
Chris@29 209 {
Chris@29 210 QString samplePath = QString("%1:%2%3%4%5%6")
Chris@29 211 .arg(getSampleDir())
Chris@29 212 .arg(QDir::homePath())
Chris@29 213 .arg(QDir::separator())
Chris@29 214 .arg(".sv")
Chris@29 215 .arg(QDir::separator())
Chris@29 216 .arg("samples");
Chris@30 217 return samplePath;
Chris@30 218 }
Chris@29 219
Chris@30 220 void
Chris@30 221 AudioGenerator::setSamplePath(RealTimePluginInstance *plugin)
Chris@30 222 {
Chris@30 223 plugin->configure("samplepath", getSamplePath().toStdString());
Chris@29 224 }
Chris@29 225
Chris@22 226 RealTimePluginInstance *
Chris@22 227 AudioGenerator::loadPluginFor(const Model *model)
Chris@22 228 {
Chris@22 229 QString pluginId, configurationXml;
Chris@22 230
Chris@22 231 PlayParameters *parameters =
Chris@22 232 PlayParameterRepository::instance()->getPlayParameters(model);
Chris@22 233 if (parameters) {
Chris@22 234 pluginId = parameters->getPlayPluginId();
Chris@22 235 configurationXml = parameters->getPlayPluginConfiguration();
Chris@22 236 }
Chris@22 237
Chris@22 238 if (pluginId == "") {
Chris@22 239 pluginId = getDefaultPlayPluginId(model);
Chris@22 240 configurationXml = getDefaultPlayPluginConfiguration(model);
Chris@22 241 }
Chris@22 242
Chris@22 243 if (pluginId == "") return 0;
Chris@22 244
Chris@22 245 RealTimePluginInstance *plugin = loadPlugin(pluginId, "");
Chris@29 246 if (!plugin) return 0;
Chris@29 247
Chris@22 248 if (configurationXml != "") {
Chris@26 249 PluginXml(plugin).setParametersFromXml(configurationXml);
Chris@22 250 }
Chris@22 251
Chris@22 252 if (parameters) {
Chris@22 253 parameters->setPlayPluginId(pluginId);
Chris@22 254 parameters->setPlayPluginConfiguration(configurationXml);
Chris@22 255 }
Chris@22 256
Chris@22 257 return plugin;
Chris@11 258 }
Chris@11 259
Chris@11 260 RealTimePluginInstance *
Chris@11 261 AudioGenerator::loadPlugin(QString pluginId, QString program)
Chris@11 262 {
Chris@0 263 RealTimePluginFactory *factory =
Chris@0 264 RealTimePluginFactory::instanceFor(pluginId);
Chris@0 265
Chris@0 266 if (!factory) {
Chris@0 267 std::cerr << "Failed to get plugin factory" << std::endl;
Chris@10 268 return false;
Chris@0 269 }
Chris@0 270
Chris@0 271 RealTimePluginInstance *instance =
Chris@0 272 factory->instantiatePlugin
Chris@0 273 (pluginId, 0, 0, m_sourceSampleRate, m_pluginBlockSize, m_targetChannelCount);
Chris@0 274
Chris@29 275 if (!instance) {
Chris@29 276 std::cerr << "Failed to instantiate plugin " << pluginId.toStdString() << std::endl;
Chris@0 277 }
Chris@10 278
Chris@29 279 setSamplePath(instance);
Chris@29 280
Chris@29 281 for (unsigned int i = 0; i < instance->getParameterCount(); ++i) {
Chris@29 282 instance->setParameterValue(i, instance->getParameterDefault(i));
Chris@29 283 }
Chris@29 284 std::string defaultProgram = instance->getProgram(0, 0);
Chris@29 285 if (defaultProgram != "") {
Chris@29 286 std::cerr << "first selecting default program " << defaultProgram << std::endl;
Chris@29 287 instance->selectProgram(defaultProgram);
Chris@29 288 }
Chris@29 289 if (program != "") {
Chris@29 290 std::cerr << "now selecting desired program " << program.toStdString() << std::endl;
Chris@29 291 instance->selectProgram(program.toStdString());
Chris@29 292 }
Chris@29 293 instance->setIdealChannelCount(m_targetChannelCount); // reset!
Chris@29 294
Chris@11 295 return instance;
Chris@0 296 }
Chris@0 297
Chris@0 298 void
Chris@0 299 AudioGenerator::removeModel(Model *model)
Chris@0 300 {
Chris@0 301 SparseOneDimensionalModel *sodm =
Chris@0 302 dynamic_cast<SparseOneDimensionalModel *>(model);
Chris@0 303 if (!sodm) return; // nothing to do
Chris@0 304
Chris@8 305 QMutexLocker locker(&m_mutex);
Chris@8 306
Chris@0 307 if (m_synthMap.find(sodm) == m_synthMap.end()) return;
Chris@0 308
Chris@0 309 RealTimePluginInstance *instance = m_synthMap[sodm];
Chris@0 310 m_synthMap.erase(sodm);
Chris@0 311 delete instance;
Chris@0 312 }
Chris@0 313
Chris@0 314 void
Chris@0 315 AudioGenerator::clearModels()
Chris@0 316 {
Chris@8 317 QMutexLocker locker(&m_mutex);
Chris@0 318 while (!m_synthMap.empty()) {
Chris@0 319 RealTimePluginInstance *instance = m_synthMap.begin()->second;
Chris@0 320 m_synthMap.erase(m_synthMap.begin());
Chris@0 321 delete instance;
Chris@0 322 }
Chris@0 323 }
Chris@0 324
Chris@0 325 void
Chris@0 326 AudioGenerator::reset()
Chris@0 327 {
Chris@8 328 QMutexLocker locker(&m_mutex);
Chris@0 329 for (PluginMap::iterator i = m_synthMap.begin(); i != m_synthMap.end(); ++i) {
Chris@0 330 if (i->second) {
Chris@0 331 i->second->silence();
Chris@0 332 i->second->discardEvents();
Chris@0 333 }
Chris@0 334 }
Chris@0 335
Chris@0 336 m_noteOffs.clear();
Chris@0 337 }
Chris@0 338
Chris@0 339 void
Chris@0 340 AudioGenerator::setTargetChannelCount(size_t targetChannelCount)
Chris@0 341 {
Chris@13 342 if (m_targetChannelCount == targetChannelCount) return;
Chris@13 343
Chris@13 344 std::cerr << "AudioGenerator::setTargetChannelCount(" << targetChannelCount << ")" << std::endl;
Chris@13 345
Chris@8 346 QMutexLocker locker(&m_mutex);
Chris@0 347 m_targetChannelCount = targetChannelCount;
Chris@0 348
Chris@0 349 for (PluginMap::iterator i = m_synthMap.begin(); i != m_synthMap.end(); ++i) {
Chris@0 350 if (i->second) i->second->setIdealChannelCount(targetChannelCount);
Chris@0 351 }
Chris@0 352 }
Chris@0 353
Chris@0 354 size_t
Chris@0 355 AudioGenerator::getBlockSize() const
Chris@0 356 {
Chris@0 357 return m_pluginBlockSize;
Chris@0 358 }
Chris@0 359
Chris@0 360 size_t
Chris@0 361 AudioGenerator::mixModel(Model *model, size_t startFrame, size_t frameCount,
Chris@4 362 float **buffer, size_t fadeIn, size_t fadeOut)
Chris@0 363 {
Chris@0 364 if (m_sourceSampleRate == 0) {
Chris@0 365 std::cerr << "WARNING: AudioGenerator::mixModel: No base source sample rate available" << std::endl;
Chris@0 366 return frameCount;
Chris@0 367 }
Chris@0 368
Chris@8 369 QMutexLocker locker(&m_mutex);
Chris@8 370
Chris@11 371 PlayParameters *parameters =
Chris@11 372 PlayParameterRepository::instance()->getPlayParameters(model);
Chris@0 373 if (!parameters) return frameCount;
Chris@0 374
Chris@0 375 bool playing = !parameters->isPlayMuted();
Chris@0 376 if (!playing) return frameCount;
Chris@0 377
Chris@0 378 float gain = parameters->getPlayGain();
Chris@0 379 float pan = parameters->getPlayPan();
Chris@0 380
Chris@0 381 DenseTimeValueModel *dtvm = dynamic_cast<DenseTimeValueModel *>(model);
Chris@0 382 if (dtvm) {
Chris@0 383 return mixDenseTimeValueModel(dtvm, startFrame, frameCount,
Chris@4 384 buffer, gain, pan, fadeIn, fadeOut);
Chris@0 385 }
Chris@0 386
Chris@0 387 SparseOneDimensionalModel *sodm = dynamic_cast<SparseOneDimensionalModel *>
Chris@0 388 (model);
Chris@0 389 if (sodm) {
Chris@0 390 return mixSparseOneDimensionalModel(sodm, startFrame, frameCount,
Chris@4 391 buffer, gain, pan, fadeIn, fadeOut);
Chris@0 392 }
Chris@0 393
Chris@11 394 NoteModel *nm = dynamic_cast<NoteModel *>(model);
Chris@11 395 if (nm) {
Chris@11 396 return mixNoteModel(nm, startFrame, frameCount,
Chris@11 397 buffer, gain, pan, fadeIn, fadeOut);
Chris@11 398 }
Chris@11 399
Chris@0 400 return frameCount;
Chris@0 401 }
Chris@0 402
Chris@0 403 size_t
Chris@0 404 AudioGenerator::mixDenseTimeValueModel(DenseTimeValueModel *dtvm,
Chris@0 405 size_t startFrame, size_t frames,
Chris@4 406 float **buffer, float gain, float pan,
Chris@4 407 size_t fadeIn, size_t fadeOut)
Chris@0 408 {
Chris@0 409 static float *channelBuffer = 0;
Chris@0 410 static size_t channelBufSiz = 0;
Chris@5 411
Chris@4 412 size_t totalFrames = frames + fadeIn/2 + fadeOut/2;
Chris@4 413
Chris@4 414 if (channelBufSiz < totalFrames) {
Chris@0 415 delete[] channelBuffer;
Chris@4 416 channelBuffer = new float[totalFrames];
Chris@4 417 channelBufSiz = totalFrames;
Chris@0 418 }
Chris@0 419
Chris@0 420 size_t got = 0;
Chris@13 421 size_t prevChannel = 999;
Chris@0 422
Chris@13 423 for (size_t c = 0; c < m_targetChannelCount; ++c) {
Chris@4 424
Chris@13 425 size_t sourceChannel = (c % dtvm->getChannelCount());
Chris@13 426
Chris@13 427 // std::cerr << "mixing channel " << c << " from source channel " << sourceChannel << std::endl;
Chris@13 428
Chris@13 429 float channelGain = gain;
Chris@13 430 if (pan != 0.0) {
Chris@13 431 if (c == 0) {
Chris@13 432 if (pan > 0.0) channelGain *= 1.0 - pan;
Chris@13 433 } else {
Chris@13 434 if (pan < 0.0) channelGain *= pan + 1.0;
Chris@13 435 }
Chris@13 436 }
Chris@13 437
Chris@13 438 if (prevChannel != sourceChannel) {
Chris@13 439 if (startFrame >= fadeIn/2) {
Chris@13 440 got = dtvm->getValues
Chris@13 441 (sourceChannel,
Chris@13 442 startFrame - fadeIn/2, startFrame + frames + fadeOut/2,
Chris@13 443 channelBuffer);
Chris@13 444 } else {
Chris@13 445 size_t missing = fadeIn/2 - startFrame;
Chris@13 446 got = dtvm->getValues
Chris@13 447 (sourceChannel,
Chris@13 448 0, startFrame + frames + fadeOut/2,
Chris@13 449 channelBuffer + missing);
Chris@13 450 }
Chris@13 451 }
Chris@13 452 prevChannel = sourceChannel;
Chris@4 453
Chris@4 454 for (size_t i = 0; i < fadeIn/2; ++i) {
Chris@4 455 float *back = buffer[c];
Chris@4 456 back -= fadeIn/2;
Chris@13 457 back[i] += (channelGain * channelBuffer[i] * i) / fadeIn;
Chris@4 458 }
Chris@4 459
Chris@4 460 for (size_t i = 0; i < frames + fadeOut/2; ++i) {
Chris@13 461 float mult = channelGain;
Chris@4 462 if (i < fadeIn/2) {
Chris@4 463 mult = (mult * i) / fadeIn;
Chris@4 464 }
Chris@4 465 if (i > frames - fadeOut/2) {
Chris@4 466 mult = (mult * ((frames + fadeOut/2) - i)) / fadeOut;
Chris@4 467 }
Chris@4 468 buffer[c][i] += mult * channelBuffer[i];
Chris@0 469 }
Chris@0 470 }
Chris@0 471
Chris@0 472 return got;
Chris@0 473 }
Chris@11 474
Chris@0 475 size_t
Chris@0 476 AudioGenerator::mixSparseOneDimensionalModel(SparseOneDimensionalModel *sodm,
Chris@0 477 size_t startFrame, size_t frames,
Chris@4 478 float **buffer, float gain, float pan,
Chris@4 479 size_t /* fadeIn */,
Chris@4 480 size_t /* fadeOut */)
Chris@0 481 {
Chris@0 482 RealTimePluginInstance *plugin = m_synthMap[sodm];
Chris@0 483 if (!plugin) return 0;
Chris@0 484
Chris@0 485 size_t latency = plugin->getLatency();
Chris@0 486 size_t blocks = frames / m_pluginBlockSize;
Chris@0 487
Chris@0 488 //!!! hang on -- the fact that the audio callback play source's
Chris@0 489 //buffer is a multiple of the plugin's buffer size doesn't mean
Chris@0 490 //that we always get called for a multiple of it here (because it
Chris@0 491 //also depends on the JACK block size). how should we ensure that
Chris@0 492 //all models write the same amount in to the mix, and that we
Chris@0 493 //always have a multiple of the plugin buffer size? I guess this
Chris@0 494 //class has to be queryable for the plugin buffer size & the
Chris@0 495 //callback play source has to use that as a multiple for all the
Chris@0 496 //calls to mixModel
Chris@0 497
Chris@0 498 size_t got = blocks * m_pluginBlockSize;
Chris@0 499
Chris@0 500 #ifdef DEBUG_AUDIO_GENERATOR
Chris@0 501 std::cout << "mixModel [sparse]: frames " << frames
Chris@0 502 << ", blocks " << blocks << std::endl;
Chris@0 503 #endif
Chris@0 504
Chris@0 505 snd_seq_event_t onEv;
Chris@0 506 onEv.type = SND_SEQ_EVENT_NOTEON;
Chris@0 507 onEv.data.note.channel = 0;
Chris@0 508 onEv.data.note.note = 64;
Chris@0 509 onEv.data.note.velocity = 127;
Chris@0 510
Chris@0 511 snd_seq_event_t offEv;
Chris@0 512 offEv.type = SND_SEQ_EVENT_NOTEOFF;
Chris@0 513 offEv.data.note.channel = 0;
Chris@0 514 offEv.data.note.velocity = 0;
Chris@0 515
Chris@0 516 NoteOffSet &noteOffs = m_noteOffs[sodm];
Chris@0 517
Chris@0 518 for (size_t i = 0; i < blocks; ++i) {
Chris@0 519
Chris@0 520 size_t reqStart = startFrame + i * m_pluginBlockSize;
Chris@0 521
Chris@0 522 SparseOneDimensionalModel::PointList points =
Chris@5 523 sodm->getPoints(reqStart + latency,
Chris@0 524 reqStart + latency + m_pluginBlockSize);
Chris@0 525
Chris@26 526 Vamp::RealTime blockTime = Vamp::RealTime::frame2RealTime
Chris@0 527 (startFrame + i * m_pluginBlockSize, m_sourceSampleRate);
Chris@0 528
Chris@0 529 for (SparseOneDimensionalModel::PointList::iterator pli =
Chris@0 530 points.begin(); pli != points.end(); ++pli) {
Chris@0 531
Chris@0 532 size_t pliFrame = pli->frame;
Chris@5 533
Chris@0 534 if (pliFrame >= latency) pliFrame -= latency;
Chris@0 535
Chris@5 536 if (pliFrame < reqStart ||
Chris@5 537 pliFrame >= reqStart + m_pluginBlockSize) continue;
Chris@5 538
Chris@0 539 while (noteOffs.begin() != noteOffs.end() &&
Chris@0 540 noteOffs.begin()->frame <= pliFrame) {
Chris@0 541
Chris@26 542 Vamp::RealTime eventTime = Vamp::RealTime::frame2RealTime
Chris@0 543 (noteOffs.begin()->frame, m_sourceSampleRate);
Chris@0 544
Chris@0 545 offEv.data.note.note = noteOffs.begin()->pitch;
Chris@5 546
Chris@5 547 #ifdef DEBUG_AUDIO_GENERATOR
Chris@5 548 std::cerr << "mixModel [sparse]: sending note-off event at time " << eventTime << " frame " << noteOffs.begin()->frame << std::endl;
Chris@5 549 #endif
Chris@5 550
Chris@0 551 plugin->sendEvent(eventTime, &offEv);
Chris@0 552 noteOffs.erase(noteOffs.begin());
Chris@0 553 }
Chris@0 554
Chris@26 555 Vamp::RealTime eventTime = Vamp::RealTime::frame2RealTime
Chris@0 556 (pliFrame, m_sourceSampleRate);
Chris@0 557
Chris@0 558 plugin->sendEvent(eventTime, &onEv);
Chris@0 559
Chris@0 560 #ifdef DEBUG_AUDIO_GENERATOR
Chris@0 561 std::cout << "mixModel [sparse]: point at frame " << pliFrame << ", block start " << (startFrame + i * m_pluginBlockSize) << ", resulting time " << eventTime << std::endl;
Chris@0 562 #endif
Chris@0 563
Chris@0 564 size_t duration = 7000; // frames [for now]
Chris@0 565 NoteOff noff;
Chris@0 566 noff.pitch = onEv.data.note.note;
Chris@0 567 noff.frame = pliFrame + duration;
Chris@0 568 noteOffs.insert(noff);
Chris@0 569 }
Chris@0 570
Chris@0 571 while (noteOffs.begin() != noteOffs.end() &&
Chris@0 572 noteOffs.begin()->frame <=
Chris@0 573 startFrame + i * m_pluginBlockSize + m_pluginBlockSize) {
Chris@0 574
Chris@26 575 Vamp::RealTime eventTime = Vamp::RealTime::frame2RealTime
Chris@0 576 (noteOffs.begin()->frame, m_sourceSampleRate);
Chris@0 577
Chris@0 578 offEv.data.note.note = noteOffs.begin()->pitch;
Chris@5 579
Chris@5 580 #ifdef DEBUG_AUDIO_GENERATOR
Chris@5 581 std::cerr << "mixModel [sparse]: sending leftover note-off event at time " << eventTime << " frame " << noteOffs.begin()->frame << std::endl;
Chris@5 582 #endif
Chris@5 583
Chris@0 584 plugin->sendEvent(eventTime, &offEv);
Chris@0 585 noteOffs.erase(noteOffs.begin());
Chris@0 586 }
Chris@0 587
Chris@0 588 plugin->run(blockTime);
Chris@0 589 float **outs = plugin->getAudioOutputBuffers();
Chris@0 590
Chris@13 591 for (size_t c = 0; c < m_targetChannelCount; ++c) {
Chris@0 592 #ifdef DEBUG_AUDIO_GENERATOR
Chris@0 593 std::cout << "mixModel [sparse]: adding " << m_pluginBlockSize << " samples from plugin output " << c << std::endl;
Chris@0 594 #endif
Chris@0 595
Chris@13 596 size_t sourceChannel = (c % plugin->getAudioOutputCount());
Chris@13 597
Chris@13 598 float channelGain = gain;
Chris@13 599 if (pan != 0.0) {
Chris@13 600 if (c == 0) {
Chris@13 601 if (pan > 0.0) channelGain *= 1.0 - pan;
Chris@13 602 } else {
Chris@13 603 if (pan < 0.0) channelGain *= pan + 1.0;
Chris@13 604 }
Chris@13 605 }
Chris@13 606
Chris@0 607 for (size_t j = 0; j < m_pluginBlockSize; ++j) {
Chris@13 608 buffer[c][i * m_pluginBlockSize + j] +=
Chris@13 609 channelGain * outs[sourceChannel][j];
Chris@0 610 }
Chris@0 611 }
Chris@0 612 }
Chris@0 613
Chris@0 614 return got;
Chris@0 615 }
Chris@0 616
Chris@11 617
Chris@11 618 //!!! mucho duplication with above -- refactor
Chris@11 619 size_t
Chris@11 620 AudioGenerator::mixNoteModel(NoteModel *nm,
Chris@11 621 size_t startFrame, size_t frames,
Chris@11 622 float **buffer, float gain, float pan,
Chris@11 623 size_t /* fadeIn */,
Chris@11 624 size_t /* fadeOut */)
Chris@11 625 {
Chris@11 626 RealTimePluginInstance *plugin = m_synthMap[nm];
Chris@11 627 if (!plugin) return 0;
Chris@11 628
Chris@11 629 size_t latency = plugin->getLatency();
Chris@11 630 size_t blocks = frames / m_pluginBlockSize;
Chris@11 631
Chris@11 632 //!!! hang on -- the fact that the audio callback play source's
Chris@11 633 //buffer is a multiple of the plugin's buffer size doesn't mean
Chris@11 634 //that we always get called for a multiple of it here (because it
Chris@11 635 //also depends on the JACK block size). how should we ensure that
Chris@11 636 //all models write the same amount in to the mix, and that we
Chris@11 637 //always have a multiple of the plugin buffer size? I guess this
Chris@11 638 //class has to be queryable for the plugin buffer size & the
Chris@11 639 //callback play source has to use that as a multiple for all the
Chris@11 640 //calls to mixModel
Chris@11 641
Chris@11 642 size_t got = blocks * m_pluginBlockSize;
Chris@11 643
Chris@11 644 #ifdef DEBUG_AUDIO_GENERATOR
Chris@11 645 std::cout << "mixModel [note]: frames " << frames
Chris@11 646 << ", blocks " << blocks << std::endl;
Chris@11 647 #endif
Chris@11 648
Chris@11 649 snd_seq_event_t onEv;
Chris@11 650 onEv.type = SND_SEQ_EVENT_NOTEON;
Chris@11 651 onEv.data.note.channel = 0;
Chris@11 652 onEv.data.note.note = 64;
Chris@11 653 onEv.data.note.velocity = 127;
Chris@11 654
Chris@11 655 snd_seq_event_t offEv;
Chris@11 656 offEv.type = SND_SEQ_EVENT_NOTEOFF;
Chris@11 657 offEv.data.note.channel = 0;
Chris@11 658 offEv.data.note.velocity = 0;
Chris@11 659
Chris@11 660 NoteOffSet &noteOffs = m_noteOffs[nm];
Chris@11 661
Chris@11 662 for (size_t i = 0; i < blocks; ++i) {
Chris@11 663
Chris@11 664 size_t reqStart = startFrame + i * m_pluginBlockSize;
Chris@11 665
Chris@11 666 NoteModel::PointList points =
Chris@11 667 nm->getPoints(reqStart + latency,
Chris@11 668 reqStart + latency + m_pluginBlockSize);
Chris@11 669
Chris@26 670 Vamp::RealTime blockTime = Vamp::RealTime::frame2RealTime
Chris@11 671 (startFrame + i * m_pluginBlockSize, m_sourceSampleRate);
Chris@11 672
Chris@11 673 for (NoteModel::PointList::iterator pli =
Chris@11 674 points.begin(); pli != points.end(); ++pli) {
Chris@11 675
Chris@11 676 size_t pliFrame = pli->frame;
Chris@11 677
Chris@11 678 if (pliFrame >= latency) pliFrame -= latency;
Chris@11 679
Chris@11 680 if (pliFrame < reqStart ||
Chris@11 681 pliFrame >= reqStart + m_pluginBlockSize) continue;
Chris@11 682
Chris@11 683 while (noteOffs.begin() != noteOffs.end() &&
Chris@11 684 noteOffs.begin()->frame <= pliFrame) {
Chris@11 685
Chris@26 686 Vamp::RealTime eventTime = Vamp::RealTime::frame2RealTime
Chris@11 687 (noteOffs.begin()->frame, m_sourceSampleRate);
Chris@11 688
Chris@11 689 offEv.data.note.note = noteOffs.begin()->pitch;
Chris@11 690
Chris@11 691 #ifdef DEBUG_AUDIO_GENERATOR
Chris@11 692 std::cerr << "mixModel [note]: sending note-off event at time " << eventTime << " frame " << noteOffs.begin()->frame << std::endl;
Chris@11 693 #endif
Chris@11 694
Chris@11 695 plugin->sendEvent(eventTime, &offEv);
Chris@11 696 noteOffs.erase(noteOffs.begin());
Chris@11 697 }
Chris@11 698
Chris@26 699 Vamp::RealTime eventTime = Vamp::RealTime::frame2RealTime
Chris@11 700 (pliFrame, m_sourceSampleRate);
Chris@11 701
Chris@11 702 onEv.data.note.note = lrintf(pli->value);
Chris@11 703
Chris@11 704 plugin->sendEvent(eventTime, &onEv);
Chris@11 705
Chris@11 706 #ifdef DEBUG_AUDIO_GENERATOR
Chris@11 707 std::cout << "mixModel [note]: point at frame " << pliFrame << ", block start " << (startFrame + i * m_pluginBlockSize) << ", resulting time " << eventTime << std::endl;
Chris@11 708 #endif
Chris@11 709
Chris@11 710 size_t duration = pli->duration;
Chris@11 711 NoteOff noff;
Chris@11 712 noff.pitch = onEv.data.note.note;
Chris@11 713 noff.frame = pliFrame + duration;
Chris@11 714 noteOffs.insert(noff);
Chris@11 715 }
Chris@11 716
Chris@11 717 while (noteOffs.begin() != noteOffs.end() &&
Chris@11 718 noteOffs.begin()->frame <=
Chris@11 719 startFrame + i * m_pluginBlockSize + m_pluginBlockSize) {
Chris@11 720
Chris@26 721 Vamp::RealTime eventTime = Vamp::RealTime::frame2RealTime
Chris@11 722 (noteOffs.begin()->frame, m_sourceSampleRate);
Chris@11 723
Chris@11 724 offEv.data.note.note = noteOffs.begin()->pitch;
Chris@11 725
Chris@11 726 #ifdef DEBUG_AUDIO_GENERATOR
Chris@11 727 std::cerr << "mixModel [note]: sending leftover note-off event at time " << eventTime << " frame " << noteOffs.begin()->frame << std::endl;
Chris@11 728 #endif
Chris@11 729
Chris@11 730 plugin->sendEvent(eventTime, &offEv);
Chris@11 731 noteOffs.erase(noteOffs.begin());
Chris@11 732 }
Chris@11 733
Chris@11 734 plugin->run(blockTime);
Chris@11 735 float **outs = plugin->getAudioOutputBuffers();
Chris@11 736
Chris@13 737 for (size_t c = 0; c < m_targetChannelCount; ++c) {
Chris@11 738 #ifdef DEBUG_AUDIO_GENERATOR
Chris@11 739 std::cout << "mixModel [note]: adding " << m_pluginBlockSize << " samples from plugin output " << c << std::endl;
Chris@11 740 #endif
Chris@11 741
Chris@13 742 size_t sourceChannel = (c % plugin->getAudioOutputCount());
Chris@13 743
Chris@13 744 float channelGain = gain;
Chris@13 745 if (pan != 0.0) {
Chris@13 746 if (c == 0) {
Chris@13 747 if (pan > 0.0) channelGain *= 1.0 - pan;
Chris@13 748 } else {
Chris@13 749 if (pan < 0.0) channelGain *= pan + 1.0;
Chris@13 750 }
Chris@13 751 }
Chris@13 752
Chris@11 753 for (size_t j = 0; j < m_pluginBlockSize; ++j) {
Chris@13 754 buffer[c][i * m_pluginBlockSize + j] +=
Chris@13 755 channelGain * outs[sourceChannel][j];
Chris@11 756 }
Chris@11 757 }
Chris@11 758 }
Chris@11 759
Chris@11 760 return got;
Chris@11 761 }
Chris@11 762