AudioStreamWAV

package
v0.0.0-...-357ca8a Latest Latest
Warning

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

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

Documentation

Overview

AudioStreamWAV stores sound samples loaded from WAV files. To play the stored sound, use an AudioStreamPlayer (for non-positional audio) or AudioStreamPlayer2D/AudioStreamPlayer3D (for positional audio). The sound can be looped.

This class can also be used to store dynamically-generated PCM audio data. See also AudioStreamGenerator for procedural audio generation.

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
	AsAudioStreamWAV() Instance
}

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]) AsAudioStreamWAV

func (self *Extension[T]) AsAudioStreamWAV() 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 Format

type Format int //gd:AudioStreamWAV.Format
const (
	// 8-bit PCM audio codec.
	Format8Bits Format = 0
	// 16-bit PCM audio codec.
	Format16Bits Format = 1
	// Audio is lossily compressed as IMA ADPCM.
	FormatImaAdpcm Format = 2
	// Audio is lossily compressed as [Quite OK Audio].
	//
	// [Quite OK Audio]: https://qoaformat.org/
	FormatQoa Format = 3
)

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.AudioStreamWAV

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 LoadFromBuffer

func LoadFromBuffer(stream_data []byte, options Options) Instance

Creates a new AudioStreamWAV instance from the given buffer. The buffer must contain WAV data.

The keys and values of 'options' match the properties of ResourceImporterWAV. The usage of 'options' is identical to AudioStreamWAV.LoadFromFile.

func LoadFromFile

func LoadFromFile(path string, options Options) Instance

Creates a new AudioStreamWAV instance from the given file path. The file must be in WAV format.

The keys and values of 'options' match the properties of ResourceImporterWAV.

Example: Load the first file dropped as a WAV and play it:

package main

import (
	"path/filepath"

	"graphics.gd/classdb/AudioStreamPlayer"
	"graphics.gd/classdb/AudioStreamWAV"
	"graphics.gd/classdb/Node"
	"graphics.gd/classdb/Window"
)

type MyAudioLoader struct {
	Node.Extension[MyAudioLoader]

	AudioPlayer AudioStreamPlayer.Instance
}

func (m *MyAudioLoader) Ready() {
	Window.Get(m.AsNode()).OnFilesDropped(func(files []string) {
		if filepath.Ext(files[0]) == "wav" {
			m.AudioPlayer.SetStream(AudioStreamWAV.LoadFromFile(files[0], AudioStreamWAV.Options{
				ForceMaxRate:   true,
				ForceMaxRateHz: 11025,
			}).AsAudioStream())
			m.AudioPlayer.Play()
		}
	})
}

func New

func New() Instance

func (Instance) AsAudioStream

func (self Instance) AsAudioStream() AudioStream.Instance

func (Instance) AsAudioStreamWAV

func (self Instance) AsAudioStreamWAV() 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) Data

func (self Instance) Data() []byte

Contains the audio data in bytes.

Note: If Format is set to Format8Bits, this property expects signed 8-bit PCM data. To convert from unsigned 8-bit PCM, subtract 128 from each byte.

Note: If Format is set to FormatQoa, this property expects data from a full QOA file.

func (Instance) Format

func (self Instance) Format() Format

Audio format.

func (Instance) ID

func (self Instance) ID() ID

func (Instance) LoopBegin

func (self Instance) LoopBegin() int

The loop start point (in number of samples, relative to the beginning of the stream).

func (Instance) LoopEnd

func (self Instance) LoopEnd() int

The loop end point (in number of samples, relative to the beginning of the stream).

func (Instance) LoopMode

func (self Instance) LoopMode() LoopMode

The loop mode.

func (Instance) MixRate

func (self Instance) MixRate() int

The sample rate for mixing this audio. Higher values require more storage space, 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 using lower-pitched sounds such as voices, lower sample rates such as 32000 or 22050 may be usable with no loss in quality.

func (Instance) SaveToWav

func (self Instance) SaveToWav(path string) error

Saves the AudioStreamWAV as a WAV file to 'path'. Samples with IMA ADPCM or Quite OK Audio formats can't be saved.

Note: A .wav extension is automatically appended to 'path' if it is missing.

func (Instance) SetData

func (self Instance) SetData(value []byte)

SetData sets the property returned by [GetData].

func (Instance) SetFormat

func (self Instance) SetFormat(value Format)

SetFormat sets the property returned by [GetFormat].

func (Instance) SetLoopBegin

func (self Instance) SetLoopBegin(value int)

SetLoopBegin sets the property returned by [GetLoopBegin].

func (Instance) SetLoopEnd

func (self Instance) SetLoopEnd(value int)

SetLoopEnd sets the property returned by [GetLoopEnd].

func (Instance) SetLoopMode

func (self Instance) SetLoopMode(value LoopMode)

SetLoopMode sets the property returned by [GetLoopMode].

func (Instance) SetMixRate

func (self Instance) SetMixRate(value int)

SetMixRate sets the property returned by [GetMixRate].

func (*Instance) SetObject

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

func (Instance) SetStereo

func (self Instance) SetStereo(value bool)

SetStereo sets the property returned by [IsStereo].

func (Instance) SetTags

func (self Instance) SetTags(value map[string]interface{})

SetTags sets the property returned by [GetTags].

func (Instance) Stereo

func (self Instance) Stereo() bool

If true, audio is stereo.

func (Instance) Tags

func (self Instance) Tags() map[string]interface{}

Contains user-defined tags if found in the WAV data.

Commonly used tags include title, artist, album, tracknumber, and date (date does not have a standard date format).

Note: No tag is guaranteed to be present in every file, so make sure to account for the keys not always existing.

Note: Only WAV files using a LIST chunk with an identifier of INFO to encode the tags are currently supported.

func (Instance) Virtual

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

type LoopMode

type LoopMode int //gd:AudioStreamWAV.LoopMode
const (
	// Audio does not loop.
	LoopDisabled LoopMode = 0
	// Audio loops the data between [LoopBegin] and [LoopEnd], playing forward only.
	//
	// [LoopBegin]: https://pkg.go.dev/graphics.gd/classdb/#Instance.LoopBegin
	// [LoopEnd]: https://pkg.go.dev/graphics.gd/classdb/#Instance.LoopEnd
	LoopForward LoopMode = 1
	// Audio loops the data between [LoopBegin] and [LoopEnd], playing back and forth.
	//
	// [LoopBegin]: https://pkg.go.dev/graphics.gd/classdb/#Instance.LoopBegin
	// [LoopEnd]: https://pkg.go.dev/graphics.gd/classdb/#Instance.LoopEnd
	LoopPingpong LoopMode = 2
	// Audio loops the data between [LoopBegin] and [LoopEnd], playing backward only.
	//
	// [LoopBegin]: https://pkg.go.dev/graphics.gd/classdb/#Instance.LoopBegin
	// [LoopEnd]: https://pkg.go.dev/graphics.gd/classdb/#Instance.LoopEnd
	LoopBackward LoopMode = 3
)

type Options

type Options struct {
	CompressionMode int     `gd:"compress/mode"`
	LoopBegin       int     `gd:"edit/loop_begin"`
	LoopEnd         int     `gd:"edit/loop_end"`
	LoopMode        int     `gd:"edit/loop_mode"`
	Normalize       int     `gd:"edit/normalize"`
	Trim            bool    `gd:"edit/trim"`
	ForceMinRate    bool    `gd:"force/8_bit"`
	ForceMaxRate    bool    `gd:"force/max_rate"`
	ForceMaxRateHz  float32 `gd:"force/max_rate_hz"`
	ForceMono       bool    `gd:"force/mono"`
}

Jump to

Keyboard shortcuts

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