AudioStreamWAV
Inherits: AudioStream < Resource < Shared < Object
Stores audio data loaded from WAV files.
Description
Section titled “Description”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.
Properties
Section titled “Properties”| Type | Name | Default |
|---|---|---|
[]u8 | data | PackedByteArray() |
| AudioStreamWAV.Format | format | 0 |
| int | loop_begin | 0 |
| int | loop_end | 0 |
| AudioStreamWAV.LoopMode | loop_mode | 0 |
| int | mix_rate | 44100 |
| bool | stereo | false |
| Dictionary | tags | {} |
Methods
Section titled “Methods”| Returns | Method |
|---|---|
| AudioStreamWAV | load_from_buffer(stream_data, options) |
| AudioStreamWAV | load_from_file(path, options) |
| Error | save_to_wav(path) |
Enumerations and constants
Section titled “Enumerations and constants”enum Format
Section titled “enum Format”- FORMAT_8_BITS =
0— 8-bit PCM audio codec.
- FORMAT_16_BITS =
1— 16-bit PCM audio codec.
- FORMAT_IMA_ADPCM =
2— Audio is lossily compressed as IMA ADPCM.
- FORMAT_QOA =
3— Audio is lossily compressed as Quite OK Audio.
enum LoopMode
Section titled “enum LoopMode”- LOOP_DISABLED =
0— Audio does not loop.
- LOOP_FORWARD =
1— Audio loops the data betweenloop_beginandloop_end, playing forward only.
- LOOP_PINGPONG =
2— Audio loops the data betweenloop_beginandloop_end, playing back and forth.
- LOOP_BACKWARD =
3— Audio loops the data betweenloop_beginandloop_end, playing backward only.
Property descriptions
Section titled “Property descriptions”[]u8 data = PackedByteArray()
Setter/getter: set_data(), get_data()
Contains the audio data in bytes.
Note: If format is set to FORMAT_8_BITS, 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 FORMAT_QOA, this property expects data from a full QOA file.
format
Section titled “format”AudioStreamWAV.Format format = 0
Setter/getter: set_format(), get_format()
Audio format.
loop_begin
Section titled “loop_begin”int loop_begin = 0
Setter/getter: set_loop_begin(), get_loop_begin()
The loop start point (in number of samples, relative to the beginning of the stream).
loop_end
Section titled “loop_end”int loop_end = 0
Setter/getter: set_loop_end(), get_loop_end()
The loop end point (in number of samples, relative to the beginning of the stream).
loop_mode
Section titled “loop_mode”AudioStreamWAV.LoopMode loop_mode = 0
Setter/getter: set_loop_mode(), get_loop_mode()
The loop mode.
mix_rate
Section titled “mix_rate”int mix_rate = 44100
Setter/getter: set_mix_rate(), get_mix_rate()
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.
stereo
Section titled “stereo”bool stereo = false
Setter/getter: set_stereo(), is_stereo()
If true, audio is stereo.
Dictionary tags = {}
Setter/getter: set_tags(), get_tags()
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.
Method descriptions
Section titled “Method descriptions”load_from_buffer
Section titled “load_from_buffer”AudioStreamWAV load_from_buffer(stream_data: []u8, options: Dictionary = {}) static
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 load_from_file().
load_from_file
Section titled “load_from_file”AudioStreamWAV load_from_file(path: String, options: Dictionary = {}) static
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:
use actor::AudioStreamPlayer as audio_player : AudioStreamPlayer
fn _ready() void { get_window().files_dropped.connect(_on_files_dropped)}
fn _on_files_dropped(files: []String) void { if files[0].get_extension() == "wav" { audio_player.stream = AudioStreamWAV.load_from_file(files[0], { "force/max_rate": true, "force/max_rate_hz": 11025 }) audio_player.play() }}save_to_wav
Section titled “save_to_wav”Error save_to_wav(path: String)
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.