Last active
May 11, 2020 17:20
-
-
Save RKiemGames/976e5e115531489d84690b3e66acae74 to your computer and use it in GitHub Desktop.
Godot Audio Generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends Node2D | |
var playback = null | |
var phase = 0.1 | |
const hz = 1024 | |
const notes = {'s':0.01,'do':0.8,'re':0.9,'mi':1.01,'fa':1.06,'sol':1.18,'la':1.34,'si':1.52} | |
# Q: DO, W: RE, E: MI, R: FA, T: SOL, Y: LA, U: SI | |
const keys = {81:'do',87:'re',69:'mi',82:'fa',84:'sol',89:'la',85:'si'} | |
func _fill_buffer(): | |
var to_fill = playback.get_frames_available() | |
while (to_fill > 0): | |
playback.push_frame(Vector2(1.0, 1.0) * sin(phase * (PI * 2.0))) | |
phase = fmod((phase + (1.0 / (hz / 8.0))), 1.0) | |
to_fill -= 1 | |
func _ready(): | |
playback = $AudioStreamPlayer.get_stream_playback() | |
_fill_buffer() | |
$AudioStreamPlayer.play() | |
set_process(true) | |
func _input(event): | |
if event is InputEventKey and event.scancode in keys.keys(): | |
$AudioStreamPlayer.pitch_scale = notes[keys[event.scancode]] | |
$mute.stop() | |
func _process(delta): | |
_fill_buffer() | |
if $mute.is_stopped(): | |
$mute.start() | |
func _on_mute_timeout(): | |
$AudioStreamPlayer.pitch_scale = 0.01 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[gd_scene load_steps=3 format=2] | |
[ext_resource path="res://audiogen.gd" type="Script" id=1] | |
[sub_resource type="AudioStreamGenerator" id=1] | |
[node name="Audiogen" type="Node2D"] | |
position = Vector2( 12, 6.27181 ) | |
script = ExtResource( 1 ) | |
[node name="AudioStreamPlayer" type="AudioStreamPlayer" parent="."] | |
stream = SubResource( 1 ) | |
pitch_scale = 0.01 | |
[node name="mute" type="Timer" parent="."] | |
process_mode = 0 | |
wait_time = 0.5 | |
autostart = true | |
[connection signal="timeout" from="mute" to="." method="_on_mute_timeout"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[gd_resource type="AudioBusLayout" load_steps=2 format=2] | |
[sub_resource type="AudioEffectReverb" id=1] | |
resource_name = "Reverb" | |
damping = 0.35 | |
spread = 0.27 | |
hipass = 0.4 | |
dry = 0.06 | |
wet = 0.32 | |
[resource] | |
bus/0/effect/0/effect = SubResource( 1 ) | |
bus/0/effect/0/enabled = true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment