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