-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPresetControlComponent.h
494 lines (428 loc) · 20.9 KB
/
PresetControlComponent.h
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
/*
==============================================================================
PresetControlComponent.h
Created: 24 May 2019 7:02:31pm
Author: Frederic Font Corbera
==============================================================================
*/
#pragma once
#include "../JuceLibraryCode/JuceHeader.h"
#include "PluginProcessor.h"
#include "KIJIMIInterface.h"
#include "BabuFrikBaseLookAndFeel.h"
#include "defines.h"
class NumericEditorLabel: public Label
{
public:
virtual TextEditor* createEditorComponent() override
{
// Sublass Label method to create an editor that only allows numeric characters and sets a max length
TextEditor* const ed = Label::createEditorComponent();
ed->setInputRestrictions(3, "0123456789");
ed->setJustification(Justification::centred);
ed->setColour(TextEditor::focusedOutlineColourId, Colours::transparentBlack);
return ed;
}
};
class PresetControlComponent: public Component,
public Button::Listener,
public ActionListener
{
public:
PresetControlComponent ()
{
currentPresetSlidersOutOfSync = false;
loadFileButton.addListener (this);
loadFileButton.setButtonText("Load bank...");
addAndMakeVisible (loadFileButton);
loadedFileLabel.setJustificationType (Justification::centred);
loadedFileLabel.setText("-", dontSendNotification);
addAndMakeVisible (loadedFileLabel);
babuFrikBaseLookAndFeelSmallerFont.defaultFontSize = 11.0;
nextPresetButton.addListener (this);
nextPresetButton.setButtonText(">");
nextPresetButton.setEnabled(false);
nextPresetButton.setLookAndFeel(&babuFrikBaseLookAndFeelSmallerFont);
addAndMakeVisible (nextPresetButton);
previousPresetButton.addListener (this);
previousPresetButton.setButtonText("<");
previousPresetButton.setEnabled(false);
previousPresetButton.setLookAndFeel(&babuFrikBaseLookAndFeelSmallerFont);
addAndMakeVisible (previousPresetButton);
presetNameLabel.setJustificationType (Justification::centred);
presetNameLabel.setText(String(PRESET_NAME_DEFAULT_TEXT), dontSendNotification);
presetNameLabel.setEnabled(false);
presetNameLabel.setEditable (true);
presetNameLabel.onTextChange = [this] { setPresetFromName(); };
addAndMakeVisible (presetNameLabel);
saveToCurrentBankLocationButton.addListener (this);
saveToCurrentBankLocationButton.setEnabled(false);
saveToCurrentBankLocationButton.setButtonText("Save to bank location");
addAndMakeVisible (saveToCurrentBankLocationButton);
saveBankFileButton.addListener (this);
saveBankFileButton.setEnabled(false);
saveBankFileButton.setButtonText("Save bank...");
addAndMakeVisible (saveBankFileButton);
}
~PresetControlComponent ()
{
processor->removeActionListener(this); // Stop receivng messages from processor
}
void initialize (BabuFrikAudioProcessor* p)
{
// Set processor object
processor = p;
// Set up listeners
processor->addActionListener(this); // Receive messages from processor
// Set initial state of mmebers by getting data from processor
setStateFromProcessor();
// Also intitialize the bank importer and dumper so it has reference to processor
kijimiBankImporterThreadModal.initialize(processor);
kijimiBankDumperThreadModal.initialize(processor);
}
void setStateFromProcessor () {
// Load preset (plus out of sync indicator) name and bank filename (if data loaded)
loadedFileLabel.setText(processor->kijimiInterface->getPresetBankLoadedFilename(), dontSendNotification);
setPresetNameLabel ();
if ((processor->currentPresetOutOfSyncWithSliders) && (presetNameLabel.getText() != String(PRESET_NAME_DEFAULT_TEXT))){
presetNameLabel.setText(presetNameLabel.getText() + String(PRESET_NAME_MODIFIED_TEXT), dontSendNotification);
//saveToCurrentBankLocationButton.setEnabled(true); // Enable save preset button as it is out of sync
}
if (processor->kijimiInterface->hasPresetsDataLoaded()){
enableBankTransportButtons();
}
}
void resized () override
{
float unitMargin = getWidth() * 10/800;
float fileNameLabelWidth = getWidth() * 120/800;
float presetNameLabelWidth = getWidth() * 45/800;
float defaultButtonWidth = getWidth() * 90/800;
float nextPrevButtonWidth = getWidth() * 22/800;
float saveBankLocationButtonWidth = getWidth() * 130/800;
float saveBankToFileButtonWidth = getWidth() * 90/800;
loadFileButton.setBounds (0, 0, defaultButtonWidth, getHeight());
loadedFileLabel.setBounds (defaultButtonWidth + unitMargin, 0, fileNameLabelWidth, getHeight());
previousPresetButton.setBounds (defaultButtonWidth + fileNameLabelWidth + 2 * unitMargin, 0, nextPrevButtonWidth, getHeight());
presetNameLabel.setBounds (defaultButtonWidth + fileNameLabelWidth + nextPrevButtonWidth + 3 * unitMargin, 0, presetNameLabelWidth, getHeight());
nextPresetButton.setBounds (defaultButtonWidth + fileNameLabelWidth + nextPrevButtonWidth + presetNameLabelWidth + 4 * unitMargin, 0, nextPrevButtonWidth, getHeight());
saveToCurrentBankLocationButton.setBounds (defaultButtonWidth + fileNameLabelWidth + 2 * nextPrevButtonWidth + presetNameLabelWidth + 5 * unitMargin, 0, saveBankLocationButtonWidth, getHeight());
saveBankFileButton.setBounds (defaultButtonWidth + fileNameLabelWidth + 2 * nextPrevButtonWidth + presetNameLabelWidth + saveBankLocationButtonWidth + 6 * unitMargin, 0, saveBankToFileButtonWidth, getHeight());
}
void loadBankFile()
{
juce::String patternFilter = "*.syx";
#if JUCE_IOS
patternFilter = "";
#endif
FileChooser fileChooser ("Please select a KIJIMI bank file to load...",
processor->getDirectoryForFileSaveLoad(), patternFilter);
if (fileChooser.browseForFileToOpen())
{
File bankFile (fileChooser.getResult());
processor->setLastUserDirectoryForFileSaveLoad(bankFile);
processor->loadBankFile(&bankFile);
processor->computeTimbreSpace();
}
}
// Modal with progress bar for importing bank from KIJIMI
class KIJIMIBankImporterThreadModal : public ThreadWithProgressWindow
{
public:
KIJIMIBankImporterThreadModal() : ThreadWithProgressWindow ("Loading bank from KIJIMI...", true, true)
{
}
void initialize (BabuFrikAudioProcessor* p)
{
// Set processor object
processor = p;
}
int bankNumber = 0;
bool succeeded = false;
BabuFrikAudioProcessor* processor;
void run()
{
processor->receivedPresetsBytes.clear();
int nPresets = 128;
int nCancelled = 0;
for (int currentPreset = 0; currentPreset < nPresets; currentPreset++)
{
if (threadShouldExit())
break;
if (nCancelled > 5)
break;
// this will update the progress bar on the dialog box
setProgress ((currentPreset + 1) / (double) nPresets);
// Get one preset and wait until received
int64 timeRequestedPreset = Time::getCurrentTime().toMilliseconds();
processor->lastReceivedKIJIMIPresetBytes = {0};
processor->waitingToReceiveKIJIMIPresetBytes = true;
processor->requestGetPresetFromKIJIMI(bankNumber, currentPreset);
bool cancelled = false;
while (processor->waitingToReceiveKIJIMIPresetBytes == true){
sleep(50);
if (Time::getCurrentTime().toMilliseconds() - timeRequestedPreset > 300){
cancelled = true;
nCancelled += 1;
break; // if waiting too much for receiving a single preset, cancel this one
}
}
if (!cancelled){
processor->receivedPresetsBytes.push_back(processor->lastReceivedKIJIMIPresetBytes);
}
}
if (nCancelled == 0){
succeeded = true;
}
}
};
KIJIMIBankImporterThreadModal kijimiBankImporterThreadModal;
void loadBankFromKijimi(int bankNumber, const String& bankName)
{
kijimiBankImporterThreadModal.bankNumber = bankNumber;
if (kijimiBankImporterThreadModal.runThread())
{
if (kijimiBankImporterThreadModal.succeeded){
// Importing succeeded, compute timbre space
processor->loadPresetBankFromPresetsBytes(processor->receivedPresetsBytes, bankName);
processor->computeTimbreSpace();
kijimiBankImporterThreadModal.succeeded = false;
} else {
// Importing failed, show message
AlertWindow w ("Oups, there was a problem loading the bank from KIJIMI...", "", AlertWindow::NoIcon);
w.addButton ("Ok", 0, KeyPress (KeyPress::returnKey, 0, 0));
w.runModalLoop();
}
}
else
{
// If user canceled, do nothing
}
}
// Modal with progress bar for dumping a bank to KIJIMI
class KIJIMIBankDumperThreadModal : public ThreadWithProgressWindow
{
public:
KIJIMIBankDumperThreadModal() : ThreadWithProgressWindow ("Dumping bank to KIJIMI...", true, true)
{
}
void initialize (BabuFrikAudioProcessor* p)
{
// Set processor object
processor = p;
}
bool succeeded = false;
BabuFrikAudioProcessor* processor;
void run()
{
int nPresets = processor->kijimiInterface->getNumLoadedPresets();
for (int currentPreset = 0; currentPreset < nPresets; currentPreset++)
{
if (threadShouldExit())
break;
// this will update the progress bar on the dialog box
setProgress ((currentPreset + 1) / (double) nPresets);
// Get bytes for the current preset
KIJIMIPresetBytes presetBytes = processor->kijimiInterface->getLoadedPresetBytesAtIndex(currentPreset);
// Transform bytes to midi message and send it to KIJIMI
if (processor->midiInput.get() != nullptr){
std::array<uint8, 263> sysexdata;
sysexdata[0] = SYSEX_BC_ID_0;
sysexdata[1] = SYSEX_BC_ID_1;
sysexdata[2] = SYSEX_BC_ID_2;
sysexdata[3] = SYSEX_KIJIMI_ID;
sysexdata[4] = SYSEX_TRANSFER_PATCH_COMMAND;
sysexdata[5] = 0x00; // bank number
sysexdata[6] = currentPreset;
// Now copy all preset bytes to the output message
// Note that controls data should start at position 7 of the "sysexdata" (without counting start/end bytes),
// and controls data starts at position 5 in "presetBytes", therefore the index needs to be set accordingly
// to i-2 (so we put presetBytes[5] to sysexdata[0]...)
for (int i=7; i<263; i++){
sysexdata[i] = presetBytes[i-2];
}
MidiMessage msg = MidiMessage::createSysExMessage(&sysexdata, 263);
processor->midiOutput.get()->sendMessageNow(msg);
}
// Sleep to give KIJIMI time to process it
sleep(150);
}
succeeded = true;
}
};
KIJIMIBankDumperThreadModal kijimiBankDumperThreadModal;
void sendBankToKijimi()
{
if (kijimiBankDumperThreadModal.runThread())
{
if (kijimiBankDumperThreadModal.succeeded){
// Importing succeeded, do nothing
kijimiBankDumperThreadModal.succeeded = false; // reset to false
} else {
// Importing failed, show message
AlertWindow w ("Oups, there was a problem dumping the bank to KIJIMI...", "", AlertWindow::NoIcon);
w.addButton ("Ok", 0, KeyPress (KeyPress::returnKey, 0, 0));
w.runModalLoop();
}
}
else
{
// If user canceled, do nothing
}
}
void enableBankTransportButtons() {
previousPresetButton.setEnabled(true);
nextPresetButton.setEnabled(true);
presetNameLabel.setEnabled(true);
saveBankFileButton.setEnabled(true);
saveToCurrentBankLocationButton.setEnabled(true);
}
void buttonClicked (Button* button) override
{
if (button == &nextPresetButton)
{
int64 currentTime = Time::getCurrentTime().toMilliseconds();
if ((currentTime - lastTimeNextButtonPressed) > MIN_TIME_BETWEEN_NEXT_PREV_RAND_PATCH_BUTTON_PRESSED){
processor->nextPreset();
lastTimeNextButtonPressed = currentTime;
}
}
else if (button == &previousPresetButton)
{
int64 currentTime = Time::getCurrentTime().toMilliseconds();
if ((currentTime - lastTimePreviousButtonPressed) > MIN_TIME_BETWEEN_NEXT_PREV_RAND_PATCH_BUTTON_PRESSED){
processor->previousPreset();
lastTimePreviousButtonPressed = currentTime;
}
}
else if (button == &loadFileButton)
{
PopupMenu loadBankSubmenu;
loadBankSubmenu.addItem (MENU_OPTION_ID_LOAD_BANK_FROM_FILE, "From bank file");
PopupMenu bankOptionsSubmenu;
bankOptionsSubmenu.addItem (1, "Bank 1");
bankOptionsSubmenu.addItem (2, "Bank 2");
bankOptionsSubmenu.addItem (4, "Bank 3");
bankOptionsSubmenu.addItem (5, "Bank 4");
bankOptionsSubmenu.addItem (6, "Bank 5");
bankOptionsSubmenu.addItem (7, "Bank 6");
bankOptionsSubmenu.addItem (8, "Bank 7");
bankOptionsSubmenu.addItem (9, "Bank 8");
bankOptionsSubmenu.addItem (10, "Bank 9");
bankOptionsSubmenu.addItem (3, "MJ");
bankOptionsSubmenu.addItem (11, "RD");
loadBankSubmenu.addSubMenu ("From KIJIMI", bankOptionsSubmenu);
int selectedActionID = loadBankSubmenu.showAt(button);
if (selectedActionID == MENU_OPTION_ID_LOAD_BANK_FROM_FILE){
loadBankFile();
} else if ((selectedActionID >= 1) && (selectedActionID <= 11)){
int selectedBank = selectedActionID - 1;
StringArray bankNames = {
"Bank 1",
"Bank 2",
"MJ",
"Bank 3",
"Bank 4",
"Bank 5",
"Bank 6",
"Bank 7",
"Bank 8",
"Bank 9",
"RD",
};
loadBankFromKijimi(selectedBank, bankNames[selectedBank]);
}
} else if (button == &saveToCurrentBankLocationButton)
{
AlertWindow w ("Please choose the location where to save the patch", "", AlertWindow::NoIcon);
w.addTextBlock ("NOTE: this will save the patch in the selected location of the bank loaded in Babu Frik, but won't save the preset in KIJIMI itself nor send any information to it.");
w.addTextEditor ("bankLocation", "", "");
w.getTextEditor ("bankLocation")->setInputRestrictions(3, "0123456789"); // Make it numbers only
w.addButton ("Cancel", 0, KeyPress (KeyPress::escapeKey, 0, 0));
w.addButton ("Save", 1, KeyPress (KeyPress::returnKey, 0, 0));
if (w.runModalLoop() == 1) // use clicked "ok"
{
// this is the text they entered..
int location = w.getTextEditorContents ("bankLocation").getIntValue() - 1;
processor->savePresetToBankLocation(location);
}
} else if (button == &saveBankFileButton)
{
PopupMenu saveBankSubmenu;
saveBankSubmenu.addItem (MENU_OPTION_ID_SAVE_BANK_TO_FILE, "To bank file");
saveBankSubmenu.addItem (MENU_OPTION_ID_SEND_BANK_TO_KIJIMI, "Dump to KIJIMI current bank");
int selectedActionID = saveBankSubmenu.showAt(button);
if (selectedActionID == MENU_OPTION_ID_SAVE_BANK_TO_FILE){
processor->saveBankFile();
} else if (selectedActionID == MENU_OPTION_ID_SEND_BANK_TO_KIJIMI) {
sendBankToKijimi();
}
}
}
void setPresetFromName ()
{
// Subtract 1 as loadPresetAtIndex is 0-indexed but user inputs 1-indexed
int presetIdx = presetNameLabel.getText().getIntValue() - 1;
if (presetIdx > -1){
// Valid number entered
processor->loadPresetAtIndex(presetIdx);
} else {
// Re-set it to the preset already had
processor->loadPresetAtIndex(processor->currentPreset);
}
}
void setPresetNameLabel () {
if (processor->currentPreset > -1){
// Add 1 as label should show preset numbers 1-indexed but currentPreset stores them 0-indexed
presetNameLabel.setText(String(PRESET_NAME_PREFIX) + (String)(processor->currentPreset + 1), dontSendNotification);
} else {
presetNameLabel.setText(String(PRESET_NAME_DEFAULT_TEXT), dontSendNotification);
}
}
void actionListenerCallback (const String &message) override
{
if (message.startsWith(String(ACTION_SET_CURRENT_PRESET_NAME))){
setPresetNameLabel ();
} else if (message.startsWith(String(ACTION_SET_CURRENT_PRESET_NAME_OUT_OF_SYNC))){
if (!currentPresetSlidersOutOfSync) {
currentPresetSlidersOutOfSync = true;
if ((presetNameLabel.getText() != String(PRESET_NAME_DEFAULT_TEXT)) && (!presetNameLabel.getText().endsWith(String(PRESET_NAME_MODIFIED_TEXT)))){
presetNameLabel.setText(presetNameLabel.getText() + String(PRESET_NAME_MODIFIED_TEXT), dontSendNotification);
//saveToCurrentBankLocationButton.setEnabled(true); // Enable save preset button as it is out of sync
}
}
} else if (message.startsWith(String(ACTION_SET_CURRENT_PRESET_NAME_IN_SYNC))){
setPresetNameLabel ();
if (currentPresetSlidersOutOfSync) {
currentPresetSlidersOutOfSync = false;
//saveToCurrentBankLocationButton.setEnabled(false); // Disable save preset button as it is out of sync
if ((presetNameLabel.getText() != String(PRESET_NAME_DEFAULT_TEXT)) && (presetNameLabel.getText().endsWith(String(PRESET_NAME_MODIFIED_TEXT)))){
String presetNameLabelText = presetNameLabel.getText();
presetNameLabel.setText(presetNameLabelText.substring(presetNameLabelText.length() - 1), dontSendNotification);
}
}
}
else if (message.startsWith(String(ACTION_BANK_FILE_LOADED))){
loadedFileLabel.setText(processor->kijimiInterface->getPresetBankLoadedFilename() , dontSendNotification);
presetNameLabel.setText(String(PRESET_NAME_DEFAULT_TEXT), dontSendNotification);
enableBankTransportButtons();
} else if (message.startsWith(String(ACTION_CURRENT_PRESENT_SAVED_TO_BANK))){
//saveToCurrentBankLocationButton.setEnabled(false);
}
}
private:
BabuFrikAudioProcessor* processor;
BabuFrikBaseLookAndFeel babuFrikBaseLookAndFeel;
BabuFrikBaseLookAndFeel babuFrikBaseLookAndFeelSmallerFont; // Needed for the next/previous preset buttons to show properly on smaller scales
TextButton loadFileButton;
Label loadedFileLabel;
TextButton nextPresetButton;
int64 lastTimeNextButtonPressed = 0;
TextButton previousPresetButton;
int64 lastTimePreviousButtonPressed = 0;
NumericEditorLabel presetNameLabel;
TextButton saveToCurrentBankLocationButton;
TextButton saveBankFileButton;
bool currentPresetSlidersOutOfSync;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PresetControlComponent);
};