Audio Generation Demo Broken on 3.5?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By creativeape

So, I’m working on some audio generation stuff, and I have problems generating low notes. C4 anda round works fine, but down at C2 (65.41 hertz), I get wierd audio.

I took the Audio Generator Demo and played around with that to see if it would work there. Turns out I get the same issue. I tried playing a sequence of notes (C2,D2,D#2,F2, G2) to see if it sounds normal or not, but nope.

I have no idea what’s going on here. My sample rate is 22050.

Here’s my slightly modified code from the Audio Generation Demo:

extends Node

var sample_hz = 22050.0 # Keep the number of samples to mix low, GDScript is not super fast.
var pulse_hz = 65.41
var phase = 0.0

var playback: AudioStreamPlayback = null # Actual playback stream, assigned in _ready().

var notes = [
var note = 0
var t = 0

func _fill_buffer():
	var increment = pulse_hz / sample_hz

	var to_fill = playback.get_frames_available()
	while to_fill > 0:
		playback.push_frame(Vector2.ONE * sin(phase * TAU)) # Audio frames are stereo.
		phase = fmod(phase + increment, 1.0)
		to_fill -= 1

func _process(_delta):
	t += _delta
	if t > 1:
		t = 0
		note+= 1
		if note > notes.size() - 1:
			note = 0
		pulse_hz = notes[note]

func _ready():
	$ = sample_hz # Setting mix rate is only possible before play().
	playback = $Player.get_stream_playback()
	_fill_buffer() # Prefill, do before play() to avoid delay.
:bust_in_silhouette: Reply From: JanSchutte

I found a similar issue on godot v4.0beta7. It seems that the call to get_stream_playback returns null, this is because the playback object is only instantiated on the call to Switching the order of these two method calls will let you run the example.

My _ready() looks like this: = sample_hz
playback = $Player.get_stream_playback()

Thanks for the heads up :slight_smile:

I’ve updated the Audio Generator demo accordingly (while also adding a volume slider and generated frequency tone slider) in

Calinou | 2022-12-12 13:14