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 = [
	65.41,
	73.42,
	77.78,
	87.31,
	98.00
]
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):
	_fill_buffer()
	t += _delta
	if t > 1:
		t = 0
		note+= 1
		if note > notes.size() - 1:
			note = 0
		
		pulse_hz = notes[note]


func _ready():
	$Player.stream.mix_rate = sample_hz # Setting mix rate is only possible before play().
	playback = $Player.get_stream_playback()
	_fill_buffer() # Prefill, do before play() to avoid delay.
	$Player.play()
: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 AudioStreamPlayer.play(). Switching the order of these two method calls will let you run the example.

My _ready() looks like this:

audio_player.stream.mix_rate = sample_hz
$Player.play()
playback = $Player.get_stream_playback()
_fill_buffer()

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 https://github.com/godotengine/godot-demo-projects/pull/782.

Calinou | 2022-12-12 13:14