I/O¶
Read and write WAV (8/16/24/32-bit PCM) and FLAC (16/24-bit) files. WAV uses the Python stdlib wave module; FLAC uses the CHOC codec.
Usage examples¶
Read and write files¶
from nanodsp import io
# Auto-detect format by extension
buf = io.read("input.wav")
buf = io.read("input.flac")
io.write("output.wav", buf) # 16-bit default
io.write("output.wav", buf, bit_depth=24) # 24-bit WAV
io.write("output.flac", buf, bit_depth=24) # 24-bit FLAC
Format-specific functions¶
buf = io.read_wav("file.wav")
io.write_wav("out.wav", buf, bit_depth=24)
buf = io.read_flac("file.flac")
io.write_flac("out.flac", buf, bit_depth=16)
Byte-level I/O (for pipes and streaming)¶
# Parse WAV from raw bytes (e.g., from stdin)
import sys
raw = sys.stdin.buffer.read()
buf = io.read_wav_bytes(raw)
# Serialize to WAV bytes (e.g., for stdout)
out_bytes = io.write_wav_bytes(buf, bit_depth=16)
sys.stdout.buffer.write(out_bytes)
API reference¶
io
¶
Audio file I/O for AudioBuffer.
Supported formats (detected by extension):
.wav -- 8/16/24/32-bit PCM read, 16/24-bit PCM write (stdlib wave)
.flac -- 16/24-bit read/write (CHOC FLAC codec, zero external dependencies)
read
¶
Read an audio file and return an AudioBuffer.
Format is detected by file extension (.wav, .flac).
write
¶
Write an AudioBuffer to an audio file.
Format is detected by file extension (.wav, .flac).
| PARAMETER | DESCRIPTION |
|---|---|
path
|
Output file path.
TYPE:
|
buf
|
Audio data to write.
TYPE:
|
bit_depth
|
Output bit depth: 16 or 24.
TYPE:
|
read_wav
¶
Read a WAV file and return an AudioBuffer.
Supports 8-bit unsigned, 16-bit signed, 24-bit signed, and 32-bit signed PCM. Output is float32 normalized to [-1, 1].
write_wav
¶
Write an AudioBuffer to a WAV file.
| PARAMETER | DESCRIPTION |
|---|---|
path
|
Output file path.
TYPE:
|
buf
|
Audio data to write.
TYPE:
|
bit_depth
|
Output bit depth: 16 or 24.
TYPE:
|
read_wav_bytes
¶
Read WAV data from raw bytes and return an AudioBuffer.
Supports 8/16/24/32-bit PCM. Output is float32 normalized to [-1, 1].
write_wav_bytes
¶
Serialize an AudioBuffer to WAV bytes.
| PARAMETER | DESCRIPTION |
|---|---|
buf
|
Audio data to write.
TYPE:
|
bit_depth
|
Output bit depth: 16 or 24.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bytes
|
WAV file content. |
read_flac
¶
Read a FLAC file and return an AudioBuffer.
Output is float32 normalized to [-1, 1].
write_flac
¶
Write an AudioBuffer to a FLAC file.
| PARAMETER | DESCRIPTION |
|---|---|
path
|
Output file path.
TYPE:
|
buf
|
Audio data to write.
TYPE:
|
bit_depth
|
Output bit depth: 16 or 24.
TYPE:
|