miniDSP
A small C library for audio DSP
Loading...
Searching...
No Matches
fileio.c File Reference

Audio file I/O and feature vector writing. More...

#include "fileio.h"
Include dependency graph for fileio.c:

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.
 

Detailed Description

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.

Macro Definition Documentation

◆ SWAP

#define SWAP (   x)    swap_bytes(&(x), sizeof(x))

Convenience macro: swap the bytes of variable x.

Definition at line 63 of file fileio.c.

Function Documentation

◆ FIO_read_audio()

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.

Parameters
infilePath to the audio file.
indataOutput: pointer to allocated float array with samples. The caller is responsible for calling free() on this.
datalenOutput: total number of samples read.
samprateOutput: sample rate in Hz.
donorm1 to normalise samples to [-1.0, 1.0]; 0 for raw values.
Returns
0 on success, -1 on error.

Definition at line 83 of file fileio.c.

◆ FIO_write_htk_feats()

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.

Parameters
outfileOutput file path.
outvecsArray of nvecs pointers, each to veclen floats.
nvecsNumber of feature vectors.
veclenNumber of float elements per vector.
vecsamprateFeature sampling rate in Hz.
Returns
0 on success, -1 on error.

Definition at line 323 of file fileio.c.

◆ FIO_write_npy()

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).

Parameters
outfilePath to the output .npy file.
outvecsArray of nvecs pointers, each pointing to veclen floats.
nvecsNumber of feature vectors (rows).
veclenNumber of floats per vector (columns).
Returns
0 on success, -1 on error.

Definition at line 167 of file fileio.c.

◆ FIO_write_safetensors()

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.

Parameters
outfilePath to the output .safetensors file.
outvecsArray of nvecs pointers, each pointing to veclen floats.
nvecsNumber of feature vectors (rows).
veclenNumber of floats per vector (columns).
Returns
0 on success, -1 on error.

Definition at line 236 of file fileio.c.

◆ FIO_write_wav()

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.

Parameters
outfilePath to the output .wav file.
dataAudio samples.
datalenNumber of samples.
samprateSampling rate in Hz.
Returns
0 on success, -1 on error.

Definition at line 282 of file fileio.c.

◆ is_bigendian()

static int is_bigendian ( void  )
static

Check if the host is big-endian.

Definition at line 45 of file fileio.c.

◆ swap_bytes()

static void swap_bytes ( void *  pv,
size_t  n 
)
static

Reverse the bytes of a value in-place (e.g.

convert little-endian to big).

Definition at line 52 of file fileio.c.