Skip to content

Sound File I/O

Structs

struct WavHeader

Struct containing WAV file header information.

Traits: AnyType, Copyable, ImplicitlyDestructible, Movable


WavHeader Functions

struct WavHeader . fn init

fn init Signature

__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 8-bit unsigned PCM sample and normalize to [-1.0, 1.0].

Signature

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

Arguments

Name Type Default Description
data List[UInt8]
offset Int

Returns

Type: Float64

fn read_16bit_sample

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

Signature

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

Arguments

Name Type Default Description
data List[UInt8]
offset Int

Returns

Type: Float64

fn read_24bit_sample

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

Signature

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

Arguments

Name Type Default Description
data List[UInt8]
offset Int

Returns

Type: Float64

fn read_32bit_sample

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

Signature

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

Arguments

Name Type Default Description
data List[UInt8]
offset Int

Returns

Type: Float64

fn read_float32_sample

Read 32-bit float sample (already normalized).

Signature

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

Arguments

Name Type Default Description
data List[UInt8]
offset Int

Returns

Type: Float64

fn read_float64_sample

Read 64-bit float sample (already normalized).

Signature

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

Arguments

Name Type Default Description
data List[UInt8]
offset Int

Returns

Type: Float64

fn read_wav_header

Parse WAV header from file data.

Signature

read_wav_header(file_name: String) -> WavHeader

Arguments

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

Returns

Type: WavHeader

Raises

fn read_wav_samples

Read all audio samples from WAV file data.

Signature

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

Raises

fn get_sample

Signature

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

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

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

Raises

fn print_wav_info

Pretty print WAV header information.

Signature

print_wav_info(header: WavHeader)

Arguments

Name Type Default Description
header WavHeader

fn print_sample_stats

Print statistics about the audio samples.

Signature

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

Arguments

Name Type Default Description
samples List[List[Float64]]

fn write_f32

Write a Float32 as 4 little-endian bytes.

Signature

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

Arguments

Name Type Default Description
data List[UInt8]
value Float32

fn write_wav_file

Write audio samples to a WAV file.

Signature

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

Arguments

Name Type Default Description
file_name String
samples Span[List[Float64]]
sample_rate Int 44100

Raises














fn write_wav_file

Write audio samples to a WAV file.

Signature

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

Arguments

Name Type Default Description
file_name String
samples Span[SIMD[DType.float64, num_channels]]
sample_rate Int 44100

Raises

fn write_str

Signature

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

Arguments

Name Type Default Description
d List[UInt8]
s String

fn write_u16

Signature

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

Arguments

Name Type Default Description
d List[UInt8]
val Int

fn write_u32

Signature

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.0b1