From 76175accf3b7906cbfddd88e7214b0db2e2b39bc Mon Sep 17 00:00:00 2001 From: Jan-Henrik Bruhn Date: Mon, 11 May 2020 01:08:50 +0200 Subject: [PATCH] Fixes in processing and attenuverter operation --- stereo_mix/drivers/leds.h | 20 +++-- stereo_mix/processor.cc | 24 +++--- stereo_mix/processor.h | 3 + stereo_mix/resources.cc | 110 +++++++++++++------------- stereo_mix/resources/lookup_tables.py | 5 +- stereo_mix/ui.cc | 10 +-- 6 files changed, 90 insertions(+), 82 deletions(-) diff --git a/stereo_mix/drivers/leds.h b/stereo_mix/drivers/leds.h index 66f5097..e2f970c 100644 --- a/stereo_mix/drivers/leds.h +++ b/stereo_mix/drivers/leds.h @@ -1,10 +1,12 @@ #pragma once #include "../resources.h" +#include "stmlib/utils/dsp.h" #include "stm32f0xx_hal_gpio.h" #include using namespace stereo_mix; +using namespace stmlib; const uint8_t kNumChannels = 4; @@ -31,10 +33,6 @@ class Leds { } void Write() { - pwm_counter++; - pwm_counter &= 0x1ff; // equals to if(pwm_counter > 512) pwm_counter = 0; - blink_counter++; - if(blink_counter > 0x2000) blink_counter = 0; #ifdef DEBUG_PIN for (size_t i = 0; i < kNumChannels - 1; i++) { #else @@ -42,17 +40,23 @@ class Leds { #endif bool in_blink_phase = blink_counter > (0x2000 / 2) || !blinking[i]; - if (intensities[0] && lut_led_gamma[intensities[i]] >= pwm_counter && in_blink_phase) { + if (intensities[i] && lut_led_gamma[intensities[i] >> 8] >= pwm_counter && in_blink_phase) { kGpioPorts[i]->BSRR |= kGpioPins[i]; } else { kGpioPorts[i]->BRR |= kGpioPins[i]; } } + pwm_counter++; + blink_counter++; + if(blink_counter > 0x2000) blink_counter = 0; } - void set_intensity(uint8_t channel, uint8_t intensity) + + + void set_intensity(uint8_t channel, uint16_t intensity) { if (channel >= kNumChannels) return; + intensities[channel] = intensity; } void set_blinking(uint8_t channel, bool blink) @@ -63,8 +67,8 @@ class Leds { } private: - uint16_t pwm_counter; + uint8_t pwm_counter; uint16_t blink_counter; - uint8_t intensities[kNumChannels]; + uint16_t intensities[kNumChannels]; bool blinking[kNumChannels]; }; diff --git a/stereo_mix/processor.cc b/stereo_mix/processor.cc index 3944f8d..4be6cb4 100644 --- a/stereo_mix/processor.cc +++ b/stereo_mix/processor.cc @@ -12,16 +12,18 @@ void Processor::Process(int16_t cvs[], uint16_t* outs) uint32_t value_l; uint32_t value_r; + // dirty low pass + this->cv_input_pan = ((cvs[0] - this->cv_input_pan) >> 3); + this->cv_input_vol = ((cvs[1] - this->cv_input_vol) >> 3); + uint16_t pan_pot = (pan_offset + 32767L); uint16_t vol_pot = (vol_offset); - int32_t pan_cv = (cvs[0] * static_cast(pan_att)) >> 15; // full attenuate gives 3x amplification :) - int32_t vol_cv = (cvs[1] * static_cast(vol_att)) >> 15; - pan_cv = Clip16(pan_cv); + int32_t vol_cv = (cv_input_vol * static_cast(vol_att)) >> 15; vol_cv = Clip16(vol_cv); // calculate value for ui - int32_t lin = vol_offset + vol_cv * 3; + int32_t lin = vol_offset + (vol_cv << 3) * 3; lin = ClipU16(lin); lin *= !mute; this->linear_vol = lin; @@ -29,13 +31,11 @@ void Processor::Process(int16_t cvs[], uint16_t* outs) uint16_t vol_pot_exp = Interpolate124(lut_linear_to_exp, vol_pot); uint16_t vol_pot_log = 65535 - InverseInterpolate124(lut_linear_to_exp, vol_pot); - vol_pot = Mix(vol_pot_exp, vol_pot_log, log_exp_mix_pot); - int32_t vol = vol_pot; - + int32_t vol = Mix(static_cast(vol_pot_exp >> 1), vol_pot_log >> 1, log_exp_mix_pot) << 1; uint16_t vol_cv_absu16 = abs(vol_cv) << 1; uint16_t vol_cv_exp = Interpolate124(lut_linear_to_exp, vol_cv_absu16); uint16_t vol_cv_log = 65535 - InverseInterpolate124(lut_linear_to_exp, vol_cv_absu16); - uint32_t vol_cv_pre = Mix(vol_cv_exp, vol_cv_log, log_exp_mix_cv) * 3; + uint32_t vol_cv_pre = (Mix(static_cast(vol_cv_exp >> 1), vol_cv_log >> 1, log_exp_mix_cv) << 3) * 3; vol += vol_cv > 0 ? vol_cv_pre : -vol_cv_pre; vol *= !mute; @@ -43,11 +43,13 @@ void Processor::Process(int16_t cvs[], uint16_t* outs) this->previous_vol = vol; - int32_t pan = pan_pot + (pan_cv * 3); + int32_t pan_cv = (cv_input_pan * static_cast(pan_att)) >> 15; // full attenuate gives 3x amplification :) + pan_cv = Clip16(pan_cv); + int32_t pan = pan_pot + ((pan_cv << 3) * 4); pan = ClipU16(pan); - value_l = (Interpolate124(lut_left_sin_pan, pan) * vol) >> 16; - value_r = (Interpolate124(lut_right_cos_pan, pan) * vol) >> 16; + value_l = (lut_left_sin_pan[pan >> 4] * vol) >> 16; + value_r = (lut_right_cos_pan[pan >> 4] * vol) >> 16; outs[0] = value_r; outs[1] = value_l; diff --git a/stereo_mix/processor.h b/stereo_mix/processor.h index 4f57d20..aefebeb 100644 --- a/stereo_mix/processor.h +++ b/stereo_mix/processor.h @@ -38,6 +38,9 @@ class Processor { uint16_t previous_vol; uint16_t linear_vol; + int16_t cv_input_pan = 0; + int16_t cv_input_vol = 0; + uint16_t vol_offset = 0; int16_t pan_offset = 0; int16_t vol_att; diff --git a/stereo_mix/resources.cc b/stereo_mix/resources.cc index 1b2a7db..8aaf073 100644 --- a/stereo_mix/resources.cc +++ b/stereo_mix/resources.cc @@ -3116,67 +3116,67 @@ const uint16_t lut_led_gamma[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, - 3, 3, 3, 3, + 2, 2, 3, 3, + 3, 3, 3, 4, 4, 4, 4, 4, - 5, 5, 5, 6, - 6, 6, 7, 7, - 8, 8, 8, 9, - 9, 10, 10, 11, - 11, 12, 12, 13, - 13, 14, 15, 15, - 16, 17, 17, 18, - 19, 19, 20, 21, - 21, 22, 23, 24, - 25, 25, 26, 27, - 28, 29, 30, 31, + 5, 5, 5, 5, + 6, 6, 6, 6, + 7, 7, 7, 8, + 8, 8, 9, 9, + 9, 10, 10, 10, + 11, 11, 11, 12, + 12, 13, 13, 14, + 14, 14, 15, 15, + 16, 16, 17, 17, + 18, 18, 19, 19, + 20, 20, 21, 22, + 22, 23, 23, 24, + 24, 25, 26, 26, + 27, 28, 28, 29, + 30, 30, 31, 32, 32, 33, 34, 35, - 36, 37, 38, 39, - 40, 41, 42, 43, - 44, 45, 47, 48, - 49, 50, 51, 53, - 54, 55, 57, 58, - 59, 61, 62, 64, - 65, 66, 68, 69, - 71, 72, 74, 76, - 77, 79, 80, 82, - 84, 85, 87, 89, - 91, 92, 94, 96, - 98, 100, 101, 103, - 105, 107, 109, 111, - 113, 115, 117, 119, - 121, 123, 125, 128, - 130, 132, 134, 136, - 138, 141, 143, 145, - 148, 150, 152, 155, - 157, 160, 162, 164, - 167, 169, 172, 175, - 177, 180, 182, 185, - 188, 190, 193, 196, - 199, 201, 204, 207, - 210, 213, 216, 219, - 222, 224, 227, 230, - 234, 237, 240, 243, - 246, 249, 252, 255, - 259, 262, 265, 268, - 272, 275, 278, 282, - 285, 289, 292, 296, - 299, 303, 306, 310, - 313, 317, 321, 324, - 328, 332, 336, 339, - 343, 347, 351, 355, - 359, 362, 366, 370, - 374, 378, 382, 387, - 391, 395, 399, 403, - 407, 412, 416, 420, - 424, 429, 433, 437, - 442, 446, 451, 455, - 460, 464, 469, 473, - 478, 483, 487, 492, - 497, 501, 506, 511, + 35, 36, 37, 38, + 39, 39, 40, 41, + 42, 43, 43, 44, + 45, 46, 47, 48, + 49, 50, 51, 52, + 53, 53, 54, 55, + 56, 57, 58, 59, + 60, 62, 63, 64, + 65, 66, 67, 68, + 69, 70, 71, 73, + 74, 75, 76, 77, + 78, 80, 81, 82, + 83, 85, 86, 87, + 88, 90, 91, 92, + 94, 95, 96, 98, + 99, 100, 102, 103, + 105, 106, 108, 109, + 111, 112, 114, 115, + 117, 118, 120, 121, + 123, 124, 126, 127, + 129, 131, 132, 134, + 136, 137, 139, 141, + 142, 144, 146, 148, + 149, 151, 153, 155, + 156, 158, 160, 162, + 164, 166, 167, 169, + 171, 173, 175, 177, + 179, 181, 183, 185, + 187, 189, 191, 193, + 195, 197, 199, 201, + 203, 205, 207, 210, + 212, 214, 216, 218, + 220, 223, 225, 227, + 229, 232, 234, 236, + 239, 241, 243, 246, + 248, 250, 253, 255, }; diff --git a/stereo_mix/resources/lookup_tables.py b/stereo_mix/resources/lookup_tables.py index 0813d07..d7f61c0 100644 --- a/stereo_mix/resources/lookup_tables.py +++ b/stereo_mix/resources/lookup_tables.py @@ -20,12 +20,11 @@ fig, ax = plt.subplots() ax.plot(space, values) other_values = OUTPUT_RESOLUTION - np.flip(values) ax.plot(space, OUTPUT_RESOLUTION - (np.flip(values))) -ax.plot(space, values / 2 + other_values / 2) ax.set(xlabel='space', ylabel='values') ax.grid() -# plt.show() +#plt.show() # Left pan Lookup table @@ -55,7 +54,7 @@ print(r_pan.size) # led gamma correction gamma = 2.4 max_in = 255 -max_out = 511 +max_out = 255 input_vals = np.linspace(0, max_in, num=max_in + 1) gamma_correction = ((input_vals / max_in) ** gamma) * max_out + 0.5 lookup_tables_u16.append(('led_gamma', np.floor(gamma_correction))) diff --git a/stereo_mix/ui.cc b/stereo_mix/ui.cc index b47a43f..31cc6cc 100644 --- a/stereo_mix/ui.cc +++ b/stereo_mix/ui.cc @@ -99,23 +99,23 @@ void UI::TaskDrawLeds() { for (size_t i = 0; i < kNumChannels; i++) { if (potControllers[i].editing_hidden_parameter()) { - leds->set_intensity(i, abs(volume_att_pots[i] - 32767) >> 7); + leds->set_intensity(i, abs(volume_att_pots[i] - 32767) << 1); leds->set_blinking(i, volume_att_pots[i] - 32767 < 0); } else if (potControllers[i + kNumChannels].editing_hidden_parameter()) { - leds->set_intensity(i, abs(pan_att_pots[i] - 32767) >> 7); + leds->set_intensity(i, abs(pan_att_pots[i] - 32767) << 1); leds->set_blinking(i, pan_att_pots[i] - 32767 < 0); } else { if (system_clock.milliseconds() - last_vol_pot_touch[i] < kShowChangedValueMilliseconds) { // show volume - leds->set_intensity(i, volume_pots[i] >> 8); + leds->set_intensity(i, volume_pots[i]); leds->set_blinking(i, false); } else if (system_clock.milliseconds() - last_pan_pot_touch[i] < kShowChangedValueMilliseconds) { // show panning - leds->set_intensity(i, abs(pan_pots[i] - 32767) >> 7); + leds->set_intensity(i, abs(pan_pots[i] - 32767) << 1); leds->set_blinking(i, pan_pots[i] - 32767 < 0); } else { // show volume if not muted - leds->set_intensity(i, processors[i].linear_volume() >> (8)); + leds->set_intensity(i, processors[i].linear_volume()); leds->set_blinking(i, false); } }