1extends Node 2 3var sample_hz = 22050.0 # Keep the number of samples to mix low, GDScript is not super fast. 4var pulse_hz = 440.0 5var phase = 0.0 6 7var playback: AudioStreamPlayback = null # Actual playback stream, assigned in _ready(). 8 9func _fill_buffer(): 10 var increment = pulse_hz / sample_hz 11 12 var to_fill = playback.get_frames_available() 13 while to_fill > 0: 14 playback.push_frame(Vector2.ONE * sin(phase * TAU)) # Audio frames are stereo. 15 phase = fmod(phase + increment, 1.0) 16 to_fill -= 1 17 18 19func _process(_delta): 20 _fill_buffer() 21 22 23func _ready(): 24 $Player.stream.mix_rate = sample_hz # Setting mix rate is only possible before play(). 25 playback = $Player.get_stream_playback() 26 _fill_buffer() # Prefill, do before play() to avoid delay. 27 $Player.play() 28