annotate audioio/AudioGenerator.cpp @ 21:ebe07d3560e6

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