@@ -252,9 +252,25 @@ static void btstack_audio_pico_sink_fill_buffers(void){
252252 }
253253 }
254254
255+ // Choose one:
256+ // #define SCALE_LOGARITHMIC
257+ #define SCALE_SQRT
258+ // #define SCALE_LINEAR
259+
260+ constexpr float max_sample_from_fft = 4000 .f + 130 .f * display.HEIGHT ;
261+ constexpr int lower_threshold = 270 - 2 * display.HEIGHT ;
262+ #ifdef SCALE_LOGARITHMIC
263+ constexpr fix15 multiple = float_to_fix15 (pow (max_sample_from_fft / lower_threshold, -1 .f / (display.HEIGHT - 1 )));
264+ #elif defined(SCALE_SQRT)
265+ constexpr fix15 subtract_step = float_to_fix15 ((max_sample_from_fft - lower_threshold) * 2 .f / (display.HEIGHT * (display.HEIGHT - 1 )));
266+ #elif defined(SCALE_LINEAR)
267+ constexpr fix15 subtract = float_to_fix15 ((max_sample_from_fft - lower_threshold) / (display.HEIGHT - 1 ));
268+ #else
269+ #error "Choose a scale mode"
270+ #endif
255271 fft.update ();
256272 for (auto i = 0u ; i < display.WIDTH ; i++) {
257- uint16_t sample = std::min (( int16_t )(display. HEIGHT * 255 ), ( int16_t ) fft.get_scaled_fix15 (i + FFT_SKIP_BINS, loudness_adjust[i]));
273+ fix15 sample = std::min (float_to_fix15 (max_sample_from_fft), fft.get_scaled_as_fix15 (i + FFT_SKIP_BINS, loudness_adjust[i]));
258274 uint8_t maxy = 0 ;
259275
260276 for (int j = 0 ; j < HISTORY_LEN; ++j) {
@@ -263,20 +279,31 @@ static void btstack_audio_pico_sink_fill_buffers(void){
263279 }
264280 }
265281
282+ #ifdef SCALE_SQRT
283+ fix15 subtract = subtract_step;
284+ #endif
266285 for (auto y = 0 ; y < display.HEIGHT ; y++) {
267286 uint8_t r = 0 ;
268287 uint8_t g = 0 ;
269288 uint8_t b = 0 ;
270- if (sample > 255 ) {
289+ if (sample > int_to_fix15 (lower_threshold) ) {
271290 r = (uint16_t )(palette_main[i].r );
272291 g = (uint16_t )(palette_main[i].g );
273292 b = (uint16_t )(palette_main[i].b );
274- sample -= 255 ;
293+ #ifdef SCALE_LOGARITHMIC
294+ sample = multiply_fix15_unit (multiple, sample);
295+ #else
296+ sample = std::max (1 , sample - subtract);
297+ #ifdef SCALE_SQRT
298+ subtract += subtract_step;
299+ #endif
300+ #endif
275301 }
276302 else if (sample > 0 ) {
277- r = std::min ((uint16_t )(palette_main[i].r ), sample);
278- g = std::min ((uint16_t )(palette_main[i].g ), sample);
279- b = std::min ((uint16_t )(palette_main[i].b ), sample);
303+ uint16_t int_sample = (uint16_t )fix15_to_int (sample);
304+ r = std::min ((uint16_t )(palette_main[i].r ), int_sample);
305+ g = std::min ((uint16_t )(palette_main[i].g ), int_sample);
306+ b = std::min ((uint16_t )(palette_main[i].b ), int_sample);
280307 eq_history[i][history_idx] = y;
281308 sample = 0 ;
282309 if (maxy < y) {
0 commit comments