-
Notifications
You must be signed in to change notification settings - Fork 2
/
Spectrum.gd
37 lines (31 loc) · 958 Bytes
/
Spectrum.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
extends Control
const VU_COUNT = 200
const FREQ_MAX = 20000.00
const MIN_DB = 100
var spectrum: AudioEffectSpectrumAnalyzerInstance
var img_input: Image
var tex := ImageTexture.new()
var playing := false
func _init():
AudioServer.add_bus_effect(0, AudioEffectSpectrumAnalyzer.new(), 0)
spectrum = AudioServer.get_bus_effect_instance(0, 0)
img_input = Image.new()
img_input.create(VU_COUNT, 1, false, Image.FORMAT_RF)
tex = ImageTexture.new()
tex.create_from_image(img_input)
self.material.set_shader_param("BAR_MAGNITUDES", tex)
func _process(_delta):
if playing:
var prev_hz = 0
img_input.lock()
for i in range(1, VU_COUNT + 1):
var hz = i * FREQ_MAX / VU_COUNT
var f = spectrum.get_magnitude_for_frequency_range(prev_hz, hz)
var energy = clamp(
(MIN_DB + linear2db(f.length())) / MIN_DB,
0, 1
)
img_input.set_pixel(i - 1, 0, Color(energy, 0.0, 0.0))
prev_hz = hz
img_input.unlock()
tex.set_data(img_input)