Refactor volume_multiplier

This commit is contained in:
2025-05-21 20:37:47 +02:00
parent 4e3d7c45a2
commit 13cf70b984
5 changed files with 181 additions and 81 deletions

View File

@@ -27,76 +27,90 @@ end led_level_controller;
architecture Behavioral of led_level_controller is
constant REFRESH_CYCLES : natural := (refresh_time_ms * 1_000_000) / clock_period_ns;
signal volume_value : signed(CHANNEL_LENGHT-1 downto 0) := (others => '0');
signal abs_audio : unsigned(CHANNEL_LENGHT-2 downto 0) := (others => '0');
signal leds_int : std_logic_vector(NUM_LEDS-1 downto 0) := (others => '0');
signal led_update : std_logic := '0';
signal refresh_counter : natural range 0 to REFRESH_CYCLES-1 := 0;
signal volume_value : signed(CHANNEL_LENGHT-1 downto 0) := (others => '0');
signal abs_audio_left : unsigned(CHANNEL_LENGHT-2 downto 0) := (others => '0');
signal abs_audio_right : unsigned(CHANNEL_LENGHT-2 downto 0) := (others => '0');
signal leds_int : std_logic_vector(NUM_LEDS-1 downto 0) := (others => '0');
signal led_update : std_logic := '0';
signal refresh_counter : natural range 0 to REFRESH_CYCLES-1 := 0;
begin
led <= leds_int;
s_axis_tready <= '1';
-- Register the audio absolute value
-- Registrazione del valore audio assoluto
process(aclk)
variable sdata_signed : signed(CHANNEL_LENGHT-1 downto 0);
variable abs_value : unsigned(CHANNEL_LENGHT-1 downto 0);
begin
if rising_edge(aclk) then
if aresetn = '0' then
volume_value <= (others => '0');
volume_value <= (others => '0');
abs_audio_left <= (others => '0');
abs_audio_right<= (others => '0');
elsif s_axis_tvalid = '1' then
volume_value <= signed(s_axis_tdata);
if volume_value(volume_value'high) = '1' then
abs_audio <= unsigned(-volume_value(CHANNEL_LENGHT-2 downto 0));
sdata_signed := signed(s_axis_tdata);
volume_value <= sdata_signed;
-- Calcolo valore assoluto
if sdata_signed(CHANNEL_LENGHT-1) = '1' then
abs_value := unsigned(-sdata_signed);
else
abs_audio <= unsigned(volume_value(CHANNEL_LENGHT-2 downto 0));
abs_value := unsigned(sdata_signed);
end if;
-- Assegna al canale corretto
if s_axis_tlast = '1' then -- Canale sinistro
abs_audio_left <= abs_value(CHANNEL_LENGHT-2 downto 0);
else -- Canale destro
abs_audio_right <= abs_value(CHANNEL_LENGHT-2 downto 0);
end if;
end if;
end if;
end process;
-- Refresh counter
-- Contatore di refresh
process(aclk)
begin
if rising_edge(aclk) then
if aresetn = '0' then
refresh_counter <= 0;
led_update <= '0';
elsif refresh_counter = REFRESH_CYCLES-1 then
refresh_counter <= 0;
led_update <= '1';
else
if refresh_counter = REFRESH_CYCLES-1 then
refresh_counter <= 0;
led_update <= '1';
else
refresh_counter <= refresh_counter + 1;
led_update <= '0';
end if;
refresh_counter <= refresh_counter + 1;
led_update <= '0';
end if;
end if;
end process;
-- Linear scaling of the audio signal to LED levels
-- Scaling lineare e aggiornamento LED
process(aclk)
variable leds_on : natural range 0 to NUM_LEDS;
variable leds_on : natural range 0 to NUM_LEDS;
variable temp_led_level : integer range 0 to NUM_LEDS;
variable abs_audio_sum : unsigned(CHANNEL_LENGHT-1 downto 0);
begin
if rising_edge(aclk) then
if aresetn = '0' then
leds_int <= (others => '0');
elsif led_update = '1' then
-- Automatic linear scaling calculation:
if to_integer(abs_audio) = 0 then
abs_audio_sum := resize(abs_audio_left, CHANNEL_LENGHT) + resize(abs_audio_right, CHANNEL_LENGHT);
if (abs_audio_left = 0 and abs_audio_right = 0) then
temp_led_level := 0;
else -- -1 bit for sign, -4 to get 15+1 levels
temp_led_level := to_integer(shift_right(abs_audio,CHANNEL_LENGHT-4-1))+1;
else
-- Scaling automatico: puoi regolare la costante di shift per la sensibilit<69>
temp_led_level := 1 + to_integer(shift_right(abs_audio_sum, CHANNEL_LENGHT-4));
end if;
-- Limit to maximum number of LEDs
-- Limita al massimo numero di LED
if temp_led_level > NUM_LEDS then
leds_on := NUM_LEDS;
else
leds_on := temp_led_level;
end if;
-- Aggiorna i LED
leds_int <= (others => '0');
if leds_on > 0 then
leds_int(leds_on-1 downto 0) <= (others => '1');