Skip to content

Instantly share code, notes, and snippets.

@fire
Created December 3, 2024 14:14
Show Gist options
  • Save fire/311949afc4a071183ecf6753d11f5b83 to your computer and use it in GitHub Desktop.
Save fire/311949afc4a071183ecf6753d11f5b83 to your computer and use it in GitHub Desktop.
extends Node
# Generate a synthetic audio stream
func generate_test_stream(sample_rate: int = 48000, duration: float = 5.0) -> AudioStreamGenerator:
var generator = AudioStreamGenerator.new()
generator.mix_rate = sample_rate
var playback = AudioStreamGeneratorPlayback.new(generator)
var t = 0.0
while t < duration:
var sample = 0.0
if t < 1.0: # 440 Hz tone
sample = sin(2.0 * PI * 440.0 * t)
elif t < 1.5: # Silence
sample = 0.0
elif t < 3.5: # 880 Hz tone with amplitude modulation
sample = sin(2.0 * PI * 880.0 * t) * (t - 1.5)
# Append the sample to the audio stream
playback.push_buffer(Vector2(sample, sample)) # Stereo output
t += 1.0 / sample_rate
return generator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment