AudioStreamGenerator

Inherits: AudioStream < Resource < RefCounted < Object

An audio stream with utilities for procedural sound generation.

Description

AudioStreamGenerator is a type of audio stream that does not play back sounds on its own; instead, it expects a script to generate audio data for it. See also AudioStreamGeneratorPlayback.

Here’s a sample on how to use it to generate a sine wave:

GDScriptC#

  1. var playback # Will hold the AudioStreamGeneratorPlayback.
  2. @onready var sample_hz = $AudioStreamPlayer.stream.mix_rate
  3. var pulse_hz = 440.0 # The frequency of the sound wave.
  4. func _ready():
  5. $AudioStreamPlayer.play()
  6. playback = $AudioStreamPlayer.get_stream_playback()
  7. fill_buffer()
  8. func fill_buffer():
  9. var phase = 0.0
  10. var increment = pulse_hz / sample_hz
  11. var frames_available = playback.get_frames_available()
  12. for i in range(frames_available):
  13. playback.push_frame(Vector2.ONE * sin(phase * TAU))
  14. phase = fmod(phase + increment, 1.0)
  1. [Export] public AudioStreamPlayer Player { get; set; }
  2. private AudioStreamGeneratorPlayback _playback; // Will hold the AudioStreamGeneratorPlayback.
  3. private float _sampleHz;
  4. private float _pulseHz = 440.0f; // The frequency of the sound wave.
  5. public override void _Ready()
  6. {
  7. if (Player.Stream is AudioStreamGenerator generator) // Type as a generator to access MixRate.
  8. {
  9. _sampleHz = generator.MixRate;
  10. Player.Play();
  11. _playback = (AudioStreamGeneratorPlayback)Player.GetStreamPlayback();
  12. FillBuffer();
  13. }
  14. }
  15. public void FillBuffer()
  16. {
  17. double phase = 0.0;
  18. float increment = _pulseHz / _sampleHz;
  19. int framesAvailable = _playback.GetFramesAvailable();
  20. for (int i = 0; i < framesAvailable; i++)
  21. {
  22. _playback.PushFrame(Vector2.One * (float)Mathf.Sin(phase * Mathf.Tau));
  23. phase = Mathf.PosMod(phase + increment, 1.0);
  24. }
  25. }

In the example above, the “AudioStreamPlayer” node must use an AudioStreamGenerator as its stream. The fill_buffer function provides audio data for approximating a sine wave.

See also AudioEffectSpectrumAnalyzer for performing real-time audio spectrum analysis.

Note: Due to performance constraints, this class is best used from C# or from a compiled language via GDExtension. If you still want to use this class from GDScript, consider using a lower mix_rate such as 11,025 Hz or 22,050 Hz.

Tutorials

Properties

float

buffer_length

0.5

float

mix_rate

44100.0


Property Descriptions

float buffer_length = 0.5 🔗

  • void set_buffer_length(value: float)

  • float get_buffer_length()

The length of the buffer to generate (in seconds). Lower values result in less latency, but require the script to generate audio data faster, resulting in increased CPU usage and more risk for audio cracking if the CPU can’t keep up.


float mix_rate = 44100.0 🔗

  • void set_mix_rate(value: float)

  • float get_mix_rate()

The sample rate to use (in Hz). Higher values are more demanding for the CPU to generate, but result in better quality.

In games, common sample rates in use are 11025, 16000, 22050, 32000, 44100, and 48000.

According to the Nyquist-Shannon sampling theorem, there is no quality difference to human hearing when going past 40,000 Hz (since most humans can only hear up to ~20,000 Hz, often less). If you are generating lower-pitched sounds such as voices, lower sample rates such as 32000 or 22050 may be usable with no loss in quality.


User-contributed notes

Please read the User-contributed notes policy before submitting a comment.

Previous Next