Skip to content

Commit 6d76a29

Browse files
committed
reworked textbox value reading for better error-proofing
1 parent cc42251 commit 6d76a29

File tree

6 files changed

+145
-85
lines changed

6 files changed

+145
-85
lines changed

Source/AmiWindowEditor.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ void AmiWindowEditor::paint (juce::Graphics& g)
156156

157157
g.fillRect(proportionOfWidth(0.88f) + 4, proportionOfHeight(0.72f) + 2, 1, proportionOfHeight(0.3f) - 2);
158158
g.fillRect(proportionOfWidth(0.71f) + 3, proportionOfHeight(0.72f) + 2, 1, proportionOfHeight(0.125f) - 2);
159-
g.fillRect(proportionOfWidth(0.17f) + 2, proportionOfHeight(0.72f) + 2, 2, proportionOfHeight(0.125f) - 2);
159+
g.fillRect(proportionOfWidth(0.182f) + 2, proportionOfHeight(0.72f) + 2, 2, proportionOfHeight(0.125f) - 2);
160160

161161
g.fillRect(proportionOfWidth(0.23f) + 3, proportionOfHeight(0.5f), 1, proportionOfHeight(0.22f));
162162
g.fillRect(proportionOfWidth(0.46f) + 2, proportionOfHeight(0.5f), 2, proportionOfHeight(0.345f));
@@ -168,7 +168,7 @@ void AmiWindowEditor::paint (juce::Graphics& g)
168168

169169
g.fillRect(proportionOfWidth(0.88f) + 1, proportionOfHeight(0.72f) + 2, 2, proportionOfHeight(0.3f) - 2);
170170
g.fillRect(proportionOfWidth(0.71f), proportionOfHeight(0.72f) + 2, 2, proportionOfHeight(0.125f) - 2);
171-
g.fillRect(proportionOfWidth(0.17f), proportionOfHeight(0.72f) + 2, 2, proportionOfHeight(0.125f) - 2);
171+
g.fillRect(proportionOfWidth(0.182f), proportionOfHeight(0.72f) + 2, 2, proportionOfHeight(0.125f) - 2);
172172

173173
g.fillRect(proportionOfWidth(0.23f), proportionOfHeight(0.5f), 2, proportionOfHeight(0.22f));
174174
g.fillRect(proportionOfWidth(0.46f), proportionOfHeight(0.5f), 2, proportionOfHeight(0.345f));
@@ -271,7 +271,7 @@ void AmiWindowEditor::resized()
271271
saveButton.setBoundsRelative(0.89f, 0.785f, 0.1f, 0.051f);
272272

273273
moreOptions.setBoundsRelative(0.72f, 0.785f, 0.15f, 0.051f);
274-
resampleButton.setBoundsRelative(0.686f, 0.663f, 0.1f, 0.051f);
274+
resampleButton.setBoundsRelative(0.7f, 0.663f, 0.1f, 0.051f);
275275

276276
keyboardComponent.setBoundsRelative(0.021f, 0.86f, 0.838f, 0.13f);
277277
logoImage.setBoundsRelative(0.69f, 0.52f, 0.3f, 0.08f);

Source/GuiComponent.cpp

Lines changed: 119 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,6 @@ void GuiComponent::paint (juce::Graphics& g)
104104
proportionOfWidth(0.2f), proportionOfHeight(0.07f)), juce::Justification::centredLeft, false);
105105

106106
automateLabelText(&sampleRateText, (int) audioProcessor.getSourceSampleRate(currentSample));
107-
automateLabelText(&resampleRateText, (int) audioProcessor.getResampleRate(currentSample));
108-
109107
return;
110108
}
111109

@@ -185,12 +183,12 @@ void GuiComponent::resized()
185183
midiLowNote.setBoundsRelative(0.595f, 0.63f, 0.035f, 0.05f);
186184
midiHiNote.setBoundsRelative(0.635f, 0.63f, 0.035f, 0.05f);
187185

188-
startLoopText.setBoundsRelative(0.1f, 0.735f, 0.07f, 0.04f);
189-
endLoopText.setBoundsRelative(0.1f, 0.765f, 0.07f, 0.04f);
190-
replenLoopText.setBoundsRelative(0.1f, 0.795f, 0.07f, 0.04f);
186+
startLoopText.setBoundsRelative(0.1f, 0.735f, 0.08f, 0.04f);
187+
endLoopText.setBoundsRelative(0.1f, 0.765f, 0.08f, 0.04f);
188+
replenLoopText.setBoundsRelative(0.1f, 0.795f, 0.08f, 0.04f);
191189

192-
sampleRateText.setBoundsRelative(0.62f, 0.63f, 0.07f, 0.04f);
193-
resampleRateText.setBoundsRelative(0.62f, 0.67f, 0.07f, 0.04f);
190+
sampleRateText.setBoundsRelative(0.62f, 0.63f, 0.08f, 0.04f);
191+
resampleRateText.setBoundsRelative(0.62f, 0.67f, 0.08f, 0.04f);
194192

195193
decText.setBoundsRelative(0.12f, 0.51f, 0.05f, 0.03f);
196194
hexText.setBoundsRelative(0.17f, 0.51f, 0.05f, 0.03f);
@@ -245,6 +243,9 @@ void GuiComponent::changeSampleChannel(const int &channel)
245243
automateLabelText(&endLoopText, loopEnd);
246244
automateLabelText(&replenLoopText, loopRpln);
247245

246+
automateLabelText(&sampleRateText, (int) audioProcessor.getSourceSampleRate(currentSample));
247+
automateLabelText(&resampleRateText, (int) audioProcessor.getResampleRate(currentSample));
248+
248249
enableLoop.setToggleState(loopEnabled, juce::NotificationType::dontSendNotification);
249250
enableLoop.setEnabled(loopEnabled);
250251

@@ -329,6 +330,9 @@ void GuiComponent::buttonClicked(juce::Button *button)
329330
automateLabelText(&endLoopText, loopEnd);
330331
automateLabelText(&replenLoopText, loopEnd - loopStart);
331332

333+
automateLabelText(&sampleRateText, (int) audioProcessor.getSourceSampleRate(currentSample));
334+
automateLabelText(&resampleRateText, (int) audioProcessor.getResampleRate(currentSample));
335+
332336
repaint();
333337
};
334338

@@ -343,6 +347,9 @@ void GuiComponent::buttonClicked(juce::Button *button)
343347
automateLabelText(&endLoopText, loopEnd);
344348
automateLabelText(&replenLoopText, loopEnd - loopStart);
345349

350+
automateLabelText(&sampleRateText, (int) audioProcessor.getSourceSampleRate(currentSample));
351+
automateLabelText(&resampleRateText, (int) audioProcessor.getResampleRate(currentSample));
352+
346353
repaint();
347354
};
348355

@@ -406,6 +413,12 @@ void GuiComponent::parameterChanged(const juce::String &parameterID, float newVa
406413
automateLabelText(&replenLoopText, loopRpln);
407414
}
408415

416+
if (parameterID.contains("SAMPLE MIDI CHAN" + juce::String(currentSample)))
417+
{
418+
const juce::String channel = newValue <= 0 ? "ALL" : juce::String((int) (newValue + 1));
419+
sampleMidiChannel.setText(channel, juce::NotificationType::dontSendNotification);
420+
}
421+
409422
if (parameterID.contains("SAMPLE ROOT NOTE" + juce::String(currentSample)))
410423
{
411424
const int midiNote = newValue < 0 ? 0 : newValue > 127 ? 127 : (int) newValue;
@@ -481,65 +494,104 @@ void GuiComponent::initCheckBox(juce::Button *b, const juce::String &name, const
481494

482495
void GuiComponent::changeLoopText(juce::Label* l, const int type)
483496
{
497+
bool success = true;
484498
juce::String txt;
485-
int val = 0;
499+
int val = -1;
486500

487501
jassert(l != nullptr && type < 3);
488502

489503
txt = l->getText().toUpperCase();
490504

491-
if (txt.isEmpty()) return;
492-
if (!txt.containsOnly("0123456789ABCDEF") || (!textInHex && !txt.containsOnly("0123456789"))) return;
505+
if (txt.isEmpty())
506+
success = false;
507+
else if(textIsHexValue(txt))
508+
val = txt.getHexValue32();
509+
else if(textIsDecValue(txt))
510+
val = txt.getIntValue();
511+
else
512+
success = false;
493513

494-
val = textInHex ? txt.getHexValue32() : txt.getIntValue();
514+
if(val < 0) success = false;
495515

496516
switch(type)
497517
{
498518
case 0:
499519

500-
if (val < 0) return;
501-
if (val >= audioProcessor.getLoopEnd(currentSample)) return;
520+
if (val >= audioProcessor.getLoopEnd(currentSample))
521+
success = false;
502522

503-
audioProcessor.setLoopStart(currentSample, val);
523+
if(success) audioProcessor.setLoopStart(currentSample, val);
524+
else val = audioProcessor.getLoopStart(currentSample);
504525

505526
break;
506527

507528
case 1:
508529

509-
val += textInHex ? startLoopText.getText().getHexValue32() : startLoopText.getText().getIntValue();
530+
if(!success)
531+
{
532+
val = audioProcessor.getLoopEnd(currentSample) - audioProcessor.getLoopStart(currentSample);
533+
break;
534+
}
535+
536+
val += audioProcessor.getLoopStart(currentSample);
510537
[[fallthrough]];
511538

512539
case 2:
513540

514-
if (val <= audioProcessor.getLoopStart(currentSample)) return;
515-
if (val > audioProcessor.getWaveForm(currentSample).getNumSamples()) return;
541+
if (val <= audioProcessor.getLoopStart(currentSample) || val > audioProcessor.getWaveForm(currentSample).getNumSamples())
542+
success = false;
516543

517-
audioProcessor.setLoopEnd(currentSample, val);
544+
if(success) audioProcessor.setLoopEnd(currentSample, val);
545+
else val = audioProcessor.getLoopEnd(currentSample);
518546

519547
break;
520548
}
521-
522-
l->setText(txt.paddedLeft('0', 6), juce::NotificationType::dontSendNotification);
549+
550+
if(type == 1) val = audioProcessor.getLoopEnd(currentSample) - audioProcessor.getLoopStart(currentSample);
551+
552+
txt = textInHex ? juce::String::toHexString(val).toUpperCase() : juce::String(val);
553+
l->setText(txt.paddedLeft('0', 8), juce::NotificationType::dontSendNotification);
523554
}
524555

525-
void GuiComponent::changeRateText(juce::Label *l)
556+
void GuiComponent::changeRateText(juce::Label *l, const int type)
526557
{
558+
bool success = true;
527559
juce::String txt;
528560
int val = 0;
529561

530-
jassert(l != nullptr);
562+
jassert(l != nullptr && type < 2);
531563

532564
txt = l->getText().toUpperCase();
533565

534-
if (txt.isEmpty()) return;
535-
if (!txt.containsOnly("0123456789ABCDEF") /* || (!textInHex && !txt.contains("0123456789")) */) return;
566+
if (txt.isEmpty())
567+
success = false;
568+
else if(textIsHexValue(txt))
569+
val = txt.getHexValue32();
570+
else if(textIsDecValue(txt))
571+
val = txt.getIntValue();
572+
else
573+
success = false;
574+
575+
if (val < 1000 || val >= 1000000) success = false;
536576

537-
val = textInHex ? txt.getHexValue32() : txt.getIntValue();
577+
switch(type)
578+
{
579+
case 0:
580+
581+
if(success) audioProcessor.setSourceSampleRate(currentSample, val);
582+
else val = audioProcessor.getSourceSampleRate(currentSample);
583+
584+
break;
538585

539-
if (val <= 0) return;
540-
if (val >= 1000000) return;
586+
case 1:
587+
588+
if(success) audioProcessor.setResampleRate(currentSample, val);
589+
else val = audioProcessor.getResampleRate(currentSample);
590+
591+
break;
592+
}
541593

542-
audioProcessor.setSourceSampleRate(currentSample, val);
594+
txt = textInHex ? juce::String::toHexString(val).toUpperCase() : juce::String(val);
543595
l->setText(txt.paddedLeft('0', 6), juce::NotificationType::dontSendNotification);
544596
}
545597

@@ -555,7 +607,7 @@ void GuiComponent::changeMidiNoteText(juce::Label *l, const int type)
555607

556608
if (txt.isEmpty()) success = false;
557609

558-
else if (!textInHex && txt.containsOnly("0123456789"))
610+
else if (textIsDecValue(txt))
559611
{
560612
note = txt.getIntValue();
561613
}
@@ -601,58 +653,52 @@ void GuiComponent::changeMidiNoteText(juce::Label *l, const int type)
601653
void GuiComponent::initAllLabels()
602654
{
603655
initLabel(&startLoopText,true);
604-
startLoopText.setText("000000", juce::NotificationType::dontSendNotification);
656+
startLoopText.setText("00000000", juce::NotificationType::dontSendNotification);
605657
startLoopText.onTextChange = [&] { changeLoopText(&startLoopText, 0); };
606658

607659
initLabel(&endLoopText,true);
608-
endLoopText.setText("000000", juce::NotificationType::dontSendNotification);
660+
endLoopText.setText("00000000", juce::NotificationType::dontSendNotification);
609661
endLoopText.onTextChange = [&] { changeLoopText(&endLoopText, 2); };
610662

611663
initLabel(&replenLoopText,true);
612-
replenLoopText.setText("000000", juce::NotificationType::dontSendNotification);
664+
replenLoopText.setText("00000000", juce::NotificationType::dontSendNotification);
613665
replenLoopText.onTextChange = [&] { changeLoopText(&replenLoopText, 1); };
614666

615667
initLabel(&sampleRateText,true);
616-
sampleRateText.setText("000000", juce::NotificationType::dontSendNotification);
617-
sampleRateText.onTextChange = [&] { changeRateText(&sampleRateText); };
668+
sampleRateText.setText("00000000", juce::NotificationType::dontSendNotification);
669+
sampleRateText.onTextChange = [&] { changeRateText(&sampleRateText, 0); };
618670
sampleRateText.setVisible(false);
619671

620672
initLabel(&resampleRateText,true);
621-
resampleRateText.setText("000000", juce::NotificationType::dontSendNotification);
622-
resampleRateText.onTextChange = [&]
623-
{
624-
double val = 0.0;
625-
const juce::String txt = resampleRateText.getText().toUpperCase();
626-
627-
if (txt.isEmpty()) return;
628-
if (!txt.containsOnly("0123456789ABCDEF") || (!textInHex && !txt.containsOnly("0123456789"))) return;
629-
630-
val = textInHex ? txt.getHexValue32() : txt.getIntValue();
631-
632-
if(val <= 0) return;
633-
634-
audioProcessor.setResampleRate(currentSample, val);
635-
resampleRateText.setText(txt.paddedLeft('0', 6), juce::NotificationType::dontSendNotification);
636-
};
637-
673+
resampleRateText.setText("00000000", juce::NotificationType::dontSendNotification);
674+
resampleRateText.onTextChange = [&] { changeRateText(&resampleRateText, 1); };
638675
resampleRateText.setVisible(false);
639676

640677
initLabel(&sampleMidiChannel, false);
641678
sampleMidiChannel.onTextChange = [&]
642679
{
643-
if (sampleMidiChannel.getText().isEmpty()) return;
680+
juce::String chanText = sampleMidiChannel.getText();
681+
bool success = true;
682+
int channel = -1;
683+
684+
if (sampleMidiChannel.getText().isEmpty())
685+
success = false;
686+
else if(textIsHexValue(chanText))
687+
channel = chanText.getHexValue32();
688+
else if(textIsDecValue(chanText))
689+
channel = chanText.getIntValue();
690+
else if(chanText.compareIgnoreCase("ALL") == 0)
691+
channel = 0;
692+
else
693+
success = false;
644694

645-
if(sampleMidiChannel.getText().containsOnly("0123456789"))
646-
{
647-
const int channel = sampleMidiChannel.getText().getIntValue();
695+
if (channel < 0 || channel > 16) success = false;
648696

649-
if (channel < 0) return;
650-
if (channel > 16) return;
697+
if(success) audioProcessor.setMidiChannel(currentSample, channel);
698+
else channel = audioProcessor.getMidiChannel(currentSample);
651699

652-
audioProcessor.setMidiChannel(currentSample, channel);
653-
}
654-
else if(sampleMidiChannel.getText().compareIgnoreCase("ALL") == 0)
655-
audioProcessor.setMidiChannel(currentSample, 0);
700+
chanText = channel == 0 ? "ALL" : juce::String(channel).paddedLeft('0', 2);
701+
sampleMidiChannel.setText(chanText, juce::NotificationType::dontSendNotification);
656702
};
657703

658704
initLabel(&midiRootNote, false);
@@ -713,20 +759,13 @@ void GuiComponent::initAllCheckboxes()
713759

714760
void GuiComponent::automateLabelText(juce::Label *l, const int value)
715761
{
716-
const juce::String valueText = textInHex ? juce::String::toHexString(value) : juce::String(value);
762+
const juce::String valueText = textInHex ? juce::String::toHexString(value).toUpperCase() : juce::String(value);
717763

718764
jassert(l != nullptr);
719765

720-
if(l->isBeingEdited())
721-
{
722-
l->getCurrentTextEditor()->setColour(juce::TextEditor::highlightColourId, JPAL(AMI_ORG));
723-
l->getCurrentTextEditor()->setJustification(juce::Justification::centred);
724-
return;
725-
}
726-
727766
if ((textInHex && (l->getText().getHexValue32() == value)) || (!textInHex && (l->getText().getIntValue() == value))) return;
728767

729-
l->setText(valueText.paddedLeft('0', 6).toUpperCase(), juce::NotificationType::dontSendNotification);
768+
l->setText(valueText.paddedLeft('0', 8).toUpperCase(), juce::NotificationType::dontSendNotification);
730769
}
731770

732771
void GuiComponent::hideMainOptions()
@@ -763,3 +802,13 @@ void GuiComponent::hideMoreOptions()
763802
forwardLoop.setVisible(showExtendedOptions);
764803
pingpongLoop.setVisible(showExtendedOptions);
765804
}
805+
806+
bool GuiComponent::textIsHexValue(const juce::String &value)
807+
{
808+
return !textInHex ? false : value.toLowerCase().containsOnly("0123456789abcdef");
809+
}
810+
811+
bool GuiComponent::textIsDecValue(const juce::String &value)
812+
{
813+
return textInHex ? false : value.containsOnly("0123456789");
814+
}

Source/GuiComponent.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class GuiComponent : public juce::Component,
4646
void initCheckBox(juce::Button*, const juce::String&, const int);
4747

4848
void changeLoopText(juce::Label*, const int);
49-
void changeRateText(juce::Label*);
49+
void changeRateText(juce::Label*, const int);
5050
void changeMidiNoteText(juce::Label*, const int);
5151

5252
void initAllLabels();
@@ -56,7 +56,9 @@ class GuiComponent : public juce::Component,
5656

5757
void hideMainOptions();
5858
void hideMoreOptions();
59-
59+
bool textIsHexValue(const juce::String& value);
60+
bool textIsDecValue(const juce::String& value);
61+
6062
juce::Rectangle<int> ledRectangle;
6163

6264
juce::Label startLoopText, endLoopText, replenLoopText, sampleRateText, resampleRateText,

0 commit comments

Comments
 (0)