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

Live audio recording and playback via PortAudio. More...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <portaudio.h>
Include dependency graph for liveio.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef int LaError_t
 Return type for live audio functions.
 

Enumerations

enum  LA_ERRORCODE { LA_OK = 0 , LA_ERROR = 1 }
 Error codes returned by live audio functions. More...
 
enum  LA_RECORDTYPE { LA_REC_ONCE = 0 , LA_REC_CONT }
 Recording modes for LA_record(). More...
 

Functions

LaError_t LA_init (void)
 Initialise the PortAudio system.
 
LaError_t LA_terminate (void)
 Shut down the PortAudio system.
 
void LA_print_last_error (FILE *stream)
 Print the last PortAudio error message to the given stream.
 
int LA_is_recording (void)
 Check if the recording stream is active.
 
double LA_get_cur_record_time (void)
 Get the current time (in seconds) of the recording stream.
 
void LA_stop_recording (void)
 Stop the current recording.
 
LaError_t LA_record (void *buffer, unsigned long bufsize, unsigned samprate, int rectype)
 Record audio from the default input device into a buffer.
 
LaError_t LA_record_callback (unsigned samprate, PaStreamCallback *cb, void *cb_data)
 Record using a user-supplied PortAudio callback function.
 
int LA_is_playing (void)
 Check if the playback stream is active.
 
double LA_get_cur_play_time (void)
 Get the current time (in seconds) of the playback stream.
 
void LA_stop_playing (void)
 Stop the current playback.
 
LaError_t LA_play (const void *buffer, unsigned long bufsize, unsigned samprate)
 Play audio from a buffer to the default output device.
 
LaError_t LA_play_callback (unsigned samprate, PaStreamCallback *cb, void *cb_data)
 Play using a user-supplied PortAudio callback function.
 

Detailed Description

Live audio recording and playback via PortAudio.

This module wraps the PortAudio library to provide simple functions for recording from a microphone and playing back to speakers. All functions are non-blocking – they return immediately while audio I/O happens in the background via callbacks.

Typical usage:

  1. Call LA_init() to initialise the audio system.
  2. Call LA_record() to start recording into a buffer.
  3. Poll LA_is_recording() until recording finishes.
  4. Call LA_play() to play back the recorded audio.
  5. Call LA_terminate() when done.

Definition in file liveio.h.

Typedef Documentation

◆ LaError_t

typedef int LaError_t

Return type for live audio functions.

Definition at line 27 of file liveio.h.

Enumeration Type Documentation

◆ LA_ERRORCODE

Error codes returned by live audio functions.

Enumerator
LA_OK 

Operation succeeded.


LA_ERROR 

Something went wrong.


Definition at line 30 of file liveio.h.

◆ LA_RECORDTYPE

Recording modes for LA_record().

Enumerator
LA_REC_ONCE 

Record until the buffer is full, then stop.

LA_REC_CONT 

Record continuously in a circular buffer.


Definition at line 36 of file liveio.h.

Function Documentation

◆ LA_get_cur_play_time()

double LA_get_cur_play_time ( void  )

Get the current time (in seconds) of the playback stream.

Returns -1.0 if inactive.

Definition at line 57 of file liveio.c.

◆ LA_get_cur_record_time()

double LA_get_cur_record_time ( void  )

Get the current time (in seconds) of the recording stream.

Returns -1.0 if inactive.

Definition at line 49 of file liveio.c.

◆ LA_init()

LaError_t LA_init ( void  )

Initialise the PortAudio system.

Must be called before any other LA_ function.

If already initialised, terminates first and re-initialises.

Returns
LA_OK on success, LA_ERROR on failure.

Definition at line 420 of file liveio.c.

◆ LA_is_playing()

int LA_is_playing ( void  )

Check if the playback stream is active.

Returns 1 if playing, 0 if not, -1 on error.

Check if the playback stream is active.

Returns
1 if playing, 0 if not, -1 on error.

Definition at line 94 of file liveio.c.

◆ LA_is_recording()

int LA_is_recording ( void  )

Check if the recording stream is active.

Returns 1 if recording, 0 if not, -1 on error.

Check if the recording stream is active.

Returns
1 if recording, 0 if not, -1 on error.

Definition at line 83 of file liveio.c.

◆ LA_play()

LaError_t LA_play ( const void *  buffer,
unsigned long  bufsize,
unsigned  samprate 
)

Play audio from a buffer to the default output device.

Parameters
bufferBuffer of 16-bit signed integers.
bufsizeNumber of samples in the buffer.
samprateSampling rate in Hz.
Returns
LA_OK on success, LA_ERROR on failure.
Note
Non-blocking: returns immediately while playback continues in the background. Mono input is duplicated to both stereo channels.

Definition at line 378 of file liveio.c.

◆ LA_play_callback()

LaError_t LA_play_callback ( unsigned  samprate,
PaStreamCallback *  cb,
void *  cb_data 
)

Play using a user-supplied PortAudio callback function.

Parameters
samprateSampling rate in Hz.
cbYour PortAudio callback function.
cb_dataData pointer passed to your callback.
Returns
LA_OK on success, LA_ERROR on failure.

Definition at line 358 of file liveio.c.

◆ LA_print_last_error()

void LA_print_last_error ( FILE *  stream)

Print the last PortAudio error message to the given stream.

Definition at line 409 of file liveio.c.

◆ LA_record()

LaError_t LA_record ( void *  buffer,
unsigned long  bufsize,
unsigned  samprate,
int  rectype 
)

Record audio from the default input device into a buffer.

Parameters
bufferPre-allocated buffer of 16-bit signed integers.
bufsizeNumber of samples the buffer can hold.
samprateDesired sampling rate in Hz (e.g. 16000, 44100).
rectypeLA_REC_ONCE (fill buffer then stop) or LA_REC_CONT (circular).
Returns
LA_OK on success, LA_ERROR on failure.
Note
Non-blocking: returns immediately while recording continues in the background.

Definition at line 314 of file liveio.c.

◆ LA_record_callback()

LaError_t LA_record_callback ( unsigned  samprate,
PaStreamCallback *  cb,
void *  cb_data 
)

Record using a user-supplied PortAudio callback function.

Parameters
samprateSampling rate in Hz.
cbYour PortAudio callback function.
cb_dataData pointer passed to your callback.
Returns
LA_OK on success, LA_ERROR on failure.

Definition at line 294 of file liveio.c.

◆ LA_stop_playing()

void LA_stop_playing ( void  )

Stop the current playback.

Definition at line 72 of file liveio.c.

◆ LA_stop_recording()

void LA_stop_recording ( void  )

Stop the current recording.

Definition at line 65 of file liveio.c.

◆ LA_terminate()

LaError_t LA_terminate ( void  )

Shut down the PortAudio system.

Call when you are finished with audio.

Shut down the PortAudio system.

Returns
LA_OK on success, LA_ERROR on failure.

Definition at line 441 of file liveio.c.