Skip to content

Sound File I/O

Structs

struct WavHeader

Struct containing WAV file header information.

Traits: AnyType, Copyable, ImplicitlyDeletable, Movable


WavHeader Functions

struct WavHeader . fn init

fn init Signature

def __init__(out self)

fn init Returns : Self

Static Method

This is a static method.

Functions

(Functions that are not associated with a Struct)

fn read_8bit_sample

Read an 8-bit unsigned PCM sample and normalize it.

Signature

def read_8bit_sample(data: List[UInt8], offset: Int) -> Float64

Arguments

Name Type Default Description
data List[UInt8] Raw WAV file bytes.
offset Int Byte offset of the sample to read.

Returns

Type: Float64 The normalized sample value.

fn read_16bit_sample

Read a 16-bit signed PCM sample and normalize it to [-1.0, 1.0].

Signature

def read_16bit_sample(data: List[UInt8], offset: Int) -> Float64

Arguments

Name Type Default Description
data List[UInt8] Raw WAV file bytes.
offset Int Byte offset of the sample to read.

Returns

Type: Float64 The normalized sample value.

fn read_24bit_sample

Read a 24-bit signed PCM sample and normalize it to [-1.0, 1.0].

Signature

def read_24bit_sample(data: List[UInt8], offset: Int) -> Float64

Arguments

Name Type Default Description
data List[UInt8] Raw WAV file bytes.
offset Int Byte offset of the sample to read.

Returns

Type: Float64 The normalized sample value.

fn read_32bit_sample

Read a 32-bit signed PCM sample and normalize it to [-1.0, 1.0].

Signature

def read_32bit_sample(data: List[UInt8], offset: Int) -> Float64

Arguments

Name Type Default Description
data List[UInt8] Raw WAV file bytes.
offset Int Byte offset of the sample to read.

Returns

Type: Float64 The normalized sample value.

fn read_float32_sample

Read a 32-bit floating-point sample.

Signature

def read_float32_sample(data: List[UInt8], offset: Int) -> Float64

Arguments

Name Type Default Description
data List[UInt8] Raw WAV file bytes.
offset Int Byte offset of the sample to read.

Returns

Type: Float64 The decoded sample value.

fn read_float64_sample

Read a 64-bit floating-point sample.

Signature

def read_float64_sample(data: List[UInt8], offset: Int) -> Float64

Arguments

Name Type Default Description
data List[UInt8] Raw WAV file bytes.
offset Int Byte offset of the sample to read.

Returns

Type: Float64 The decoded sample value.

fn read_wav_header

Parse WAV header from file data.

Signature

def read_wav_header(file_name: String) -> WavHeader

Arguments

Name Type Default Description
file_name String Path to the WAV file.

Returns

Type: WavHeader WavHeader struct containing header information.

Raises

Error: If the file is missing required WAV header data.

fn read_wav_samples

Read all audio samples from WAV file data.

Signature

def read_wav_samples(file_name: String, header: WavHeader, num_wavetables: Int = 1) -> List[List[Float64]]

Arguments

Name Type Default Description
file_name String Path to the WAV file.
header WavHeader Parsed WAV header.
num_wavetables Int 1 Number of wavetables per channel. If > 1, splits samples into multiple wavetables of equal size (for large files).

Returns

Type: List[List[Float64]] List of channels, each containing normalized Float64 samples [-1.0, 1.0].

Raises

Error: If the file uses an unsupported sample format.

fn get_sample

Signature

def get_sample(file_data: List[UInt8], offset: Int, bits_per_sample: Int, is_pcm: Bool, is_float: Bool) -> Float64

Arguments

Name Type Default Description
file_data List[UInt8]
offset Int
bits_per_sample Int
is_pcm Bool
is_float Bool

Returns

Type: Float64

fn read_wav_SIMDs

Read all audio samples from s WAV file and return them as a List of SIMD vectors.

Signature

def read_wav_SIMDs[num_channels: Int](file_name: String, header: WavHeader, num_wavetables: Int = 1) -> List[SIMD[DType.float64, num_channels]]

Parameters

Name Type Description
num_channels Int Number of channels in each SIMD output sample.

Arguments

Name Type Default Description
file_name String Path to the WAV file.
header WavHeader Parsed WAV header.
num_wavetables Int 1 If > 1, split samples into multiple wavetables of equal size (for large files).

Returns

Type: List[SIMD[DType.float64, num_channels]] A list of SIMD samples containing audio amplitudes [-1.0 to 1.0]. Each element in the list is one xample in time and each SIMD vector contains num_channels channels of audio data for that sample. If the original file has fewer channels than num_channels, the remaining channels in the SIMD vector will be filled with 0.0.

Raises

Error: If the file uses an unsupported sample format.

fn print_wav_info

Pretty print WAV header information.

Signature

def print_wav_info(header: WavHeader)

Arguments

Name Type Default Description
header WavHeader WAV header values to display.

fn print_sample_stats

Print statistics about the audio samples.

Signature

def print_sample_stats(samples: List[List[Float64]])

Arguments

Name Type Default Description
samples List[List[Float64]] Per-channel sample lists to summarize.

fn write_f32

Write a Float32 as 4 little-endian bytes.

Signature

def write_f32(mut data: List[UInt8], value: Float32)

Arguments

Name Type Default Description
data List[UInt8] Output byte buffer to append to.
value Float32 Float32 value to encode.

fn write_wav_file

Write audio samples to a WAV file.

Signature

def write_wav_file(file_name: String, samples: Span[List[Float64]], sample_rate: Int = 44100)

Arguments

Name Type Default Description
file_name String Output WAV file path.
samples Span[List[Float64]] Per-channel sample data to write.
sample_rate Int 44100 Output sample rate in Hz.

Raises

Error: If the output file cannot be written.














fn write_wav_file

Write audio samples to a WAV file.

Signature

def write_wav_file[num_channels: Int](file_name: String, samples: Span[SIMD[DType.float64, num_channels]], sample_rate: Int = 44100)

Parameters

Name Type Description
num_channels Int Number of channels stored in each SIMD sample.

Arguments

Name Type Default Description
file_name String Output WAV file path.
samples Span[SIMD[DType.float64, num_channels]] SIMD sample data to write.
sample_rate Int 44100 Output sample rate in Hz.

Raises

Error: If the output file cannot be written.

fn write_str

Signature

def write_str(mut d: List[UInt8], s: String)

Arguments

Name Type Default Description
d List[UInt8]
s String

fn write_u16

Signature

def write_u16(mut d: List[UInt8], val: Int)

Arguments

Name Type Default Description
d List[UInt8]
val Int

fn write_u32

Signature

def write_u32(mut d: List[UInt8], val: Int)

Arguments

Name Type Default Description
d List[UInt8]
val Int

Documentation generated with mojo doc from Mojo version 1.0.0b2