extends Node3D

@export var base_spin_speed := 0.5        # Constant slow spin speed
@export var beat_boost := 2.0             # How much faster it spins on the beat
@export var smoothness := 0.1             # Lower = smoother response
@export var music_bus := "MusicBus"       # Bus name where analyzer is added

@onready var analyzer = AudioServer.get_bus_effect_instance(
	AudioServer.get_bus_index(music_bus), 0
)

var smoothed_bass := 0.0

func _process(delta: float) -> void:
	if not analyzer:
		return

	# Analyze low frequencies (20–200Hz) for beat detection
	var bass = analyzer.get_magnitude_for_frequency_range(20, 200)
	var bass_strength = bass.length() * 10.0

	# Smooth the value to avoid jitter
	smoothed_bass = lerp(smoothed_bass, bass_strength, smoothness)

	# Spin speed: base + a small boost from the beat
	var spin_speed = base_spin_speed + smoothed_bass * beat_boost

	# Apply rotation (e.g. around Y-axis)
	rotation.y += spin_speed * delta
