\hypertarget{mysound_8c}{}\doxysection{src/mysound.c File Reference}
\label{mysound_8c}\index{src/mysound.c@{src/mysound.c}}
{\ttfamily \#include \char`\"{}mysound.\+h\char`\"{}}\newline
{\ttfamily \#include \char`\"{}config.\+h\char`\"{}}\newline
{\ttfamily \#include \char`\"{}myarr.\+h\char`\"{}}\newline
{\ttfamily \#include \char`\"{}mydefs.\+h\char`\"{}}\newline
{\ttfamily \#include \char`\"{}myfft.\+h\char`\"{}}\newline
{\ttfamily \#include \char`\"{}mysync.\+h\char`\"{}}\newline
{\ttfamily \#include \char`\"{}parseargs.\+h\char`\"{}}\newline
{\ttfamily \#include \char`\"{}printing.\+h\char`\"{}}\newline
{\ttfamily \#include $<$alsa/asoundlib.\+h$>$}\newline
{\ttfamily \#include $<$ctype.\+h$>$}\newline
{\ttfamily \#include $<$errno.\+h$>$}\newline
{\ttfamily \#include $<$fftw3.\+h$>$}\newline
{\ttfamily \#include $<$limits.\+h$>$}\newline
{\ttfamily \#include $<$math.\+h$>$}\newline
{\ttfamily \#include $<$poll.\+h$>$}\newline
{\ttfamily \#include $<$pthread.\+h$>$}\newline
{\ttfamily \#include $<$stddef.\+h$>$}\newline
{\ttfamily \#include $<$stdint.\+h$>$}\newline
{\ttfamily \#include $<$stdlib.\+h$>$}\newline
{\ttfamily \#include $<$string.\+h$>$}\newline
{\ttfamily \#include $<$sys/timerfd.\+h$>$}\newline
{\ttfamily \#include $<$time.\+h$>$}\newline
{\ttfamily \#include $<$unistd.\+h$>$}\newline
Include dependency graph for mysound.\+c\+:\nopagebreak
\begin{figure}[H]
\begin{center}
\leavevmode
\includegraphics[width=350pt]{mysound_8c__incl}
\end{center}
\end{figure}
\doxysubsection*{Functions}
\begin{DoxyCompactItemize}
\item 
void \mbox{\hyperlink{mysound_8c_a19fd732c6566678a83efb08084b64c6a}{open\+\_\+capture\+\_\+elem}} (\mbox{\hyperlink{structCaptureCtx}{Capture\+Ctx}} $\ast$ctx, const \mbox{\hyperlink{structCapConfig}{Cap\+Config}} $\ast$cfg)
\begin{DoxyCompactList}\small\item\em Open the ALSA mixer and find the \textquotesingle{}Capture\textquotesingle{} element for microphone control. \end{DoxyCompactList}\item 
snd\+\_\+pcm\+\_\+t $\ast$ \mbox{\hyperlink{mysound_8c_a48f8c4cb9fd9917352fd118141aa4501}{init\+Audio}} (snd\+\_\+pcm\+\_\+format\+\_\+t format, char $\ast$device, unsigned int $\ast$rate)
\begin{DoxyCompactList}\small\item\em Initializes the ALSA audio capture device with the specified format, device name, and sample rate. \end{DoxyCompactList}\item 
int \mbox{\hyperlink{mysound_8c_a9eb1bfb6d399e3b6e7b537cd422c2654}{init\+Audio\+Source}} (\mbox{\hyperlink{structCapConfig}{Cap\+Config}} $\ast$cfg, unsigned int $\ast$actual\+Rate)
\begin{DoxyCompactList}\small\item\em 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. \end{DoxyCompactList}\item 
int \mbox{\hyperlink{mysound_8c_aa310cc9b4cfd0f227a9e3143a101393f}{read\+Buffer\+Or\+File}} (int $\ast$derivative, size\+\_\+t Array\+Length, FILE $\ast$fp\+Input, \mbox{\hyperlink{structCaptureCtx}{Capture\+Ctx}} $\ast$ctx, int16\+\_\+t $\ast$buffer16)
\begin{DoxyCompactList}\small\item\em Reads audio data from the capture device or a file into a buffer. \end{DoxyCompactList}\item 
int \mbox{\hyperlink{mysound_8c_a4c693ee8e2155813d63204597ee13447}{get\+Data}} (FILE $\ast$fp\+Input, struct \mbox{\hyperlink{structmyarr}{myarr}} $\ast$derivative, \mbox{\hyperlink{structCaptureCtx}{Capture\+Ctx}} $\ast$ctx, int16\+\_\+t $\ast$out)
\begin{DoxyCompactList}\small\item\em Retrieves audio data from the capture context or input file and processes it into a derivative array. \end{DoxyCompactList}\item 
int \mbox{\hyperlink{mysound_8c_a470c1d105d80aa605f7c57a0247b3c63}{capture\+Setup}} (\mbox{\hyperlink{structCaptureCtx}{Capture\+Ctx}} $\ast$ctx, \mbox{\hyperlink{structCapConfig}{Cap\+Config}} $\ast$cfg, unsigned int rate)
\begin{DoxyCompactList}\small\item\em capture\+Setup \end{DoxyCompactList}\item 
void \mbox{\hyperlink{mysound_8c_af0c1c5f075f1e4df5e7d6606a4909b84}{capture\+Teardown}} (\mbox{\hyperlink{structCaptureCtx}{Capture\+Ctx}} $\ast$ctx)
\begin{DoxyCompactList}\small\item\em Cleans up and releases resources associated with the audio capture context. \end{DoxyCompactList}\item 
int \mbox{\hyperlink{mysound_8c_a839355f40bec4ad51529e11ca71ac843}{read\+Samples}} (snd\+\_\+pcm\+\_\+t $\ast$cap, size\+\_\+t Array\+Length, int16\+\_\+t $\ast$out)
\begin{DoxyCompactList}\small\item\em Reads audio samples from the ALSA capture device into a buffer. \end{DoxyCompactList}\item 
const char $\ast$ \mbox{\hyperlink{mysound_8c_a335a86e7bb84220e0e05b8e89dde135b}{get\+\_\+default\+\_\+device}} (void)
\begin{DoxyCompactList}\small\item\em Retrieves the default ALSA audio capture device, preferring sysdefault devices with \textquotesingle{}usb\textquotesingle{} in their description. \end{DoxyCompactList}\item 
int \mbox{\hyperlink{mysound_8c_a54b162f247c2b5a30d97466af6320412}{get\+\_\+mic\+\_\+amplification}} (const \mbox{\hyperlink{structCaptureCtx}{Capture\+Ctx}} $\ast$ctx, long $\ast$value)
\begin{DoxyCompactList}\small\item\em Get the current microphone amplification (capture volume) from ALSA mixer. \end{DoxyCompactList}\item 
int \mbox{\hyperlink{mysound_8c_a9813c32cfc7c4c332daca0aa44874b95}{set\+\_\+mic\+\_\+amplification}} (const \mbox{\hyperlink{structCaptureCtx}{Capture\+Ctx}} $\ast$ctx, long value)
\begin{DoxyCompactList}\small\item\em Set the microphone amplification (capture volume) using ALSA mixer. \end{DoxyCompactList}\end{DoxyCompactItemize}


\doxysubsection{Function Documentation}
\mbox{\Hypertarget{mysound_8c_a470c1d105d80aa605f7c57a0247b3c63}\label{mysound_8c_a470c1d105d80aa605f7c57a0247b3c63}} 
\index{mysound.c@{mysound.c}!captureSetup@{captureSetup}}
\index{captureSetup@{captureSetup}!mysound.c@{mysound.c}}
\doxysubsubsection{\texorpdfstring{captureSetup()}{captureSetup()}}
{\footnotesize\ttfamily int capture\+Setup (\begin{DoxyParamCaption}\item[{\mbox{\hyperlink{structCaptureCtx}{Capture\+Ctx}} $\ast$}]{ctx,  }\item[{\mbox{\hyperlink{structCapConfig}{Cap\+Config}} $\ast$}]{cfg,  }\item[{unsigned int}]{rate }\end{DoxyParamCaption})}



capture\+Setup 

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


\begin{DoxyItemize}
\item Accepts an already-\/opened/initialized ALSA handle (from init\+Audio).
\item Computes Array\+Length = rate $\ast$ SECS\+\_\+\+HOUR $\ast$ 2 / bph
\item Queries period\+Size and builds poll descriptors. 
\end{DoxyItemize}\mbox{\Hypertarget{mysound_8c_af0c1c5f075f1e4df5e7d6606a4909b84}\label{mysound_8c_af0c1c5f075f1e4df5e7d6606a4909b84}} 
\index{mysound.c@{mysound.c}!captureTeardown@{captureTeardown}}
\index{captureTeardown@{captureTeardown}!mysound.c@{mysound.c}}
\doxysubsubsection{\texorpdfstring{captureTeardown()}{captureTeardown()}}
{\footnotesize\ttfamily void capture\+Teardown (\begin{DoxyParamCaption}\item[{\mbox{\hyperlink{structCaptureCtx}{Capture\+Ctx}} $\ast$}]{ctx }\end{DoxyParamCaption})}



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


\begin{DoxyItemize}
\item This function resets the \mbox{\hyperlink{structCaptureCtx}{Capture\+Ctx}} 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.
\end{DoxyItemize}


\begin{DoxyParams}{Parameters}
{\em ctx} & A pointer to the \mbox{\hyperlink{structCaptureCtx}{Capture\+Ctx}} structure to be cleaned up. \\
\hline
\end{DoxyParams}
\mbox{\Hypertarget{mysound_8c_a335a86e7bb84220e0e05b8e89dde135b}\label{mysound_8c_a335a86e7bb84220e0e05b8e89dde135b}} 
\index{mysound.c@{mysound.c}!get\_default\_device@{get\_default\_device}}
\index{get\_default\_device@{get\_default\_device}!mysound.c@{mysound.c}}
\doxysubsubsection{\texorpdfstring{get\_default\_device()}{get\_default\_device()}}
{\footnotesize\ttfamily const char $\ast$ get\+\_\+default\+\_\+device (\begin{DoxyParamCaption}\item[{void}]{ }\end{DoxyParamCaption})}



Retrieves the default ALSA audio capture device, preferring sysdefault devices with \textquotesingle{}usb\textquotesingle{} in their description. 

This function executes the \textquotesingle{}arecord -\/L\textquotesingle{} command to list available ALSA devices and parses the output to find a suitable default device. It first looks for devices with \textquotesingle{}sysdefault\+:\textquotesingle{} in their name and \textquotesingle{}usb\textquotesingle{} 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 \textquotesingle{}default\textquotesingle{}. \begin{DoxyReturn}{Returns}
A string representing the default ALSA device to use for audio capture. 
\end{DoxyReturn}
\mbox{\Hypertarget{mysound_8c_a54b162f247c2b5a30d97466af6320412}\label{mysound_8c_a54b162f247c2b5a30d97466af6320412}} 
\index{mysound.c@{mysound.c}!get\_mic\_amplification@{get\_mic\_amplification}}
\index{get\_mic\_amplification@{get\_mic\_amplification}!mysound.c@{mysound.c}}
\doxysubsubsection{\texorpdfstring{get\_mic\_amplification()}{get\_mic\_amplification()}}
{\footnotesize\ttfamily int get\+\_\+mic\+\_\+amplification (\begin{DoxyParamCaption}\item[{const \mbox{\hyperlink{structCaptureCtx}{Capture\+Ctx}} $\ast$}]{ctx,  }\item[{long $\ast$}]{value }\end{DoxyParamCaption})}



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.


\begin{DoxyParams}{Parameters}
{\em ctx} & Pointer to the \mbox{\hyperlink{structCaptureCtx}{Capture\+Ctx}} containing the mixer handle and element. \\
\hline
{\em value} & Output pointer for the amplification value. \\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{Returns}
0 on success, -\/1 on failure. 
\end{DoxyReturn}
\mbox{\Hypertarget{mysound_8c_a4c693ee8e2155813d63204597ee13447}\label{mysound_8c_a4c693ee8e2155813d63204597ee13447}} 
\index{mysound.c@{mysound.c}!getData@{getData}}
\index{getData@{getData}!mysound.c@{mysound.c}}
\doxysubsubsection{\texorpdfstring{getData()}{getData()}}
{\footnotesize\ttfamily int get\+Data (\begin{DoxyParamCaption}\item[{FILE $\ast$}]{fp\+Input,  }\item[{struct \mbox{\hyperlink{structmyarr}{myarr}} $\ast$}]{derivative,  }\item[{\mbox{\hyperlink{structCaptureCtx}{Capture\+Ctx}} $\ast$}]{ctx,  }\item[{int16\+\_\+t $\ast$}]{out }\end{DoxyParamCaption})}



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


\begin{DoxyItemize}
\item 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.
\end{DoxyItemize}


\begin{DoxyParams}{Parameters}
{\em fp\+Input} & A pointer to a FILE object representing the input file to read from. If NULL, audio will be read from the capture device. \\
\hline
{\em derivative} & A pointer to a myarr structure where the computed derivative values will be stored. \\
\hline
{\em ctx} & A pointer to the \mbox{\hyperlink{structCaptureCtx}{Capture\+Ctx}} structure containing the capture context and configuration. \\
\hline
{\em out} & A pointer to an int16\+\_\+t array where the raw audio samples will be stored before being processed into the derivative array. \\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{Returns}
An integer status code (0 for success, non-\/zero for failure). 
\end{DoxyReturn}
\mbox{\Hypertarget{mysound_8c_a48f8c4cb9fd9917352fd118141aa4501}\label{mysound_8c_a48f8c4cb9fd9917352fd118141aa4501}} 
\index{mysound.c@{mysound.c}!initAudio@{initAudio}}
\index{initAudio@{initAudio}!mysound.c@{mysound.c}}
\doxysubsubsection{\texorpdfstring{initAudio()}{initAudio()}}
{\footnotesize\ttfamily snd\+\_\+pcm\+\_\+t $\ast$ init\+Audio (\begin{DoxyParamCaption}\item[{snd\+\_\+pcm\+\_\+format\+\_\+t}]{format,  }\item[{char $\ast$}]{device,  }\item[{unsigned int $\ast$}]{rate }\end{DoxyParamCaption})}



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


\begin{DoxyItemize}
\item 
\begin{DoxyParams}{Parameters}
{\em format} & The audio format to use (e.\+g., SND\+\_\+\+PCM\+\_\+\+FORMAT\+\_\+\+S16\+\_\+\+LE). \\
\hline
{\em device} & The name of the ALSA device to open (e.\+g., \char`\"{}default\char`\"{}). \\
\hline
{\em rate} & A pointer to an unsigned int where the actual sample rate will be stored. This may be modified if the requested rate is not supported. \\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{Returns}
A pointer to the initialized snd\+\_\+pcm\+\_\+t capture handle, or NULL on failure. 
\end{DoxyReturn}

\end{DoxyItemize}\mbox{\Hypertarget{mysound_8c_a9eb1bfb6d399e3b6e7b537cd422c2654}\label{mysound_8c_a9eb1bfb6d399e3b6e7b537cd422c2654}} 
\index{mysound.c@{mysound.c}!initAudioSource@{initAudioSource}}
\index{initAudioSource@{initAudioSource}!mysound.c@{mysound.c}}
\doxysubsubsection{\texorpdfstring{initAudioSource()}{initAudioSource()}}
{\footnotesize\ttfamily int init\+Audio\+Source (\begin{DoxyParamCaption}\item[{\mbox{\hyperlink{structCapConfig}{Cap\+Config}} $\ast$}]{cfg,  }\item[{unsigned int $\ast$}]{actual\+Rate }\end{DoxyParamCaption})}



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. 


\begin{DoxyItemize}
\item 
\begin{DoxyParams}{Parameters}
{\em cfg} & A pointer to the \mbox{\hyperlink{structCapConfig}{Cap\+Config}} structure containing the desired configuration for audio capture. \\
\hline
{\em actual\+Rate} & A pointer to an unsigned int where the actual sample rate will be stored after initialization. \\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{Returns}
An integer status code (0 for success, non-\/zero for failure). 
\end{DoxyReturn}

\end{DoxyItemize}\mbox{\Hypertarget{mysound_8c_a19fd732c6566678a83efb08084b64c6a}\label{mysound_8c_a19fd732c6566678a83efb08084b64c6a}} 
\index{mysound.c@{mysound.c}!open\_capture\_elem@{open\_capture\_elem}}
\index{open\_capture\_elem@{open\_capture\_elem}!mysound.c@{mysound.c}}
\doxysubsubsection{\texorpdfstring{open\_capture\_elem()}{open\_capture\_elem()}}
{\footnotesize\ttfamily void open\+\_\+capture\+\_\+elem (\begin{DoxyParamCaption}\item[{\mbox{\hyperlink{structCaptureCtx}{Capture\+Ctx}} $\ast$}]{ctx,  }\item[{const \mbox{\hyperlink{structCapConfig}{Cap\+Config}} $\ast$}]{cfg }\end{DoxyParamCaption})}



Open the ALSA mixer and find the \textquotesingle{}Capture\textquotesingle{} 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 \textquotesingle{}Capture\textquotesingle{} element. The found element is stored in the configuration for future use.


\begin{DoxyParams}{Parameters}
{\em ctx} & Capture context to store the mixer handle and element. \\
\hline
{\em cfg} & Configuration containing device information and mixer state. \\
\hline
\end{DoxyParams}
\mbox{\Hypertarget{mysound_8c_aa310cc9b4cfd0f227a9e3143a101393f}\label{mysound_8c_aa310cc9b4cfd0f227a9e3143a101393f}} 
\index{mysound.c@{mysound.c}!readBufferOrFile@{readBufferOrFile}}
\index{readBufferOrFile@{readBufferOrFile}!mysound.c@{mysound.c}}
\doxysubsubsection{\texorpdfstring{readBufferOrFile()}{readBufferOrFile()}}
{\footnotesize\ttfamily int read\+Buffer\+Or\+File (\begin{DoxyParamCaption}\item[{int $\ast$}]{derivative,  }\item[{size\+\_\+t}]{Array\+Length,  }\item[{FILE $\ast$}]{fp\+Input,  }\item[{\mbox{\hyperlink{structCaptureCtx}{Capture\+Ctx}} $\ast$}]{ctx,  }\item[{int16\+\_\+t $\ast$}]{buffer16 }\end{DoxyParamCaption})}



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


\begin{DoxyItemize}
\item 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.
\end{DoxyItemize}


\begin{DoxyParams}{Parameters}
{\em derivative} & A pointer to an integer array where the read audio samples will be stored. \\
\hline
{\em Array\+Length} & The number of audio samples to read into the buffer. \\
\hline
{\em fp\+Input} & A pointer to a FILE object representing the input file to read from. If NULL, audio will be read from the capture device. \\
\hline
{\em ctx} & A pointer to the \mbox{\hyperlink{structCaptureCtx}{Capture\+Ctx}} structure containing the capture context and configuration. \\
\hline
{\em buffer16} & A pointer to an int16\+\_\+t array where the raw audio samples will be stored before being processed into the derivative array. \\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{Returns}
An integer status code (0 for success, non-\/zero for failure). 
\end{DoxyReturn}
\mbox{\Hypertarget{mysound_8c_a839355f40bec4ad51529e11ca71ac843}\label{mysound_8c_a839355f40bec4ad51529e11ca71ac843}} 
\index{mysound.c@{mysound.c}!readSamples@{readSamples}}
\index{readSamples@{readSamples}!mysound.c@{mysound.c}}
\doxysubsubsection{\texorpdfstring{readSamples()}{readSamples()}}
{\footnotesize\ttfamily int read\+Samples (\begin{DoxyParamCaption}\item[{snd\+\_\+pcm\+\_\+t $\ast$}]{cap,  }\item[{size\+\_\+t}]{Array\+Length,  }\item[{int16\+\_\+t $\ast$}]{out }\end{DoxyParamCaption})}



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


\begin{DoxyItemize}
\item 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.
\end{DoxyItemize}


\begin{DoxyParams}{Parameters}
{\em cap} & A pointer to the snd\+\_\+pcm\+\_\+t capture handle representing the ALSA capture device. \\
\hline
{\em Array\+Length} & The number of audio samples to read into the output buffer. \\
\hline
{\em out} & A pointer to an int16\+\_\+t array where the read audio samples will be stored. \\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{Returns}
The number of samples successfully read, or a negative error code on failure. 
\end{DoxyReturn}
\mbox{\Hypertarget{mysound_8c_a9813c32cfc7c4c332daca0aa44874b95}\label{mysound_8c_a9813c32cfc7c4c332daca0aa44874b95}} 
\index{mysound.c@{mysound.c}!set\_mic\_amplification@{set\_mic\_amplification}}
\index{set\_mic\_amplification@{set\_mic\_amplification}!mysound.c@{mysound.c}}
\doxysubsubsection{\texorpdfstring{set\_mic\_amplification()}{set\_mic\_amplification()}}
{\footnotesize\ttfamily int set\+\_\+mic\+\_\+amplification (\begin{DoxyParamCaption}\item[{const \mbox{\hyperlink{structCaptureCtx}{Capture\+Ctx}} $\ast$}]{ctx,  }\item[{long}]{value }\end{DoxyParamCaption})}



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\textquotesingle{}s \mbox{\hyperlink{structAmpResult}{Amp\+Result}} struct.


\begin{DoxyParams}{Parameters}
{\em ctx} & Pointer to the \mbox{\hyperlink{structCaptureCtx}{Capture\+Ctx}} containing the mixer handle and element. \\
\hline
{\em value} & The amplification value to set. \\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{Returns}
0 on success, -\/1 on failure. 
\end{DoxyReturn}
