瀏覽代碼

Demos for sound generation and audio spectrum analysis.

Juan Linietsky 6 年之前
父節點
當前提交
c819e3b276

+ 11 - 0
audio/generator/generator.tscn

@@ -0,0 +1,11 @@
+[gd_scene load_steps=3 format=2]
+
+[ext_resource path="res://generator_demo.gd" type="Script" id=1]
+
+[sub_resource type="AudioStreamGenerator" id=1]
+
+[node name="generator" type="Node"]
+script = ExtResource( 1 )
+
+[node name="player" type="AudioStreamPlayer" parent="."]
+stream = SubResource( 1 )

+ 28 - 0
audio/generator/generator_demo.gd

@@ -0,0 +1,28 @@
+extends Node
+
+
+var hz = 22050.0 # less samples to mix, GDScript is not super fast for this
+var phase = 0.0
+
+var pulse_hz = 440.0
+var playback = null #object that does the actual playback
+
+func _fill_buffer():
+	var increment = (1.0 / (hz / pulse_hz)) 
+		
+	var to_fill = playback.get_frames_available()
+	while (to_fill > 0):
+		playback.push_frame( Vector2(1.0,1.0) * sin(phase * (PI * 2.0)) ) # frames are stereo
+		phase = fmod((phase + increment), 1.0)
+		to_fill-=1;
+
+func _process(delta):
+	_fill_buffer()
+
+	
+func _ready():
+	$player.stream.mix_rate=hz #setting hz is only possible before playing
+	playback = $player.get_stream_playback()
+	_fill_buffer() # prefill, do before play to avoid delay 
+	$player.play() # start
+

+ 18 - 0
audio/generator/project.godot

@@ -0,0 +1,18 @@
+; Engine configuration file.
+; It's best edited using the editor UI and not directly,
+; since the parameters that go here are not all obvious.
+;
+; Format:
+;   [section] ; section goes between []
+;   param=value ; assign values to parameters
+
+config_version=4
+
+_global_script_classes=[  ]
+_global_script_class_icons={
+
+}
+
+[application]
+
+run/main_scene="res://generator.tscn"

+ 8 - 0
audio/spectrum/default_bus_layout.tres

@@ -0,0 +1,8 @@
+[gd_resource type="AudioBusLayout" load_steps=2 format=2]
+
+[sub_resource type="AudioEffectSpectrumAnalyzer" id=1]
+resource_name = "SpectrumAnalyzer"
+
+[resource]
+bus/0/effect/0/effect = SubResource( 1 )
+bus/0/effect/0/enabled = true

二進制
audio/spectrum/maldita.ogg


+ 15 - 0
audio/spectrum/maldita.ogg.import

@@ -0,0 +1,15 @@
+[remap]
+
+importer="ogg_vorbis"
+type="AudioStreamOGGVorbis"
+path="res://.import/maldita.ogg-16f33b83786e8d938ac9e0b887e47ec6.oggstr"
+
+[deps]
+
+source_file="res://maldita.ogg"
+dest_files=[ "res://.import/maldita.ogg-16f33b83786e8d938ac9e0b887e47ec6.oggstr" ]
+
+[params]
+
+loop=true
+loop_offset=0

+ 21 - 0
audio/spectrum/maldita.wav.import

@@ -0,0 +1,21 @@
+[remap]
+
+importer="wav"
+type="AudioStreamSample"
+path="res://.import/maldita.wav-82d9f1ad42df5bdfaeda0654a708cb7a.sample"
+
+[deps]
+
+source_file="res://maldita.wav"
+dest_files=[ "res://.import/maldita.wav-82d9f1ad42df5bdfaeda0654a708cb7a.sample" ]
+
+[params]
+
+force/8_bit=false
+force/mono=false
+force/max_rate=false
+force/max_rate_hz=44100
+edit/trim=true
+edit/normalize=true
+edit/loop=false
+compress/mode=0

+ 18 - 0
audio/spectrum/project.godot

@@ -0,0 +1,18 @@
+; Engine configuration file.
+; It's best edited using the editor UI and not directly,
+; since the parameters that go here are not all obvious.
+;
+; Format:
+;   [section] ; section goes between []
+;   param=value ; assign values to parameters
+
+config_version=4
+
+_global_script_classes=[  ]
+_global_script_class_icons={
+
+}
+
+[application]
+
+run/main_scene="res://show_spectrum.tscn"

+ 36 - 0
audio/spectrum/show_spectrum.gd

@@ -0,0 +1,36 @@
+extends Node2D
+
+
+const VU_COUNT=16
+const FREQ_MAX = 11050.0
+
+const WIDTH = 400
+const HEIGHT = 100
+
+const MIN_DB = 60
+
+var spectrum
+
+func _draw():
+		
+	var w = WIDTH / VU_COUNT
+	var prev_hz = 0
+	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)
+		#print("db ",db,": ",f.length())
+		var height = energy * HEIGHT
+		draw_rect(Rect2(w*i,HEIGHT-height,w,height),Color(1,1,1))
+		prev_hz = hz
+	
+
+func _process(delta):
+	update()
+
+func _ready():
+	spectrum = AudioServer.get_bus_effect_instance(0,0)
+
+# Called every frame. 'delta' is the elapsed time since the previous frame.
+#func _process(delta):
+#	pass

+ 11 - 0
audio/spectrum/show_spectrum.tscn

@@ -0,0 +1,11 @@
+[gd_scene load_steps=3 format=2]
+
+[ext_resource path="res://show_spectrum.gd" type="Script" id=1]
+[ext_resource path="res://maldita.ogg" type="AudioStream" id=2]
+
+[node name="show_spectrum" type="Node2D"]
+script = ExtResource( 1 )
+
+[node name="player" type="AudioStreamPlayer" parent="."]
+stream = ExtResource( 2 )
+autoplay = true