|
miniDSP
A small C library for audio DSP
|
Audio file I/O and feature vector writing. More...
#include "fileio.h"
Go to the source code of this file.
Data Structures | |
| struct | FIO_HTKheader |
| HTK file header structure (12 bytes total). More... | |
Macros | |
| #define | SWAP(x) swap_bytes(&(x), sizeof(x)) |
| Convenience macro: swap the bytes of variable x. | |
Functions | |
| static int | is_bigendian (void) |
| Check if the host is big-endian. | |
| static void | swap_bytes (void *pv, size_t n) |
| Reverse the bytes of a value in-place (e.g. | |
| int | FIO_read_audio (const char *infile, float **indata, size_t *datalen, unsigned *samprate, unsigned donorm) |
| Read a single-channel audio file into a float array. | |
| int | FIO_write_npy (const char *outfile, const float **outvecs, size_t nvecs, size_t veclen) |
| Write a 2D float32 array in NumPy .npy v1.0 format. | |
| int | FIO_write_safetensors (const char *outfile, const float **outvecs, size_t nvecs, size_t veclen) |
| Write a 2D float32 array in safetensors format. | |
| int | FIO_write_wav (const char *outfile, const float *data, size_t datalen, unsigned samprate) |
| Write mono float audio to a WAV file. | |
| int | FIO_write_htk_feats (const char *outfile, const float **outvecs, size_t nvecs, size_t veclen, unsigned vecsamprate) |
| Write feature vectors in HTK binary file format. | |
Audio file I/O and feature vector writing.
This module reads audio using libsndfile (which supports dozens of formats including WAV, FLAC, AIFF, and Ogg), writes audio to WAV, and writes feature vectors in NumPy .npy, safetensors, and HTK formats.
Definition in file fileio.c.
| #define SWAP | ( | x | ) | swap_bytes(&(x), sizeof(x)) |
| int FIO_read_audio | ( | const char * | infile, |
| float ** | indata, | ||
| size_t * | datalen, | ||
| unsigned * | samprate, | ||
| unsigned | donorm | ||
| ) |
Read a single-channel audio file into a float array.
Read a single-channel audio file into memory.
Supported formats include everything that libsndfile can open: WAV, FLAC, AIFF, OGG, and many more.
| infile | Path to the audio file. |
| indata | Output: pointer to allocated float array with samples. The caller is responsible for calling free() on this. |
| datalen | Output: total number of samples read. |
| samprate | Output: sample rate in Hz. |
| donorm | 1 to normalise samples to [-1.0, 1.0]; 0 for raw values. |
| int FIO_write_htk_feats | ( | const char * | outfile, |
| const float ** | outvecs, | ||
| size_t | nvecs, | ||
| size_t | veclen, | ||
| unsigned | vecsamprate | ||
| ) |
Write feature vectors in HTK binary file format.
Write feature vectors in HTK binary format.
The HTK format is: [12-byte header][vector 1][vector 2]...[vector N]
Each vector is a sequence of big-endian 32-bit floats.
| outfile | Output file path. |
| outvecs | Array of nvecs pointers, each to veclen floats. |
| nvecs | Number of feature vectors. |
| veclen | Number of float elements per vector. |
| vecsamprate | Feature sampling rate in Hz. |
| int FIO_write_npy | ( | const char * | outfile, |
| const float ** | outvecs, | ||
| size_t | nvecs, | ||
| size_t | veclen | ||
| ) |
Write a 2D float32 array in NumPy .npy v1.0 format.
Produces a file readable by numpy.load(). Data is stored as little-endian float32, row-major (C order).
| outfile | Path to the output .npy file. |
| outvecs | Array of nvecs pointers, each pointing to veclen floats. |
| nvecs | Number of feature vectors (rows). |
| veclen | Number of floats per vector (columns). |
| int FIO_write_safetensors | ( | const char * | outfile, |
| const float ** | outvecs, | ||
| size_t | nvecs, | ||
| size_t | veclen | ||
| ) |
Write a 2D float32 array in safetensors format.
Produces a file readable by the safetensors Python library. The tensor is stored under the key "features" as little-endian float32.
| outfile | Path to the output .safetensors file. |
| outvecs | Array of nvecs pointers, each pointing to veclen floats. |
| nvecs | Number of feature vectors (rows). |
| veclen | Number of floats per vector (columns). |
| int FIO_write_wav | ( | const char * | outfile, |
| const float * | data, | ||
| size_t | datalen, | ||
| unsigned | samprate | ||
| ) |
Write mono float audio to a WAV file.
Uses IEEE float format (SF_FORMAT_FLOAT) for lossless DSP round-trips.
| outfile | Path to the output .wav file. |
| data | Audio samples. |
| datalen | Number of samples. |
| samprate | Sampling rate in Hz. |
|
static |