glwav
Types
Represents the bit depth of audio samples.
U8: 8-bit unsigned samplesI16: 16-bit signed samples (most common)I24: 24-bit signed samplesF32: 32-bit floating point samples
pub type Bits {
U8
I16
I24
F32
}
Constructors
-
U8 -
I16 -
I24 -
F32
Represents the data contained in a WAV chunk.
pub type ChunkData {
Fmt(
format_code: FormatCode,
sample_rate: Int,
channels: Int,
bits: Bits,
)
Data(data_bits: BitArray)
}
Constructors
-
Fmt( format_code: FormatCode, sample_rate: Int, channels: Int, bits: Bits, )Format chunk containing audio properties
-
Data(data_bits: BitArray)Data chunk containing the actual audio samples
Represents the audio encoding format.
PCM: Pulse Code Modulation (uncompressed)IeeeFloat: IEEE floating point formatAlaw: A-law logarithmic encodingMulaw: μ-law logarithmic encodingExtensible: Extensible format with additional metadata
pub type FormatCode {
PCM
IeeeFloat
Alaw
Mulaw
Extensible
}
Constructors
-
PCM -
IeeeFloat -
Alaw -
Mulaw -
Extensible
Errors that can occur when parsing a WAV file from a bit array.
pub type FromBitArrayError {
RiffFormatError(inner: glriff.FromBitArrayError)
InvalidFormat
}
Constructors
-
RiffFormatError(inner: glriff.FromBitArrayError)The underlying RIFF format is invalid or corrupted
-
InvalidFormatThe WAV format is invalid (missing chunks, incorrect structure, etc.)
pub type ReadChunkError {
NotSupported
InvalidFormatCode
InvalidChannels
InvalidSampleRate
InvalidBytesPerSecond
InvalidBlockAlign
InvalidBits
}
Constructors
-
NotSupported -
InvalidFormatCode -
InvalidChannels -
InvalidSampleRate -
InvalidBytesPerSecond -
InvalidBlockAlign -
InvalidBits
Represents a WAV audio file with its properties and sample data.
Fields
format_code: The audio format (PCM, IEEE Float, etc.)sample_rate: Sample rate in Hz (e.g., 44100, 48000)channels: Number of audio channels (1 for mono, 2 for stereo)bits: Bit depth of samplessamples: Normalized audio samples as floating point values (-1.0 to 1.0)
Note
bytes_per_second and block_align are calculated automatically from other fields:
block_align=channels * (bits_per_sample / 8)bytes_per_second=sample_rate * block_align
pub type Wave {
Wave(
format_code: FormatCode,
sample_rate: Int,
channels: Int,
bits: Bits,
samples: List(Float),
)
}
Constructors
-
Wave( format_code: FormatCode, sample_rate: Int, channels: Int, bits: Bits, samples: List(Float), )
Values
pub fn from_bit_array(
bits: BitArray,
) -> Result(Wave, FromBitArrayError)
Parse a WAV file from a bit array.
Reads a WAV file in RIFF format and extracts all audio properties and samples. Samples are normalized to floating point values in the range -1.0 to 1.0.
Example
let assert Ok(bits) = simplifile.read_bits("audio.wav")
let assert Ok(wave) = glwav.from_bit_array(bits)
// Access wave properties
wave.sample_rate // e.g., 44100
wave.channels // e.g., 2
wave.samples // List of normalized samples
Returns
Ok(Wave)if parsing succeedsError(FromBitArrayError)if the file is invalid or unsupported
pub fn to_bit_array(wave: Wave) -> BitArray
Convert a Wave structure to a bit array in WAV format.
Creates a complete WAV file including RIFF header, fmt chunk, and data chunk. Samples are converted from normalized floating point values (-1.0 to 1.0) to the appropriate bit depth specified in the Wave structure.
The bytes_per_second and block_align values are calculated automatically:
block_align=channels * (bits_per_sample / 8)bytes_per_second=sample_rate * block_align
Example
let wave = glwav.Wave(
format_code: glwav.PCM,
sample_rate: 44_100,
channels: 1,
bits: glwav.I16,
samples: [0.0, 0.5, 1.0, 0.5, 0.0, -0.5, -1.0],
)
let bits = glwav.to_bit_array(wave)
// Write to file
simplifile.write_bits(bits, "output.wav")
Returns
A bit array containing the complete WAV file data