1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
* drumgizmo.cc
*
* Thu Sep 16 10:24:40 CEST 2010
* Copyright 2010 Bent Bisballe Nyeng
* deva@aasimon.org
****************************************************************************/
/*
* This file is part of DrumGizmo.
*
* DrumGizmo is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* DrumGizmo is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with DrumGizmo; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#include "drumgizmo.h"
#include <cmath>
#include <cstdio>
#include <cassert>
#include <cstring>
#include <mutex>
#include <event.h>
#include <audiotypes.h>
#include <config.h>
#include <hugin.hpp>
#include "drumkitparser.h"
#include "audioinputenginemidi.h"
DrumGizmo::DrumGizmo(Settings& settings,
AudioOutputEngine& o, AudioInputEngine& i)
: loader(settings, kit, i, rand, audio_cache)
, oe(o)
, ie(i)
, audio_cache(settings)
, input_processor(settings, kit, activeevents, rand)
, settings(settings)
, settings_getter(settings)
{
audio_cache.init(10000); // start thread
events.reserve(1000);
loader.init();
setSamplerate(44100.0f);
}
DrumGizmo::~DrumGizmo()
{
loader.deinit();
audio_cache.deinit(); // stop thread
}
bool DrumGizmo::init()
{
if(!ie.init(kit.instruments))
{
return false;
}
if(!oe.init(kit.channels))
{
return false;
}
return true;
}
void DrumGizmo::setFrameSize(size_t framesize)
{
settings.buffer_size.store(framesize);
if(this->framesize != framesize)
{
DEBUG(drumgizmo, "New framesize: %d\n", (int)framesize);
this->framesize = framesize;
// Update framesize in drumkitloader and cachemanager:
loader.setFrameSize(framesize);
audio_cache.setFrameSize(framesize);
}
}
void DrumGizmo::setFreeWheel(bool freewheel)
{
// Freewheel = true means that we are bouncing and therefore running faster
// than realtime.
this->freewheel = freewheel;
audio_cache.setAsyncMode(!freewheel);
}
void DrumGizmo::setRandomSeed(unsigned int seed)
{
rand.setSeed(seed);
}
bool DrumGizmo::run(size_t pos, sample_t *samples, size_t nsamples)
{
if(settings_getter.enable_resampling.hasChanged())
{
enable_resampling = settings_getter.enable_resampling.getValue();
}
if(settings_getter.drumkit_samplerate.hasChanged())
{
settings_getter.drumkit_samplerate.getValue(); // stage new value
setSamplerate(settings.samplerate.load());
}
setFrameSize(nsamples);
setFreeWheel(ie.isFreewheeling() && oe.isFreewheeling());
ie.pre();
oe.pre(nsamples); // Clears all output buffers
std::memset(samples, 0, nsamples * sizeof(sample_t));
//
// Read new events
//
ie.run(pos, nsamples, events);
double resample_ratio = ratio;
if(enable_resampling == false)
{
resample_ratio = 1.0;
}
bool active_events_left =
input_processor.process(events, pos, resample_ratio);
if(!active_events_left)
{
return false;
}
events.clear();
//
// Write audio
//
if(!enable_resampling || ratio == 1.0)
{
// No resampling needed
for(size_t c = 0; c < kit.channels.size(); ++c)
{
sample_t *buf = samples;
bool internal = false;
if(oe.getBuffer(c))
{
buf = oe.getBuffer(c);
internal = true;
}
if(buf)
{
std::memset(buf, 0, nsamples * sizeof(sample_t));
getSamples(c, pos, buf, nsamples);
if(!internal)
{
oe.run(c, samples, nsamples);
}
}
}
}
else
{
// Resampling needed
size_t kitpos = pos * ratio;
for(size_t c = 0; c < kit.channels.size(); ++c)
{
sample_t *buf = samples;
bool internal = false;
if(oe.getBuffer(c))
{
buf = oe.getBuffer(c);
internal = true;
}
zita[c].out_data = buf;
zita[c].out_count = nsamples;
if(zita[c].inp_count > 0)
{
// Samples left from last iteration, process that one first
zita[c].process();
}
std::memset(_resampler_input_buffer[c], 0,
sizeof(_resampler_input_buffer[c]));
zita[c].inp_data = _resampler_input_buffer[c];
std::size_t sample_count =
std::ceil((nsamples - (nsamples - zita[c].out_count)) * ratio);
getSamples(c, kitpos, zita[c].inp_data, sample_count);
zita[c].inp_count = sample_count;
zita[c].process();
if(!internal)
{
oe.run(c, samples, nsamples);
}
}
}
ie.post();
oe.post(nsamples);
pos += nsamples;
return true;
}
void DrumGizmo::renderSampleEvent(EventSample& evt, int pos, sample_t *s, std::size_t sz)
{
size_t n = 0; // default start point is 0.
// If we are not at offset 0 in current buffer:
if(evt.offset > (size_t)pos)
{
n = evt.offset - pos;
}
size_t end = sz; // default end point is the end of the buffer.
// Find the end point intra-buffer
if((evt.t + end - n) > evt.sample_size)
{
end = evt.sample_size - evt.t + n;
}
// This should not be necessary but make absolutely sure that we do
// not write over the end of the buffer.
if(end > sz)
{
end = sz;
}
size_t t = 0; // Internal buffer counter
repeat:
float scale = 1.0f;
for(; (n < end) && (t < (evt.buffer_size - evt.buffer_ptr)); ++n)
{
assert(n >= 0);
assert(n < sz);
assert(t >= 0);
assert(t < evt.buffer_size - evt.buffer_ptr);
if(evt.rampdownInProgress() && evt.rampdown_offset < (evt.t + t) &&
evt.rampdown_count > 0)
{
scale = std::min((float)evt.rampdown_count / evt.ramp_length, 1.f);
evt.rampdown_count--;
}
s[n] += evt.buffer[evt.buffer_ptr + t] * evt.scale * scale;
++t;
}
// Add internal buffer counter to "global" event counter.
evt.t += t;//evt.buffer_size;
evt.buffer_ptr += t;
if(n != sz && evt.t < evt.sample_size)
{
evt.buffer_size = sz - n;// Hint new size
// More samples needed for current buffer
evt.buffer = audio_cache.next(evt.cache_id, evt.buffer_size);
evt.buffer_ptr = 0;
t = 0;
goto repeat;
}
}
void DrumGizmo::getSamples(int ch, int pos, sample_t* s, size_t sz)
{
// Store local values of settings to ensure they don't change intra-iteration
const auto enable_bleed_control = settings.enable_bleed_control.load();
const auto master_bleed = settings.master_bleed.load();
std::vector< Event* > erase_list;
std::list< Event* >::iterator i = activeevents[ch].begin();
for(; i != activeevents[ch].end(); ++i)
{
bool removeevent = false;
Event* event = *i;
Event::type_t type = event->getType();
switch(type)
{
case Event::sample:
{
EventSample& evt = *static_cast<EventSample*>(event);
AudioFile& af = *evt.file;
if(!af.isLoaded() || !af.isValid() || (s == nullptr))
{
removeevent = true;
break;
}
if(evt.offset > (pos + sz))
{
// Don't handle event now. It is scheduled for a future iteration.
continue;
}
if(evt.cache_id == CACHE_NOID)
{
size_t initial_chunksize = (pos + sz) - evt.offset;
evt.buffer = audio_cache.open(af, initial_chunksize,
af.filechannel, evt.cache_id);
if((af.mainState() == main_state_t::is_not_main) &&
enable_bleed_control)
{
evt.scale = master_bleed;
}
evt.buffer_size = initial_chunksize;
evt.sample_size = af.size;
}
{
std::lock_guard<std::mutex> guard(af.mutex);
renderSampleEvent(evt, pos, s, sz);
if((evt.t >= evt.sample_size) || (evt.rampdown_count == 0))
{
removeevent = true;
}
if(evt.buffer_ptr >= evt.buffer_size && removeevent == false)
{
evt.buffer_size = sz;
evt.buffer = audio_cache.next(evt.cache_id, evt.buffer_size);
evt.buffer_ptr = 0;
}
if(removeevent)
{
audio_cache.close(evt.cache_id);
}
}
}
break;
}
if(removeevent)
{
erase_list.push_back(event); // don't delete until we are out of the loop.
continue;
}
}
for(auto& event : erase_list)
{
activeevents[ch].remove(event);
delete event;
}
}
void DrumGizmo::stop()
{
// engine.stop();
}
std::size_t DrumGizmo::getLatency() const
{
auto latency = input_processor.getLatency();
if(enable_resampling && ratio != 0.0)
{
// TODO:
latency += zita[0].inpsize(); // resampler latency
}
return latency;
}
float DrumGizmo::samplerate()
{
return settings.samplerate.load();
}
void DrumGizmo::setSamplerate(float samplerate)
{
DEBUG(dgeditor, "%s samplerate: %f\n", __PRETTY_FUNCTION__, samplerate);
settings.samplerate.store(samplerate);
// Notify input engine of the samplerate change.
ie.setSampleRate(samplerate);
auto input_fs = kit.getSamplerate();
auto output_fs = samplerate;
ratio = input_fs / output_fs;
settings.resamplig_recommended.store(ratio != 1.0);
for(int c = 0; c < MAX_NUM_CHANNELS; ++c)
{
zita[c].reset();
auto nchan = 1u; // mono
auto hlen = 72u; // 16 ≤ hlen ≤ 96
zita[c].setup(input_fs, output_fs, nchan, hlen);
// Prefill
auto null_size = zita[c].inpsize() - 1;// / 2 - 1;
zita[c].inp_data = nullptr;
zita[c].inp_count = null_size;
constexpr auto sz = 4096 * 16;
sample_t s[sz];
zita[c].out_data = s;
zita[c].out_count = sz;
zita[c].process();
}
}
|