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
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)
¶
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
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Mono float32 waveform. Returns an empty array on error. |
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
|
int
|
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 empty list on error. |
Source code in birdnet_stm32/audio/io.py
save_wav(audio, path, sample_rate=24000)
¶
Save an audio signal to a WAV file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio
|
ndarray
|
1D audio array (mono). |
required |
path
|
str
|
Output file path (.wav). |
required |
sample_rate
|
int
|
Sampling rate (Hz). |
24000
|