tico 0.1
Mechanical Watch Terminal Timegrapher
Data Structures | Typedefs | Functions
mysound.h File Reference
#include "config.h"
#include "myarr.h"
#include <alsa/asoundlib.h>
#include <stdio.h>
Include dependency graph for mysound.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  AmpResult
 
struct  CaptureCtx
 Context for audio capture, containing ALSA parameters and configuration. More...
 

Typedefs

typedef struct CaptureCtx CaptureCtx
 Context for audio capture, containing ALSA parameters and configuration. More...
 

Functions

void open_capture_elem (CaptureCtx *ctx, const CapConfig *cfg)
 Open the ALSA mixer and find the 'Capture' element for microphone control. More...
 
const char * get_default_device (void)
 Retrieves the default ALSA audio capture device, preferring sysdefault devices with 'usb' in their description. More...
 
snd_pcm_t * initAudio (snd_pcm_format_t format, char *device, unsigned int *rate)
 Initializes the ALSA audio capture device with the specified format, device name, and sample rate. More...
 
int initAudioSource (CapConfig *cfg, unsigned int *actualRate)
 Initializes the audio source for capture based on the provided configuration. This function sets up the ALSA capture device and retrieves the actual sample rate being used. More...
 
int readBufferOrFile (int *derivative, size_t ArrayLength, FILE *fpInput, CaptureCtx *ctx, int16_t *buffer16)
 Reads audio data from the capture device or a file into a buffer. More...
 
int getData (FILE *fpInput, struct myarr *derivative, CaptureCtx *ctx, int16_t *out)
 Retrieves audio data from the capture context or input file and processes it into a derivative array. More...
 
void captureTeardown (CaptureCtx *ctx)
 Cleans up and releases resources associated with the audio capture context. More...
 
int captureSetup (CaptureCtx *ctx, CapConfig *cfg, unsigned int rate)
 Sets up the ALSA audio capture device based on the provided context and configuration. More...
 
int readSamples (snd_pcm_t *cap, size_t ArrayLength, int16_t *out)
 Reads audio samples from the ALSA capture device into a buffer. More...
 
int get_mic_amplification (const CaptureCtx *ctx, long *value)
 Get the current microphone amplification (capture volume) from ALSA mixer. More...
 
int set_mic_amplification (const CaptureCtx *ctx, long value)
 Set the microphone amplification (capture volume) using ALSA mixer. More...
 

Typedef Documentation

◆ CaptureCtx

typedef struct CaptureCtx CaptureCtx

Context for audio capture, containing ALSA parameters and configuration.

Function Documentation

◆ captureSetup()

int captureSetup ( CaptureCtx ctx,
CapConfig cfg,
unsigned int  rate 
)

Sets up the ALSA audio capture device based on the provided context and configuration.

  • This function configures the ALSA capture device according to the parameters specified in the CaptureCtx structure and the CapConfig configuration. It initializes the capture device and prepares it for audio data capture.
Parameters
ctxA pointer to the CaptureCtx structure containing the capture context and configuration.
cfgA pointer to the CapConfig structure containing the desired configuration for audio capture.
rateThe sample rate to be used for audio capture.
Returns
An integer status code (0 for success, non-zero for failure).

Sets up the ALSA audio capture device based on the provided context and configuration.

  • Accepts an already-opened/initialized ALSA handle (from initAudio).
  • Computes ArrayLength = rate * SECS_HOUR * 2 / bph
  • Queries periodSize and builds poll descriptors.

◆ captureTeardown()

void captureTeardown ( CaptureCtx ctx)

Cleans up and releases resources associated with the audio capture context.

  • This function resets the CaptureCtx structure, effectively releasing any resources associated with the audio capture context. It is important to call this function when the capture context is no longer needed to prevent resource leaks.
Parameters
ctxA pointer to the CaptureCtx structure to be cleaned up.

◆ get_default_device()

const char * get_default_device ( void  )

Retrieves the default ALSA audio capture device, preferring sysdefault devices with 'usb' in their description.

This function executes the 'arecord -L' command to list available ALSA devices and parses the output to find a suitable default device. It first looks for devices with 'sysdefault:' in their name and 'usb' in their description. If such a device is found, it is returned as the default. If no USB sysdefault device is found, it falls back to any sysdefault device. If no sysdefault devices are found, it returns 'default'.

Returns
A string representing the default ALSA device to use for audio capture.

◆ get_mic_amplification()

int get_mic_amplification ( const CaptureCtx ctx,
long *  value 
)

Get the current microphone amplification (capture volume) from ALSA mixer.

This function queries the ALSA mixer for the current capture volume (microphone gain) for the specified capture context.

Parameters
ctxPointer to the CaptureCtx containing the mixer handle and element.
valueOutput pointer for the amplification value.
Returns
0 on success, -1 on failure.

◆ getData()

int getData ( FILE *  fpInput,
struct myarr derivative,
CaptureCtx ctx,
int16_t *  out 
)

Retrieves audio data from the capture context or input file and processes it into a derivative array.

  • This function reads audio samples from the ALSA capture device or from a specified input file, depending on the context. The read samples are stored in the provided output buffer, and a derivative array is computed based on the raw audio samples.
Parameters
fpInputA pointer to a FILE object representing the input file to read from. If NULL, audio will be read from the capture device.
derivativeA pointer to a myarr structure where the computed derivative values will be stored.
ctxA pointer to the CaptureCtx structure containing the capture context and configuration.
outA pointer to an int16_t array where the raw audio samples will be stored before being processed into the derivative array.
Returns
An integer status code (0 for success, non-zero for failure).

◆ initAudio()

snd_pcm_t * initAudio ( snd_pcm_format_t  format,
char *  device,
unsigned int *  rate 
)

Initializes the ALSA audio capture device with the specified format, device name, and sample rate.

  • Parameters
    formatThe audio format to use (e.g., SND_PCM_FORMAT_S16_LE).
    deviceThe name of the ALSA device to open (e.g., "default").
    rateA pointer to an unsigned int where the actual sample rate will be stored. This may be modified if the requested rate is not supported.
    Returns
    A pointer to the initialized snd_pcm_t capture handle, or NULL on failure.

◆ initAudioSource()

int initAudioSource ( CapConfig cfg,
unsigned int *  actualRate 
)

Initializes the audio source for capture based on the provided configuration. This function sets up the ALSA capture device and retrieves the actual sample rate being used.

  • Parameters
    cfgA pointer to the CapConfig structure containing the desired configuration for audio capture.
    actualRateA pointer to an unsigned int where the actual sample rate will be stored after initialization.
    Returns
    An integer status code (0 for success, non-zero for failure).

◆ open_capture_elem()

void open_capture_elem ( CaptureCtx ctx,
const CapConfig cfg 
)

Open the ALSA mixer and find the 'Capture' element for microphone control.

This function checks if the mixer handle and element are already stored in the configuration. If not, it opens the mixer, attaches to the specified device, registers simple elements, loads the mixer, and finds the 'Capture' element. The found element is stored in the configuration for future use.

Parameters
ctxCapture context to store the mixer handle and element.
cfgConfiguration containing device information and mixer state.

◆ readBufferOrFile()

int readBufferOrFile ( int *  derivative,
size_t  ArrayLength,
FILE *  fpInput,
CaptureCtx ctx,
int16_t *  buffer16 
)

Reads audio data from the capture device or a file into a buffer.

  • This function reads audio samples from the ALSA capture device or from a specified input file, depending on the context. The read samples are stored in the provided buffer.
Parameters
derivativeA pointer to an integer array where the read audio samples will be stored.
ArrayLengthThe number of audio samples to read into the buffer.
fpInputA pointer to a FILE object representing the input file to read from. If NULL, audio will be read from the capture device.
ctxA pointer to the CaptureCtx structure containing the capture context and configuration.
buffer16A pointer to an int16_t array where the raw audio samples will be stored before being processed into the derivative array.
Returns
An integer status code (0 for success, non-zero for failure).

◆ readSamples()

int readSamples ( snd_pcm_t *  cap,
size_t  ArrayLength,
int16_t *  out 
)

Reads audio samples from the ALSA capture device into a buffer.

  • This function reads a specified number of audio samples from the ALSA capture device and stores them in the provided output buffer. It handles potential errors such as EAGAIN (no data available) and XRUN (buffer underrun) by implementing appropriate recovery strategies.
Parameters
capA pointer to the snd_pcm_t capture handle representing the ALSA capture device.
ArrayLengthThe number of audio samples to read into the output buffer.
outA pointer to an int16_t array where the read audio samples will be stored.
Returns
The number of samples successfully read, or a negative error code on failure.

◆ set_mic_amplification()

int set_mic_amplification ( const CaptureCtx ctx,
long  value 
)

Set the microphone amplification (capture volume) using ALSA mixer.

This function sets the ALSA mixer capture volume (microphone gain) to the specified value for the given capture context. The value should be within the hardware limits stored in the context's AmpResult struct.

Parameters
ctxPointer to the CaptureCtx containing the mixer handle and element.
valueThe amplification value to set.
Returns
0 on success, -1 on failure.