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