Documentation
¶
Overview ¶
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:
package main import ( "graphics.gd/classdb/AudioStreamGenerator" "graphics.gd/classdb/AudioStreamGeneratorPlayback" "graphics.gd/classdb/AudioStreamPlayer" "graphics.gd/classdb/Node" "graphics.gd/variant/Angle" "graphics.gd/variant/Float" "graphics.gd/variant/Object" "graphics.gd/variant/Vector2" ) type ExampleAudioStreamGenerator struct { Node.Extension[ExampleAudioStreamGenerator] AudioStreamPlayer AudioStreamPlayer.Instance playback AudioStreamGeneratorPlayback.Instance sample_hz, pulse_hz Float.X phase Angle.Radians } func (eg *ExampleAudioStreamGenerator) Ready() { eg.sample_hz = Object.To[AudioStreamGenerator.Instance](eg.AudioStreamPlayer.Stream()).AsAudioStreamGenerator().MixRate() eg.AudioStreamPlayer.Play() eg.playback = Object.To[AudioStreamGeneratorPlayback.Instance](eg.AudioStreamPlayer.GetStreamPlayback()) eg.FillBuffer() } func (eg *ExampleAudioStreamGenerator) FillBuffer() { increment := Angle.Radians(eg.pulse_hz / eg.sample_hz) frames_available := eg.playback.GetFramesAvailable() for range frames_available { eg.playback.PushFrame(Vector2.AddX(Vector2.One, Angle.Sin(eg.phase*Angle.Tau))) eg.phase = Float.Mod(eg.phase+increment, 1.0) } }
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 MixRate such as 11,025 Hz or 22,050 Hz.
Index ¶
- type Advanced
- type Any
- type AudioStreamGeneratorMixRate
- type Extension
- type ID
- type Instance
- func (self Instance) AsAudioStream() AudioStream.Instance
- func (self Instance) AsAudioStreamGenerator() Instance
- func (self Instance) AsObject() [1]gd.Object
- func (self Instance) AsRefCounted() [1]gd.RefCounted
- func (self Instance) AsResource() Resource.Instance
- func (self Instance) BufferLength() Float.X
- func (self Instance) ID() ID
- func (self Instance) MixRate() Float.X
- func (self Instance) MixRateMode() AudioStreamGeneratorMixRate
- func (self Instance) SetBufferLength(value Float.X)
- func (self Instance) SetMixRate(value Float.X)
- func (self Instance) SetMixRateMode(value AudioStreamGeneratorMixRate)
- func (self *Instance) SetObject(obj [1]gd.Object) bool
- func (self Instance) Virtual(name string) reflect.Value
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Advanced ¶
type Advanced = class
Advanced exposes a 1:1 low-level instance of the class, undocumented, for those who know what they are doing.
type AudioStreamGeneratorMixRate ¶
type AudioStreamGeneratorMixRate int //gd:AudioStreamGenerator.AudioStreamGeneratorMixRate
const ( // Current [AudioServer] output mixing rate. // // [AudioServer]: https://pkg.go.dev/graphics.gd/classdb/AudioServer MixRateOutput AudioStreamGeneratorMixRate = 0 // Current [AudioServer] input mixing rate. // // [AudioServer]: https://pkg.go.dev/graphics.gd/classdb/AudioServer MixRateInput AudioStreamGeneratorMixRate = 1 // Custom mixing rate, specified by [MixRate]. // // [MixRate]: https://pkg.go.dev/graphics.gd/classdb/#Instance.MixRate MixRateCustom AudioStreamGeneratorMixRate = 2 // Maximum value for the mixing rate mode enum. MixRateMax AudioStreamGeneratorMixRate = 3 )
type Extension ¶
Extension can be embedded in a new struct to create an extension of this class. T should be the type that is embedding this Extension
func (*Extension[T]) AsAudioStream ¶
func (self *Extension[T]) AsAudioStream() AudioStream.Instance
func (*Extension[T]) AsAudioStreamGenerator ¶
func (*Extension[T]) AsRefCounted ¶
func (self *Extension[T]) AsRefCounted() [1]gd.RefCounted
func (*Extension[T]) AsResource ¶
type ID ¶
ID is a typed object ID (reference) to an instance of this class, use it to store references to objects with unknown lifetimes, as an ID will not panic on use if the underlying object has been destroyed.
type Instance ¶
type Instance [1]gdclass.AudioStreamGenerator
Instance of the class with convieniently typed arguments and results.
var Nil Instance
Nil is a nil/null instance of the class. Equivalent to the zero value.
func (Instance) AsAudioStream ¶
func (self Instance) AsAudioStream() AudioStream.Instance
func (Instance) AsAudioStreamGenerator ¶
func (Instance) AsRefCounted ¶
func (self Instance) AsRefCounted() [1]gd.RefCounted
func (Instance) AsResource ¶
func (Instance) BufferLength ¶
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.
func (Instance) MixRate ¶
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.
Note: AudioStreamGenerator is not automatically resampling input data, to produce expected result MixRateMode should match the sampling rate of input data.
Note: If you are using AudioEffectCapture as the source of your data, set MixRateMode to MixRateInput or MixRateOutput to automatically match current AudioServer mixing rate.
func (Instance) MixRateMode ¶
func (self Instance) MixRateMode() AudioStreamGeneratorMixRate
Mixing rate mode. If set to MixRateCustom, MixRate is used, otherwise current AudioServer mixing rate is used.
func (Instance) SetBufferLength ¶
SetBufferLength sets the property returned by [GetBufferLength].
func (Instance) SetMixRate ¶
SetMixRate sets the property returned by [GetMixRate].
func (Instance) SetMixRateMode ¶
func (self Instance) SetMixRateMode(value AudioStreamGeneratorMixRate)
SetMixRateMode sets the property returned by [GetMixRateMode].