Can you improve this in some way? I would like it to change colors according to the beats of the audio.

-- Minimal Audio-Only Cyberpunk Disco Script for MPV activated by a key press

-- User Options:
local opts = {
    bpm = 360,  -- Beats per minute (color updates every 60/BPM seconds)
    -- Cyberpunk color palette in RRGGBB format:
    colors = "660000,006600,000066,000000,666600,660066,006666,000000,664400,440066",
}
local options = require 'mp.options'
options.read_options(opts)

-- Split the comma-separated colors into a Lua table.
local color_list = {}
for col in string.gmatch(opts.colors, "([^,]+)") do
    table.insert(color_list, col)
end

local mp = mp
local msg = require 'mp.msg'

-- Global state.
local current_index = 1
local disco_timer = nil
local overlay = nil

-- Get the full window dimensions.
local function get_window_dimensions()
    local w = mp.get_property_number("window-width", 1280)
    local h = mp.get_property_number("window-height", 720)
    if w == 0 or h == 0 then
        w, h = 1280, 720
    end
    return w, h
end

-- Convert a hex color (RRGGBB) to the ASS format, i.e. "&HBBGGRR&".
local function convert_color(hex)
    local r = hex:sub(1,2)
    local g = hex:sub(3,4)
    local b = hex:sub(5,6)
    return "&H" .. b .. g .. r .. "&"
end

-- Create or update an ASS overlay that draws a full-window filled rectangle.
local function update_overlay(color_hex)
    local width, height = get_window_dimensions()
    local ass_color = convert_color(color_hex)
    local ass_event = string.format("{\\r}{\\p1}{\\1c%s}m 0 0 l %d 0 l %d %d l 0 %d{\\p0}",
                                      ass_color, width, width, height, height)
    if not overlay then
        overlay = mp.create_osd_overlay("ass-events")
    end
    overlay.data = ass_event
    overlay:update()
end

-- Remove the overlay.
local function remove_overlay()
    if overlay then
        overlay:remove()
        overlay = nil
    end
end

-- Determine if the file is audio-only.
local function is_audio_only()
    local vtracks = mp.get_property_native("current-tracks/video")
    if not vtracks then
        return true
    end
    for _, t in ipairs(vtracks) do
        if not t.albumart then
            return false
        end
    end
    return true
end

-- Cycle to the next color and update the overlay.
local function update_disco()
    if not is_audio_only() then return end
    current_index = current_index + 1
    if current_index > #color_list then
        current_index = 1
    end
    local color = color_list[current_index]
    update_overlay(color)
end

-- Start the disco timer based on BPM.
local function start_disco()
    if disco_timer then
        disco_timer:stop()
        disco_timer = nil
    end
    local interval = 60 / opts.bpm
    disco_timer = mp.add_periodic_timer(interval, update_disco)
    disco_timer:resume()
    update_disco()  -- Immediate initial update.
end

-- Stop the disco timer, remove the overlay, and revert force-window.
local function stop_disco()
    if disco_timer then
        disco_timer:stop()
        disco_timer = nil
    end
    remove_overlay()
    mp.set_property("force-window", "no")
end

-- Toggle the disco mode:
-- If currently enabled, disable it (video window removed for audio-only playback).
-- If not running and the file is audio-only, force a window and start disco.
local function toggle_disco()
    if disco_timer then
        stop_disco()
        mp.msg.info("Disco mode disabled: now playing audio only.")
    else
        if is_audio_only() then
            mp.set_property("force-window", "yes")
            start_disco()
            mp.msg.info("Disco mode enabled: showing video with colors.")
        else
            mp.msg.warn("Current file is not audio-only!")
        end
    end
end

-- Bind the toggle to key "d". You can change "d" if desired.
mp.add_key_binding("TAB", "toggle-disco", toggle_disco)