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@299
|
28 #include "data/model/NoteData.h"
|
Chris@43
|
29
|
Chris@43
|
30 #include <iostream>
|
Chris@167
|
31 #include <cmath>
|
Chris@43
|
32
|
Chris@43
|
33 #include <QDir>
|
Chris@43
|
34 #include <QFile>
|
Chris@43
|
35
|
Chris@43
|
36 const size_t
|
Chris@305
|
37 AudioGenerator::m_processingBlockSize = 2048;
|
Chris@43
|
38
|
Chris@43
|
39 QString
|
Chris@43
|
40 AudioGenerator::m_sampleDir = "";
|
Chris@43
|
41
|
Chris@43
|
42 //#define DEBUG_AUDIO_GENERATOR 1
|
Chris@43
|
43
|
Chris@43
|
44 AudioGenerator::AudioGenerator() :
|
Chris@43
|
45 m_sourceSampleRate(0),
|
Chris@43
|
46 m_targetChannelCount(1),
|
Chris@43
|
47 m_soloing(false)
|
Chris@43
|
48 {
|
Chris@108
|
49 initialiseSampleDir();
|
Chris@43
|
50
|
Chris@43
|
51 connect(PlayParameterRepository::getInstance(),
|
Chris@305
|
52 SIGNAL(playSampleIdChanged(const Playable *, QString)),
|
Chris@43
|
53 this,
|
Chris@305
|
54 SLOT(playSampleIdChanged(const Playable *, QString)));
|
Chris@43
|
55 }
|
Chris@43
|
56
|
Chris@43
|
57 AudioGenerator::~AudioGenerator()
|
Chris@43
|
58 {
|
Chris@177
|
59 #ifdef DEBUG_AUDIO_GENERATOR
|
Chris@233
|
60 SVDEBUG << "AudioGenerator::~AudioGenerator" << endl;
|
Chris@177
|
61 #endif
|
Chris@43
|
62 }
|
Chris@43
|
63
|
Chris@108
|
64 void
|
Chris@108
|
65 AudioGenerator::initialiseSampleDir()
|
Chris@43
|
66 {
|
Chris@108
|
67 if (m_sampleDir != "") return;
|
Chris@108
|
68
|
Chris@108
|
69 try {
|
Chris@108
|
70 m_sampleDir = TempDirectory::getInstance()->getSubDirectoryPath("samples");
|
Chris@108
|
71 } catch (DirectoryCreationFailed f) {
|
Chris@293
|
72 cerr << "WARNING: AudioGenerator::initialiseSampleDir:"
|
Chris@108
|
73 << " Failed to create temporary sample directory"
|
Chris@293
|
74 << endl;
|
Chris@108
|
75 m_sampleDir = "";
|
Chris@108
|
76 return;
|
Chris@108
|
77 }
|
Chris@108
|
78
|
Chris@108
|
79 QDir sampleResourceDir(":/samples", "*.wav");
|
Chris@108
|
80
|
Chris@108
|
81 for (unsigned int i = 0; i < sampleResourceDir.count(); ++i) {
|
Chris@108
|
82
|
Chris@108
|
83 QString fileName(sampleResourceDir[i]);
|
Chris@108
|
84 QFile file(sampleResourceDir.filePath(fileName));
|
Chris@151
|
85 QString target = QDir(m_sampleDir).filePath(fileName);
|
Chris@108
|
86
|
Chris@151
|
87 if (!file.copy(target)) {
|
Chris@293
|
88 cerr << "WARNING: AudioGenerator::getSampleDir: "
|
Chris@294
|
89 << "Unable to copy " << fileName
|
Chris@108
|
90 << " into temporary directory \""
|
Chris@293
|
91 << m_sampleDir << "\"" << endl;
|
Chris@151
|
92 } else {
|
Chris@151
|
93 QFile tf(target);
|
Chris@151
|
94 tf.setPermissions(tf.permissions() |
|
Chris@151
|
95 QFile::WriteOwner |
|
Chris@151
|
96 QFile::WriteUser);
|
Chris@108
|
97 }
|
Chris@43
|
98 }
|
Chris@43
|
99 }
|
Chris@43
|
100
|
Chris@43
|
101 bool
|
Chris@43
|
102 AudioGenerator::addModel(Model *model)
|
Chris@43
|
103 {
|
Chris@43
|
104 if (m_sourceSampleRate == 0) {
|
Chris@43
|
105
|
Chris@43
|
106 m_sourceSampleRate = model->getSampleRate();
|
Chris@43
|
107
|
Chris@43
|
108 } else {
|
Chris@43
|
109
|
Chris@43
|
110 DenseTimeValueModel *dtvm =
|
Chris@43
|
111 dynamic_cast<DenseTimeValueModel *>(model);
|
Chris@43
|
112
|
Chris@43
|
113 if (dtvm) {
|
Chris@43
|
114 m_sourceSampleRate = model->getSampleRate();
|
Chris@43
|
115 return true;
|
Chris@43
|
116 }
|
Chris@43
|
117 }
|
Chris@305
|
118 /*!!!
|
Chris@43
|
119 RealTimePluginInstance *plugin = loadPluginFor(model);
|
Chris@43
|
120 if (plugin) {
|
Chris@43
|
121 QMutexLocker locker(&m_mutex);
|
Chris@43
|
122 m_synthMap[model] = plugin;
|
Chris@43
|
123 return true;
|
Chris@43
|
124 }
|
Chris@305
|
125 */
|
Chris@43
|
126 return false;
|
Chris@43
|
127 }
|
Chris@43
|
128
|
Chris@43
|
129 void
|
Chris@305
|
130 AudioGenerator::playSampleIdChanged(const Playable *playable, QString)
|
Chris@43
|
131 {
|
Chris@108
|
132 const Model *model = dynamic_cast<const Model *>(playable);
|
Chris@108
|
133 if (!model) {
|
Chris@305
|
134 cerr << "WARNING: AudioGenerator::playSampleIdChanged: playable "
|
Chris@108
|
135 << playable << " is not a supported model type"
|
Chris@293
|
136 << endl;
|
Chris@108
|
137 return;
|
Chris@108
|
138 }
|
Chris@108
|
139
|
Chris@43
|
140 if (m_synthMap.find(model) == m_synthMap.end()) return;
|
Chris@305
|
141 /*!!!
|
Chris@43
|
142 RealTimePluginInstance *plugin = loadPluginFor(model);
|
Chris@43
|
143 if (plugin) {
|
Chris@43
|
144 QMutexLocker locker(&m_mutex);
|
Chris@43
|
145 delete m_synthMap[model];
|
Chris@43
|
146 m_synthMap[model] = plugin;
|
Chris@43
|
147 }
|
Chris@305
|
148 */
|
Chris@43
|
149 }
|
Chris@305
|
150 /*!!!
|
Chris@43
|
151 void
|
Chris@108
|
152 AudioGenerator::playPluginConfigurationChanged(const Playable *playable,
|
Chris@43
|
153 QString configurationXml)
|
Chris@43
|
154 {
|
Chris@233
|
155 // SVDEBUG << "AudioGenerator::playPluginConfigurationChanged" << endl;
|
Chris@43
|
156
|
Chris@108
|
157 const Model *model = dynamic_cast<const Model *>(playable);
|
Chris@108
|
158 if (!model) {
|
Chris@305
|
159 cerr << "WARNING: AudioGenerator::playSampleIdChanged: playable "
|
Chris@108
|
160 << playable << " is not a supported model type"
|
Chris@293
|
161 << endl;
|
Chris@108
|
162 return;
|
Chris@108
|
163 }
|
Chris@108
|
164
|
Chris@43
|
165 if (m_synthMap.find(model) == m_synthMap.end()) {
|
Chris@233
|
166 SVDEBUG << "AudioGenerator::playPluginConfigurationChanged: We don't know about this plugin" << endl;
|
Chris@43
|
167 return;
|
Chris@43
|
168 }
|
Chris@43
|
169
|
Chris@43
|
170 RealTimePluginInstance *plugin = m_synthMap[model];
|
Chris@43
|
171 if (plugin) {
|
Chris@43
|
172 PluginXml(plugin).setParametersFromXml(configurationXml);
|
Chris@43
|
173 }
|
Chris@43
|
174 }
|
Chris@43
|
175
|
Chris@43
|
176 void
|
Chris@43
|
177 AudioGenerator::setSampleDir(RealTimePluginInstance *plugin)
|
Chris@43
|
178 {
|
Chris@108
|
179 if (m_sampleDir != "") {
|
Chris@108
|
180 plugin->configure("sampledir", m_sampleDir.toStdString());
|
Chris@108
|
181 }
|
Chris@43
|
182 }
|
Chris@43
|
183
|
Chris@43
|
184 RealTimePluginInstance *
|
Chris@43
|
185 AudioGenerator::loadPluginFor(const Model *model)
|
Chris@43
|
186 {
|
Chris@43
|
187 QString pluginId, configurationXml;
|
Chris@43
|
188
|
Chris@108
|
189 const Playable *playable = model;
|
Chris@108
|
190 if (!playable || !playable->canPlay()) return 0;
|
Chris@108
|
191
|
Chris@43
|
192 PlayParameters *parameters =
|
Chris@108
|
193 PlayParameterRepository::getInstance()->getPlayParameters(playable);
|
Chris@43
|
194 if (parameters) {
|
Chris@305
|
195 pluginId = parameters->getPlaySampleId();
|
Chris@43
|
196 configurationXml = parameters->getPlayPluginConfiguration();
|
Chris@43
|
197 }
|
Chris@43
|
198
|
Chris@276
|
199 std::cerr << "AudioGenerator::loadPluginFor(" << model << "): id = " << pluginId << std::endl;
|
Chris@276
|
200
|
Chris@276
|
201 if (pluginId == "") {
|
Chris@276
|
202 SVDEBUG << "AudioGenerator::loadPluginFor(" << model << "): parameters contain empty plugin ID, skipping" << endl;
|
Chris@276
|
203 return 0;
|
Chris@276
|
204 }
|
Chris@43
|
205
|
Chris@43
|
206 RealTimePluginInstance *plugin = loadPlugin(pluginId, "");
|
Chris@43
|
207 if (!plugin) return 0;
|
Chris@43
|
208
|
Chris@276
|
209 std::cerr << "AudioGenerator::loadPluginFor(" << model << "): loaded plugin "
|
Chris@276
|
210 << plugin << std::endl;
|
Chris@276
|
211
|
Chris@43
|
212 if (configurationXml != "") {
|
Chris@43
|
213 PluginXml(plugin).setParametersFromXml(configurationXml);
|
Chris@179
|
214 setSampleDir(plugin);
|
Chris@43
|
215 }
|
Chris@43
|
216
|
Chris@108
|
217 configurationXml = PluginXml(plugin).toXmlString();
|
Chris@108
|
218
|
Chris@43
|
219 if (parameters) {
|
Chris@305
|
220 parameters->setPlaySampleId(pluginId);
|
Chris@43
|
221 parameters->setPlayPluginConfiguration(configurationXml);
|
Chris@43
|
222 }
|
Chris@43
|
223
|
Chris@43
|
224 return plugin;
|
Chris@43
|
225 }
|
Chris@43
|
226
|
Chris@43
|
227 RealTimePluginInstance *
|
Chris@43
|
228 AudioGenerator::loadPlugin(QString pluginId, QString program)
|
Chris@43
|
229 {
|
Chris@43
|
230 RealTimePluginFactory *factory =
|
Chris@43
|
231 RealTimePluginFactory::instanceFor(pluginId);
|
Chris@43
|
232
|
Chris@43
|
233 if (!factory) {
|
Chris@293
|
234 cerr << "Failed to get plugin factory" << endl;
|
Chris@274
|
235 return 0;
|
Chris@43
|
236 }
|
Chris@43
|
237
|
Chris@43
|
238 RealTimePluginInstance *instance =
|
Chris@43
|
239 factory->instantiatePlugin
|
Chris@305
|
240 (pluginId, 0, 0, m_sourceSampleRate, m_processingBlockSize, m_targetChannelCount);
|
Chris@43
|
241
|
Chris@43
|
242 if (!instance) {
|
Chris@293
|
243 cerr << "Failed to instantiate plugin " << pluginId << endl;
|
Chris@43
|
244 return 0;
|
Chris@43
|
245 }
|
Chris@43
|
246
|
Chris@43
|
247 setSampleDir(instance);
|
Chris@43
|
248
|
Chris@43
|
249 for (unsigned int i = 0; i < instance->getParameterCount(); ++i) {
|
Chris@43
|
250 instance->setParameterValue(i, instance->getParameterDefault(i));
|
Chris@43
|
251 }
|
Chris@43
|
252 std::string defaultProgram = instance->getProgram(0, 0);
|
Chris@43
|
253 if (defaultProgram != "") {
|
Chris@295
|
254 cerr << "first selecting default program " << defaultProgram << endl;
|
Chris@43
|
255 instance->selectProgram(defaultProgram);
|
Chris@43
|
256 }
|
Chris@43
|
257 if (program != "") {
|
Chris@295
|
258 cerr << "now selecting desired program " << program << endl;
|
Chris@43
|
259 instance->selectProgram(program.toStdString());
|
Chris@43
|
260 }
|
Chris@43
|
261 instance->setIdealChannelCount(m_targetChannelCount); // reset!
|
Chris@43
|
262
|
Chris@43
|
263 return instance;
|
Chris@43
|
264 }
|
Chris@305
|
265 */
|
Chris@43
|
266 void
|
Chris@43
|
267 AudioGenerator::removeModel(Model *model)
|
Chris@43
|
268 {
|
Chris@43
|
269 SparseOneDimensionalModel *sodm =
|
Chris@43
|
270 dynamic_cast<SparseOneDimensionalModel *>(model);
|
Chris@43
|
271 if (!sodm) return; // nothing to do
|
Chris@43
|
272
|
Chris@43
|
273 QMutexLocker locker(&m_mutex);
|
Chris@43
|
274
|
Chris@43
|
275 if (m_synthMap.find(sodm) == m_synthMap.end()) return;
|
Chris@43
|
276
|
Chris@305
|
277 //!!! RealTimePluginInstance *instance = m_synthMap[sodm];
|
Chris@305
|
278 // m_synthMap.erase(sodm);
|
Chris@43
|
279 delete instance;
|
Chris@43
|
280 }
|
Chris@43
|
281
|
Chris@43
|
282 void
|
Chris@43
|
283 AudioGenerator::clearModels()
|
Chris@43
|
284 {
|
Chris@43
|
285 QMutexLocker locker(&m_mutex);
|
Chris@305
|
286 /*!!!
|
Chris@43
|
287 while (!m_synthMap.empty()) {
|
Chris@43
|
288 RealTimePluginInstance *instance = m_synthMap.begin()->second;
|
Chris@43
|
289 m_synthMap.erase(m_synthMap.begin());
|
Chris@43
|
290 delete instance;
|
Chris@43
|
291 }
|
Chris@305
|
292 */
|
Chris@43
|
293 }
|
Chris@43
|
294
|
Chris@43
|
295 void
|
Chris@43
|
296 AudioGenerator::reset()
|
Chris@43
|
297 {
|
Chris@43
|
298 QMutexLocker locker(&m_mutex);
|
Chris@305
|
299 /*!!!
|
Chris@43
|
300 for (PluginMap::iterator i = m_synthMap.begin(); i != m_synthMap.end(); ++i) {
|
Chris@43
|
301 if (i->second) {
|
Chris@43
|
302 i->second->silence();
|
Chris@43
|
303 i->second->discardEvents();
|
Chris@43
|
304 }
|
Chris@43
|
305 }
|
Chris@305
|
306 */
|
Chris@43
|
307
|
Chris@43
|
308 m_noteOffs.clear();
|
Chris@43
|
309 }
|
Chris@43
|
310
|
Chris@43
|
311 void
|
Chris@43
|
312 AudioGenerator::setTargetChannelCount(size_t targetChannelCount)
|
Chris@43
|
313 {
|
Chris@43
|
314 if (m_targetChannelCount == targetChannelCount) return;
|
Chris@43
|
315
|
Chris@233
|
316 // SVDEBUG << "AudioGenerator::setTargetChannelCount(" << targetChannelCount << ")" << endl;
|
Chris@43
|
317
|
Chris@43
|
318 QMutexLocker locker(&m_mutex);
|
Chris@43
|
319 m_targetChannelCount = targetChannelCount;
|
Chris@43
|
320
|
Chris@305
|
321 /*!!!
|
Chris@43
|
322 for (PluginMap::iterator i = m_synthMap.begin(); i != m_synthMap.end(); ++i) {
|
Chris@43
|
323 if (i->second) i->second->setIdealChannelCount(targetChannelCount);
|
Chris@43
|
324 }
|
Chris@305
|
325 */
|
Chris@43
|
326 }
|
Chris@43
|
327
|
Chris@43
|
328 size_t
|
Chris@43
|
329 AudioGenerator::getBlockSize() const
|
Chris@43
|
330 {
|
Chris@305
|
331 return m_processingBlockSize;
|
Chris@43
|
332 }
|
Chris@43
|
333
|
Chris@43
|
334 void
|
Chris@43
|
335 AudioGenerator::setSoloModelSet(std::set<Model *> s)
|
Chris@43
|
336 {
|
Chris@43
|
337 QMutexLocker locker(&m_mutex);
|
Chris@43
|
338
|
Chris@43
|
339 m_soloModelSet = s;
|
Chris@43
|
340 m_soloing = true;
|
Chris@43
|
341 }
|
Chris@43
|
342
|
Chris@43
|
343 void
|
Chris@43
|
344 AudioGenerator::clearSoloModelSet()
|
Chris@43
|
345 {
|
Chris@43
|
346 QMutexLocker locker(&m_mutex);
|
Chris@43
|
347
|
Chris@43
|
348 m_soloModelSet.clear();
|
Chris@43
|
349 m_soloing = false;
|
Chris@43
|
350 }
|
Chris@43
|
351
|
Chris@43
|
352 size_t
|
Chris@43
|
353 AudioGenerator::mixModel(Model *model, size_t startFrame, size_t frameCount,
|
Chris@43
|
354 float **buffer, size_t fadeIn, size_t fadeOut)
|
Chris@43
|
355 {
|
Chris@43
|
356 if (m_sourceSampleRate == 0) {
|
Chris@293
|
357 cerr << "WARNING: AudioGenerator::mixModel: No base source sample rate available" << endl;
|
Chris@43
|
358 return frameCount;
|
Chris@43
|
359 }
|
Chris@43
|
360
|
Chris@43
|
361 QMutexLocker locker(&m_mutex);
|
Chris@43
|
362
|
Chris@108
|
363 Playable *playable = model;
|
Chris@108
|
364 if (!playable || !playable->canPlay()) return frameCount;
|
Chris@108
|
365
|
Chris@43
|
366 PlayParameters *parameters =
|
Chris@108
|
367 PlayParameterRepository::getInstance()->getPlayParameters(playable);
|
Chris@43
|
368 if (!parameters) return frameCount;
|
Chris@43
|
369
|
Chris@43
|
370 bool playing = !parameters->isPlayMuted();
|
Chris@43
|
371 if (!playing) {
|
Chris@43
|
372 #ifdef DEBUG_AUDIO_GENERATOR
|
Chris@293
|
373 cout << "AudioGenerator::mixModel(" << model << "): muted" << endl;
|
Chris@43
|
374 #endif
|
Chris@43
|
375 return frameCount;
|
Chris@43
|
376 }
|
Chris@43
|
377
|
Chris@43
|
378 if (m_soloing) {
|
Chris@43
|
379 if (m_soloModelSet.find(model) == m_soloModelSet.end()) {
|
Chris@43
|
380 #ifdef DEBUG_AUDIO_GENERATOR
|
Chris@293
|
381 cout << "AudioGenerator::mixModel(" << model << "): not one of the solo'd models" << endl;
|
Chris@43
|
382 #endif
|
Chris@43
|
383 return frameCount;
|
Chris@43
|
384 }
|
Chris@43
|
385 }
|
Chris@43
|
386
|
Chris@43
|
387 float gain = parameters->getPlayGain();
|
Chris@43
|
388 float pan = parameters->getPlayPan();
|
Chris@43
|
389
|
Chris@43
|
390 DenseTimeValueModel *dtvm = dynamic_cast<DenseTimeValueModel *>(model);
|
Chris@43
|
391 if (dtvm) {
|
Chris@43
|
392 return mixDenseTimeValueModel(dtvm, startFrame, frameCount,
|
Chris@43
|
393 buffer, gain, pan, fadeIn, fadeOut);
|
Chris@43
|
394 }
|
Chris@43
|
395
|
Chris@279
|
396 bool synthetic =
|
Chris@279
|
397 (qobject_cast<SparseOneDimensionalModel *>(model) ||
|
Chris@278
|
398 qobject_cast<NoteModel *>(model) ||
|
Chris@278
|
399 qobject_cast<FlexiNoteModel *>(model));
|
Chris@43
|
400
|
Chris@279
|
401 if (synthetic) {
|
Chris@275
|
402 return mixSyntheticNoteModel(model, startFrame, frameCount,
|
Chris@275
|
403 buffer, gain, pan, fadeIn, fadeOut);
|
Chris@43
|
404 }
|
Chris@43
|
405
|
Chris@276
|
406 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
|
407
|
Chris@43
|
408 return frameCount;
|
Chris@43
|
409 }
|
Chris@43
|
410
|
Chris@43
|
411 size_t
|
Chris@43
|
412 AudioGenerator::mixDenseTimeValueModel(DenseTimeValueModel *dtvm,
|
Chris@43
|
413 size_t startFrame, size_t frames,
|
Chris@43
|
414 float **buffer, float gain, float pan,
|
Chris@43
|
415 size_t fadeIn, size_t fadeOut)
|
Chris@43
|
416 {
|
Chris@80
|
417 static float **channelBuffer = 0;
|
Chris@80
|
418 static size_t channelBufSiz = 0;
|
Chris@80
|
419 static size_t channelBufCount = 0;
|
Chris@43
|
420
|
Chris@43
|
421 size_t totalFrames = frames + fadeIn/2 + fadeOut/2;
|
Chris@43
|
422
|
Chris@80
|
423 size_t modelChannels = dtvm->getChannelCount();
|
Chris@80
|
424
|
Chris@80
|
425 if (channelBufSiz < totalFrames || channelBufCount < modelChannels) {
|
Chris@80
|
426
|
Chris@80
|
427 for (size_t c = 0; c < channelBufCount; ++c) {
|
Chris@80
|
428 delete[] channelBuffer[c];
|
Chris@80
|
429 }
|
Chris@80
|
430
|
Chris@43
|
431 delete[] channelBuffer;
|
Chris@80
|
432 channelBuffer = new float *[modelChannels];
|
Chris@80
|
433
|
Chris@80
|
434 for (size_t c = 0; c < modelChannels; ++c) {
|
Chris@80
|
435 channelBuffer[c] = new float[totalFrames];
|
Chris@80
|
436 }
|
Chris@80
|
437
|
Chris@80
|
438 channelBufCount = modelChannels;
|
Chris@43
|
439 channelBufSiz = totalFrames;
|
Chris@43
|
440 }
|
Chris@80
|
441
|
Chris@43
|
442 size_t got = 0;
|
Chris@80
|
443
|
Chris@80
|
444 if (startFrame >= fadeIn/2) {
|
Chris@80
|
445 got = dtvm->getData(0, modelChannels - 1,
|
Chris@80
|
446 startFrame - fadeIn/2,
|
Chris@80
|
447 frames + fadeOut/2 + fadeIn/2,
|
Chris@80
|
448 channelBuffer);
|
Chris@80
|
449 } else {
|
Chris@80
|
450 size_t missing = fadeIn/2 - startFrame;
|
Chris@80
|
451
|
Chris@80
|
452 for (size_t c = 0; c < modelChannels; ++c) {
|
Chris@80
|
453 channelBuffer[c] += missing;
|
Chris@80
|
454 }
|
Chris@80
|
455
|
Chris@80
|
456 got = dtvm->getData(0, modelChannels - 1,
|
Chris@80
|
457 startFrame,
|
Chris@80
|
458 frames + fadeOut/2,
|
Chris@80
|
459 channelBuffer);
|
Chris@80
|
460
|
Chris@80
|
461 for (size_t c = 0; c < modelChannels; ++c) {
|
Chris@80
|
462 channelBuffer[c] -= missing;
|
Chris@80
|
463 }
|
Chris@80
|
464
|
Chris@80
|
465 got += missing;
|
Chris@80
|
466 }
|
Chris@43
|
467
|
Chris@43
|
468 for (size_t c = 0; c < m_targetChannelCount; ++c) {
|
Chris@43
|
469
|
Chris@80
|
470 size_t sourceChannel = (c % modelChannels);
|
Chris@43
|
471
|
Chris@233
|
472 // SVDEBUG << "mixing channel " << c << " from source channel " << sourceChannel << endl;
|
Chris@43
|
473
|
Chris@43
|
474 float channelGain = gain;
|
Chris@43
|
475 if (pan != 0.0) {
|
Chris@43
|
476 if (c == 0) {
|
Chris@43
|
477 if (pan > 0.0) channelGain *= 1.0 - pan;
|
Chris@43
|
478 } else {
|
Chris@43
|
479 if (pan < 0.0) channelGain *= pan + 1.0;
|
Chris@43
|
480 }
|
Chris@43
|
481 }
|
Chris@43
|
482
|
Chris@43
|
483 for (size_t i = 0; i < fadeIn/2; ++i) {
|
Chris@43
|
484 float *back = buffer[c];
|
Chris@43
|
485 back -= fadeIn/2;
|
Chris@80
|
486 back[i] += (channelGain * channelBuffer[sourceChannel][i] * i) / fadeIn;
|
Chris@43
|
487 }
|
Chris@43
|
488
|
Chris@43
|
489 for (size_t i = 0; i < frames + fadeOut/2; ++i) {
|
Chris@43
|
490 float mult = channelGain;
|
Chris@43
|
491 if (i < fadeIn/2) {
|
Chris@43
|
492 mult = (mult * i) / fadeIn;
|
Chris@43
|
493 }
|
Chris@43
|
494 if (i > frames - fadeOut/2) {
|
Chris@43
|
495 mult = (mult * ((frames + fadeOut/2) - i)) / fadeOut;
|
Chris@43
|
496 }
|
Chris@80
|
497 float val = channelBuffer[sourceChannel][i];
|
Chris@80
|
498 if (i >= got) val = 0.f;
|
Chris@80
|
499 buffer[c][i] += mult * val;
|
Chris@43
|
500 }
|
Chris@43
|
501 }
|
Chris@43
|
502
|
Chris@43
|
503 return got;
|
Chris@43
|
504 }
|
Chris@43
|
505
|
Chris@43
|
506 size_t
|
Chris@275
|
507 AudioGenerator::mixSyntheticNoteModel(Model *model,
|
Chris@275
|
508 size_t startFrame, size_t frames,
|
Chris@275
|
509 float **buffer, float gain, float pan,
|
Chris@275
|
510 size_t /* fadeIn */,
|
Chris@275
|
511 size_t /* fadeOut */)
|
Chris@43
|
512 {
|
Chris@275
|
513 RealTimePluginInstance *plugin = m_synthMap[model];
|
Chris@43
|
514 if (!plugin) return 0;
|
Chris@43
|
515
|
Chris@43
|
516 size_t latency = plugin->getLatency();
|
Chris@305
|
517 size_t blocks = frames / m_processingBlockSize;
|
Chris@43
|
518
|
Chris@43
|
519 //!!! hang on -- the fact that the audio callback play source's
|
Chris@43
|
520 //buffer is a multiple of the plugin's buffer size doesn't mean
|
Chris@43
|
521 //that we always get called for a multiple of it here (because it
|
Chris@43
|
522 //also depends on the JACK block size). how should we ensure that
|
Chris@43
|
523 //all models write the same amount in to the mix, and that we
|
Chris@43
|
524 //always have a multiple of the plugin buffer size? I guess this
|
Chris@43
|
525 //class has to be queryable for the plugin buffer size & the
|
Chris@43
|
526 //callback play source has to use that as a multiple for all the
|
Chris@43
|
527 //calls to mixModel
|
Chris@43
|
528
|
Chris@305
|
529 size_t got = blocks * m_processingBlockSize;
|
Chris@43
|
530
|
Chris@43
|
531 #ifdef DEBUG_AUDIO_GENERATOR
|
Chris@293
|
532 cout << "mixModel [synthetic note]: frames " << frames
|
Chris@293
|
533 << ", blocks " << blocks << endl;
|
Chris@43
|
534 #endif
|
Chris@43
|
535
|
Chris@43
|
536 snd_seq_event_t onEv;
|
Chris@43
|
537 onEv.type = SND_SEQ_EVENT_NOTEON;
|
Chris@43
|
538 onEv.data.note.channel = 0;
|
Chris@43
|
539
|
Chris@43
|
540 snd_seq_event_t offEv;
|
Chris@43
|
541 offEv.type = SND_SEQ_EVENT_NOTEOFF;
|
Chris@43
|
542 offEv.data.note.channel = 0;
|
Chris@43
|
543 offEv.data.note.velocity = 0;
|
Chris@43
|
544
|
Chris@275
|
545 NoteOffSet ¬eOffs = m_noteOffs[model];
|
Chris@43
|
546
|
Chris@43
|
547 for (size_t i = 0; i < blocks; ++i) {
|
Chris@43
|
548
|
Chris@305
|
549 size_t reqStart = startFrame + i * m_processingBlockSize;
|
Chris@43
|
550
|
Chris@299
|
551 NoteList notes;
|
Chris@299
|
552 NoteExportable *exportable = dynamic_cast<NoteExportable *>(model);
|
Chris@299
|
553 if (exportable) {
|
Chris@299
|
554 notes = exportable->getNotes(reqStart + latency,
|
Chris@305
|
555 reqStart + latency + m_processingBlockSize);
|
Chris@299
|
556 }
|
Chris@43
|
557
|
Chris@43
|
558 Vamp::RealTime blockTime = Vamp::RealTime::frame2RealTime
|
Chris@305
|
559 (startFrame + i * m_processingBlockSize, 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@305
|
569 noteFrame >= reqStart + m_processingBlockSize) 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@293
|
580 cerr << "mixModel [synthetic]: sending note-off event at time " << eventTime << " frame " << noteOffs.begin()->frame << " pitch " << noteOffs.begin()->pitch << 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@293
|
594 cerr << "mixModel [synthetic]: non-pitch-quantized notes are not supported [yet], quantizing" << 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@305
|
604 cout << "mixModel [synthetic]: note at frame " << noteFrame << ", block start " << (startFrame + i * m_processingBlockSize) << ", resulting time " << eventTime << 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@305
|
613 startFrame + i * m_processingBlockSize + m_processingBlockSize) {
|
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@293
|
621 cerr << "mixModel [synthetic]: sending leftover note-off event at time " << eventTime << " frame " << noteOffs.begin()->frame << " pitch " << noteOffs.begin()->pitch << 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@305
|
633 cout << "mixModel [synthetic]: adding " << m_processingBlockSize << " samples from plugin output " << c << 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@305
|
647 for (size_t j = 0; j < m_processingBlockSize; ++j) {
|
Chris@305
|
648 buffer[c][i * m_processingBlockSize + 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 }
|