mirror of
https://github.com/godotengine/godot-demo-projects.git
synced 2026-01-04 15:00:09 +01:00
Demos for sound generation and audio spectrum analysis.
This commit is contained in:
11
audio/generator/generator.tscn
Normal file
11
audio/generator/generator.tscn
Normal file
@@ -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
audio/generator/generator_demo.gd
Normal file
28
audio/generator/generator_demo.gd
Normal file
@@ -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
audio/generator/project.godot
Normal file
18
audio/generator/project.godot
Normal file
@@ -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
audio/spectrum/default_bus_layout.tres
Normal file
8
audio/spectrum/default_bus_layout.tres
Normal file
@@ -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
|
||||
BIN
audio/spectrum/maldita.ogg
Normal file
BIN
audio/spectrum/maldita.ogg
Normal file
Binary file not shown.
15
audio/spectrum/maldita.ogg.import
Normal file
15
audio/spectrum/maldita.ogg.import
Normal file
@@ -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
audio/spectrum/maldita.wav.import
Normal file
21
audio/spectrum/maldita.wav.import
Normal file
@@ -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
audio/spectrum/project.godot
Normal file
18
audio/spectrum/project.godot
Normal file
@@ -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
audio/spectrum/show_spectrum.gd
Normal file
36
audio/spectrum/show_spectrum.gd
Normal file
@@ -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
audio/spectrum/show_spectrum.tscn
Normal file
11
audio/spectrum/show_spectrum.tscn
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user