io
birdnet_stm32.audio.io
¶
Audio file loading, resampling, chunking, and saving.
This module handles all I/O operations for audio files: loading with soundfile, resampling via scipy, splitting into fixed-length chunks, and writing WAV files.
fast_resample(y, sr_in, sr_out)
¶
Resample audio using scipy.signal.resample_poly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
ndarray
|
1D float32 audio signal. |
required |
sr_in
|
int
|
Original sample rate (Hz). |
required |
sr_out
|
int
|
Target sample rate (Hz). |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Resampled audio as float32. |
Source code in birdnet_stm32/audio/io.py
peak_normalize(x)
¶
Divide raw waveform windows by their peak, along the last axis.
The raw models' input normalization, per window: x / (max|x| + 1e-6).
Training, calibration and evaluation all go through here, and the firmware
and reference/ implement the same expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
One window |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
The scaled window(s), same shape. |
Source code in birdnet_stm32/audio/io.py
estimate_num_chunks(num_samples, sample_rate, chunk_duration, chunk_overlap=0.0)
¶
Estimate how many fixed-length chunks an audio window will produce.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_samples
|
int
|
Number of audio samples in the waveform. |
required |
sample_rate
|
int
|
Sampling rate of the waveform. |
required |
chunk_duration
|
float
|
Duration of each chunk in seconds. |
required |
chunk_overlap
|
float
|
Overlap between adjacent chunks in seconds. |
0.0
|
Returns:
| Type | Description |
|---|---|
int
|
Number of chunks that :func: |
Source code in birdnet_stm32/audio/io.py
load_audio_window(path, sample_rate=24000, max_duration=30, chunk_duration=3.0, random_offset=False, return_offset=False)
¶
Load one contiguous mono waveform window from disk.
The window is read directly from the source file, resampled, and peak
normalized to [-1, 1]. This is the lowest-overhead path for callers
that want to perform their own chunk selection after reading a file once.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to the audio file on disk. |
required |
sample_rate
|
int
|
Target sampling rate in Hz. |
24000
|
max_duration
|
float | None
|
Maximum duration to read in seconds. |
30
|
chunk_duration
|
float
|
Reference chunk duration in seconds, used when choosing a random start offset. |
3.0
|
random_offset
|
bool
|
Whether to read from a random offset instead of the beginning of the file. |
False
|
return_offset
|
bool
|
Also return where in the file the window starts, in seconds. Callers that attach per-window targets (teacher scores) need it to place a chunk on the file's own timeline. |
False
|
Returns:
| Type | Description |
|---|---|
ndarray | tuple[ndarray, float]
|
Mono float32 waveform, or |
ndarray | tuple[ndarray, float]
|
|
Source code in birdnet_stm32/audio/io.py
chunk_start_samples(num_samples, sample_rate, chunk_duration, chunk_overlap=0.0)
¶
Start sample of every chunk :func:split_audio_into_chunks emits.
Chunks step by chunk_duration - chunk_overlap, and a final chunk is
right-aligned to the end of the waveform when the steps do not land on it
exactly. A waveform no longer than one chunk yields one chunk at 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_samples
|
int
|
Length of the waveform in samples. |
required |
sample_rate
|
int
|
Sampling rate in Hz. |
required |
chunk_duration
|
float
|
Duration of each chunk in seconds. |
required |
chunk_overlap
|
float
|
Overlap between chunks in seconds. |
0.0
|
Returns:
| Type | Description |
|---|---|
ndarray
|
int64 array of chunk start positions, in samples. |
Source code in birdnet_stm32/audio/io.py
split_audio_into_chunks(audio, sample_rate=24000, chunk_duration=3.0, chunk_overlap=0.0)
¶
Split a waveform into fixed-length chunks, padding short files once.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio
|
ndarray
|
Mono float32 waveform. |
required |
sample_rate
|
int
|
Sampling rate in Hz. |
24000
|
chunk_duration
|
float
|
Duration of each chunk in seconds. |
3.0
|
chunk_overlap
|
float
|
Overlap between chunks in seconds. |
0.0
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of shape |
ndarray
|
If the waveform is shorter than one chunk, the single returned chunk is |
ndarray
|
right-padded with zeros. |
Source code in birdnet_stm32/audio/io.py
load_audio_file(path, sample_rate=24000, max_duration=30, chunk_duration=3.0, chunk_overlap=0.0, random_offset=False)
¶
Load an audio file, resample, normalize, and split into fixed-length chunks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to the audio file on disk. |
required |
sample_rate
|
int
|
Target sampling rate (Hz). |
24000
|
max_duration
|
float | None
|
Maximum duration to load (seconds). |
30
|
chunk_duration
|
float
|
Duration of each chunk (seconds). |
3.0
|
chunk_overlap
|
float
|
Overlap between chunks (seconds, 0 <= overlap < chunk_duration). |
0.0
|
random_offset
|
bool
|
Whether to start reading at a random offset. |
False
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of shape (num_chunks, chunk_size) with float32 audio chunks. |
ndarray
|
Returns an empty array on error. |