annotate sv/audioio/AudioGenerator.cpp @ 282:d9319859a4cf tip

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