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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
* drumgizmo_lv2.cc
*
* Wed Mar 2 17:31:32 CET 2016
* Copyright 2016 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 2 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_plugin.h"
#include <cstring>
#include <algorithm>
#include <string>
#include <midievent.h>
#include <hugin.hpp>
#include "configparser.h"
#include "nolocale.h"
#ifdef LV2
// Entry point for lv2 plugin instantiation.
PluginLV2* createEffectInstance()
{
return new DrumGizmoPlugin();
}
#endif
#ifdef VST
// Entry point for vst plugin instantiation.
AudioEffect* createEffectInstance(audioMasterCallback audioMaster)
{
return new DrumGizmoPlugin(audioMaster);
}
DrumGizmoPlugin::DrumGizmoPlugin(audioMasterCallback audioMaster)
: PluginVST(audioMaster),
#else
DrumGizmoPlugin::DrumGizmoPlugin()
:
#endif
config_string_io(settings)
{
init();
drumgizmo = std::make_shared<DrumGizmo>(settings, output, input);
resizeWindow(width, height);
drumgizmo->setFreeWheel(true);
drumgizmo->setSamplerate(44100);
drumgizmo->setFrameSize(2048);
}
void DrumGizmoPlugin::onSamplerateChange(float samplerate)
{
drumgizmo->setSamplerate(samplerate);
}
void DrumGizmoPlugin::onFramesizeChange(size_t framesize)
{
drumgizmo->setFrameSize(framesize);
}
void DrumGizmoPlugin::onActiveChange(bool active)
{
}
std::string DrumGizmoPlugin::onStateSave()
{
return config_string_io.get();
}
void DrumGizmoPlugin::onStateRestore(const std::string& config)
{
config_string_io.set(config);
}
size_t DrumGizmoPlugin::getNumberOfMidiInputs()
{
return 1;
}
size_t DrumGizmoPlugin::getNumberOfMidiOutputs()
{
return 0;
}
size_t DrumGizmoPlugin::getNumberOfAudioInputs()
{
return 0;
}
size_t DrumGizmoPlugin::getNumberOfAudioOutputs()
{
return 16;
}
std::string DrumGizmoPlugin::getId()
{
return "DrumGizmo";
}
std::string DrumGizmoPlugin::getEffectName()
{
return "DrumGizmo";
}
std::string DrumGizmoPlugin::getVendorString()
{
return "DrumGizmo Team";
}
std::string DrumGizmoPlugin::getProductString()
{
return "DrumGizmo";
}
PluginCategory DrumGizmoPlugin::getPluginCategory()
{
return PluginCategory::Synth;
}
static float g_samples[16* 4096];
void DrumGizmoPlugin::process(size_t pos,
const std::vector<MidiEvent>& input_events,
std::vector<MidiEvent>& output_events,
const std::vector<const float*>& input_samples,
const std::vector<float*>& output_samples,
size_t count)
{
setLatency(drumgizmo->getLatency());
this->input_events = &input_events;
this->output_samples = &output_samples;
drumgizmo->run(pos, g_samples, count);
this->input_events = nullptr;
this->output_samples = nullptr;
}
bool DrumGizmoPlugin::hasInlineGUI()
{
return true;
}
class InlinePixelBufferAlpha
: public GUI::PixelBufferAlpha
{
public:
InlinePixelBufferAlpha(Plugin::InlineDrawContext& context)
{
buf = context.data;
width = context.width;
height = context.height;
x = 0;
y = 0;
}
};
class InlineCanvas
: public GUI::Canvas
{
public:
InlineCanvas(Plugin::InlineDrawContext& context)
: pixbuf(context)
{
}
// From Canvas:
GUI::PixelBufferAlpha& GetPixelBuffer() override
{
return pixbuf;
}
private:
InlinePixelBufferAlpha pixbuf;
};
void DrumGizmoPlugin::onInlineRedraw(std::size_t width,
std::size_t max_height,
InlineDrawContext& context)
{
std::size_t bar_height = bar_red.height();
std::size_t image_height = ((double)width / inline_display_image.width()) * inline_display_image.height();
bool show_bar{false};
bool show_image{false};
std::size_t height = 0;
if(bar_height <= max_height)
{
show_bar = true;
height += bar_height;
}
if(bar_height + image_height <= max_height)
{
show_image = true;
height += image_height;
}
// They have to be called seperately to avoid lazy evaluation
bool nof_changed = settingsGetter.number_of_files.hasChanged();
bool nofl_changed = settingsGetter.number_of_files_loaded.hasChanged();
bool dls_changed = settingsGetter.drumkit_load_status.hasChanged();
bool context_needs_update =
!context.data || context.width != width || context.height != height;
bool bar_needs_update =
nof_changed || nofl_changed || dls_changed || context_needs_update;
bool image_needs_update = inline_image_first_draw || context_needs_update;
// TODO: settingsGetter.inline_image_filename.hasChanged();
bool something_needs_update =
context_needs_update || bar_needs_update || image_needs_update;
if (something_needs_update)
{
context.width = width;
context.height = height;
assert(context.width * context.height <= sizeof(inlineDisplayBuffer));
context.data = (unsigned char*)inlineDisplayBuffer;
InlineCanvas canvas(context);
GUI::Painter painter(canvas);
if(show_bar && bar_needs_update)
{
box.setSize(context.width, bar_height);
painter.drawImage(0, height - bar_height, box);
double progress =
(double)settingsGetter.number_of_files_loaded.getValue() /
(double)settingsGetter.number_of_files.getValue();
int brd = 4;
int val = (width - (2 * brd)) * progress;
switch(settingsGetter.drumkit_load_status.getValue())
{
case LoadStatus::Error:
bar_red.setSize(val, bar_height);
painter.drawImage(brd, height - bar_height, bar_red);
break;
case LoadStatus::Done:
bar_green.setSize(val, bar_height);
painter.drawImage(brd, height - bar_height, bar_green);
break;
case LoadStatus::Loading:
case LoadStatus::Idle:
bar_blue.setSize(val, bar_height);
painter.drawImage(brd, height - bar_height, bar_blue);
break;
}
}
if(show_image && image_needs_update)
{
// TODO: load new image and remove the bool
inline_image_first_draw = false;
painter.setColour(.5);
painter.drawFilledRectangle(0, 0, width, image_height);
painter.drawImageStretched(0, 0, inline_display_image, width, image_height);
}
// Convert to correct pixel format
for(std::size_t i = 0; i < context.height * context.width; ++i)
{
std::uint32_t pixel = inlineDisplayBuffer[i];
unsigned char* p = (unsigned char*)&pixel;
inlineDisplayBuffer[i] = pgzRGBA(p[0], p[1], p[2], p[3]);
}
}
}
bool DrumGizmoPlugin::hasGUI()
{
return true;
}
void* DrumGizmoPlugin::createWindow(void* parent)
{
plugin_gui = std::make_shared<GUI::MainWindow>(settings, parent);
resizeWindow(width, height);
onShowWindow();
return plugin_gui->getNativeWindowHandle();
}
void DrumGizmoPlugin::onDestroyWindow()
{
plugin_gui = nullptr;
}
void DrumGizmoPlugin::onShowWindow()
{
plugin_gui->show();
}
void DrumGizmoPlugin::onHideWindow()
{
plugin_gui->hide();
}
void DrumGizmoPlugin::onIdle()
{
plugin_gui->processEvents();
}
void DrumGizmoPlugin::closeWindow()
{
}
//
// Input Engine
//
DrumGizmoPlugin::Input::Input(DrumGizmoPlugin& plugin)
: plugin(plugin)
{
}
bool DrumGizmoPlugin::Input::init(const Instruments &instruments)
{
this->instruments = &instruments;
return true;
}
void DrumGizmoPlugin::Input::setParm(const std::string& parm, const std::string& value)
{
}
bool DrumGizmoPlugin::Input::start()
{
return true;
}
void DrumGizmoPlugin::Input::stop()
{
}
void DrumGizmoPlugin::Input::pre()
{
}
void DrumGizmoPlugin::Input::run(size_t pos, size_t len, std::vector<event_t>& events)
{
assert(events.empty());
assert(plugin.input_events);
events.reserve(plugin.input_events->size());
for(auto& event : *plugin.input_events)
{
if(event.type != MidiEventType::NoteOn)
{
continue;
}
int i = mmap.lookup(event.key);
if(event.velocity && (i != -1))
{
events.push_back({EventType::OnSet, (size_t)i, (size_t)event.getTime(), event.velocity / 127.0f});
}
}
}
void DrumGizmoPlugin::Input::post()
{
}
bool DrumGizmoPlugin::Input::isFreewheeling() const
{
return plugin.getFreeWheel();
}
//
// Output Engine
//
DrumGizmoPlugin::Output::Output(DrumGizmoPlugin& plugin)
: plugin(plugin)
{
}
bool DrumGizmoPlugin::Output::init(const Channels& channels)
{
return true;
}
void DrumGizmoPlugin::Output::setParm(const std::string& parm, const std::string& value)
{
}
bool DrumGizmoPlugin::Output::start()
{
return true;
}
void DrumGizmoPlugin::Output::stop()
{
}
void DrumGizmoPlugin::Output::pre(size_t nsamples)
{
// Clear all channels
for(auto& channel : *plugin.output_samples)
{
if(channel)
{
std::memset(channel, 0, nsamples * sizeof(sample_t));
}
}
}
void DrumGizmoPlugin::Output::run(int ch, sample_t* samples, size_t nsamples)
{
assert(plugin.output_samples);
assert(sizeof(float) == sizeof(sample_t));
if((std::size_t)ch >= plugin.output_samples->size())
{
return;
}
if((*plugin.output_samples)[ch] == nullptr)
{
// Port not connected.
return;
}
// We are not running directly on the internal buffer: do a copy.
if((*plugin.output_samples)[ch] != samples)
{
memcpy((*plugin.output_samples)[ch], samples, nsamples * sizeof(sample_t));
}
}
void DrumGizmoPlugin::Output::post(size_t nsamples)
{
}
sample_t* DrumGizmoPlugin::Output::getBuffer(int ch) const
{
assert(plugin.output_samples);
if((std::size_t)ch >= plugin.output_samples->size())
{
return nullptr;
}
return (*plugin.output_samples)[ch];
}
size_t DrumGizmoPlugin::Output::getBufferSize() const
{
return plugin.getFramesize();
}
std::size_t DrumGizmoPlugin::Output::getSamplerate() const
{
return plugin.drumgizmo->samplerate();
}
bool DrumGizmoPlugin::Output::isFreewheeling() const
{
return plugin.getFreeWheel();
}
//
// ConfigStringIO
//
// anonymous namespace for helper furnctions of ConfigStringIO
namespace
{
std::string float2str(float a)
{
char buf[256];
snprintf_nol(buf, sizeof(buf) - 1, "%f", a);
return buf;
}
std::string bool2str(bool a)
{
return a?"true":"false";
}
std::string int2str(int a)
{
char buf[256];
snprintf(buf, sizeof(buf) - 1, "%d", a);
return buf;
}
float str2float(std::string a)
{
if(a == "")
{
return 0.0;
}
return atof_nol(a.c_str());
}
int str2int(std::string a)
{
try
{
return std::stoi(a);
}
catch(...)
{
return 0;
}
}
} // end anonymous namespace
DrumGizmoPlugin::ConfigStringIO::ConfigStringIO(Settings& settings)
: settings(settings)
{
}
std::string DrumGizmoPlugin::ConfigStringIO::get()
{
return
"<config>\n"
" <value name=\"drumkitfile\">" + settings.drumkit_file.load() + "</value>\n"
" <value name=\"midimapfile\">" + settings.midimap_file.load() + "</value>\n"
" <value name=\"enable_velocity_modifier\">" +
bool2str(settings.enable_velocity_modifier.load()) + "</value>\n"
" <value name=\"velocity_modifier_falloff\">" +
float2str(settings.velocity_modifier_falloff.load()) + "</value>\n"
" <value name=\"velocity_modifier_weight\">" +
float2str(settings.velocity_modifier_weight.load()) + "</value>\n"
" <value name=\"velocity_stddev\">" +
float2str(settings.velocity_stddev.load()) + "</value>\n"
" <value name=\"enable_velocity_randomiser\">" +
bool2str(settings.enable_velocity_randomiser.load()) + "</value>\n"
" <value name=\"velocity_randomiser_weight\">" +
float2str(settings.velocity_randomiser_weight.load()) + "</value>\n"
" <value name=\"enable_resampling\">" +
bool2str(settings.enable_resampling.load()) + "</value>\n"
" <value name=\"disk_cache_upper_limit\">" +
int2str(settings.disk_cache_upper_limit.load()) + "</value>\n"
" <value name=\"disk_cache_chunk_size\">" +
int2str(settings.disk_cache_chunk_size.load()) + "</value>\n"
" <value name=\"disk_cache_enable\">" +
bool2str(settings.disk_cache_enable.load()) + "</value>\n"
" <value name=\"enable_bleed_control\">" +
bool2str(settings.enable_bleed_control.load()) + "</value>\n"
" <value name=\"master_bleed\">" +
float2str(settings.master_bleed.load()) + "</value>\n"
" <value name=\"enable_latency_modifier\">" +
bool2str(settings.enable_latency_modifier.load()) + "</value>\n"
// Do not store/reload this value
//" <value name=\"latency_max\">" +
//int2str(settings.latency_max.load()) + "</value>\n"
" <value name=\"latency_laid_back_ms\">" +
float2str(settings.latency_laid_back_ms.load()) + "</value>\n"
" <value name=\"latency_stddev\">" +
float2str(settings.latency_stddev.load()) + "</value>\n"
" <value name=\"latency_regain\">" +
float2str(settings.latency_regain.load()) + "</value>\n"
"</config>";
}
bool DrumGizmoPlugin::ConfigStringIO::set(std::string config_string)
{
DEBUG(config, "Load config: %s\n", config_string.c_str());
ConfigParser p;
if(!p.parseString(config_string))
{
ERR(config, "Config parse error.\n");
return false;
}
if(p.value("enable_velocity_modifier") != "")
{
settings.enable_velocity_modifier.store(p.value("enable_velocity_modifier") == "true");
}
if(p.value("velocity_modifier_falloff") != "")
{
settings.velocity_modifier_falloff.store(str2float(p.value("velocity_modifier_falloff")));
}
if(p.value("velocity_modifier_weight") != "")
{
settings.velocity_modifier_weight.store(str2float(p.value("velocity_modifier_weight")));
}
if(p.value("velocity_stddev") != "")
{
settings.velocity_stddev.store(str2float(p.value("velocity_stddev")));
}
if(p.value("enable_velocity_randomiser") != "")
{
settings.enable_velocity_randomiser.store(p.value("enable_velocity_randomiser") == "true");
}
if(p.value("velocity_randomiser_weight") != "")
{
settings.velocity_randomiser_weight.store(str2float(p.value("velocity_randomiser_weight")));
}
if(p.value("enable_resampling") != "")
{
settings.enable_resampling.store(p.value("enable_resampling") == "true");
}
if(p.value("disk_cache_upper_limit") != "")
{
settings.disk_cache_upper_limit.store(str2int(p.value("disk_cache_upper_limit")));
}
if(p.value("disk_cache_chunk_size") != "")
{
settings.disk_cache_chunk_size.store(str2int(p.value("disk_cache_chunk_size")));
}
if(p.value("disk_cache_enable") != "")
{
settings.disk_cache_enable.store(p.value("disk_cache_enable") == "true");
}
if(p.value("enable_bleed_control") != "")
{
settings.enable_bleed_control.store(p.value("enable_bleed_control") == "true");
}
if(p.value("master_bleed") != "")
{
settings.master_bleed.store(str2float(p.value("master_bleed")));
}
if(p.value("enable_latency_modifier") != "")
{
settings.enable_latency_modifier.store(p.value("enable_latency_modifier") == "true");
}
// Do not store/reload this value
//if(p.value("latency_max") != "")
//{
// settings.latency_max.store(str2int(p.value("latency_max")));
//}
if(p.value("latency_laid_back_ms") != "")
{
settings.latency_laid_back_ms.store(str2float(p.value("latency_laid_back_ms")));
}
if(p.value("latency_stddev") != "")
{
settings.latency_stddev.store(str2float(p.value("latency_stddev")));
}
if(p.value("latency_regain") != "")
{
settings.latency_regain.store(str2float(p.value("latency_regain")));
}
std::string newkit = p.value("drumkitfile");
if(newkit != "")
{
settings.drumkit_file.store(newkit);
}
std::string newmidimap = p.value("midimapfile");
if(newmidimap != "")
{
settings.midimap_file.store(newmidimap);
}
return true;
}
|