11#include " plugin.hpp"
2+ #include " SwitchBase.hpp"
23
34
4- struct Switch18 : Module {
5+ struct Switch18 : Module, SwitchBase {
56 enum ParamId {
67 MODE_PARAM ,
78 STEP_1_PARAM ,
@@ -49,12 +50,6 @@ struct Switch18 : Module {
4950 STEP_8_LIGHT ,
5051 LIGHTS_LEN
5152 };
52- enum Mode {
53- SELECT_CHANCE ,
54- SKIP_CHANCE ,
55- REPEAT_WEIGHT ,
56- FIXED_PATTERN
57- };
5853
5954 Switch18 () {
6055 config (PARAMS_LEN , INPUTS_LEN , OUTPUTS_LEN , LIGHTS_LEN );
@@ -88,13 +83,6 @@ struct Switch18 : Module {
8883 configOutput (STEP_8_OUTPUT , " step 8" );
8984 }
9085
91- int mode = 0 ;
92- int step = 0 ;
93- float weights[8 ] = { 0 .f };
94- float pattern[8 ] = { 0 .f };
95- float repeat_value = 0 .f;
96- dsp::SchmittTrigger trigger;
97-
9886 void compute_weights () {
9987 for (int i = 0 ; i < 8 ; i++) {
10088 if (outputs[STEP_1_OUTPUT + i].isConnected ()) {
@@ -106,170 +94,19 @@ struct Switch18 : Module {
10694 }
10795 }
10896
109- float calculate_sum (float w[8 ]) {
110- float sum = 0 .f ;
111- for (int i = 0 ; i < 8 ; i++) {
112- sum += w[i];
113- }
114- return sum;
115- }
116-
117- void skip_steps (int depth) {
118- int d = depth;
119- if (d < 1 ) {
120- step = random::uniform () * 8 ;
121- return ;
122- }
123- if (depth > 8 ) {
124- d = 8 ;
125- }
126- DEBUG (" incrementing step" );
127- step++;
128- if (step > 7 ) {
129- step = 0 ;
130- }
131- DEBUG (" step is now %d" , step);
132- DEBUG (" rolling dice for random step" );
133- float r = random::uniform ();
134- float w = weights[STEP_1_OUTPUT + step];
135- DEBUG (" dice roll is %f" , r);
136- DEBUG (" step probability is %f" , w);
137- if (r > w) {
138- DEBUG (" dice roll > step probability, calling skip_steps() again" );
139- skip_steps (d - 1 );
140- }
141- }
142-
14397 void process (const ProcessArgs& args) override {
14498 float signal = inputs[SIGNAL_INPUT ].getVoltage ();
14599 mode = (int )params[MODE_PARAM ].getValue ();
146100
147101 compute_weights ();
148-
149102
150103 if (trigger.process (inputs[TRIGGER_INPUT ].getVoltage ())) {
151-
152- switch (mode) {
153- case SELECT_CHANCE :
154- {
155- float sum = calculate_sum (weights);
156- if (sum == 0 .f ) {
157- step = random::uniform () * 8 ;
158-
159- }
160- else {
161- float r = random::uniform () * sum;
162-
163- for (int i = 0 ; i < 8 ; i++) {
164- r -= weights[i];
165- if (r <= 0 .f ) {
166- if (weights[i] > 0 .f ) {
167- step = i;
168- }
169- else {
170- continue ;
171- }
172- break ;
173- }
174- }
175- break ;
176- }
177- break ;
178- }
179- case SKIP_CHANCE :
180- {
181- bool all_zero = true ;
182- for (int i = 0 ; i < 8 ; i++) {
183- if (weights[i] > 0 .f ) {
184- all_zero = false ;
185- DEBUG (" non-zero weight of %f found at index %d" , weights[i], i);
186- break ;
187- }
188- }
189- DEBUG (" all_zero is %d" , all_zero);
190- if (!all_zero) {
191- skip_steps (8 );
192- }
193- else {
194- step = random::uniform () * 8 ;
195- }
196- break ;
197- }
198- case REPEAT_WEIGHT :
199- {
200- // if all weights are zero, just pick a random step
201- bool all_zero = true ;
202- for (int i = 0 ; i < 8 ; i++) {
203- if (weights[i] > 0 .f ) {
204- all_zero = false ;
205- break ;
206- }
207- }
208- if (!all_zero) {
209- // find the min param value that isn't 0
210- float min = 1 .f ;
211- for (int i = 0 ; i < 8 ; i++) {
212- if (weights[i] < min && weights[i] > 0 .f ) {
213- min = weights[i];
214- }
215- }
216- DEBUG (" min is %f" , min);
217- // decrement the repeat value by the min value
218- repeat_value -= min;
219- DEBUG (" repeat_value after decrement is %f" , repeat_value);
220- // if repeat value is greater than 0, keep the same step,
221- // otherwise, advance to next step with a weight greater than 0
222- if (repeat_value > 0 .f ) {
223- break ;
224- }
225- else {
226- DEBUG (" repeat_value is 0, advancing to next step" );
227- for (int i = 0 ; i < 8 ; i++) {
228- if (weights[(step + i + 1 ) % 8 ] > 0 .f ) {
229- step = (step + i + 1 ) % 8 ;
230- break ;
231- }
232- }
233- repeat_value = params[STEP_1_PARAM + step].getValue ();
234- DEBUG (" step is now %d" , step);
235- DEBUG (" repeat_value is now %f" , repeat_value);
236- }
237- }
238- else {
239- step = random::uniform () * 8 ;
240- }
241- break ;
242- }
243- case FIXED_PATTERN :
244- {
245- float sum = calculate_sum (weights);
246- if (sum == 0 .f ) {
247- step = random::uniform () * 8 ;
248- break ;
249- }
250- // increment stored weights by their param values
251- for (int i = 0 ; i < 8 ; i++) {
252- pattern[i] += params[STEP_1_PARAM + i].getValue ();
253- }
254- // select the port that has the highest stored value
255- float max = 0 .f ;
256- int max_index = 0 ;
257- for (int i = 0 ; i < 8 ; i++) {
258- if (pattern[i] > max) {
259- max = pattern[i];
260- max_index = i;
261- }
262- }
263- step = max_index;
264- // decrement the selected port's value by the sum of all the weights
265- pattern[step] -= sum;
266- break ;
267- }
268- }
104+ do_it_all ();
269105 }
106+
270107 for (int i = 0 ; i < OUTPUTS_LEN ; i++) {
271- outputs[i].setVoltage (i == step ? signal : 0 .f );
272- lights[i].setBrightness (i == step ? 1 .f : 0 .f );
108+ outputs[STEP_1_OUTPUT + i].setVoltage (i == current_step ? signal : 0 .f );
109+ lights[STEP_1_LIGHT + i].setBrightness (i == current_step ? 1 .f : 0 .f );
273110 }
274111 }
275112};
0 commit comments