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

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <sndfile.h>
Include dependency graph for fileio.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int FIO_read_audio (const char *infile, float **indata, size_t *datalen, unsigned *samprate, unsigned donorm)
 Read a single-channel audio file into memory.
 
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 format.
 

Detailed Description

Audio file I/O and feature vector writing.

This module provides:

  • Reading audio files (WAV, FLAC, AIFF, etc.) via libsndfile.
  • Writing audio files (WAV float) via libsndfile.
  • Writing feature vectors in NumPy .npy format for Python interop.
  • Writing feature vectors in safetensors format for ML pipelines.
  • Writing feature vectors in HTK binary format (deprecated).

Definition in file fileio.h.

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

Parameters
infilePath to the audio file.
indataOutput: pointer to the audio samples (caller must free).
datalenOutput: number of samples read.
samprateOutput: sampling rate of the file (Hz).
donormIf 1, normalise samples to [-1.0, 1.0].
Returns
0 on success, -1 on error.
Note
Only single-channel (mono) files are supported. Use the 'sox' utility to split multi-channel files first.

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

Deprecated:
Use FIO_write_npy() or FIO_write_safetensors() instead.
Parameters
outfilePath to the output file.
outvecsArray of nvecs pointers, each pointing to veclen floats.
nvecsNumber of feature vectors.
veclenNumber of floats per vector.
vecsamprateSampling rate of the feature vectors (Hz).
Returns
0 on success, -1 on error.

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.