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@0
|
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@0
|
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@4
|
206 got = dtvm->getValues
|
Chris@4
|
207 (c, startFrame - fadeIn/2, startFrame + frames + fadeOut/2,
|
Chris@4
|
208 channelBuffer);
|
Chris@4
|
209
|
Chris@4
|
210 for (size_t i = 0; i < fadeIn/2; ++i) {
|
Chris@4
|
211 float *back = buffer[c];
|
Chris@4
|
212 back -= fadeIn/2;
|
Chris@4
|
213 back[i] += (gain * channelBuffer[i] * i) / fadeIn;
|
Chris@4
|
214 }
|
Chris@4
|
215
|
Chris@4
|
216 for (size_t i = 0; i < frames + fadeOut/2; ++i) {
|
Chris@4
|
217 float mult = gain;
|
Chris@4
|
218 if (i < fadeIn/2) {
|
Chris@4
|
219 mult = (mult * i) / fadeIn;
|
Chris@4
|
220 }
|
Chris@4
|
221 if (i > frames - fadeOut/2) {
|
Chris@4
|
222 mult = (mult * ((frames + fadeOut/2) - i)) / fadeOut;
|
Chris@4
|
223 }
|
Chris@4
|
224 buffer[c][i] += mult * channelBuffer[i];
|
Chris@0
|
225 }
|
Chris@0
|
226 }
|
Chris@0
|
227
|
Chris@0
|
228 return got;
|
Chris@0
|
229 }
|
Chris@0
|
230
|
Chris@0
|
231 size_t
|
Chris@0
|
232 AudioGenerator::mixSparseOneDimensionalModel(SparseOneDimensionalModel *sodm,
|
Chris@0
|
233 size_t startFrame, size_t frames,
|
Chris@4
|
234 float **buffer, float gain, float pan,
|
Chris@4
|
235 size_t /* fadeIn */,
|
Chris@4
|
236 size_t /* fadeOut */)
|
Chris@0
|
237 {
|
Chris@0
|
238 RealTimePluginInstance *plugin = m_synthMap[sodm];
|
Chris@0
|
239 if (!plugin) return 0;
|
Chris@0
|
240
|
Chris@0
|
241 size_t latency = plugin->getLatency();
|
Chris@0
|
242 size_t blocks = frames / m_pluginBlockSize;
|
Chris@0
|
243
|
Chris@0
|
244 //!!! hang on -- the fact that the audio callback play source's
|
Chris@0
|
245 //buffer is a multiple of the plugin's buffer size doesn't mean
|
Chris@0
|
246 //that we always get called for a multiple of it here (because it
|
Chris@0
|
247 //also depends on the JACK block size). how should we ensure that
|
Chris@0
|
248 //all models write the same amount in to the mix, and that we
|
Chris@0
|
249 //always have a multiple of the plugin buffer size? I guess this
|
Chris@0
|
250 //class has to be queryable for the plugin buffer size & the
|
Chris@0
|
251 //callback play source has to use that as a multiple for all the
|
Chris@0
|
252 //calls to mixModel
|
Chris@0
|
253
|
Chris@0
|
254 size_t got = blocks * m_pluginBlockSize;
|
Chris@0
|
255
|
Chris@0
|
256 #ifdef DEBUG_AUDIO_GENERATOR
|
Chris@0
|
257 std::cout << "mixModel [sparse]: frames " << frames
|
Chris@0
|
258 << ", blocks " << blocks << std::endl;
|
Chris@0
|
259 #endif
|
Chris@0
|
260
|
Chris@0
|
261 snd_seq_event_t onEv;
|
Chris@0
|
262 onEv.type = SND_SEQ_EVENT_NOTEON;
|
Chris@0
|
263 onEv.data.note.channel = 0;
|
Chris@0
|
264 onEv.data.note.note = 64;
|
Chris@0
|
265 onEv.data.note.velocity = 127;
|
Chris@0
|
266
|
Chris@0
|
267 snd_seq_event_t offEv;
|
Chris@0
|
268 offEv.type = SND_SEQ_EVENT_NOTEOFF;
|
Chris@0
|
269 offEv.data.note.channel = 0;
|
Chris@0
|
270 offEv.data.note.velocity = 0;
|
Chris@0
|
271
|
Chris@0
|
272 NoteOffSet ¬eOffs = m_noteOffs[sodm];
|
Chris@0
|
273
|
Chris@0
|
274 for (size_t i = 0; i < blocks; ++i) {
|
Chris@0
|
275
|
Chris@0
|
276 size_t reqStart = startFrame + i * m_pluginBlockSize;
|
Chris@0
|
277
|
Chris@0
|
278 SparseOneDimensionalModel::PointList points =
|
Chris@0
|
279 sodm->getPoints(reqStart > 0 ? reqStart + latency : reqStart,
|
Chris@0
|
280 reqStart + latency + m_pluginBlockSize);
|
Chris@0
|
281
|
Chris@0
|
282 RealTime blockTime = RealTime::frame2RealTime
|
Chris@0
|
283 (startFrame + i * m_pluginBlockSize, m_sourceSampleRate);
|
Chris@0
|
284
|
Chris@0
|
285 for (SparseOneDimensionalModel::PointList::iterator pli =
|
Chris@0
|
286 points.begin(); pli != points.end(); ++pli) {
|
Chris@0
|
287
|
Chris@0
|
288 size_t pliFrame = pli->frame;
|
Chris@0
|
289 if (pliFrame >= latency) pliFrame -= latency;
|
Chris@0
|
290
|
Chris@0
|
291 while (noteOffs.begin() != noteOffs.end() &&
|
Chris@0
|
292 noteOffs.begin()->frame <= pliFrame) {
|
Chris@0
|
293
|
Chris@0
|
294 RealTime eventTime = RealTime::frame2RealTime
|
Chris@0
|
295 (noteOffs.begin()->frame, m_sourceSampleRate);
|
Chris@0
|
296
|
Chris@0
|
297 offEv.data.note.note = noteOffs.begin()->pitch;
|
Chris@0
|
298 plugin->sendEvent(eventTime, &offEv);
|
Chris@0
|
299 noteOffs.erase(noteOffs.begin());
|
Chris@0
|
300 }
|
Chris@0
|
301
|
Chris@0
|
302 RealTime eventTime = RealTime::frame2RealTime
|
Chris@0
|
303 (pliFrame, m_sourceSampleRate);
|
Chris@0
|
304
|
Chris@0
|
305 plugin->sendEvent(eventTime, &onEv);
|
Chris@0
|
306
|
Chris@0
|
307 #ifdef DEBUG_AUDIO_GENERATOR
|
Chris@0
|
308 std::cout << "mixModel [sparse]: point at frame " << pliFrame << ", block start " << (startFrame + i * m_pluginBlockSize) << ", resulting time " << eventTime << std::endl;
|
Chris@0
|
309 #endif
|
Chris@0
|
310
|
Chris@0
|
311 size_t duration = 7000; // frames [for now]
|
Chris@0
|
312 NoteOff noff;
|
Chris@0
|
313 noff.pitch = onEv.data.note.note;
|
Chris@0
|
314 noff.frame = pliFrame + duration;
|
Chris@0
|
315 noteOffs.insert(noff);
|
Chris@0
|
316 }
|
Chris@0
|
317
|
Chris@0
|
318 while (noteOffs.begin() != noteOffs.end() &&
|
Chris@0
|
319 noteOffs.begin()->frame <=
|
Chris@0
|
320 startFrame + i * m_pluginBlockSize + m_pluginBlockSize) {
|
Chris@0
|
321
|
Chris@0
|
322 RealTime eventTime = RealTime::frame2RealTime
|
Chris@0
|
323 (noteOffs.begin()->frame, m_sourceSampleRate);
|
Chris@0
|
324
|
Chris@0
|
325 offEv.data.note.note = noteOffs.begin()->pitch;
|
Chris@0
|
326 plugin->sendEvent(eventTime, &offEv);
|
Chris@0
|
327 noteOffs.erase(noteOffs.begin());
|
Chris@0
|
328 }
|
Chris@0
|
329
|
Chris@0
|
330 plugin->run(blockTime);
|
Chris@0
|
331 float **outs = plugin->getAudioOutputBuffers();
|
Chris@0
|
332
|
Chris@0
|
333 for (size_t c = 0; c < m_targetChannelCount && c < plugin->getAudioOutputCount(); ++c) {
|
Chris@0
|
334 #ifdef DEBUG_AUDIO_GENERATOR
|
Chris@0
|
335 std::cout << "mixModel [sparse]: adding " << m_pluginBlockSize << " samples from plugin output " << c << std::endl;
|
Chris@0
|
336 #endif
|
Chris@0
|
337
|
Chris@0
|
338 for (size_t j = 0; j < m_pluginBlockSize; ++j) {
|
Chris@0
|
339 buffer[c][i * m_pluginBlockSize + j] += gain * outs[c][j];
|
Chris@0
|
340 }
|
Chris@0
|
341 }
|
Chris@0
|
342 }
|
Chris@0
|
343
|
Chris@0
|
344 return got;
|
Chris@0
|
345 }
|
Chris@0
|
346
|