annotate audioio/AudioGenerator.cpp @ 29:dd2084d32652

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