Attention | Topic was automatically imported from the old Question2Answer platform. | |
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()