Skip to content

Commit c819e3b

Browse files
committed
Demos for sound generation and audio spectrum analysis.
1 parent 56a2b28 commit c819e3b

File tree

10 files changed

+166
-0
lines changed

10 files changed

+166
-0
lines changed

audio/generator/generator.tscn

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[gd_scene load_steps=3 format=2]
2+
3+
[ext_resource path="res://generator_demo.gd" type="Script" id=1]
4+
5+
[sub_resource type="AudioStreamGenerator" id=1]
6+
7+
[node name="generator" type="Node"]
8+
script = ExtResource( 1 )
9+
10+
[node name="player" type="AudioStreamPlayer" parent="."]
11+
stream = SubResource( 1 )

audio/generator/generator_demo.gd

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
extends Node
2+
3+
4+
var hz = 22050.0 # less samples to mix, GDScript is not super fast for this
5+
var phase = 0.0
6+
7+
var pulse_hz = 440.0
8+
var playback = null #object that does the actual playback
9+
10+
func _fill_buffer():
11+
var increment = (1.0 / (hz / pulse_hz))
12+
13+
var to_fill = playback.get_frames_available()
14+
while (to_fill > 0):
15+
playback.push_frame( Vector2(1.0,1.0) * sin(phase * (PI * 2.0)) ) # frames are stereo
16+
phase = fmod((phase + increment), 1.0)
17+
to_fill-=1;
18+
19+
func _process(delta):
20+
_fill_buffer()
21+
22+
23+
func _ready():
24+
$player.stream.mix_rate=hz #setting hz is only possible before playing
25+
playback = $player.get_stream_playback()
26+
_fill_buffer() # prefill, do before play to avoid delay
27+
$player.play() # start
28+

audio/generator/project.godot

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; Engine configuration file.
2+
; It's best edited using the editor UI and not directly,
3+
; since the parameters that go here are not all obvious.
4+
;
5+
; Format:
6+
; [section] ; section goes between []
7+
; param=value ; assign values to parameters
8+
9+
config_version=4
10+
11+
_global_script_classes=[ ]
12+
_global_script_class_icons={
13+
14+
}
15+
16+
[application]
17+
18+
run/main_scene="res://generator.tscn"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[gd_resource type="AudioBusLayout" load_steps=2 format=2]
2+
3+
[sub_resource type="AudioEffectSpectrumAnalyzer" id=1]
4+
resource_name = "SpectrumAnalyzer"
5+
6+
[resource]
7+
bus/0/effect/0/effect = SubResource( 1 )
8+
bus/0/effect/0/enabled = true

audio/spectrum/maldita.ogg

1.51 MB
Binary file not shown.

audio/spectrum/maldita.ogg.import

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[remap]
2+
3+
importer="ogg_vorbis"
4+
type="AudioStreamOGGVorbis"
5+
path="res://.import/maldita.ogg-16f33b83786e8d938ac9e0b887e47ec6.oggstr"
6+
7+
[deps]
8+
9+
source_file="res://maldita.ogg"
10+
dest_files=[ "res://.import/maldita.ogg-16f33b83786e8d938ac9e0b887e47ec6.oggstr" ]
11+
12+
[params]
13+
14+
loop=true
15+
loop_offset=0

audio/spectrum/maldita.wav.import

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[remap]
2+
3+
importer="wav"
4+
type="AudioStreamSample"
5+
path="res://.import/maldita.wav-82d9f1ad42df5bdfaeda0654a708cb7a.sample"
6+
7+
[deps]
8+
9+
source_file="res://maldita.wav"
10+
dest_files=[ "res://.import/maldita.wav-82d9f1ad42df5bdfaeda0654a708cb7a.sample" ]
11+
12+
[params]
13+
14+
force/8_bit=false
15+
force/mono=false
16+
force/max_rate=false
17+
force/max_rate_hz=44100
18+
edit/trim=true
19+
edit/normalize=true
20+
edit/loop=false
21+
compress/mode=0

audio/spectrum/project.godot

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; Engine configuration file.
2+
; It's best edited using the editor UI and not directly,
3+
; since the parameters that go here are not all obvious.
4+
;
5+
; Format:
6+
; [section] ; section goes between []
7+
; param=value ; assign values to parameters
8+
9+
config_version=4
10+
11+
_global_script_classes=[ ]
12+
_global_script_class_icons={
13+
14+
}
15+
16+
[application]
17+
18+
run/main_scene="res://show_spectrum.tscn"

audio/spectrum/show_spectrum.gd

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
extends Node2D
2+
3+
4+
const VU_COUNT=16
5+
const FREQ_MAX = 11050.0
6+
7+
const WIDTH = 400
8+
const HEIGHT = 100
9+
10+
const MIN_DB = 60
11+
12+
var spectrum
13+
14+
func _draw():
15+
16+
var w = WIDTH / VU_COUNT
17+
var prev_hz = 0
18+
for i in range(1,VU_COUNT+1):
19+
var hz = i * FREQ_MAX / VU_COUNT;
20+
var f = spectrum.get_magnitude_for_frequency_range(prev_hz,hz)
21+
var energy = clamp((MIN_DB + linear2db(f.length()))/MIN_DB,0,1)
22+
#print("db ",db,": ",f.length())
23+
var height = energy * HEIGHT
24+
draw_rect(Rect2(w*i,HEIGHT-height,w,height),Color(1,1,1))
25+
prev_hz = hz
26+
27+
28+
func _process(delta):
29+
update()
30+
31+
func _ready():
32+
spectrum = AudioServer.get_bus_effect_instance(0,0)
33+
34+
# Called every frame. 'delta' is the elapsed time since the previous frame.
35+
#func _process(delta):
36+
# pass

audio/spectrum/show_spectrum.tscn

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[gd_scene load_steps=3 format=2]
2+
3+
[ext_resource path="res://show_spectrum.gd" type="Script" id=1]
4+
[ext_resource path="res://maldita.ogg" type="AudioStream" id=2]
5+
6+
[node name="show_spectrum" type="Node2D"]
7+
script = ExtResource( 1 )
8+
9+
[node name="player" type="AudioStreamPlayer" parent="."]
10+
stream = ExtResource( 2 )
11+
autoplay = true

0 commit comments

Comments
 (0)