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