annotate audioio/AudioGenerator.cpp @ 26:cc48a7189152

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