AudioStreamGenerator

package
v0.0.0-...-9b8b246 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 23, 2025 License: MIT Imports: 25 Imported by: 0

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

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 Any

type Any interface {
	gd.IsClass
	AsAudioStreamGenerator() Instance
}

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

type Extension[T gdclass.Interface] struct{ gdclass.Extension[T, Instance] }

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 (self *Extension[T]) AsAudioStreamGenerator() Instance

func (*Extension[T]) AsObject

func (self *Extension[T]) AsObject() [1]gd.Object

func (*Extension[T]) AsRefCounted

func (self *Extension[T]) AsRefCounted() [1]gd.RefCounted

func (*Extension[T]) AsResource

func (self *Extension[T]) AsResource() Resource.Instance

type ID

type ID Object.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.

func (ID) Instance

func (id ID) Instance() (Instance, bool)

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 New

func New() Instance

func (Instance) AsAudioStream

func (self Instance) AsAudioStream() AudioStream.Instance

func (Instance) AsAudioStreamGenerator

func (self Instance) AsAudioStreamGenerator() Instance

func (Instance) AsObject

func (self Instance) AsObject() [1]gd.Object

func (Instance) AsRefCounted

func (self Instance) AsRefCounted() [1]gd.RefCounted

func (Instance) AsResource

func (self Instance) AsResource() Resource.Instance

func (Instance) BufferLength

func (self Instance) BufferLength() Float.X

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) ID

func (self Instance) ID() ID

func (Instance) MixRate

func (self Instance) MixRate() Float.X

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

func (self Instance) SetBufferLength(value Float.X)

SetBufferLength sets the property returned by [GetBufferLength].

func (Instance) SetMixRate

func (self Instance) SetMixRate(value Float.X)

SetMixRate sets the property returned by [GetMixRate].

func (Instance) SetMixRateMode

func (self Instance) SetMixRateMode(value AudioStreamGeneratorMixRate)

SetMixRateMode sets the property returned by [GetMixRateMode].

func (*Instance) SetObject

func (self *Instance) SetObject(obj [1]gd.Object) bool

func (Instance) Virtual

func (self Instance) Virtual(name string) reflect.Value

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL