commit 0da5f6e41f2cdbc0cc10af9c10ef5733a2ea0a4a
parent 403d5117d0e52319ceefe632a5a8e5562ec9bb12
Author: Matt Demanett <matt@demanett.net>
Date: Thu, 12 Sep 2019 21:33:11 -0400
Poly: NOISE.
Diffstat:
2 files changed, 103 insertions(+), 20 deletions(-)
diff --git a/src/Noise.cpp b/src/Noise.cpp
@@ -1,31 +1,56 @@
#include "Noise.hpp"
-void Noise::processChannel(const ProcessArgs& args, int _c) {
- if (outputs[BLUE_OUTPUT].isConnected()) {
- outputs[BLUE_OUTPUT].setVoltage(clamp(_blue.next() * 20.0f, -10.0f, 10.f));
- }
- if (outputs[WHITE_OUTPUT].isConnected()) {
- outputs[WHITE_OUTPUT].setVoltage(clamp(_white.next() * 10.0f, -10.0f, 10.f));
- }
- if (outputs[PINK_OUTPUT].isConnected()) {
- outputs[PINK_OUTPUT].setVoltage(clamp(_pink.next() * 15.0f, -10.0f, 10.f));
- }
- if (outputs[RED_OUTPUT].isConnected()) {
- outputs[RED_OUTPUT].setVoltage(clamp(_red.next() * 20.0f, -10.0f, 10.f));
+#define NOISE_CHANNELS "noise_channels"
+
+json_t* Noise::dataToJson() {
+ json_t* root = json_object();
+ json_object_set_new(root, NOISE_CHANNELS, json_integer(_noiseChannels));
+ return root;
+}
+
+void Noise::dataFromJson(json_t* root) {
+ json_t* nc = json_object_get(root, NOISE_CHANNELS);
+ if (nc) {
+ _noiseChannels = json_integer_value(nc);
}
- if (outputs[GAUSS_OUTPUT].isConnected()) {
- outputs[GAUSS_OUTPUT].setVoltage(clamp(_gauss.next(), -10.0f, 10.f));
+}
+
+void Noise::processChannel(const ProcessArgs& args, int c) {
+ assert(c == 0);
+
+ for (int i = 0; i < _noiseChannels; ++i) {
+ if (outputs[BLUE_OUTPUT].isConnected()) {
+ outputs[BLUE_OUTPUT].setChannels(_noiseChannels);
+ outputs[BLUE_OUTPUT].setVoltage(clamp(_blue.next() * 20.0f, -10.0f, 10.f), i);
+ }
+ if (outputs[WHITE_OUTPUT].isConnected()) {
+ outputs[WHITE_OUTPUT].setChannels(_noiseChannels);
+ outputs[WHITE_OUTPUT].setVoltage(clamp(_white.next() * 10.0f, -10.0f, 10.f), i);
+ }
+ if (outputs[PINK_OUTPUT].isConnected()) {
+ outputs[PINK_OUTPUT].setChannels(_noiseChannels);
+ outputs[PINK_OUTPUT].setVoltage(clamp(_pink.next() * 15.0f, -10.0f, 10.f), i);
+ }
+ if (outputs[RED_OUTPUT].isConnected()) {
+ outputs[RED_OUTPUT].setChannels(_noiseChannels);
+ outputs[RED_OUTPUT].setVoltage(clamp(_red.next() * 20.0f, -10.0f, 10.f), i);
+ }
+ if (outputs[GAUSS_OUTPUT].isConnected()) {
+ outputs[GAUSS_OUTPUT].setChannels(_noiseChannels);
+ outputs[GAUSS_OUTPUT].setVoltage(clamp(_gauss.next(), -10.0f, 10.f), i);
+ }
}
- float in = 0.0;
- if (inputs[ABS_INPUT].isConnected()) {
- in = inputs[ABS_INPUT].getVoltageSum();
+ int n = inputs[ABS_INPUT].getChannels();
+ outputs[ABS_OUTPUT].setChannels(n);
+ for (int i = 0; i < n; ++i) {
+ float in = inputs[ABS_INPUT].getPolyVoltage(i);
if (in < 0.0) {
in = -in;
}
+ outputs[ABS_OUTPUT].setVoltage(in, i);
}
- outputs[ABS_OUTPUT].setVoltage(in);
}
struct NoiseWidget : ModuleWidget {
@@ -65,6 +90,61 @@ struct NoiseWidget : ModuleWidget {
addOutput(createOutput<Port24>(gaussOutputPosition, module, Noise::GAUSS_OUTPUT));
addOutput(createOutput<Port24>(absOutputPosition, module, Noise::ABS_OUTPUT));
}
+
+ struct ChannelMenuItemX : MenuItem {
+ Noise* _module;
+ int _channels;
+
+ ChannelMenuItemX(Noise* module, const char* label, int channels)
+ : _module(module)
+ , _channels(channels)
+ {
+ this->text = label;
+ }
+
+ void onAction(const event::Action& e) override {
+ _module->_noiseChannels = _channels;
+ }
+
+ void step() override {
+ MenuItem::step();
+ this->rightText = _module->_noiseChannels == _channels ? "✔" : "";
+ }
+ };
+
+ struct ChannelsMenuItemX : MenuItem {
+ Noise* _module;
+
+ ChannelsMenuItemX(Noise* module, const char* label) : _module(module) {
+ this->text = label;
+ }
+
+ Menu* createChildMenu() override {
+ Menu* menu = new Menu;
+ menu->addChild(new ChannelMenuItemX(_module, "Monophonic", 1));
+ for (int i = 2; i <= BGModule::maxChannels; i++) {
+ char s[10];
+ snprintf(s, 10, "%d", i);
+ menu->addChild(new ChannelMenuItemX(_module, s, i));
+ }
+ return menu;
+ }
+
+ void step() override {
+ MenuItem::step();
+ char s[10];
+ snprintf(s, 10, "%d ▸", _module->_noiseChannels);
+ this->rightText = s;
+ }
+ };
+
+ void appendContextMenu(Menu* menu) override {
+ Noise* m = dynamic_cast<Noise*>(module);
+ assert(m);
+
+ menu->addChild(new MenuLabel());
+ menu->addChild(new ChannelsMenuItemX(m, "Polyphony channels"));
+ }
};
Model* modelNoise = bogaudio::createModel<Noise, NoiseWidget>("Bogaudio-Noise", "Noise", "noise source", "Noise", "Random");
diff --git a/src/Noise.hpp b/src/Noise.hpp
@@ -33,6 +33,7 @@ struct Noise : BGModule {
NUM_LIGHTS
};
+ int _noiseChannels = 1;
BlueNoiseGenerator _blue;
WhiteNoiseGenerator _white;
PinkNoiseGenerator _pink;
@@ -41,9 +42,11 @@ struct Noise : BGModule {
Noise() {
config(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS);
- }
+ }
- void processChannel(const ProcessArgs& args, int _c) override;
+ json_t* dataToJson() override;
+ void dataFromJson(json_t* root) override;
+ void processChannel(const ProcessArgs& args, int c) override;
};
} // namespace bogaudio