AudioBuffer

PCM audio kept in one buffer, the same on the Lune and native runtimes, read from and saved to WAV files.

What it is

An AudioBuffer holds audio as 32-bit float samples from -1 to 1, in one buffer. It works the same on the Lune runtime and the native one, where an AudioClip exists only natively. Frames and channels count from 1, like AudioClip's samples. Only the file members end in Async; the rest never yield.

Making one

new(sampleRate, channels, frames)silence of that many samples a channel
FromBuffer(bytes)audio from a WAV file's bytes
LoadAsync(path)audio from a WAV file

Both stop the run with the reason when the bytes or the file cannot be read; OGG and MP3 are refused.

Reading

SampleRatesamples a second
Channelshow many channels
Frameshow many samples each channel has
Durationthe length in seconds
GetSample(frame, channel)the sample, from -1 to 1; outside the buffer is an error
SetSample(frame, channel, value)sets a sample (not limited here; Encode clips to -1..1); outside is an error
Peak()the largest absolute sample

Operations

Slice, Resample, ToMono and Concat give a new buffer and leave the first one as it was; Gain, Fade and Mix change the buffer in place.

Slice(fromSeconds, toSeconds)the part between the two times, rounded to frames; outside is an error
Gain(factor)multiplies every sample by factor
Fade(inSeconds, outSeconds)a linear fade in over the first seconds and out over the last
Mix(other, atSeconds, gain)adds the other buffer (times gain, 1 when left out) from atSeconds (0 when left out), cut at this buffer's end; the sample rates and the channel counts must match
Resample(rate)a new buffer at that sample rate, by linear interpolation
ToMono()a new one-channel buffer, the mean of the channels
Concat(other)a new buffer of this one followed by the other (same rate and channels)

Saving

Encode()the bytes of a 16-bit PCM WAV file
SaveAsync(path)writes a 16-bit WAV file, replacing it; a path that does not end in .wav is an error

Native runtime only

AudioBuffer.FromAudioClip(clip)a copy of an AudioClip's samples
buffer:ToAudioClip()a new AudioClip with this audio, which the caller owns

Both refuse on the Lune runtime, which has no AudioClip.

See also

Editables (AudioClip, WAV files and what needs the native runtime), FileService (paths).