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