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@313
|
27 #include "data/model/SparseTimeValueModel.h"
|
Chris@43
|
28 #include "data/model/SparseOneDimensionalModel.h"
|
Chris@299
|
29 #include "data/model/NoteData.h"
|
Chris@43
|
30
|
Chris@307
|
31 #include "ClipMixer.h"
|
Chris@313
|
32 #include "ContinuousSynth.h"
|
Chris@307
|
33
|
Chris@43
|
34 #include <iostream>
|
Chris@167
|
35 #include <cmath>
|
Chris@43
|
36
|
Chris@43
|
37 #include <QDir>
|
Chris@43
|
38 #include <QFile>
|
Chris@43
|
39
|
Chris@366
|
40 const int
|
Chris@315
|
41 AudioGenerator::m_processingBlockSize = 1024;
|
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@348
|
51 m_waveType(0),
|
Chris@382
|
52 m_soloing(false),
|
Chris@382
|
53 m_channelBuffer(0),
|
Chris@382
|
54 m_channelBufSiz(0),
|
Chris@382
|
55 m_channelBufCount(0)
|
Chris@43
|
56 {
|
Chris@108
|
57 initialiseSampleDir();
|
Chris@43
|
58
|
Chris@43
|
59 connect(PlayParameterRepository::getInstance(),
|
Chris@309
|
60 SIGNAL(playClipIdChanged(const Playable *, QString)),
|
Chris@43
|
61 this,
|
Chris@309
|
62 SLOT(playClipIdChanged(const Playable *, QString)));
|
Chris@43
|
63 }
|
Chris@43
|
64
|
Chris@43
|
65 AudioGenerator::~AudioGenerator()
|
Chris@43
|
66 {
|
Chris@177
|
67 #ifdef DEBUG_AUDIO_GENERATOR
|
Chris@233
|
68 SVDEBUG << "AudioGenerator::~AudioGenerator" << endl;
|
Chris@177
|
69 #endif
|
Chris@43
|
70 }
|
Chris@43
|
71
|
Chris@108
|
72 void
|
Chris@108
|
73 AudioGenerator::initialiseSampleDir()
|
Chris@43
|
74 {
|
Chris@108
|
75 if (m_sampleDir != "") return;
|
Chris@108
|
76
|
Chris@108
|
77 try {
|
Chris@108
|
78 m_sampleDir = TempDirectory::getInstance()->getSubDirectoryPath("samples");
|
Chris@108
|
79 } catch (DirectoryCreationFailed f) {
|
Chris@293
|
80 cerr << "WARNING: AudioGenerator::initialiseSampleDir:"
|
Chris@108
|
81 << " Failed to create temporary sample directory"
|
Chris@293
|
82 << endl;
|
Chris@108
|
83 m_sampleDir = "";
|
Chris@108
|
84 return;
|
Chris@108
|
85 }
|
Chris@108
|
86
|
Chris@108
|
87 QDir sampleResourceDir(":/samples", "*.wav");
|
Chris@108
|
88
|
Chris@108
|
89 for (unsigned int i = 0; i < sampleResourceDir.count(); ++i) {
|
Chris@108
|
90
|
Chris@108
|
91 QString fileName(sampleResourceDir[i]);
|
Chris@108
|
92 QFile file(sampleResourceDir.filePath(fileName));
|
Chris@151
|
93 QString target = QDir(m_sampleDir).filePath(fileName);
|
Chris@108
|
94
|
Chris@151
|
95 if (!file.copy(target)) {
|
Chris@293
|
96 cerr << "WARNING: AudioGenerator::getSampleDir: "
|
Chris@294
|
97 << "Unable to copy " << fileName
|
Chris@108
|
98 << " into temporary directory \""
|
Chris@293
|
99 << m_sampleDir << "\"" << endl;
|
Chris@151
|
100 } else {
|
Chris@151
|
101 QFile tf(target);
|
Chris@151
|
102 tf.setPermissions(tf.permissions() |
|
Chris@151
|
103 QFile::WriteOwner |
|
Chris@151
|
104 QFile::WriteUser);
|
Chris@108
|
105 }
|
Chris@43
|
106 }
|
Chris@43
|
107 }
|
Chris@43
|
108
|
Chris@43
|
109 bool
|
Chris@43
|
110 AudioGenerator::addModel(Model *model)
|
Chris@43
|
111 {
|
Chris@43
|
112 if (m_sourceSampleRate == 0) {
|
Chris@43
|
113
|
Chris@43
|
114 m_sourceSampleRate = model->getSampleRate();
|
Chris@43
|
115
|
Chris@43
|
116 } else {
|
Chris@43
|
117
|
Chris@43
|
118 DenseTimeValueModel *dtvm =
|
Chris@43
|
119 dynamic_cast<DenseTimeValueModel *>(model);
|
Chris@43
|
120
|
Chris@43
|
121 if (dtvm) {
|
Chris@43
|
122 m_sourceSampleRate = model->getSampleRate();
|
Chris@43
|
123 return true;
|
Chris@43
|
124 }
|
Chris@43
|
125 }
|
Chris@307
|
126
|
Chris@418
|
127 const Playable *playable = model;
|
Chris@418
|
128 if (!playable || !playable->canPlay()) return 0;
|
Chris@418
|
129
|
Chris@418
|
130 PlayParameters *parameters =
|
Chris@418
|
131 PlayParameterRepository::getInstance()->getPlayParameters(playable);
|
Chris@418
|
132
|
Chris@418
|
133 bool willPlay = !parameters->isPlayMuted();
|
Chris@418
|
134
|
Chris@313
|
135 if (usesClipMixer(model)) {
|
Chris@313
|
136 ClipMixer *mixer = makeClipMixerFor(model);
|
Chris@313
|
137 if (mixer) {
|
Chris@313
|
138 QMutexLocker locker(&m_mutex);
|
Chris@313
|
139 m_clipMixerMap[model] = mixer;
|
Chris@418
|
140 return willPlay;
|
Chris@313
|
141 }
|
Chris@313
|
142 }
|
Chris@313
|
143
|
Chris@313
|
144 if (usesContinuousSynth(model)) {
|
Chris@313
|
145 ContinuousSynth *synth = makeSynthFor(model);
|
Chris@313
|
146 if (synth) {
|
Chris@313
|
147 QMutexLocker locker(&m_mutex);
|
Chris@313
|
148 m_continuousSynthMap[model] = synth;
|
Chris@418
|
149 return willPlay;
|
Chris@313
|
150 }
|
Chris@43
|
151 }
|
Chris@307
|
152
|
Chris@43
|
153 return false;
|
Chris@43
|
154 }
|
Chris@43
|
155
|
Chris@43
|
156 void
|
Chris@309
|
157 AudioGenerator::playClipIdChanged(const Playable *playable, QString)
|
Chris@43
|
158 {
|
Chris@108
|
159 const Model *model = dynamic_cast<const Model *>(playable);
|
Chris@108
|
160 if (!model) {
|
Chris@309
|
161 cerr << "WARNING: AudioGenerator::playClipIdChanged: playable "
|
Chris@108
|
162 << playable << " is not a supported model type"
|
Chris@293
|
163 << endl;
|
Chris@108
|
164 return;
|
Chris@108
|
165 }
|
Chris@108
|
166
|
Chris@307
|
167 if (m_clipMixerMap.find(model) == m_clipMixerMap.end()) return;
|
Chris@307
|
168
|
Chris@307
|
169 ClipMixer *mixer = makeClipMixerFor(model);
|
Chris@307
|
170 if (mixer) {
|
Chris@43
|
171 QMutexLocker locker(&m_mutex);
|
Chris@307
|
172 m_clipMixerMap[model] = mixer;
|
Chris@43
|
173 }
|
Chris@43
|
174 }
|
Chris@308
|
175
|
Chris@313
|
176 bool
|
Chris@313
|
177 AudioGenerator::usesClipMixer(const Model *model)
|
Chris@43
|
178 {
|
Chris@313
|
179 bool clip =
|
Chris@313
|
180 (qobject_cast<const SparseOneDimensionalModel *>(model) ||
|
Chris@313
|
181 qobject_cast<const NoteModel *>(model) ||
|
Chris@313
|
182 qobject_cast<const FlexiNoteModel *>(model));
|
Chris@313
|
183 return clip;
|
Chris@43
|
184 }
|
Chris@43
|
185
|
Chris@313
|
186 bool
|
Chris@349
|
187 AudioGenerator::wantsQuieterClips(const Model *model)
|
Chris@349
|
188 {
|
Chris@349
|
189 // basically, anything that usually has sustain (like notes) or
|
Chris@349
|
190 // often has multiple sounds at once (like notes) wants to use a
|
Chris@349
|
191 // quieter level than simple click tracks
|
Chris@349
|
192 bool does =
|
Chris@349
|
193 (qobject_cast<const NoteModel *>(model) ||
|
Chris@349
|
194 qobject_cast<const FlexiNoteModel *>(model));
|
Chris@349
|
195 return does;
|
Chris@349
|
196 }
|
Chris@349
|
197
|
Chris@349
|
198 bool
|
Chris@313
|
199 AudioGenerator::usesContinuousSynth(const Model *model)
|
Chris@43
|
200 {
|
Chris@313
|
201 bool cont =
|
Chris@313
|
202 (qobject_cast<const SparseTimeValueModel *>(model));
|
Chris@313
|
203 return cont;
|
Chris@313
|
204 }
|
Chris@313
|
205
|
Chris@307
|
206 ClipMixer *
|
Chris@307
|
207 AudioGenerator::makeClipMixerFor(const Model *model)
|
Chris@43
|
208 {
|
Chris@309
|
209 QString clipId;
|
Chris@43
|
210
|
Chris@108
|
211 const Playable *playable = model;
|
Chris@108
|
212 if (!playable || !playable->canPlay()) return 0;
|
Chris@108
|
213
|
Chris@43
|
214 PlayParameters *parameters =
|
Chris@108
|
215 PlayParameterRepository::getInstance()->getPlayParameters(playable);
|
Chris@43
|
216 if (parameters) {
|
Chris@309
|
217 clipId = parameters->getPlayClipId();
|
Chris@43
|
218 }
|
Chris@43
|
219
|
Chris@309
|
220 std::cerr << "AudioGenerator::makeClipMixerFor(" << model << "): sample id = " << clipId << std::endl;
|
Chris@276
|
221
|
Chris@309
|
222 if (clipId == "") {
|
Chris@308
|
223 SVDEBUG << "AudioGenerator::makeClipMixerFor(" << model << "): no sample, skipping" << endl;
|
Chris@276
|
224 return 0;
|
Chris@276
|
225 }
|
Chris@43
|
226
|
Chris@308
|
227 ClipMixer *mixer = new ClipMixer(m_targetChannelCount,
|
Chris@308
|
228 m_sourceSampleRate,
|
Chris@308
|
229 m_processingBlockSize);
|
Chris@307
|
230
|
Chris@308
|
231 float clipF0 = Pitch::getFrequencyForPitch(60, 0, 440.0f); // required
|
Chris@307
|
232
|
Chris@309
|
233 QString clipPath = QString("%1/%2.wav").arg(m_sampleDir).arg(clipId);
|
Chris@307
|
234
|
Chris@349
|
235 float level = wantsQuieterClips(model) ? 0.5 : 1.0;
|
Chris@349
|
236 if (!mixer->loadClipData(clipPath, clipF0, level)) {
|
Chris@308
|
237 delete mixer;
|
Chris@43
|
238 return 0;
|
Chris@43
|
239 }
|
Chris@43
|
240
|
Chris@309
|
241 std::cerr << "AudioGenerator::makeClipMixerFor(" << model << "): loaded clip " << clipId << std::endl;
|
Chris@43
|
242
|
Chris@308
|
243 return mixer;
|
Chris@308
|
244 }
|
Chris@43
|
245
|
Chris@313
|
246 ContinuousSynth *
|
Chris@313
|
247 AudioGenerator::makeSynthFor(const Model *model)
|
Chris@313
|
248 {
|
Chris@313
|
249 const Playable *playable = model;
|
Chris@313
|
250 if (!playable || !playable->canPlay()) return 0;
|
Chris@313
|
251
|
Chris@313
|
252 ContinuousSynth *synth = new ContinuousSynth(m_targetChannelCount,
|
Chris@313
|
253 m_sourceSampleRate,
|
rmb456@323
|
254 m_processingBlockSize,
|
rmb456@323
|
255 m_waveType);
|
Chris@313
|
256
|
Chris@313
|
257 std::cerr << "AudioGenerator::makeSynthFor(" << model << "): created synth" << std::endl;
|
Chris@313
|
258
|
Chris@313
|
259 return synth;
|
Chris@313
|
260 }
|
Chris@313
|
261
|
Chris@43
|
262 void
|
Chris@43
|
263 AudioGenerator::removeModel(Model *model)
|
Chris@43
|
264 {
|
Chris@43
|
265 SparseOneDimensionalModel *sodm =
|
Chris@43
|
266 dynamic_cast<SparseOneDimensionalModel *>(model);
|
Chris@43
|
267 if (!sodm) return; // nothing to do
|
Chris@43
|
268
|
Chris@43
|
269 QMutexLocker locker(&m_mutex);
|
Chris@43
|
270
|
Chris@308
|
271 if (m_clipMixerMap.find(sodm) == m_clipMixerMap.end()) return;
|
Chris@43
|
272
|
Chris@308
|
273 ClipMixer *mixer = m_clipMixerMap[sodm];
|
Chris@308
|
274 m_clipMixerMap.erase(sodm);
|
Chris@308
|
275 delete mixer;
|
Chris@43
|
276 }
|
Chris@43
|
277
|
Chris@43
|
278 void
|
Chris@43
|
279 AudioGenerator::clearModels()
|
Chris@43
|
280 {
|
Chris@43
|
281 QMutexLocker locker(&m_mutex);
|
Chris@308
|
282
|
Chris@308
|
283 while (!m_clipMixerMap.empty()) {
|
Chris@308
|
284 ClipMixer *mixer = m_clipMixerMap.begin()->second;
|
Chris@308
|
285 m_clipMixerMap.erase(m_clipMixerMap.begin());
|
Chris@308
|
286 delete mixer;
|
Chris@43
|
287 }
|
Chris@43
|
288 }
|
Chris@43
|
289
|
Chris@43
|
290 void
|
Chris@43
|
291 AudioGenerator::reset()
|
Chris@43
|
292 {
|
Chris@43
|
293 QMutexLocker locker(&m_mutex);
|
Chris@308
|
294
|
Chris@397
|
295 cerr << "AudioGenerator::reset()" << endl;
|
Chris@397
|
296
|
Chris@308
|
297 for (ClipMixerMap::iterator i = m_clipMixerMap.begin(); i != m_clipMixerMap.end(); ++i) {
|
Chris@43
|
298 if (i->second) {
|
Chris@308
|
299 i->second->reset();
|
Chris@43
|
300 }
|
Chris@43
|
301 }
|
Chris@43
|
302
|
Chris@43
|
303 m_noteOffs.clear();
|
Chris@43
|
304 }
|
Chris@43
|
305
|
Chris@43
|
306 void
|
Chris@366
|
307 AudioGenerator::setTargetChannelCount(int targetChannelCount)
|
Chris@43
|
308 {
|
Chris@43
|
309 if (m_targetChannelCount == targetChannelCount) return;
|
Chris@43
|
310
|
Chris@233
|
311 // SVDEBUG << "AudioGenerator::setTargetChannelCount(" << targetChannelCount << ")" << endl;
|
Chris@43
|
312
|
Chris@43
|
313 QMutexLocker locker(&m_mutex);
|
Chris@43
|
314 m_targetChannelCount = targetChannelCount;
|
Chris@43
|
315
|
Chris@308
|
316 for (ClipMixerMap::iterator i = m_clipMixerMap.begin(); i != m_clipMixerMap.end(); ++i) {
|
Chris@308
|
317 if (i->second) i->second->setChannelCount(targetChannelCount);
|
Chris@43
|
318 }
|
Chris@43
|
319 }
|
Chris@43
|
320
|
Chris@366
|
321 int
|
Chris@43
|
322 AudioGenerator::getBlockSize() const
|
Chris@43
|
323 {
|
Chris@305
|
324 return m_processingBlockSize;
|
Chris@43
|
325 }
|
Chris@43
|
326
|
Chris@43
|
327 void
|
Chris@43
|
328 AudioGenerator::setSoloModelSet(std::set<Model *> s)
|
Chris@43
|
329 {
|
Chris@43
|
330 QMutexLocker locker(&m_mutex);
|
Chris@43
|
331
|
Chris@43
|
332 m_soloModelSet = s;
|
Chris@43
|
333 m_soloing = true;
|
Chris@43
|
334 }
|
Chris@43
|
335
|
Chris@43
|
336 void
|
Chris@43
|
337 AudioGenerator::clearSoloModelSet()
|
Chris@43
|
338 {
|
Chris@43
|
339 QMutexLocker locker(&m_mutex);
|
Chris@43
|
340
|
Chris@43
|
341 m_soloModelSet.clear();
|
Chris@43
|
342 m_soloing = false;
|
Chris@43
|
343 }
|
Chris@43
|
344
|
Chris@366
|
345 int
|
Chris@366
|
346 AudioGenerator::mixModel(Model *model, int startFrame, int frameCount,
|
Chris@366
|
347 float **buffer, int fadeIn, int fadeOut)
|
Chris@43
|
348 {
|
Chris@43
|
349 if (m_sourceSampleRate == 0) {
|
Chris@293
|
350 cerr << "WARNING: AudioGenerator::mixModel: No base source sample rate available" << endl;
|
Chris@43
|
351 return frameCount;
|
Chris@43
|
352 }
|
Chris@43
|
353
|
Chris@43
|
354 QMutexLocker locker(&m_mutex);
|
Chris@43
|
355
|
Chris@108
|
356 Playable *playable = model;
|
Chris@108
|
357 if (!playable || !playable->canPlay()) return frameCount;
|
Chris@108
|
358
|
Chris@43
|
359 PlayParameters *parameters =
|
Chris@108
|
360 PlayParameterRepository::getInstance()->getPlayParameters(playable);
|
Chris@43
|
361 if (!parameters) return frameCount;
|
Chris@43
|
362
|
Chris@43
|
363 bool playing = !parameters->isPlayMuted();
|
Chris@43
|
364 if (!playing) {
|
Chris@43
|
365 #ifdef DEBUG_AUDIO_GENERATOR
|
Chris@293
|
366 cout << "AudioGenerator::mixModel(" << model << "): muted" << endl;
|
Chris@43
|
367 #endif
|
Chris@43
|
368 return frameCount;
|
Chris@43
|
369 }
|
Chris@43
|
370
|
Chris@43
|
371 if (m_soloing) {
|
Chris@43
|
372 if (m_soloModelSet.find(model) == m_soloModelSet.end()) {
|
Chris@43
|
373 #ifdef DEBUG_AUDIO_GENERATOR
|
Chris@293
|
374 cout << "AudioGenerator::mixModel(" << model << "): not one of the solo'd models" << endl;
|
Chris@43
|
375 #endif
|
Chris@43
|
376 return frameCount;
|
Chris@43
|
377 }
|
Chris@43
|
378 }
|
Chris@43
|
379
|
Chris@43
|
380 float gain = parameters->getPlayGain();
|
Chris@43
|
381 float pan = parameters->getPlayPan();
|
Chris@43
|
382
|
Chris@43
|
383 DenseTimeValueModel *dtvm = dynamic_cast<DenseTimeValueModel *>(model);
|
Chris@43
|
384 if (dtvm) {
|
Chris@43
|
385 return mixDenseTimeValueModel(dtvm, startFrame, frameCount,
|
Chris@43
|
386 buffer, gain, pan, fadeIn, fadeOut);
|
Chris@43
|
387 }
|
Chris@43
|
388
|
Chris@313
|
389 if (usesClipMixer(model)) {
|
Chris@313
|
390 return mixClipModel(model, startFrame, frameCount,
|
Chris@313
|
391 buffer, gain, pan);
|
Chris@313
|
392 }
|
Chris@43
|
393
|
Chris@313
|
394 if (usesContinuousSynth(model)) {
|
Chris@313
|
395 return mixContinuousSynthModel(model, startFrame, frameCount,
|
Chris@313
|
396 buffer, gain, pan);
|
Chris@43
|
397 }
|
Chris@43
|
398
|
Chris@276
|
399 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
|
400
|
Chris@43
|
401 return frameCount;
|
Chris@43
|
402 }
|
Chris@43
|
403
|
Chris@366
|
404 int
|
Chris@43
|
405 AudioGenerator::mixDenseTimeValueModel(DenseTimeValueModel *dtvm,
|
Chris@366
|
406 int startFrame, int frames,
|
Chris@43
|
407 float **buffer, float gain, float pan,
|
Chris@366
|
408 int fadeIn, int fadeOut)
|
Chris@43
|
409 {
|
Chris@382
|
410 int maxFrames = frames + std::max(fadeIn, fadeOut);
|
Chris@43
|
411
|
Chris@366
|
412 int modelChannels = dtvm->getChannelCount();
|
Chris@80
|
413
|
Chris@382
|
414 if (m_channelBufSiz < maxFrames || m_channelBufCount < modelChannels) {
|
Chris@80
|
415
|
Chris@382
|
416 for (int c = 0; c < m_channelBufCount; ++c) {
|
Chris@382
|
417 delete[] m_channelBuffer[c];
|
Chris@80
|
418 }
|
Chris@80
|
419
|
Chris@382
|
420 delete[] m_channelBuffer;
|
Chris@382
|
421 m_channelBuffer = new float *[modelChannels];
|
Chris@80
|
422
|
Chris@366
|
423 for (int c = 0; c < modelChannels; ++c) {
|
Chris@382
|
424 m_channelBuffer[c] = new float[maxFrames];
|
Chris@80
|
425 }
|
Chris@80
|
426
|
Chris@382
|
427 m_channelBufCount = modelChannels;
|
Chris@382
|
428 m_channelBufSiz = maxFrames;
|
Chris@43
|
429 }
|
Chris@80
|
430
|
Chris@366
|
431 int got = 0;
|
Chris@80
|
432
|
Chris@80
|
433 if (startFrame >= fadeIn/2) {
|
Chris@80
|
434 got = dtvm->getData(0, modelChannels - 1,
|
Chris@80
|
435 startFrame - fadeIn/2,
|
Chris@80
|
436 frames + fadeOut/2 + fadeIn/2,
|
Chris@382
|
437 m_channelBuffer);
|
Chris@80
|
438 } else {
|
Chris@366
|
439 int missing = fadeIn/2 - startFrame;
|
Chris@80
|
440
|
Chris@366
|
441 for (int c = 0; c < modelChannels; ++c) {
|
Chris@382
|
442 m_channelBuffer[c] += missing;
|
Chris@382
|
443 }
|
Chris@382
|
444
|
Chris@382
|
445 if (missing > 0) {
|
Chris@382
|
446 cerr << "note: channelBufSiz = " << m_channelBufSiz
|
Chris@382
|
447 << ", frames + fadeOut/2 = " << frames + fadeOut/2
|
Chris@382
|
448 << ", startFrame = " << startFrame
|
Chris@382
|
449 << ", missing = " << missing << endl;
|
Chris@80
|
450 }
|
Chris@80
|
451
|
Chris@80
|
452 got = dtvm->getData(0, modelChannels - 1,
|
Chris@80
|
453 startFrame,
|
Chris@80
|
454 frames + fadeOut/2,
|
Chris@382
|
455 m_channelBuffer);
|
Chris@80
|
456
|
Chris@366
|
457 for (int c = 0; c < modelChannels; ++c) {
|
Chris@382
|
458 m_channelBuffer[c] -= missing;
|
Chris@80
|
459 }
|
Chris@80
|
460
|
Chris@80
|
461 got += missing;
|
Chris@80
|
462 }
|
Chris@43
|
463
|
Chris@366
|
464 for (int c = 0; c < m_targetChannelCount; ++c) {
|
Chris@43
|
465
|
Chris@366
|
466 int sourceChannel = (c % modelChannels);
|
Chris@43
|
467
|
Chris@233
|
468 // SVDEBUG << "mixing channel " << c << " from source channel " << sourceChannel << endl;
|
Chris@43
|
469
|
Chris@43
|
470 float channelGain = gain;
|
Chris@43
|
471 if (pan != 0.0) {
|
Chris@43
|
472 if (c == 0) {
|
Chris@43
|
473 if (pan > 0.0) channelGain *= 1.0 - pan;
|
Chris@43
|
474 } else {
|
Chris@43
|
475 if (pan < 0.0) channelGain *= pan + 1.0;
|
Chris@43
|
476 }
|
Chris@43
|
477 }
|
Chris@43
|
478
|
Chris@366
|
479 for (int i = 0; i < fadeIn/2; ++i) {
|
Chris@43
|
480 float *back = buffer[c];
|
Chris@43
|
481 back -= fadeIn/2;
|
Chris@382
|
482 back[i] += (channelGain * m_channelBuffer[sourceChannel][i] * i) / fadeIn;
|
Chris@43
|
483 }
|
Chris@43
|
484
|
Chris@366
|
485 for (int i = 0; i < frames + fadeOut/2; ++i) {
|
Chris@43
|
486 float mult = channelGain;
|
Chris@43
|
487 if (i < fadeIn/2) {
|
Chris@43
|
488 mult = (mult * i) / fadeIn;
|
Chris@43
|
489 }
|
Chris@43
|
490 if (i > frames - fadeOut/2) {
|
Chris@43
|
491 mult = (mult * ((frames + fadeOut/2) - i)) / fadeOut;
|
Chris@43
|
492 }
|
Chris@382
|
493 float val = m_channelBuffer[sourceChannel][i];
|
Chris@80
|
494 if (i >= got) val = 0.f;
|
Chris@80
|
495 buffer[c][i] += mult * val;
|
Chris@43
|
496 }
|
Chris@43
|
497 }
|
Chris@43
|
498
|
Chris@43
|
499 return got;
|
Chris@43
|
500 }
|
Chris@43
|
501
|
Chris@366
|
502 int
|
Chris@313
|
503 AudioGenerator::mixClipModel(Model *model,
|
Chris@366
|
504 int startFrame, int frames,
|
Chris@313
|
505 float **buffer, float gain, float pan)
|
Chris@43
|
506 {
|
Chris@308
|
507 ClipMixer *clipMixer = m_clipMixerMap[model];
|
Chris@308
|
508 if (!clipMixer) return 0;
|
Chris@43
|
509
|
Chris@366
|
510 int blocks = frames / m_processingBlockSize;
|
Chris@43
|
511
|
Chris@313
|
512 //!!! todo: the below -- it matters
|
Chris@313
|
513
|
Chris@43
|
514 //!!! hang on -- the fact that the audio callback play source's
|
Chris@43
|
515 //buffer is a multiple of the plugin's buffer size doesn't mean
|
Chris@43
|
516 //that we always get called for a multiple of it here (because it
|
Chris@43
|
517 //also depends on the JACK block size). how should we ensure that
|
Chris@43
|
518 //all models write the same amount in to the mix, and that we
|
Chris@43
|
519 //always have a multiple of the plugin buffer size? I guess this
|
Chris@43
|
520 //class has to be queryable for the plugin buffer size & the
|
Chris@43
|
521 //callback play source has to use that as a multiple for all the
|
Chris@43
|
522 //calls to mixModel
|
Chris@43
|
523
|
Chris@366
|
524 int got = blocks * m_processingBlockSize;
|
Chris@43
|
525
|
Chris@43
|
526 #ifdef DEBUG_AUDIO_GENERATOR
|
Chris@313
|
527 cout << "mixModel [clip]: frames " << frames
|
Chris@293
|
528 << ", blocks " << blocks << endl;
|
Chris@43
|
529 #endif
|
Chris@43
|
530
|
Chris@308
|
531 ClipMixer::NoteStart on;
|
Chris@308
|
532 ClipMixer::NoteEnd off;
|
Chris@43
|
533
|
Chris@275
|
534 NoteOffSet ¬eOffs = m_noteOffs[model];
|
Chris@43
|
535
|
Chris@308
|
536 float **bufferIndexes = new float *[m_targetChannelCount];
|
Chris@308
|
537
|
Chris@366
|
538 for (int i = 0; i < blocks; ++i) {
|
Chris@43
|
539
|
Chris@366
|
540 int reqStart = startFrame + i * m_processingBlockSize;
|
Chris@43
|
541
|
Chris@299
|
542 NoteList notes;
|
Chris@299
|
543 NoteExportable *exportable = dynamic_cast<NoteExportable *>(model);
|
Chris@299
|
544 if (exportable) {
|
Chris@366
|
545 notes = exportable->getNotesWithin(reqStart,
|
Chris@366
|
546 reqStart + m_processingBlockSize);
|
Chris@299
|
547 }
|
Chris@43
|
548
|
Chris@308
|
549 std::vector<ClipMixer::NoteStart> starts;
|
Chris@308
|
550 std::vector<ClipMixer::NoteEnd> ends;
|
Chris@43
|
551
|
Chris@275
|
552 for (NoteList::const_iterator ni = notes.begin();
|
Chris@275
|
553 ni != notes.end(); ++ni) {
|
Chris@43
|
554
|
Chris@366
|
555 int noteFrame = ni->start;
|
Chris@43
|
556
|
Chris@275
|
557 if (noteFrame < reqStart ||
|
Chris@305
|
558 noteFrame >= reqStart + m_processingBlockSize) continue;
|
Chris@43
|
559
|
Chris@43
|
560 while (noteOffs.begin() != noteOffs.end() &&
|
Chris@275
|
561 noteOffs.begin()->frame <= noteFrame) {
|
Chris@43
|
562
|
Chris@366
|
563 int eventFrame = noteOffs.begin()->frame;
|
Chris@308
|
564 if (eventFrame < reqStart) eventFrame = reqStart;
|
Chris@43
|
565
|
Chris@308
|
566 off.frameOffset = eventFrame - reqStart;
|
Chris@308
|
567 off.frequency = noteOffs.begin()->frequency;
|
Chris@43
|
568
|
Chris@43
|
569 #ifdef DEBUG_AUDIO_GENERATOR
|
Chris@313
|
570 cerr << "mixModel [clip]: adding note-off at frame " << eventFrame << " frame offset " << off.frameOffset << " frequency " << off.frequency << endl;
|
Chris@43
|
571 #endif
|
Chris@43
|
572
|
Chris@308
|
573 ends.push_back(off);
|
Chris@43
|
574 noteOffs.erase(noteOffs.begin());
|
Chris@43
|
575 }
|
Chris@43
|
576
|
Chris@308
|
577 on.frameOffset = noteFrame - reqStart;
|
Chris@308
|
578 on.frequency = ni->getFrequency();
|
Chris@308
|
579 on.level = float(ni->velocity) / 127.0;
|
Chris@308
|
580 on.pan = pan;
|
Chris@43
|
581
|
Chris@43
|
582 #ifdef DEBUG_AUDIO_GENERATOR
|
Chris@346
|
583 cout << "mixModel [clip]: adding note at frame " << noteFrame << ", frame offset " << on.frameOffset << " frequency " << on.frequency << ", level " << on.level << endl;
|
Chris@43
|
584 #endif
|
Chris@43
|
585
|
Chris@308
|
586 starts.push_back(on);
|
Chris@275
|
587 noteOffs.insert
|
Chris@308
|
588 (NoteOff(on.frequency, noteFrame + ni->duration));
|
Chris@43
|
589 }
|
Chris@43
|
590
|
Chris@43
|
591 while (noteOffs.begin() != noteOffs.end() &&
|
Chris@308
|
592 noteOffs.begin()->frame <= reqStart + m_processingBlockSize) {
|
Chris@43
|
593
|
Chris@366
|
594 int eventFrame = noteOffs.begin()->frame;
|
Chris@308
|
595 if (eventFrame < reqStart) eventFrame = reqStart;
|
Chris@43
|
596
|
Chris@308
|
597 off.frameOffset = eventFrame - reqStart;
|
Chris@308
|
598 off.frequency = noteOffs.begin()->frequency;
|
Chris@43
|
599
|
Chris@43
|
600 #ifdef DEBUG_AUDIO_GENERATOR
|
Chris@313
|
601 cerr << "mixModel [clip]: adding leftover note-off at frame " << eventFrame << " frame offset " << off.frameOffset << " frequency " << off.frequency << endl;
|
Chris@43
|
602 #endif
|
Chris@43
|
603
|
Chris@308
|
604 ends.push_back(off);
|
Chris@308
|
605 noteOffs.erase(noteOffs.begin());
|
Chris@43
|
606 }
|
Chris@43
|
607
|
Chris@366
|
608 for (int c = 0; c < m_targetChannelCount; ++c) {
|
Chris@308
|
609 bufferIndexes[c] = buffer[c] + i * m_processingBlockSize;
|
Chris@308
|
610 }
|
Chris@43
|
611
|
Chris@308
|
612 clipMixer->mix(bufferIndexes, gain, starts, ends);
|
Chris@308
|
613 }
|
Chris@43
|
614
|
Chris@308
|
615 delete[] bufferIndexes;
|
Chris@43
|
616
|
Chris@43
|
617 return got;
|
Chris@43
|
618 }
|
Chris@313
|
619
|
Chris@366
|
620 int
|
Chris@313
|
621 AudioGenerator::mixContinuousSynthModel(Model *model,
|
Chris@366
|
622 int startFrame,
|
Chris@366
|
623 int frames,
|
Chris@313
|
624 float **buffer,
|
Chris@313
|
625 float gain,
|
Chris@313
|
626 float pan)
|
Chris@313
|
627 {
|
Chris@313
|
628 ContinuousSynth *synth = m_continuousSynthMap[model];
|
Chris@313
|
629 if (!synth) return 0;
|
Chris@313
|
630
|
Chris@313
|
631 // only type we support here at the moment
|
Chris@313
|
632 SparseTimeValueModel *stvm = qobject_cast<SparseTimeValueModel *>(model);
|
Chris@313
|
633 if (stvm->getScaleUnits() != "Hz") return 0;
|
Chris@313
|
634
|
Chris@366
|
635 int blocks = frames / m_processingBlockSize;
|
Chris@313
|
636
|
Chris@313
|
637 //!!! todo: see comment in mixClipModel
|
Chris@313
|
638
|
Chris@366
|
639 int got = blocks * m_processingBlockSize;
|
Chris@313
|
640
|
Chris@313
|
641 #ifdef DEBUG_AUDIO_GENERATOR
|
Chris@313
|
642 cout << "mixModel [synth]: frames " << frames
|
Chris@313
|
643 << ", blocks " << blocks << endl;
|
Chris@313
|
644 #endif
|
Chris@313
|
645
|
Chris@313
|
646 float **bufferIndexes = new float *[m_targetChannelCount];
|
Chris@313
|
647
|
Chris@366
|
648 for (int i = 0; i < blocks; ++i) {
|
Chris@313
|
649
|
Chris@366
|
650 int reqStart = startFrame + i * m_processingBlockSize;
|
Chris@313
|
651
|
Chris@366
|
652 for (int c = 0; c < m_targetChannelCount; ++c) {
|
Chris@313
|
653 bufferIndexes[c] = buffer[c] + i * m_processingBlockSize;
|
Chris@313
|
654 }
|
Chris@313
|
655
|
Chris@313
|
656 SparseTimeValueModel::PointList points =
|
Chris@313
|
657 stvm->getPoints(reqStart, reqStart + m_processingBlockSize);
|
Chris@313
|
658
|
Chris@313
|
659 // by default, repeat last frequency
|
Chris@313
|
660 float f0 = 0.f;
|
Chris@313
|
661
|
Chris@313
|
662 // go straight to the last freq that is genuinely in this range
|
Chris@313
|
663 for (SparseTimeValueModel::PointList::const_iterator itr = points.end();
|
Chris@313
|
664 itr != points.begin(); ) {
|
Chris@313
|
665 --itr;
|
Chris@313
|
666 if (itr->frame >= reqStart &&
|
Chris@313
|
667 itr->frame < reqStart + m_processingBlockSize) {
|
Chris@313
|
668 f0 = itr->value;
|
Chris@313
|
669 break;
|
Chris@313
|
670 }
|
Chris@313
|
671 }
|
Chris@313
|
672
|
Chris@314
|
673 // if we found no such frequency and the next point is further
|
Chris@314
|
674 // away than twice the model resolution, go silent (same
|
Chris@314
|
675 // criterion TimeValueLayer uses for ending a discrete curve
|
Chris@314
|
676 // segment)
|
Chris@314
|
677 if (f0 == 0.f) {
|
Chris@314
|
678 SparseTimeValueModel::PointList nextPoints =
|
Chris@314
|
679 stvm->getNextPoints(reqStart + m_processingBlockSize);
|
Chris@314
|
680 if (nextPoints.empty() ||
|
Chris@314
|
681 nextPoints.begin()->frame > reqStart + 2 * stvm->getResolution()) {
|
Chris@314
|
682 f0 = -1.f;
|
Chris@314
|
683 }
|
Chris@314
|
684 }
|
Chris@314
|
685
|
Chris@315
|
686 // cerr << "f0 = " << f0 << endl;
|
Chris@313
|
687
|
Chris@313
|
688 synth->mix(bufferIndexes,
|
Chris@313
|
689 gain,
|
Chris@313
|
690 pan,
|
Chris@313
|
691 f0);
|
Chris@313
|
692 }
|
Chris@313
|
693
|
Chris@313
|
694 delete[] bufferIndexes;
|
Chris@313
|
695
|
Chris@313
|
696 return got;
|
Chris@313
|
697 }
|
Chris@313
|
698
|