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