-
Notifications
You must be signed in to change notification settings - Fork 31
/
audio.gd
75 lines (56 loc) · 1.75 KB
/
audio.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
extends Spatial
# shadertoy texture logic https://gist.github.com/soulthreads/2efe50da4be1fb5f7ab60ff14ca434b8
var img_width = 512
var img_heigh = 2
const VU_COUNT = 512
const FREQ_MAX = 11025.0
const MIN_DB = 72
const dB_min = -100
const dB_max = -30
onready var spectrum=AudioServer.get_bus_effect_instance(1,0)
onready var anode=get_node("AudioStreamPlayer3D")
var prev_array=[]
var this_array=[]
var cmat=[]
func _ready():
cmat.append(get_node("../debug/audio/audio").material)
cmat.append(get_node("../bg_particles/cubes").material_override)
for a in range(img_width*img_heigh):
prev_array.append(0)
this_array.append(0)
func gen_spect_texture(update_audio):
#fft
var prev_hz = 0
for a in range(1, VU_COUNT+1):
var hz = a * FREQ_MAX / VU_COUNT;
var magnitude: float = spectrum.get_magnitude_for_frequency_range(prev_hz, hz).length()
var energy = 0
if(update_audio):
energy=clamp((MIN_DB + clamp(linear2db(magnitude),dB_min,dB_max)) / MIN_DB, 0, 1)
energy=0.8*prev_array[a-1]+0.2*energy
this_array[a-1]=int(clamp((energy*255),0,255))
prev_array[a-1]=energy
prev_hz = hz
#wave (not implemented)
# for a in range(img_width):
# this_array[img_width+a]=0
var byte_array = PoolByteArray(this_array)
var img = Image.new()
img.create_from_data(img_width, img_heigh, false, Image.FORMAT_R8, byte_array)
var texture = ImageTexture.new()
texture.create_from_image(img, 0)
return texture
var iTime=0
var ptime=0
func _process(delta):
iTime+=delta
if(anode.playing):
ptime=iTime
var tmpt=gen_spect_texture(true)
for a in range(cmat.size()):
cmat[a].set("shader_param/iChannel0",tmpt)
else:
if((iTime-ptime)<1):
var tmpt=gen_spect_texture(false)
for a in range(cmat.size()):
cmat[a].set("shader_param/iChannel0",tmpt)