Sound File I/O
struct WavHeader¶
Struct containing WAV file header information.
Traits: AnyType, Copyable, ImplicitlyDestructible, Movable
WavHeader Functions¶
struct WavHeader . fn init¶
(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
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
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
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
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
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
Arguments
| Name | Type | Default | Description |
|---|---|---|---|
| data | List[UInt8] |
— | — |
| offset | Int |
— | — |
Returns
Type: Float64
fn read_wav_header¶
Parse WAV header from file data.
Signature
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
Arguments
| Name | Type | Default | Description |
|---|---|---|---|
| header | WavHeader |
— | — |
fn print_sample_stats¶
Print statistics about the audio samples.
Signature
Arguments
| Name | Type | Default | Description |
|---|---|---|---|
| samples | List[List[Float64]] |
— | — |
fn write_f32¶
Write a Float32 as 4 little-endian bytes.
Signature
Arguments
| Name | Type | Default | Description |
|---|---|---|---|
| data | List[UInt8] |
— | — |
| value | Float32 |
— | — |
fn write_wav_file¶
Write audio samples to a WAV file.
Signature
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
Arguments
| Name | Type | Default | Description |
|---|---|---|---|
| d | List[UInt8] |
— | — |
| s | String |
— | — |
fn write_u16¶
Signature
Arguments
| Name | Type | Default | Description |
|---|---|---|---|
| d | List[UInt8] |
— | — |
| val | Int |
— | — |
fn write_u32¶
Signature
Arguments
| Name | Type | Default | Description |
|---|---|---|---|
| d | List[UInt8] |
— | — |
| val | Int |
— | — |
Documentation generated with mojo doc from Mojo version 1.0.0b1