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@0
|
14
|
Chris@0
|
15 #include "model/DenseTimeValueModel.h"
|
Chris@0
|
16 #include "model/SparseOneDimensionalModel.h"
|
Chris@0
|
17
|
Chris@0
|
18 #include "plugin/RealTimePluginFactory.h"
|
Chris@0
|
19 #include "plugin/RealTimePluginInstance.h"
|
Chris@0
|
20 #include "plugin/PluginIdentifier.h"
|
Chris@0
|
21 #include "plugin/api/alsa/seq_event.h"
|
Chris@0
|
22
|
Chris@0
|
23 #include <iostream>
|
Chris@0
|
24
|
Chris@0
|
25 const size_t
|
Chris@0
|
26 AudioGenerator::m_pluginBlockSize = 2048;
|
Chris@0
|
27
|
Chris@5
|
28 //#define DEBUG_AUDIO_GENERATOR 1
|
Chris@0
|
29
|
Chris@0
|
30 AudioGenerator::AudioGenerator(ViewManager *manager) :
|
Chris@0
|
31 m_viewManager(manager),
|
Chris@0
|
32 m_sourceSampleRate(0),
|
Chris@0
|
33 m_targetChannelCount(1)
|
Chris@0
|
34 {
|
Chris@0
|
35 }
|
Chris@0
|
36
|
Chris@0
|
37 AudioGenerator::~AudioGenerator()
|
Chris@0
|
38 {
|
Chris@0
|
39 }
|
Chris@0
|
40
|
Chris@10
|
41 bool
|
Chris@0
|
42 AudioGenerator::addModel(Model *model)
|
Chris@0
|
43 {
|
Chris@0
|
44 if (m_sourceSampleRate == 0) {
|
Chris@0
|
45
|
Chris@0
|
46 m_sourceSampleRate = model->getSampleRate();
|
Chris@0
|
47
|
Chris@0
|
48 } else {
|
Chris@0
|
49
|
Chris@0
|
50 DenseTimeValueModel *dtvm =
|
Chris@0
|
51 dynamic_cast<DenseTimeValueModel *>(model);
|
Chris@0
|
52
|
Chris@0
|
53 if (dtvm) {
|
Chris@0
|
54 m_sourceSampleRate = model->getSampleRate();
|
Chris@10
|
55 return true;
|
Chris@0
|
56 }
|
Chris@0
|
57 }
|
Chris@0
|
58
|
Chris@0
|
59 SparseOneDimensionalModel *sodm =
|
Chris@0
|
60 dynamic_cast<SparseOneDimensionalModel *>(model);
|
Chris@10
|
61 if (!sodm) return false; // nothing else to initialise
|
Chris@0
|
62
|
Chris@0
|
63 // QString pluginId = "dssi:/usr/lib/dssi/dssi-vst.so:FEARkILLERrev1.dll";
|
Chris@0
|
64 // QString pluginId = "dssi:/usr/lib/dssi/hexter.so:hexter";
|
Chris@0
|
65 // QString pluginId = "dssi:/usr/lib/dssi/sineshaper.so:sineshaper";
|
Chris@0
|
66 // QString pluginId = "dssi:/usr/local/lib/dssi/xsynth-dssi.so:Xsynth";
|
Chris@0
|
67 // QString pluginId = "dssi:/usr/local/lib/dssi/trivial_synth.so:TS";
|
Chris@0
|
68 QString pluginId = QString("dssi:%1:sample_player").
|
Chris@0
|
69 arg(PluginIdentifier::BUILTIN_PLUGIN_SONAME);
|
Chris@0
|
70 RealTimePluginFactory *factory =
|
Chris@0
|
71 RealTimePluginFactory::instanceFor(pluginId);
|
Chris@0
|
72
|
Chris@0
|
73 if (!factory) {
|
Chris@0
|
74 std::cerr << "Failed to get plugin factory" << std::endl;
|
Chris@10
|
75 return false;
|
Chris@0
|
76 }
|
Chris@0
|
77
|
Chris@0
|
78 RealTimePluginInstance *instance =
|
Chris@0
|
79 factory->instantiatePlugin
|
Chris@0
|
80 (pluginId, 0, 0, m_sourceSampleRate, m_pluginBlockSize, m_targetChannelCount);
|
Chris@0
|
81
|
Chris@0
|
82 if (instance) {
|
Chris@8
|
83 QMutexLocker locker(&m_mutex);
|
Chris@0
|
84 m_synthMap[sodm] = instance;
|
Chris@0
|
85 for (unsigned int i = 0; i < instance->getParameterCount(); ++i) {
|
Chris@0
|
86 instance->setParameterValue(i, instance->getParameterDefault(i));
|
Chris@0
|
87 }
|
Chris@0
|
88 QString program = instance->getProgram(0, 0);
|
Chris@0
|
89 if (program != "") {
|
Chris@0
|
90 std::cerr << "selecting program " << program.toLocal8Bit().data() << std::endl;
|
Chris@0
|
91 instance->selectProgram(program);
|
Chris@0
|
92 }
|
Chris@0
|
93 instance->selectProgram("cowbell"); //!!!
|
Chris@0
|
94 instance->setIdealChannelCount(m_targetChannelCount); // reset!
|
Chris@0
|
95 } else {
|
Chris@0
|
96 std::cerr << "Failed to instantiate plugin" << std::endl;
|
Chris@10
|
97 return false;
|
Chris@0
|
98 }
|
Chris@10
|
99
|
Chris@10
|
100 return true;
|
Chris@0
|
101 }
|
Chris@0
|
102
|
Chris@0
|
103 void
|
Chris@0
|
104 AudioGenerator::removeModel(Model *model)
|
Chris@0
|
105 {
|
Chris@0
|
106 SparseOneDimensionalModel *sodm =
|
Chris@0
|
107 dynamic_cast<SparseOneDimensionalModel *>(model);
|
Chris@0
|
108 if (!sodm) return; // nothing to do
|
Chris@0
|
109
|
Chris@8
|
110 QMutexLocker locker(&m_mutex);
|
Chris@8
|
111
|
Chris@0
|
112 if (m_synthMap.find(sodm) == m_synthMap.end()) return;
|
Chris@0
|
113
|
Chris@0
|
114 RealTimePluginInstance *instance = m_synthMap[sodm];
|
Chris@0
|
115 m_synthMap.erase(sodm);
|
Chris@0
|
116 delete instance;
|
Chris@0
|
117 }
|
Chris@0
|
118
|
Chris@0
|
119 void
|
Chris@0
|
120 AudioGenerator::clearModels()
|
Chris@0
|
121 {
|
Chris@8
|
122 QMutexLocker locker(&m_mutex);
|
Chris@0
|
123 while (!m_synthMap.empty()) {
|
Chris@0
|
124 RealTimePluginInstance *instance = m_synthMap.begin()->second;
|
Chris@0
|
125 m_synthMap.erase(m_synthMap.begin());
|
Chris@0
|
126 delete instance;
|
Chris@0
|
127 }
|
Chris@0
|
128 }
|
Chris@0
|
129
|
Chris@0
|
130 void
|
Chris@0
|
131 AudioGenerator::reset()
|
Chris@0
|
132 {
|
Chris@8
|
133 QMutexLocker locker(&m_mutex);
|
Chris@0
|
134 for (PluginMap::iterator i = m_synthMap.begin(); i != m_synthMap.end(); ++i) {
|
Chris@0
|
135 if (i->second) {
|
Chris@0
|
136 i->second->silence();
|
Chris@0
|
137 i->second->discardEvents();
|
Chris@0
|
138 }
|
Chris@0
|
139 }
|
Chris@0
|
140
|
Chris@0
|
141 m_noteOffs.clear();
|
Chris@0
|
142 }
|
Chris@0
|
143
|
Chris@0
|
144 void
|
Chris@0
|
145 AudioGenerator::setTargetChannelCount(size_t targetChannelCount)
|
Chris@0
|
146 {
|
Chris@8
|
147 QMutexLocker locker(&m_mutex);
|
Chris@0
|
148 m_targetChannelCount = targetChannelCount;
|
Chris@0
|
149
|
Chris@0
|
150 for (PluginMap::iterator i = m_synthMap.begin(); i != m_synthMap.end(); ++i) {
|
Chris@0
|
151 if (i->second) i->second->setIdealChannelCount(targetChannelCount);
|
Chris@0
|
152 }
|
Chris@0
|
153 }
|
Chris@0
|
154
|
Chris@0
|
155 size_t
|
Chris@0
|
156 AudioGenerator::getBlockSize() const
|
Chris@0
|
157 {
|
Chris@0
|
158 return m_pluginBlockSize;
|
Chris@0
|
159 }
|
Chris@0
|
160
|
Chris@0
|
161 size_t
|
Chris@0
|
162 AudioGenerator::mixModel(Model *model, size_t startFrame, size_t frameCount,
|
Chris@4
|
163 float **buffer, size_t fadeIn, size_t fadeOut)
|
Chris@0
|
164 {
|
Chris@0
|
165 if (m_sourceSampleRate == 0) {
|
Chris@0
|
166 std::cerr << "WARNING: AudioGenerator::mixModel: No base source sample rate available" << std::endl;
|
Chris@0
|
167 return frameCount;
|
Chris@0
|
168 }
|
Chris@0
|
169
|
Chris@8
|
170 QMutexLocker locker(&m_mutex);
|
Chris@8
|
171
|
Chris@0
|
172 PlayParameters *parameters = m_viewManager->getPlayParameters(model);
|
Chris@0
|
173 if (!parameters) return frameCount;
|
Chris@0
|
174
|
Chris@0
|
175 bool playing = !parameters->isPlayMuted();
|
Chris@0
|
176 if (!playing) return frameCount;
|
Chris@0
|
177
|
Chris@0
|
178 float gain = parameters->getPlayGain();
|
Chris@0
|
179 float pan = parameters->getPlayPan();
|
Chris@0
|
180
|
Chris@0
|
181 DenseTimeValueModel *dtvm = dynamic_cast<DenseTimeValueModel *>(model);
|
Chris@0
|
182 if (dtvm) {
|
Chris@0
|
183 return mixDenseTimeValueModel(dtvm, startFrame, frameCount,
|
Chris@4
|
184 buffer, gain, pan, fadeIn, fadeOut);
|
Chris@0
|
185 }
|
Chris@0
|
186
|
Chris@0
|
187 SparseOneDimensionalModel *sodm = dynamic_cast<SparseOneDimensionalModel *>
|
Chris@0
|
188 (model);
|
Chris@0
|
189 if (sodm) {
|
Chris@0
|
190 return mixSparseOneDimensionalModel(sodm, startFrame, frameCount,
|
Chris@4
|
191 buffer, gain, pan, fadeIn, fadeOut);
|
Chris@0
|
192 }
|
Chris@0
|
193
|
Chris@0
|
194 return frameCount;
|
Chris@0
|
195 }
|
Chris@0
|
196
|
Chris@0
|
197 size_t
|
Chris@0
|
198 AudioGenerator::mixDenseTimeValueModel(DenseTimeValueModel *dtvm,
|
Chris@0
|
199 size_t startFrame, size_t frames,
|
Chris@4
|
200 float **buffer, float gain, float pan,
|
Chris@4
|
201 size_t fadeIn, size_t fadeOut)
|
Chris@0
|
202 {
|
Chris@0
|
203 static float *channelBuffer = 0;
|
Chris@0
|
204 static size_t channelBufSiz = 0;
|
Chris@5
|
205
|
Chris@4
|
206 size_t totalFrames = frames + fadeIn/2 + fadeOut/2;
|
Chris@4
|
207
|
Chris@4
|
208 if (channelBufSiz < totalFrames) {
|
Chris@0
|
209 delete[] channelBuffer;
|
Chris@4
|
210 channelBuffer = new float[totalFrames];
|
Chris@4
|
211 channelBufSiz = totalFrames;
|
Chris@0
|
212 }
|
Chris@0
|
213
|
Chris@0
|
214 size_t got = 0;
|
Chris@0
|
215
|
Chris@0
|
216 for (size_t c = 0; c < m_targetChannelCount && c < dtvm->getChannelCount(); ++c) {
|
Chris@4
|
217
|
Chris@5
|
218 if (startFrame >= fadeIn/2) {
|
Chris@5
|
219 got = dtvm->getValues
|
Chris@5
|
220 (c, startFrame - fadeIn/2, startFrame + frames + fadeOut/2,
|
Chris@5
|
221 channelBuffer);
|
Chris@5
|
222 } else {
|
Chris@5
|
223 size_t missing = fadeIn/2 - startFrame;
|
Chris@5
|
224 got = dtvm->getValues
|
Chris@5
|
225 (c, 0, startFrame + frames + fadeOut/2,
|
Chris@5
|
226 channelBuffer + missing);
|
Chris@5
|
227 }
|
Chris@4
|
228
|
Chris@4
|
229 for (size_t i = 0; i < fadeIn/2; ++i) {
|
Chris@4
|
230 float *back = buffer[c];
|
Chris@4
|
231 back -= fadeIn/2;
|
Chris@4
|
232 back[i] += (gain * channelBuffer[i] * i) / fadeIn;
|
Chris@4
|
233 }
|
Chris@4
|
234
|
Chris@4
|
235 for (size_t i = 0; i < frames + fadeOut/2; ++i) {
|
Chris@4
|
236 float mult = gain;
|
Chris@4
|
237 if (i < fadeIn/2) {
|
Chris@4
|
238 mult = (mult * i) / fadeIn;
|
Chris@4
|
239 }
|
Chris@4
|
240 if (i > frames - fadeOut/2) {
|
Chris@4
|
241 mult = (mult * ((frames + fadeOut/2) - i)) / fadeOut;
|
Chris@4
|
242 }
|
Chris@4
|
243 buffer[c][i] += mult * channelBuffer[i];
|
Chris@0
|
244 }
|
Chris@0
|
245 }
|
Chris@0
|
246
|
Chris@0
|
247 return got;
|
Chris@0
|
248 }
|
Chris@0
|
249
|
Chris@0
|
250 size_t
|
Chris@0
|
251 AudioGenerator::mixSparseOneDimensionalModel(SparseOneDimensionalModel *sodm,
|
Chris@0
|
252 size_t startFrame, size_t frames,
|
Chris@4
|
253 float **buffer, float gain, float pan,
|
Chris@4
|
254 size_t /* fadeIn */,
|
Chris@4
|
255 size_t /* fadeOut */)
|
Chris@0
|
256 {
|
Chris@0
|
257 RealTimePluginInstance *plugin = m_synthMap[sodm];
|
Chris@0
|
258 if (!plugin) return 0;
|
Chris@0
|
259
|
Chris@0
|
260 size_t latency = plugin->getLatency();
|
Chris@0
|
261 size_t blocks = frames / m_pluginBlockSize;
|
Chris@0
|
262
|
Chris@0
|
263 //!!! hang on -- the fact that the audio callback play source's
|
Chris@0
|
264 //buffer is a multiple of the plugin's buffer size doesn't mean
|
Chris@0
|
265 //that we always get called for a multiple of it here (because it
|
Chris@0
|
266 //also depends on the JACK block size). how should we ensure that
|
Chris@0
|
267 //all models write the same amount in to the mix, and that we
|
Chris@0
|
268 //always have a multiple of the plugin buffer size? I guess this
|
Chris@0
|
269 //class has to be queryable for the plugin buffer size & the
|
Chris@0
|
270 //callback play source has to use that as a multiple for all the
|
Chris@0
|
271 //calls to mixModel
|
Chris@0
|
272
|
Chris@0
|
273 size_t got = blocks * m_pluginBlockSize;
|
Chris@0
|
274
|
Chris@0
|
275 #ifdef DEBUG_AUDIO_GENERATOR
|
Chris@0
|
276 std::cout << "mixModel [sparse]: frames " << frames
|
Chris@0
|
277 << ", blocks " << blocks << std::endl;
|
Chris@0
|
278 #endif
|
Chris@0
|
279
|
Chris@0
|
280 snd_seq_event_t onEv;
|
Chris@0
|
281 onEv.type = SND_SEQ_EVENT_NOTEON;
|
Chris@0
|
282 onEv.data.note.channel = 0;
|
Chris@0
|
283 onEv.data.note.note = 64;
|
Chris@0
|
284 onEv.data.note.velocity = 127;
|
Chris@0
|
285
|
Chris@0
|
286 snd_seq_event_t offEv;
|
Chris@0
|
287 offEv.type = SND_SEQ_EVENT_NOTEOFF;
|
Chris@0
|
288 offEv.data.note.channel = 0;
|
Chris@0
|
289 offEv.data.note.velocity = 0;
|
Chris@0
|
290
|
Chris@0
|
291 NoteOffSet ¬eOffs = m_noteOffs[sodm];
|
Chris@0
|
292
|
Chris@0
|
293 for (size_t i = 0; i < blocks; ++i) {
|
Chris@0
|
294
|
Chris@0
|
295 size_t reqStart = startFrame + i * m_pluginBlockSize;
|
Chris@0
|
296
|
Chris@0
|
297 SparseOneDimensionalModel::PointList points =
|
Chris@5
|
298 sodm->getPoints(reqStart + latency,
|
Chris@0
|
299 reqStart + latency + m_pluginBlockSize);
|
Chris@0
|
300
|
Chris@0
|
301 RealTime blockTime = RealTime::frame2RealTime
|
Chris@0
|
302 (startFrame + i * m_pluginBlockSize, m_sourceSampleRate);
|
Chris@0
|
303
|
Chris@0
|
304 for (SparseOneDimensionalModel::PointList::iterator pli =
|
Chris@0
|
305 points.begin(); pli != points.end(); ++pli) {
|
Chris@0
|
306
|
Chris@0
|
307 size_t pliFrame = pli->frame;
|
Chris@5
|
308
|
Chris@0
|
309 if (pliFrame >= latency) pliFrame -= latency;
|
Chris@0
|
310
|
Chris@5
|
311 if (pliFrame < reqStart ||
|
Chris@5
|
312 pliFrame >= reqStart + m_pluginBlockSize) continue;
|
Chris@5
|
313
|
Chris@0
|
314 while (noteOffs.begin() != noteOffs.end() &&
|
Chris@0
|
315 noteOffs.begin()->frame <= pliFrame) {
|
Chris@0
|
316
|
Chris@0
|
317 RealTime eventTime = RealTime::frame2RealTime
|
Chris@0
|
318 (noteOffs.begin()->frame, m_sourceSampleRate);
|
Chris@0
|
319
|
Chris@0
|
320 offEv.data.note.note = noteOffs.begin()->pitch;
|
Chris@5
|
321
|
Chris@5
|
322 #ifdef DEBUG_AUDIO_GENERATOR
|
Chris@5
|
323 std::cerr << "mixModel [sparse]: sending note-off event at time " << eventTime << " frame " << noteOffs.begin()->frame << std::endl;
|
Chris@5
|
324 #endif
|
Chris@5
|
325
|
Chris@0
|
326 plugin->sendEvent(eventTime, &offEv);
|
Chris@0
|
327 noteOffs.erase(noteOffs.begin());
|
Chris@0
|
328 }
|
Chris@0
|
329
|
Chris@0
|
330 RealTime eventTime = RealTime::frame2RealTime
|
Chris@0
|
331 (pliFrame, m_sourceSampleRate);
|
Chris@0
|
332
|
Chris@0
|
333 plugin->sendEvent(eventTime, &onEv);
|
Chris@0
|
334
|
Chris@0
|
335 #ifdef DEBUG_AUDIO_GENERATOR
|
Chris@0
|
336 std::cout << "mixModel [sparse]: point at frame " << pliFrame << ", block start " << (startFrame + i * m_pluginBlockSize) << ", resulting time " << eventTime << std::endl;
|
Chris@0
|
337 #endif
|
Chris@0
|
338
|
Chris@0
|
339 size_t duration = 7000; // frames [for now]
|
Chris@0
|
340 NoteOff noff;
|
Chris@0
|
341 noff.pitch = onEv.data.note.note;
|
Chris@0
|
342 noff.frame = pliFrame + duration;
|
Chris@0
|
343 noteOffs.insert(noff);
|
Chris@0
|
344 }
|
Chris@0
|
345
|
Chris@0
|
346 while (noteOffs.begin() != noteOffs.end() &&
|
Chris@0
|
347 noteOffs.begin()->frame <=
|
Chris@0
|
348 startFrame + i * m_pluginBlockSize + m_pluginBlockSize) {
|
Chris@0
|
349
|
Chris@0
|
350 RealTime eventTime = RealTime::frame2RealTime
|
Chris@0
|
351 (noteOffs.begin()->frame, m_sourceSampleRate);
|
Chris@0
|
352
|
Chris@0
|
353 offEv.data.note.note = noteOffs.begin()->pitch;
|
Chris@5
|
354
|
Chris@5
|
355 #ifdef DEBUG_AUDIO_GENERATOR
|
Chris@5
|
356 std::cerr << "mixModel [sparse]: sending leftover note-off event at time " << eventTime << " frame " << noteOffs.begin()->frame << std::endl;
|
Chris@5
|
357 #endif
|
Chris@5
|
358
|
Chris@0
|
359 plugin->sendEvent(eventTime, &offEv);
|
Chris@0
|
360 noteOffs.erase(noteOffs.begin());
|
Chris@0
|
361 }
|
Chris@0
|
362
|
Chris@0
|
363 plugin->run(blockTime);
|
Chris@0
|
364 float **outs = plugin->getAudioOutputBuffers();
|
Chris@0
|
365
|
Chris@0
|
366 for (size_t c = 0; c < m_targetChannelCount && c < plugin->getAudioOutputCount(); ++c) {
|
Chris@0
|
367 #ifdef DEBUG_AUDIO_GENERATOR
|
Chris@0
|
368 std::cout << "mixModel [sparse]: adding " << m_pluginBlockSize << " samples from plugin output " << c << std::endl;
|
Chris@0
|
369 #endif
|
Chris@0
|
370
|
Chris@0
|
371 for (size_t j = 0; j < m_pluginBlockSize; ++j) {
|
Chris@0
|
372 buffer[c][i * m_pluginBlockSize + j] += gain * outs[c][j];
|
Chris@0
|
373 }
|
Chris@0
|
374 }
|
Chris@0
|
375 }
|
Chris@0
|
376
|
Chris@0
|
377 return got;
|
Chris@0
|
378 }
|
Chris@0
|
379
|