Chris@0
|
1 /* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 A waveform viewer and audio annotation editor.
|
Chris@2
|
5 Chris Cannam, Queen Mary University of London, 2005-2006
|
Chris@0
|
6
|
Chris@0
|
7 This is experimental software. Not for distribution.
|
Chris@0
|
8 */
|
Chris@0
|
9
|
Chris@0
|
10 #include "AudioGenerator.h"
|
Chris@0
|
11
|
Chris@0
|
12 #include "base/ViewManager.h"
|
Chris@0
|
13 #include "base/PlayParameters.h"
|
Chris@11
|
14 #include "base/PlayParameterRepository.h"
|
Chris@0
|
15
|
Chris@11
|
16 #include "model/NoteModel.h"
|
Chris@0
|
17 #include "model/DenseTimeValueModel.h"
|
Chris@0
|
18 #include "model/SparseOneDimensionalModel.h"
|
Chris@0
|
19
|
Chris@0
|
20 #include "plugin/RealTimePluginFactory.h"
|
Chris@0
|
21 #include "plugin/RealTimePluginInstance.h"
|
Chris@0
|
22 #include "plugin/PluginIdentifier.h"
|
Chris@0
|
23 #include "plugin/api/alsa/seq_event.h"
|
Chris@0
|
24
|
Chris@0
|
25 #include <iostream>
|
Martin@14
|
26 #include <math.h>
|
Chris@0
|
27
|
Chris@0
|
28 const size_t
|
Chris@0
|
29 AudioGenerator::m_pluginBlockSize = 2048;
|
Chris@0
|
30
|
Chris@5
|
31 //#define DEBUG_AUDIO_GENERATOR 1
|
Chris@0
|
32
|
Chris@0
|
33 AudioGenerator::AudioGenerator(ViewManager *manager) :
|
Chris@0
|
34 m_viewManager(manager),
|
Chris@0
|
35 m_sourceSampleRate(0),
|
Chris@0
|
36 m_targetChannelCount(1)
|
Chris@0
|
37 {
|
Chris@0
|
38 }
|
Chris@0
|
39
|
Chris@0
|
40 AudioGenerator::~AudioGenerator()
|
Chris@0
|
41 {
|
Chris@0
|
42 }
|
Chris@0
|
43
|
Chris@10
|
44 bool
|
Chris@13
|
45 AudioGenerator::canPlay(const Model *model)
|
Chris@13
|
46 {
|
Chris@13
|
47 if (dynamic_cast<const DenseTimeValueModel *>(model) ||
|
Chris@13
|
48 dynamic_cast<const SparseOneDimensionalModel *>(model) ||
|
Chris@13
|
49 dynamic_cast<const NoteModel *>(model)) {
|
Chris@13
|
50 return true;
|
Chris@13
|
51 } else {
|
Chris@13
|
52 return false;
|
Chris@13
|
53 }
|
Chris@13
|
54 }
|
Chris@13
|
55
|
Chris@13
|
56 bool
|
Chris@0
|
57 AudioGenerator::addModel(Model *model)
|
Chris@0
|
58 {
|
Chris@0
|
59 if (m_sourceSampleRate == 0) {
|
Chris@0
|
60
|
Chris@0
|
61 m_sourceSampleRate = model->getSampleRate();
|
Chris@0
|
62
|
Chris@0
|
63 } else {
|
Chris@0
|
64
|
Chris@0
|
65 DenseTimeValueModel *dtvm =
|
Chris@0
|
66 dynamic_cast<DenseTimeValueModel *>(model);
|
Chris@0
|
67
|
Chris@0
|
68 if (dtvm) {
|
Chris@0
|
69 m_sourceSampleRate = model->getSampleRate();
|
Chris@10
|
70 return true;
|
Chris@0
|
71 }
|
Chris@0
|
72 }
|
Chris@0
|
73
|
Chris@0
|
74 SparseOneDimensionalModel *sodm =
|
Chris@0
|
75 dynamic_cast<SparseOneDimensionalModel *>(model);
|
Chris@11
|
76 if (sodm) {
|
Chris@11
|
77 QString pluginId = QString("dssi:%1:sample_player").
|
Chris@11
|
78 arg(PluginIdentifier::BUILTIN_PLUGIN_SONAME);
|
Chris@11
|
79 RealTimePluginInstance *plugin = loadPlugin(pluginId, "cowbell");
|
Chris@11
|
80 if (plugin) {
|
Chris@11
|
81 QMutexLocker locker(&m_mutex);
|
Chris@11
|
82 m_synthMap[sodm] = plugin;
|
Chris@11
|
83 return true;
|
Chris@11
|
84 } else {
|
Chris@11
|
85 return false;
|
Chris@11
|
86 }
|
Chris@11
|
87 }
|
Chris@0
|
88
|
Chris@11
|
89 NoteModel *nm = dynamic_cast<NoteModel *>(model);
|
Chris@11
|
90 if (nm) {
|
Chris@11
|
91 QString pluginId = QString("dssi:%1:sample_player").
|
Chris@11
|
92 arg(PluginIdentifier::BUILTIN_PLUGIN_SONAME);
|
Chris@11
|
93 RealTimePluginInstance *plugin = loadPlugin(pluginId, "piano");
|
Chris@11
|
94 if (plugin) {
|
Chris@11
|
95 QMutexLocker locker(&m_mutex);
|
Chris@11
|
96 m_synthMap[nm] = plugin;
|
Chris@11
|
97 return true;
|
Chris@11
|
98 } else {
|
Chris@11
|
99 return false;
|
Chris@11
|
100 }
|
Chris@11
|
101 }
|
Chris@11
|
102
|
Chris@11
|
103
|
Chris@11
|
104 return false;
|
Chris@11
|
105 }
|
Chris@11
|
106
|
Chris@11
|
107 RealTimePluginInstance *
|
Chris@11
|
108 AudioGenerator::loadPlugin(QString pluginId, QString program)
|
Chris@11
|
109 {
|
Chris@0
|
110 // QString pluginId = "dssi:/usr/lib/dssi/dssi-vst.so:FEARkILLERrev1.dll";
|
Chris@0
|
111 // QString pluginId = "dssi:/usr/lib/dssi/hexter.so:hexter";
|
Chris@0
|
112 // QString pluginId = "dssi:/usr/lib/dssi/sineshaper.so:sineshaper";
|
Chris@0
|
113 // QString pluginId = "dssi:/usr/local/lib/dssi/xsynth-dssi.so:Xsynth";
|
Chris@0
|
114 // QString pluginId = "dssi:/usr/local/lib/dssi/trivial_synth.so:TS";
|
Chris@0
|
115 RealTimePluginFactory *factory =
|
Chris@0
|
116 RealTimePluginFactory::instanceFor(pluginId);
|
Chris@0
|
117
|
Chris@0
|
118 if (!factory) {
|
Chris@0
|
119 std::cerr << "Failed to get plugin factory" << std::endl;
|
Chris@10
|
120 return false;
|
Chris@0
|
121 }
|
Chris@0
|
122
|
Chris@0
|
123 RealTimePluginInstance *instance =
|
Chris@0
|
124 factory->instantiatePlugin
|
Chris@0
|
125 (pluginId, 0, 0, m_sourceSampleRate, m_pluginBlockSize, m_targetChannelCount);
|
Chris@0
|
126
|
Chris@0
|
127 if (instance) {
|
Chris@0
|
128 for (unsigned int i = 0; i < instance->getParameterCount(); ++i) {
|
Chris@0
|
129 instance->setParameterValue(i, instance->getParameterDefault(i));
|
Chris@0
|
130 }
|
Chris@11
|
131 QString defaultProgram = instance->getProgram(0, 0);
|
Chris@11
|
132 if (defaultProgram != "") {
|
Chris@11
|
133 std::cerr << "first selecting default program " << defaultProgram.toLocal8Bit().data() << std::endl;
|
Chris@11
|
134 instance->selectProgram(defaultProgram);
|
Chris@11
|
135 }
|
Chris@0
|
136 if (program != "") {
|
Chris@11
|
137 std::cerr << "now selecting desired program " << program.toLocal8Bit().data() << std::endl;
|
Chris@0
|
138 instance->selectProgram(program);
|
Chris@0
|
139 }
|
Chris@0
|
140 instance->setIdealChannelCount(m_targetChannelCount); // reset!
|
Chris@0
|
141 } else {
|
Chris@0
|
142 std::cerr << "Failed to instantiate plugin" << std::endl;
|
Chris@0
|
143 }
|
Chris@10
|
144
|
Chris@11
|
145 return instance;
|
Chris@0
|
146 }
|
Chris@0
|
147
|
Chris@0
|
148 void
|
Chris@0
|
149 AudioGenerator::removeModel(Model *model)
|
Chris@0
|
150 {
|
Chris@0
|
151 SparseOneDimensionalModel *sodm =
|
Chris@0
|
152 dynamic_cast<SparseOneDimensionalModel *>(model);
|
Chris@0
|
153 if (!sodm) return; // nothing to do
|
Chris@0
|
154
|
Chris@8
|
155 QMutexLocker locker(&m_mutex);
|
Chris@8
|
156
|
Chris@0
|
157 if (m_synthMap.find(sodm) == m_synthMap.end()) return;
|
Chris@0
|
158
|
Chris@0
|
159 RealTimePluginInstance *instance = m_synthMap[sodm];
|
Chris@0
|
160 m_synthMap.erase(sodm);
|
Chris@0
|
161 delete instance;
|
Chris@0
|
162 }
|
Chris@0
|
163
|
Chris@0
|
164 void
|
Chris@0
|
165 AudioGenerator::clearModels()
|
Chris@0
|
166 {
|
Chris@8
|
167 QMutexLocker locker(&m_mutex);
|
Chris@0
|
168 while (!m_synthMap.empty()) {
|
Chris@0
|
169 RealTimePluginInstance *instance = m_synthMap.begin()->second;
|
Chris@0
|
170 m_synthMap.erase(m_synthMap.begin());
|
Chris@0
|
171 delete instance;
|
Chris@0
|
172 }
|
Chris@0
|
173 }
|
Chris@0
|
174
|
Chris@0
|
175 void
|
Chris@0
|
176 AudioGenerator::reset()
|
Chris@0
|
177 {
|
Chris@8
|
178 QMutexLocker locker(&m_mutex);
|
Chris@0
|
179 for (PluginMap::iterator i = m_synthMap.begin(); i != m_synthMap.end(); ++i) {
|
Chris@0
|
180 if (i->second) {
|
Chris@0
|
181 i->second->silence();
|
Chris@0
|
182 i->second->discardEvents();
|
Chris@0
|
183 }
|
Chris@0
|
184 }
|
Chris@0
|
185
|
Chris@0
|
186 m_noteOffs.clear();
|
Chris@0
|
187 }
|
Chris@0
|
188
|
Chris@0
|
189 void
|
Chris@0
|
190 AudioGenerator::setTargetChannelCount(size_t targetChannelCount)
|
Chris@0
|
191 {
|
Chris@13
|
192 if (m_targetChannelCount == targetChannelCount) return;
|
Chris@13
|
193
|
Chris@13
|
194 std::cerr << "AudioGenerator::setTargetChannelCount(" << targetChannelCount << ")" << std::endl;
|
Chris@13
|
195
|
Chris@8
|
196 QMutexLocker locker(&m_mutex);
|
Chris@0
|
197 m_targetChannelCount = targetChannelCount;
|
Chris@0
|
198
|
Chris@0
|
199 for (PluginMap::iterator i = m_synthMap.begin(); i != m_synthMap.end(); ++i) {
|
Chris@0
|
200 if (i->second) i->second->setIdealChannelCount(targetChannelCount);
|
Chris@0
|
201 }
|
Chris@0
|
202 }
|
Chris@0
|
203
|
Chris@0
|
204 size_t
|
Chris@0
|
205 AudioGenerator::getBlockSize() const
|
Chris@0
|
206 {
|
Chris@0
|
207 return m_pluginBlockSize;
|
Chris@0
|
208 }
|
Chris@0
|
209
|
Chris@0
|
210 size_t
|
Chris@0
|
211 AudioGenerator::mixModel(Model *model, size_t startFrame, size_t frameCount,
|
Chris@4
|
212 float **buffer, size_t fadeIn, size_t fadeOut)
|
Chris@0
|
213 {
|
Chris@0
|
214 if (m_sourceSampleRate == 0) {
|
Chris@0
|
215 std::cerr << "WARNING: AudioGenerator::mixModel: No base source sample rate available" << std::endl;
|
Chris@0
|
216 return frameCount;
|
Chris@0
|
217 }
|
Chris@0
|
218
|
Chris@8
|
219 QMutexLocker locker(&m_mutex);
|
Chris@8
|
220
|
Chris@11
|
221 PlayParameters *parameters =
|
Chris@11
|
222 PlayParameterRepository::instance()->getPlayParameters(model);
|
Chris@0
|
223 if (!parameters) return frameCount;
|
Chris@0
|
224
|
Chris@0
|
225 bool playing = !parameters->isPlayMuted();
|
Chris@0
|
226 if (!playing) return frameCount;
|
Chris@0
|
227
|
Chris@0
|
228 float gain = parameters->getPlayGain();
|
Chris@0
|
229 float pan = parameters->getPlayPan();
|
Chris@0
|
230
|
Chris@0
|
231 DenseTimeValueModel *dtvm = dynamic_cast<DenseTimeValueModel *>(model);
|
Chris@0
|
232 if (dtvm) {
|
Chris@0
|
233 return mixDenseTimeValueModel(dtvm, startFrame, frameCount,
|
Chris@4
|
234 buffer, gain, pan, fadeIn, fadeOut);
|
Chris@0
|
235 }
|
Chris@0
|
236
|
Chris@0
|
237 SparseOneDimensionalModel *sodm = dynamic_cast<SparseOneDimensionalModel *>
|
Chris@0
|
238 (model);
|
Chris@0
|
239 if (sodm) {
|
Chris@0
|
240 return mixSparseOneDimensionalModel(sodm, startFrame, frameCount,
|
Chris@4
|
241 buffer, gain, pan, fadeIn, fadeOut);
|
Chris@0
|
242 }
|
Chris@0
|
243
|
Chris@11
|
244 NoteModel *nm = dynamic_cast<NoteModel *>(model);
|
Chris@11
|
245 if (nm) {
|
Chris@11
|
246 return mixNoteModel(nm, startFrame, frameCount,
|
Chris@11
|
247 buffer, gain, pan, fadeIn, fadeOut);
|
Chris@11
|
248 }
|
Chris@11
|
249
|
Chris@0
|
250 return frameCount;
|
Chris@0
|
251 }
|
Chris@0
|
252
|
Chris@0
|
253 size_t
|
Chris@0
|
254 AudioGenerator::mixDenseTimeValueModel(DenseTimeValueModel *dtvm,
|
Chris@0
|
255 size_t startFrame, size_t frames,
|
Chris@4
|
256 float **buffer, float gain, float pan,
|
Chris@4
|
257 size_t fadeIn, size_t fadeOut)
|
Chris@0
|
258 {
|
Chris@0
|
259 static float *channelBuffer = 0;
|
Chris@0
|
260 static size_t channelBufSiz = 0;
|
Chris@5
|
261
|
Chris@4
|
262 size_t totalFrames = frames + fadeIn/2 + fadeOut/2;
|
Chris@4
|
263
|
Chris@4
|
264 if (channelBufSiz < totalFrames) {
|
Chris@0
|
265 delete[] channelBuffer;
|
Chris@4
|
266 channelBuffer = new float[totalFrames];
|
Chris@4
|
267 channelBufSiz = totalFrames;
|
Chris@0
|
268 }
|
Chris@0
|
269
|
Chris@0
|
270 size_t got = 0;
|
Chris@13
|
271 size_t prevChannel = 999;
|
Chris@0
|
272
|
Chris@13
|
273 for (size_t c = 0; c < m_targetChannelCount; ++c) {
|
Chris@4
|
274
|
Chris@13
|
275 size_t sourceChannel = (c % dtvm->getChannelCount());
|
Chris@13
|
276
|
Chris@13
|
277 // std::cerr << "mixing channel " << c << " from source channel " << sourceChannel << std::endl;
|
Chris@13
|
278
|
Chris@13
|
279 float channelGain = gain;
|
Chris@13
|
280 if (pan != 0.0) {
|
Chris@13
|
281 if (c == 0) {
|
Chris@13
|
282 if (pan > 0.0) channelGain *= 1.0 - pan;
|
Chris@13
|
283 } else {
|
Chris@13
|
284 if (pan < 0.0) channelGain *= pan + 1.0;
|
Chris@13
|
285 }
|
Chris@13
|
286 }
|
Chris@13
|
287
|
Chris@13
|
288 if (prevChannel != sourceChannel) {
|
Chris@13
|
289 if (startFrame >= fadeIn/2) {
|
Chris@13
|
290 got = dtvm->getValues
|
Chris@13
|
291 (sourceChannel,
|
Chris@13
|
292 startFrame - fadeIn/2, startFrame + frames + fadeOut/2,
|
Chris@13
|
293 channelBuffer);
|
Chris@13
|
294 } else {
|
Chris@13
|
295 size_t missing = fadeIn/2 - startFrame;
|
Chris@13
|
296 got = dtvm->getValues
|
Chris@13
|
297 (sourceChannel,
|
Chris@13
|
298 0, startFrame + frames + fadeOut/2,
|
Chris@13
|
299 channelBuffer + missing);
|
Chris@13
|
300 }
|
Chris@13
|
301 }
|
Chris@13
|
302 prevChannel = sourceChannel;
|
Chris@4
|
303
|
Chris@4
|
304 for (size_t i = 0; i < fadeIn/2; ++i) {
|
Chris@4
|
305 float *back = buffer[c];
|
Chris@4
|
306 back -= fadeIn/2;
|
Chris@13
|
307 back[i] += (channelGain * channelBuffer[i] * i) / fadeIn;
|
Chris@4
|
308 }
|
Chris@4
|
309
|
Chris@4
|
310 for (size_t i = 0; i < frames + fadeOut/2; ++i) {
|
Chris@13
|
311 float mult = channelGain;
|
Chris@4
|
312 if (i < fadeIn/2) {
|
Chris@4
|
313 mult = (mult * i) / fadeIn;
|
Chris@4
|
314 }
|
Chris@4
|
315 if (i > frames - fadeOut/2) {
|
Chris@4
|
316 mult = (mult * ((frames + fadeOut/2) - i)) / fadeOut;
|
Chris@4
|
317 }
|
Chris@4
|
318 buffer[c][i] += mult * channelBuffer[i];
|
Chris@0
|
319 }
|
Chris@0
|
320 }
|
Chris@0
|
321
|
Chris@0
|
322 return got;
|
Chris@0
|
323 }
|
Chris@11
|
324
|
Chris@0
|
325 size_t
|
Chris@0
|
326 AudioGenerator::mixSparseOneDimensionalModel(SparseOneDimensionalModel *sodm,
|
Chris@0
|
327 size_t startFrame, size_t frames,
|
Chris@4
|
328 float **buffer, float gain, float pan,
|
Chris@4
|
329 size_t /* fadeIn */,
|
Chris@4
|
330 size_t /* fadeOut */)
|
Chris@0
|
331 {
|
Chris@0
|
332 RealTimePluginInstance *plugin = m_synthMap[sodm];
|
Chris@0
|
333 if (!plugin) return 0;
|
Chris@0
|
334
|
Chris@0
|
335 size_t latency = plugin->getLatency();
|
Chris@0
|
336 size_t blocks = frames / m_pluginBlockSize;
|
Chris@0
|
337
|
Chris@0
|
338 //!!! hang on -- the fact that the audio callback play source's
|
Chris@0
|
339 //buffer is a multiple of the plugin's buffer size doesn't mean
|
Chris@0
|
340 //that we always get called for a multiple of it here (because it
|
Chris@0
|
341 //also depends on the JACK block size). how should we ensure that
|
Chris@0
|
342 //all models write the same amount in to the mix, and that we
|
Chris@0
|
343 //always have a multiple of the plugin buffer size? I guess this
|
Chris@0
|
344 //class has to be queryable for the plugin buffer size & the
|
Chris@0
|
345 //callback play source has to use that as a multiple for all the
|
Chris@0
|
346 //calls to mixModel
|
Chris@0
|
347
|
Chris@0
|
348 size_t got = blocks * m_pluginBlockSize;
|
Chris@0
|
349
|
Chris@0
|
350 #ifdef DEBUG_AUDIO_GENERATOR
|
Chris@0
|
351 std::cout << "mixModel [sparse]: frames " << frames
|
Chris@0
|
352 << ", blocks " << blocks << std::endl;
|
Chris@0
|
353 #endif
|
Chris@0
|
354
|
Chris@0
|
355 snd_seq_event_t onEv;
|
Chris@0
|
356 onEv.type = SND_SEQ_EVENT_NOTEON;
|
Chris@0
|
357 onEv.data.note.channel = 0;
|
Chris@0
|
358 onEv.data.note.note = 64;
|
Chris@0
|
359 onEv.data.note.velocity = 127;
|
Chris@0
|
360
|
Chris@0
|
361 snd_seq_event_t offEv;
|
Chris@0
|
362 offEv.type = SND_SEQ_EVENT_NOTEOFF;
|
Chris@0
|
363 offEv.data.note.channel = 0;
|
Chris@0
|
364 offEv.data.note.velocity = 0;
|
Chris@0
|
365
|
Chris@0
|
366 NoteOffSet ¬eOffs = m_noteOffs[sodm];
|
Chris@0
|
367
|
Chris@0
|
368 for (size_t i = 0; i < blocks; ++i) {
|
Chris@0
|
369
|
Chris@0
|
370 size_t reqStart = startFrame + i * m_pluginBlockSize;
|
Chris@0
|
371
|
Chris@0
|
372 SparseOneDimensionalModel::PointList points =
|
Chris@5
|
373 sodm->getPoints(reqStart + latency,
|
Chris@0
|
374 reqStart + latency + m_pluginBlockSize);
|
Chris@0
|
375
|
Chris@0
|
376 RealTime blockTime = RealTime::frame2RealTime
|
Chris@0
|
377 (startFrame + i * m_pluginBlockSize, m_sourceSampleRate);
|
Chris@0
|
378
|
Chris@0
|
379 for (SparseOneDimensionalModel::PointList::iterator pli =
|
Chris@0
|
380 points.begin(); pli != points.end(); ++pli) {
|
Chris@0
|
381
|
Chris@0
|
382 size_t pliFrame = pli->frame;
|
Chris@5
|
383
|
Chris@0
|
384 if (pliFrame >= latency) pliFrame -= latency;
|
Chris@0
|
385
|
Chris@5
|
386 if (pliFrame < reqStart ||
|
Chris@5
|
387 pliFrame >= reqStart + m_pluginBlockSize) continue;
|
Chris@5
|
388
|
Chris@0
|
389 while (noteOffs.begin() != noteOffs.end() &&
|
Chris@0
|
390 noteOffs.begin()->frame <= pliFrame) {
|
Chris@0
|
391
|
Chris@0
|
392 RealTime eventTime = RealTime::frame2RealTime
|
Chris@0
|
393 (noteOffs.begin()->frame, m_sourceSampleRate);
|
Chris@0
|
394
|
Chris@0
|
395 offEv.data.note.note = noteOffs.begin()->pitch;
|
Chris@5
|
396
|
Chris@5
|
397 #ifdef DEBUG_AUDIO_GENERATOR
|
Chris@5
|
398 std::cerr << "mixModel [sparse]: sending note-off event at time " << eventTime << " frame " << noteOffs.begin()->frame << std::endl;
|
Chris@5
|
399 #endif
|
Chris@5
|
400
|
Chris@0
|
401 plugin->sendEvent(eventTime, &offEv);
|
Chris@0
|
402 noteOffs.erase(noteOffs.begin());
|
Chris@0
|
403 }
|
Chris@0
|
404
|
Chris@0
|
405 RealTime eventTime = RealTime::frame2RealTime
|
Chris@0
|
406 (pliFrame, m_sourceSampleRate);
|
Chris@0
|
407
|
Chris@0
|
408 plugin->sendEvent(eventTime, &onEv);
|
Chris@0
|
409
|
Chris@0
|
410 #ifdef DEBUG_AUDIO_GENERATOR
|
Chris@0
|
411 std::cout << "mixModel [sparse]: point at frame " << pliFrame << ", block start " << (startFrame + i * m_pluginBlockSize) << ", resulting time " << eventTime << std::endl;
|
Chris@0
|
412 #endif
|
Chris@0
|
413
|
Chris@0
|
414 size_t duration = 7000; // frames [for now]
|
Chris@0
|
415 NoteOff noff;
|
Chris@0
|
416 noff.pitch = onEv.data.note.note;
|
Chris@0
|
417 noff.frame = pliFrame + duration;
|
Chris@0
|
418 noteOffs.insert(noff);
|
Chris@0
|
419 }
|
Chris@0
|
420
|
Chris@0
|
421 while (noteOffs.begin() != noteOffs.end() &&
|
Chris@0
|
422 noteOffs.begin()->frame <=
|
Chris@0
|
423 startFrame + i * m_pluginBlockSize + m_pluginBlockSize) {
|
Chris@0
|
424
|
Chris@0
|
425 RealTime eventTime = RealTime::frame2RealTime
|
Chris@0
|
426 (noteOffs.begin()->frame, m_sourceSampleRate);
|
Chris@0
|
427
|
Chris@0
|
428 offEv.data.note.note = noteOffs.begin()->pitch;
|
Chris@5
|
429
|
Chris@5
|
430 #ifdef DEBUG_AUDIO_GENERATOR
|
Chris@5
|
431 std::cerr << "mixModel [sparse]: sending leftover note-off event at time " << eventTime << " frame " << noteOffs.begin()->frame << std::endl;
|
Chris@5
|
432 #endif
|
Chris@5
|
433
|
Chris@0
|
434 plugin->sendEvent(eventTime, &offEv);
|
Chris@0
|
435 noteOffs.erase(noteOffs.begin());
|
Chris@0
|
436 }
|
Chris@0
|
437
|
Chris@0
|
438 plugin->run(blockTime);
|
Chris@0
|
439 float **outs = plugin->getAudioOutputBuffers();
|
Chris@0
|
440
|
Chris@13
|
441 for (size_t c = 0; c < m_targetChannelCount; ++c) {
|
Chris@0
|
442 #ifdef DEBUG_AUDIO_GENERATOR
|
Chris@0
|
443 std::cout << "mixModel [sparse]: adding " << m_pluginBlockSize << " samples from plugin output " << c << std::endl;
|
Chris@0
|
444 #endif
|
Chris@0
|
445
|
Chris@13
|
446 size_t sourceChannel = (c % plugin->getAudioOutputCount());
|
Chris@13
|
447
|
Chris@13
|
448 float channelGain = gain;
|
Chris@13
|
449 if (pan != 0.0) {
|
Chris@13
|
450 if (c == 0) {
|
Chris@13
|
451 if (pan > 0.0) channelGain *= 1.0 - pan;
|
Chris@13
|
452 } else {
|
Chris@13
|
453 if (pan < 0.0) channelGain *= pan + 1.0;
|
Chris@13
|
454 }
|
Chris@13
|
455 }
|
Chris@13
|
456
|
Chris@0
|
457 for (size_t j = 0; j < m_pluginBlockSize; ++j) {
|
Chris@13
|
458 buffer[c][i * m_pluginBlockSize + j] +=
|
Chris@13
|
459 channelGain * outs[sourceChannel][j];
|
Chris@0
|
460 }
|
Chris@0
|
461 }
|
Chris@0
|
462 }
|
Chris@0
|
463
|
Chris@0
|
464 return got;
|
Chris@0
|
465 }
|
Chris@0
|
466
|
Chris@11
|
467
|
Chris@11
|
468 //!!! mucho duplication with above -- refactor
|
Chris@11
|
469 size_t
|
Chris@11
|
470 AudioGenerator::mixNoteModel(NoteModel *nm,
|
Chris@11
|
471 size_t startFrame, size_t frames,
|
Chris@11
|
472 float **buffer, float gain, float pan,
|
Chris@11
|
473 size_t /* fadeIn */,
|
Chris@11
|
474 size_t /* fadeOut */)
|
Chris@11
|
475 {
|
Chris@11
|
476 RealTimePluginInstance *plugin = m_synthMap[nm];
|
Chris@11
|
477 if (!plugin) return 0;
|
Chris@11
|
478
|
Chris@11
|
479 size_t latency = plugin->getLatency();
|
Chris@11
|
480 size_t blocks = frames / m_pluginBlockSize;
|
Chris@11
|
481
|
Chris@11
|
482 //!!! hang on -- the fact that the audio callback play source's
|
Chris@11
|
483 //buffer is a multiple of the plugin's buffer size doesn't mean
|
Chris@11
|
484 //that we always get called for a multiple of it here (because it
|
Chris@11
|
485 //also depends on the JACK block size). how should we ensure that
|
Chris@11
|
486 //all models write the same amount in to the mix, and that we
|
Chris@11
|
487 //always have a multiple of the plugin buffer size? I guess this
|
Chris@11
|
488 //class has to be queryable for the plugin buffer size & the
|
Chris@11
|
489 //callback play source has to use that as a multiple for all the
|
Chris@11
|
490 //calls to mixModel
|
Chris@11
|
491
|
Chris@11
|
492 size_t got = blocks * m_pluginBlockSize;
|
Chris@11
|
493
|
Chris@11
|
494 #ifdef DEBUG_AUDIO_GENERATOR
|
Chris@11
|
495 std::cout << "mixModel [note]: frames " << frames
|
Chris@11
|
496 << ", blocks " << blocks << std::endl;
|
Chris@11
|
497 #endif
|
Chris@11
|
498
|
Chris@11
|
499 snd_seq_event_t onEv;
|
Chris@11
|
500 onEv.type = SND_SEQ_EVENT_NOTEON;
|
Chris@11
|
501 onEv.data.note.channel = 0;
|
Chris@11
|
502 onEv.data.note.note = 64;
|
Chris@11
|
503 onEv.data.note.velocity = 127;
|
Chris@11
|
504
|
Chris@11
|
505 snd_seq_event_t offEv;
|
Chris@11
|
506 offEv.type = SND_SEQ_EVENT_NOTEOFF;
|
Chris@11
|
507 offEv.data.note.channel = 0;
|
Chris@11
|
508 offEv.data.note.velocity = 0;
|
Chris@11
|
509
|
Chris@11
|
510 NoteOffSet ¬eOffs = m_noteOffs[nm];
|
Chris@11
|
511
|
Chris@11
|
512 for (size_t i = 0; i < blocks; ++i) {
|
Chris@11
|
513
|
Chris@11
|
514 size_t reqStart = startFrame + i * m_pluginBlockSize;
|
Chris@11
|
515
|
Chris@11
|
516 NoteModel::PointList points =
|
Chris@11
|
517 nm->getPoints(reqStart + latency,
|
Chris@11
|
518 reqStart + latency + m_pluginBlockSize);
|
Chris@11
|
519
|
Chris@11
|
520 RealTime blockTime = RealTime::frame2RealTime
|
Chris@11
|
521 (startFrame + i * m_pluginBlockSize, m_sourceSampleRate);
|
Chris@11
|
522
|
Chris@11
|
523 for (NoteModel::PointList::iterator pli =
|
Chris@11
|
524 points.begin(); pli != points.end(); ++pli) {
|
Chris@11
|
525
|
Chris@11
|
526 size_t pliFrame = pli->frame;
|
Chris@11
|
527
|
Chris@11
|
528 if (pliFrame >= latency) pliFrame -= latency;
|
Chris@11
|
529
|
Chris@11
|
530 if (pliFrame < reqStart ||
|
Chris@11
|
531 pliFrame >= reqStart + m_pluginBlockSize) continue;
|
Chris@11
|
532
|
Chris@11
|
533 while (noteOffs.begin() != noteOffs.end() &&
|
Chris@11
|
534 noteOffs.begin()->frame <= pliFrame) {
|
Chris@11
|
535
|
Chris@11
|
536 RealTime eventTime = RealTime::frame2RealTime
|
Chris@11
|
537 (noteOffs.begin()->frame, m_sourceSampleRate);
|
Chris@11
|
538
|
Chris@11
|
539 offEv.data.note.note = noteOffs.begin()->pitch;
|
Chris@11
|
540
|
Chris@11
|
541 #ifdef DEBUG_AUDIO_GENERATOR
|
Chris@11
|
542 std::cerr << "mixModel [note]: sending note-off event at time " << eventTime << " frame " << noteOffs.begin()->frame << std::endl;
|
Chris@11
|
543 #endif
|
Chris@11
|
544
|
Chris@11
|
545 plugin->sendEvent(eventTime, &offEv);
|
Chris@11
|
546 noteOffs.erase(noteOffs.begin());
|
Chris@11
|
547 }
|
Chris@11
|
548
|
Chris@11
|
549 RealTime eventTime = RealTime::frame2RealTime
|
Chris@11
|
550 (pliFrame, m_sourceSampleRate);
|
Chris@11
|
551
|
Chris@11
|
552 onEv.data.note.note = lrintf(pli->value);
|
Chris@11
|
553
|
Chris@11
|
554 plugin->sendEvent(eventTime, &onEv);
|
Chris@11
|
555
|
Chris@11
|
556 #ifdef DEBUG_AUDIO_GENERATOR
|
Chris@11
|
557 std::cout << "mixModel [note]: point at frame " << pliFrame << ", block start " << (startFrame + i * m_pluginBlockSize) << ", resulting time " << eventTime << std::endl;
|
Chris@11
|
558 #endif
|
Chris@11
|
559
|
Chris@11
|
560 size_t duration = pli->duration;
|
Chris@11
|
561 NoteOff noff;
|
Chris@11
|
562 noff.pitch = onEv.data.note.note;
|
Chris@11
|
563 noff.frame = pliFrame + duration;
|
Chris@11
|
564 noteOffs.insert(noff);
|
Chris@11
|
565 }
|
Chris@11
|
566
|
Chris@11
|
567 while (noteOffs.begin() != noteOffs.end() &&
|
Chris@11
|
568 noteOffs.begin()->frame <=
|
Chris@11
|
569 startFrame + i * m_pluginBlockSize + m_pluginBlockSize) {
|
Chris@11
|
570
|
Chris@11
|
571 RealTime eventTime = RealTime::frame2RealTime
|
Chris@11
|
572 (noteOffs.begin()->frame, m_sourceSampleRate);
|
Chris@11
|
573
|
Chris@11
|
574 offEv.data.note.note = noteOffs.begin()->pitch;
|
Chris@11
|
575
|
Chris@11
|
576 #ifdef DEBUG_AUDIO_GENERATOR
|
Chris@11
|
577 std::cerr << "mixModel [note]: sending leftover note-off event at time " << eventTime << " frame " << noteOffs.begin()->frame << std::endl;
|
Chris@11
|
578 #endif
|
Chris@11
|
579
|
Chris@11
|
580 plugin->sendEvent(eventTime, &offEv);
|
Chris@11
|
581 noteOffs.erase(noteOffs.begin());
|
Chris@11
|
582 }
|
Chris@11
|
583
|
Chris@11
|
584 plugin->run(blockTime);
|
Chris@11
|
585 float **outs = plugin->getAudioOutputBuffers();
|
Chris@11
|
586
|
Chris@13
|
587 for (size_t c = 0; c < m_targetChannelCount; ++c) {
|
Chris@11
|
588 #ifdef DEBUG_AUDIO_GENERATOR
|
Chris@11
|
589 std::cout << "mixModel [note]: adding " << m_pluginBlockSize << " samples from plugin output " << c << std::endl;
|
Chris@11
|
590 #endif
|
Chris@11
|
591
|
Chris@13
|
592 size_t sourceChannel = (c % plugin->getAudioOutputCount());
|
Chris@13
|
593
|
Chris@13
|
594 float channelGain = gain;
|
Chris@13
|
595 if (pan != 0.0) {
|
Chris@13
|
596 if (c == 0) {
|
Chris@13
|
597 if (pan > 0.0) channelGain *= 1.0 - pan;
|
Chris@13
|
598 } else {
|
Chris@13
|
599 if (pan < 0.0) channelGain *= pan + 1.0;
|
Chris@13
|
600 }
|
Chris@13
|
601 }
|
Chris@13
|
602
|
Chris@11
|
603 for (size_t j = 0; j < m_pluginBlockSize; ++j) {
|
Chris@13
|
604 buffer[c][i * m_pluginBlockSize + j] +=
|
Chris@13
|
605 channelGain * outs[sourceChannel][j];
|
Chris@11
|
606 }
|
Chris@11
|
607 }
|
Chris@11
|
608 }
|
Chris@11
|
609
|
Chris@11
|
610 return got;
|
Chris@11
|
611 }
|
Chris@11
|
612
|