Skip to content

Commit

Permalink
Add randomise button
Browse files Browse the repository at this point in the history
  • Loading branch information
jameshball committed Feb 20, 2024
1 parent 4554158 commit 566ecc0
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Resources/svg/random.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 11 additions & 1 deletion Source/EffectsComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ EffectsComponent::EffectsComponent(OscirenderAudioProcessor& p, OscirenderAudioP
};
addAndMakeVisible(addBtn);*/

addAndMakeVisible(randomiseButton);

randomiseButton.onClick = [this] {
itemData.randomise();
listBox.updateContent();
};

{
juce::MessageManagerLock lock;
audioProcessor.broadcaster.addChangeListener(this);
Expand All @@ -38,7 +45,10 @@ EffectsComponent::~EffectsComponent() {
}

void EffectsComponent::resized() {
auto area = getLocalBounds().withTrimmedTop(20).reduced(20);
auto area = getLocalBounds();
auto titleBar = area.removeFromTop(20);
randomiseButton.setBounds(titleBar.removeFromRight(20));
area = area.reduced(20);
frequency.setBounds(area.removeFromTop(30));

area.removeFromTop(6);
Expand Down
3 changes: 3 additions & 0 deletions Source/EffectsComponent.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <JuceHeader.h>
#include "LookAndFeel.h"
#include "audio/BitCrushEffect.h"
#include "PluginProcessor.h"
#include "components/DraggableListBox.h"
Expand All @@ -20,6 +21,8 @@ class EffectsComponent : public juce::GroupComponent, public juce::ChangeListene

// juce::TextButton addBtn;

SvgButton randomiseButton{ "randomise", juce::String(BinaryData::random_svg), Colours::accentColor };

AudioEffectListBoxItemData itemData;
EffectsListBoxModel listBoxModel;
DraggableListBox listBox;
Expand Down
2 changes: 1 addition & 1 deletion Source/audio/WobbleEffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Point WobbleEffect::apply(int index, Point input, const std::vector<double>& val
// TODO: this doesn't consider sample rate
smoothedFrequency = smoothedFrequency * 0.99995 + pitchDetector.frequency * 0.00005;
double theta = nextPhase(smoothedFrequency, sampleRate);
double delta = values[0] * std::sin(theta);
double delta = 0.5 * values[0] * std::sin(theta);

return input + delta;
}
38 changes: 38 additions & 0 deletions Source/components/EffectsListComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "../audio/Effect.h"
#include "EffectComponent.h"
#include "ComponentList.h"
#include <random>

// Application-specific data container
class OscirenderAudioProcessorEditor;
Expand All @@ -18,6 +19,43 @@ struct AudioEffectListBoxItemData : public DraggableListBoxItemData
resetData();
}

void randomise() {
juce::SpinLock::ScopedLockType lock(audioProcessor.effectsLock);
for (int i = 0; i < data.size(); i++) {
auto effect = data[i];
auto id = effect->getId();

if (id.contains("scale") || id.contains("translate")) {
continue;
}

for (auto& parameter : effect->parameters) {
parameter->setValueNotifyingHost(juce::Random::getSystemRandom().nextFloat());
if (parameter->lfo != nullptr) {
parameter->lfo->setUnnormalisedValueNotifyingHost((int) LfoType::Static);
parameter->lfoRate->setUnnormalisedValueNotifyingHost(1);

if (juce::Random::getSystemRandom().nextFloat() > 0.8) {
parameter->lfo->setUnnormalisedValueNotifyingHost((int)(juce::Random::getSystemRandom().nextFloat() * (int)LfoType::Noise));
parameter->lfoRate->setValueNotifyingHost(juce::Random::getSystemRandom().nextFloat() * 0.1);
}
}
}
effect->enabled->setValueNotifyingHost(juce::Random::getSystemRandom().nextFloat() > 0.8);
}

// shuffle precedence
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(data.begin(), data.end(), g);

for (int i = 0; i < data.size(); i++) {
data[i]->setPrecedence(i);
}

audioProcessor.updateEffectPrecedence();
}

void resetData() {
juce::SpinLock::ScopedLockType lock(audioProcessor.effectsLock);
data.clear();
Expand Down

0 comments on commit 566ecc0

Please sign in to comment.