Skip to content

Commit

Permalink
Fixed auto-calibration scaling for better dynamic range
Browse files Browse the repository at this point in the history
  • Loading branch information
connornishijima committed May 13, 2024
1 parent e97e524 commit a083691
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 17 deletions.
1 change: 0 additions & 1 deletion src/commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,6 @@ void parse_command(uint32_t t_now_ms, command com) {
}
else{
toggle_standby();
broadcast("reload_config");
}
}
else if (fastcmp(substring, "button_hold")) {
Expand Down
2 changes: 1 addition & 1 deletion src/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void load_config(){
configuration.saturation = preferences.getFloat("saturation", 0.75);

// Background
configuration.background = preferences.getFloat("background", 0.00);
configuration.background = preferences.getFloat("background", 0.25);

// Current Mode
configuration.current_mode = preferences.getInt("current_mode", 1);
Expand Down
15 changes: 7 additions & 8 deletions src/goertzel.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,21 +113,20 @@ void init_goertzel_constants_musical() {
}

void init_window_lookup() {
float sigma = 0.8; // For gaussian window

for (uint16_t i = 0; i < 2048; i++) {
float ratio = i / 2047.0; // Not used for Gaussian Window

// Hamming window
float weighing_factor = 0.54 * (1.0 - cos(TWOPI * ratio));
//float weighing_factor = 0.54 * (1.0 - cos(TWOPI * ratio));

// Blackman-Harris window
//float weighing_factor = 0.3635819 - (0.4891775 * cos(TWOPI * ratio)) + (0.1365995 * cos(FOURPI * ratio)) - (0.0106411 * cos(SIXPI * ratio));

// Gaussian window
//float n_minus_halfN = i - 2048 / 2;
//float gaussian_weighing_factor = exp(-0.5 * pow((n_minus_halfN / (sigma * 2048 / 2)), 2));
//float weighing_factor = gaussian_weighing_factor;
float sigma = 0.8;
float n_minus_halfN = i - 2048 / 2;
float gaussian_weighing_factor = exp(-0.5 * pow((n_minus_halfN / (sigma * 2048 / 2)), 2));
float weighing_factor = gaussian_weighing_factor;

window_lookup[i] = weighing_factor;
window_lookup[4095 - i] = weighing_factor; // Mirror the value for the second half
Expand Down Expand Up @@ -208,7 +207,7 @@ float calculate_magnitude_of_bin(uint16_t bin_number) {
float progress = float(bin_number) / NUM_FREQS;
progress *= progress;
progress *= progress;
scale = (progress * 0.995) + 0.005;
scale = (progress * 0.9975) + 0.0025;

}, __func__ );

Expand Down Expand Up @@ -263,7 +262,7 @@ void calculate_magnitudes() {
avg_val += noise_history[j][i];
}
avg_val /= 10.0;
//avg_val *= 1.05;
avg_val *= 0.90;

noise_floor[i] = noise_floor[i] * 0.99 + avg_val * 0.01;

Expand Down
2 changes: 0 additions & 2 deletions src/touch.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ void read_touch(){
}
else if(touch_pins[t].pin == TOUCH_CENTER_PIN){
toggle_standby();
broadcast("reload_config");
}
else if(touch_pins[t].pin == TOUCH_RIGHT_PIN){
// nothing
Expand Down Expand Up @@ -171,7 +170,6 @@ void read_touch(){
}
else{
toggle_standby();
broadcast("reload_config");
}
}
else if(touch_pins[t].pin == TOUCH_RIGHT_PIN){
Expand Down
15 changes: 10 additions & 5 deletions src/vu.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#define NUM_VU_LOG_SAMPLES 20
#define NUM_VU_SMOOTH_SAMPLES 6
#define NUM_VU_SMOOTH_SAMPLES 12

float vu_log[NUM_VU_LOG_SAMPLES] = { 0 };
uint16_t vu_log_index = 0;
Expand Down Expand Up @@ -39,16 +39,21 @@ void run_vu(){
max_amplitude_now = clip_float(max_amplitude_now);

// LOG AMPLITUDE FOR NOISE REMOVAL ------------------------------------
if(t_now_ms - last_vu_log >= 1000){
if(t_now_ms < 2000){
memset(vu_log, max_amplitude_now, sizeof(float)*NUM_VU_LOG_SAMPLES);
}
else if(t_now_ms - last_vu_log >= 250){
last_vu_log = t_now_ms;
vu_log[vu_log_index] = max_amplitude_now;
vu_log_index = (vu_log_index + 1) % NUM_VU_LOG_SAMPLES;

float vu_sum = 0.0;
for(uint8_t i = 0; i < NUM_VU_LOG_SAMPLES; i++){
for(uint16_t i = 0; i < NUM_VU_LOG_SAMPLES; i++){
vu_sum += vu_log[i];
}
vu_floor = vu_sum / NUM_VU_LOG_SAMPLES;

vu_floor *= 0.90;
}

// SCALE OUTPUT -------------------------------------------------------
Expand All @@ -60,7 +65,7 @@ void run_vu(){
}
else if(max_amplitude_cap > max_amplitude_now){
float distance = max_amplitude_cap - max_amplitude_now;
max_amplitude_cap -= (distance * 0.01);
max_amplitude_cap -= (distance * 0.1);
}
max_amplitude_cap = clip_float(max_amplitude_cap);

Expand All @@ -83,6 +88,6 @@ void run_vu(){
vu_level = vu_sum / NUM_VU_SMOOTH_SAMPLES;

// MAX VALUE ---------------------------------------------------------
vu_max = max(vu_max, vu_level);
vu_max = fmaxf(vu_max, vu_level);
}, __func__);
}

0 comments on commit a083691

Please sign in to comment.