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

Live audio recording and playback implementation. More...

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

Go to the source code of this file.

Data Structures

struct  LA_simpleData
 Simple audio buffer with read/write positions. More...
 

Functions

double LA_get_cur_record_time (void)
 Get the current time (in seconds) of the recording stream.
 
double LA_get_cur_play_time (void)
 Get the current time (in seconds) of the playback stream.
 
void LA_stop_recording (void)
 Stop the current recording.
 
void LA_stop_playing (void)
 Stop the current playback.
 
int LA_is_recording (void)
 Check if the recording stream is currently active.
 
int LA_is_playing (void)
 Check if the playback stream is currently active.
 
static int _ContinuousRecordCallback (const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo, PaStreamCallbackFlags statusFlags, void *userData)
 Callback for continuous (circular buffer) recording.
 
static int _RecordCallback (const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo, PaStreamCallbackFlags statusFlags, void *userData)
 Callback for one-shot recording (fill the buffer once, then stop).
 
static int _PlayCallback (const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo, PaStreamCallbackFlags statusFlags, void *userData)
 Callback for playback: reads mono samples and duplicates them to both stereo output channels (left and right get the same data).
 
static void _RecordStreamFinished (void *userData)
 Called by PortAudio when a one-shot recording stream finishes.
 
static void _PlayStreamFinished (void *userData)
 Called by PortAudio when a playback stream finishes.
 
static LaError_t _ensure_init (void)
 Auto-initialise PortAudio if not already done.
 
static LaError_t _setup_input_params (PaStreamParameters *p)
 Configure mono 16-bit input parameters for the default device.
 
static LaError_t _setup_output_params (PaStreamParameters *p)
 Configure stereo 16-bit output parameters for the default device.
 
LaError_t LA_record_callback (unsigned samprate, PaStreamCallback *cb, void *cb_data)
 Record using a user-supplied PortAudio callback function.
 
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_play_callback (unsigned samprate, PaStreamCallback *cb, void *cb_data)
 Play using a user-supplied PortAudio callback function.
 
LaError_t LA_play (const void *buffer, unsigned long bufsize, unsigned samprate)
 Play audio from a buffer to the default output device.
 
void LA_print_last_error (FILE *stream)
 Print the last PortAudio error message to the given stream.
 
LaError_t LA_init (void)
 Initialise the PortAudio system.
 
LaError_t LA_terminate (void)
 Terminate the PortAudio system and release resources.
 

Variables

static int LA_InitDone = 0
 Has PA been initialised?

 
static PaError LA_LastError = paNoError
 Most recent PA error code.
 
static PaStream * LA_PlayStream = nullptr
 Active playback stream

 
static PaStream * LA_RecordStream = nullptr
 Active recording stream

 
static LA_simpleData LA_data
 Shared buffer descriptor

 

Detailed Description

Live audio recording and playback implementation.

Author
Chuck Wooters woote.nosp@m.rs@h.nosp@m.ey.co.nosp@m.m

This is a wrapper around PortAudio (http://portaudio.com), a cross-platform library for real-time audio I/O.

PortAudio works on a callback model: you give it a function, and it calls that function whenever it needs more audio data (for playback) or has new data available (for recording). The callbacks run on a separate high-priority thread managed by the OS audio subsystem.

Definition in file liveio.c.

Function Documentation

◆ _ContinuousRecordCallback()

static int _ContinuousRecordCallback ( const void *  inputBuffer,
void *  outputBuffer,
unsigned long  framesPerBuffer,
const PaStreamCallbackTimeInfo *  timeInfo,
PaStreamCallbackFlags  statusFlags,
void *  userData 
)
static

Callback for continuous (circular buffer) recording.

When the write position reaches the end of the buffer, it wraps around to the beginning. This means old data is overwritten – the buffer always contains the most recent audio.

Definition at line 115 of file liveio.c.

◆ _ensure_init()

static LaError_t _ensure_init ( void  )
static

Auto-initialise PortAudio if not already done.

Definition at line 252 of file liveio.c.

◆ _PlayCallback()

static int _PlayCallback ( const void *  inputBuffer,
void *  outputBuffer,
unsigned long  framesPerBuffer,
const PaStreamCallbackTimeInfo *  timeInfo,
PaStreamCallbackFlags  statusFlags,
void *  userData 
)
static

Callback for playback: reads mono samples and duplicates them to both stereo output channels (left and right get the same data).

Definition at line 188 of file liveio.c.

◆ _PlayStreamFinished()

static void _PlayStreamFinished ( void *  userData)
static

Called by PortAudio when a playback stream finishes.

Closes the stream so resources are released.

Definition at line 238 of file liveio.c.

◆ _RecordCallback()

static int _RecordCallback ( const void *  inputBuffer,
void *  outputBuffer,
unsigned long  framesPerBuffer,
const PaStreamCallbackTimeInfo *  timeInfo,
PaStreamCallbackFlags  statusFlags,
void *  userData 
)
static

Callback for one-shot recording (fill the buffer once, then stop).

Returns paComplete when the buffer is full, which tells PortAudio to stop the stream after this callback returns.

Definition at line 147 of file liveio.c.

◆ _RecordStreamFinished()

static void _RecordStreamFinished ( void *  userData)
static

Called by PortAudio when a one-shot recording stream finishes.

Closes the stream so resources are released.

Definition at line 225 of file liveio.c.

◆ _setup_input_params()

static LaError_t _setup_input_params ( PaStreamParameters *  p)
static

Configure mono 16-bit input parameters for the default device.

Definition at line 261 of file liveio.c.

◆ _setup_output_params()

static LaError_t _setup_output_params ( PaStreamParameters *  p)
static

Configure stereo 16-bit output parameters for the default device.

Definition at line 276 of file liveio.c.

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

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 currently active.

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 currently active.

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  )

Terminate the PortAudio system and release resources.

Shut down the PortAudio system.

Returns
LA_OK on success, LA_ERROR on failure.

Definition at line 441 of file liveio.c.

Variable Documentation

◆ LA_data

LA_simpleData LA_data
static

Shared buffer descriptor

Definition at line 43 of file liveio.c.

◆ LA_InitDone

int LA_InitDone = 0
static

Has PA been initialised?

Definition at line 39 of file liveio.c.

◆ LA_LastError

PaError LA_LastError = paNoError
static

Most recent PA error code.

Definition at line 40 of file liveio.c.

◆ LA_PlayStream

PaStream* LA_PlayStream = nullptr
static

Active playback stream

Definition at line 41 of file liveio.c.

◆ LA_RecordStream

PaStream* LA_RecordStream = nullptr
static

Active recording stream

Definition at line 42 of file liveio.c.