\doxysection{ad\+\_\+pulse.\+c}
\label{ad__pulse_8c_source}\index{src/libsphinxad/ad\_pulse.c@{src/libsphinxad/ad\_pulse.c}}

\begin{DoxyCode}{0}
\DoxyCodeLine{00001\ \textcolor{comment}{/*\ -\/*-\/\ c-\/basic-\/offset:\ 4;\ indent-\/tabs-\/mode:\ nil\ -\/*-\/\ */}}
\DoxyCodeLine{00002\ \textcolor{comment}{/*\ ====================================================================}}
\DoxyCodeLine{00003\ \textcolor{comment}{\ *\ Copyright\ (c)\ 1999-\/2011\ Carnegie\ Mellon\ University.\ \ All\ rights}}
\DoxyCodeLine{00004\ \textcolor{comment}{\ *\ reserved.}}
\DoxyCodeLine{00005\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00006\ \textcolor{comment}{\ *\ Redistribution\ and\ use\ in\ source\ and\ binary\ forms,\ with\ or\ without}}
\DoxyCodeLine{00007\ \textcolor{comment}{\ *\ modification,\ are\ permitted\ provided\ that\ the\ following\ conditions}}
\DoxyCodeLine{00008\ \textcolor{comment}{\ *\ are\ met:}}
\DoxyCodeLine{00009\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00010\ \textcolor{comment}{\ *\ 1.\ Redistributions\ of\ source\ code\ must\ retain\ the\ above\ copyright}}
\DoxyCodeLine{00011\ \textcolor{comment}{\ *\ \ \ \ notice,\ this\ list\ of\ conditions\ and\ the\ following\ disclaimer.\ }}
\DoxyCodeLine{00012\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00013\ \textcolor{comment}{\ *\ 2.\ Redistributions\ in\ binary\ form\ must\ reproduce\ the\ above\ copyright}}
\DoxyCodeLine{00014\ \textcolor{comment}{\ *\ \ \ \ notice,\ this\ list\ of\ conditions\ and\ the\ following\ disclaimer\ in}}
\DoxyCodeLine{00015\ \textcolor{comment}{\ *\ \ \ \ the\ documentation\ and/or\ other\ materials\ provided\ with\ the}}
\DoxyCodeLine{00016\ \textcolor{comment}{\ *\ \ \ \ distribution.}}
\DoxyCodeLine{00017\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00018\ \textcolor{comment}{\ *\ This\ work\ was\ supported\ in\ part\ by\ funding\ from\ the\ Defense\ Advanced\ }}
\DoxyCodeLine{00019\ \textcolor{comment}{\ *\ Research\ Projects\ Agency\ and\ the\ National\ Science\ Foundation\ of\ the\ }}
\DoxyCodeLine{00020\ \textcolor{comment}{\ *\ United\ States\ of\ America,\ and\ the\ CMU\ Sphinx\ Speech\ Consortium.}}
\DoxyCodeLine{00021\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00022\ \textcolor{comment}{\ *\ THIS\ SOFTWARE\ IS\ PROVIDED\ BY\ CARNEGIE\ MELLON\ UNIVERSITY\ \`{}\`{}AS\ IS''\ AND\ }}
\DoxyCodeLine{00023\ \textcolor{comment}{\ *\ ANY\ EXPRESSED\ OR\ IMPLIED\ WARRANTIES,\ INCLUDING,\ BUT\ NOT\ LIMITED\ TO,\ }}
\DoxyCodeLine{00024\ \textcolor{comment}{\ *\ THE\ IMPLIED\ WARRANTIES\ OF\ MERCHANTABILITY\ AND\ FITNESS\ FOR\ A\ PARTICULAR}}
\DoxyCodeLine{00025\ \textcolor{comment}{\ *\ PURPOSE\ ARE\ DISCLAIMED.\ \ IN\ NO\ EVENT\ SHALL\ CARNEGIE\ MELLON\ UNIVERSITY}}
\DoxyCodeLine{00026\ \textcolor{comment}{\ *\ NOR\ ITS\ EMPLOYEES\ BE\ LIABLE\ FOR\ ANY\ DIRECT,\ INDIRECT,\ INCIDENTAL,}}
\DoxyCodeLine{00027\ \textcolor{comment}{\ *\ SPECIAL,\ EXEMPLARY,\ OR\ CONSEQUENTIAL\ DAMAGES\ (INCLUDING,\ BUT\ NOT\ }}
\DoxyCodeLine{00028\ \textcolor{comment}{\ *\ LIMITED\ TO,\ PROCUREMENT\ OF\ SUBSTITUTE\ GOODS\ OR\ SERVICES;\ LOSS\ OF\ USE,\ }}
\DoxyCodeLine{00029\ \textcolor{comment}{\ *\ DATA,\ OR\ PROFITS;\ OR\ BUSINESS\ INTERRUPTION)\ HOWEVER\ CAUSED\ AND\ ON\ ANY\ }}
\DoxyCodeLine{00030\ \textcolor{comment}{\ *\ THEORY\ OF\ LIABILITY,\ WHETHER\ IN\ CONTRACT,\ STRICT\ LIABILITY,\ OR\ TORT\ }}
\DoxyCodeLine{00031\ \textcolor{comment}{\ *\ (INCLUDING\ NEGLIGENCE\ OR\ OTHERWISE)\ ARISING\ IN\ ANY\ WAY\ OUT\ OF\ THE\ USE\ }}
\DoxyCodeLine{00032\ \textcolor{comment}{\ *\ OF\ THIS\ SOFTWARE,\ EVEN\ IF\ ADVISED\ OF\ THE\ POSSIBILITY\ OF\ SUCH\ DAMAGE.}}
\DoxyCodeLine{00033\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00034\ \textcolor{comment}{\ *\ ====================================================================}}
\DoxyCodeLine{00035\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00036\ \textcolor{comment}{\ */}}
\DoxyCodeLine{00037\ }
\DoxyCodeLine{00038\ \textcolor{comment}{/*\ Input\ for\ Pulseaudio\ */}}
\DoxyCodeLine{00039\ }
\DoxyCodeLine{00040\ \textcolor{preprocessor}{\#include\ <stdio.h>}}
\DoxyCodeLine{00041\ \textcolor{preprocessor}{\#include\ <string.h>}}
\DoxyCodeLine{00042\ \textcolor{preprocessor}{\#include\ <config.h>}}
\DoxyCodeLine{00043\ }
\DoxyCodeLine{00044\ \textcolor{preprocessor}{\#include\ <pulse/pulseaudio.h>}}
\DoxyCodeLine{00045\ \textcolor{preprocessor}{\#include\ <pulse/simple.h>}}
\DoxyCodeLine{00046\ }
\DoxyCodeLine{00047\ \textcolor{preprocessor}{\#include\ "{}prim\_type.h"{}}}
\DoxyCodeLine{00048\ \textcolor{preprocessor}{\#include\ "{}ad.h"{}}}
\DoxyCodeLine{00049\ }
\DoxyCodeLine{00050\ ad\_rec\_t\ *}
\DoxyCodeLine{00051\ ad\_open\_dev(\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *dev,\ int32\ samples\_per\_sec)}
\DoxyCodeLine{00052\ \{}
\DoxyCodeLine{00053\ \ \ \ \ ad\_rec\_t\ *handle;}
\DoxyCodeLine{00054\ \ \ \ \ pa\_simple\ *pa;}
\DoxyCodeLine{00055\ \ \ \ \ pa\_sample\_spec\ ss;}
\DoxyCodeLine{00056\ \ \ \ \ \textcolor{keywordtype}{int}\ error;}
\DoxyCodeLine{00057\ }
\DoxyCodeLine{00058\ \ \ \ \ ss.format\ =\ PA\_SAMPLE\_S16LE;}
\DoxyCodeLine{00059\ \ \ \ \ ss.channels\ =\ 1;}
\DoxyCodeLine{00060\ \ \ \ \ ss.rate\ =\ 16000;\ \textcolor{comment}{//samples\_per\_sec;}}
\DoxyCodeLine{00061\ \ \ \ \ }
\DoxyCodeLine{00062\ \ \ \ \ pa\ =\ pa\_simple\_new(NULL,\ \textcolor{stringliteral}{"{}ASR"{}},\ PA\_STREAM\_RECORD,\ dev,\ \textcolor{stringliteral}{"{}Speech"{}},\ \&ss,\ NULL,\ NULL,\ \&error);}
\DoxyCodeLine{00063\ \ \ \ \ \textcolor{keywordflow}{if}\ (pa\ ==\ NULL)\ \{}
\DoxyCodeLine{00064\ \ \ \ \ \ \ \ \ fprintf(stderr,\ \textcolor{stringliteral}{"{}Error\ opening\ audio\ device\ \%s\ for\ capture:\ \%s\(\backslash\)n"{}},\ dev,\ pa\_strerror(error));}
\DoxyCodeLine{00065\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00066\ \ \ \ \ \}}
\DoxyCodeLine{00067\ }
\DoxyCodeLine{00068\ \ \ \ \ \textcolor{keywordflow}{if}\ ((handle\ =\ (ad\_rec\_t\ *)\ calloc(1,\ \textcolor{keyword}{sizeof}(ad\_rec\_t)))\ ==\ NULL)\ \{}
\DoxyCodeLine{00069\ \ \ \ \ \ \ \ \ fprintf(stderr,\ \textcolor{stringliteral}{"{}Failed\ to\ allocate\ memory\ for\ ad\ device\(\backslash\)n"{}});}
\DoxyCodeLine{00070\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00071\ \ \ \ \ \}}
\DoxyCodeLine{00072\ }
\DoxyCodeLine{00073\ \ \ \ \ handle-\/>pa\ =\ pa;}
\DoxyCodeLine{00074\ \ \ \ \ handle-\/>recording\ =\ 0;}
\DoxyCodeLine{00075\ \ \ \ \ handle-\/>sps\ =\ samples\_per\_sec;}
\DoxyCodeLine{00076\ \ \ \ \ handle-\/>bps\ =\ \textcolor{keyword}{sizeof}(int16);}
\DoxyCodeLine{00077\ }
\DoxyCodeLine{00078\ \ \ \ \ \textcolor{keywordflow}{return}\ handle;}
\DoxyCodeLine{00079\ \}}
\DoxyCodeLine{00080\ }
\DoxyCodeLine{00081\ }
\DoxyCodeLine{00082\ ad\_rec\_t\ *}
\DoxyCodeLine{00083\ ad\_open\_sps(int32\ samples\_per\_sec)}
\DoxyCodeLine{00084\ \{}
\DoxyCodeLine{00085\ \ \ \ \ \textcolor{keywordflow}{return}\ ad\_open\_dev(DEFAULT\_DEVICE,\ samples\_per\_sec);}
\DoxyCodeLine{00086\ \}}
\DoxyCodeLine{00087\ }
\DoxyCodeLine{00088\ ad\_rec\_t\ *}
\DoxyCodeLine{00089\ ad\_open(\textcolor{keywordtype}{void})}
\DoxyCodeLine{00090\ \{}
\DoxyCodeLine{00091\ \ \ \ \ \textcolor{keywordflow}{return}\ ad\_open\_sps(DEFAULT\_SAMPLES\_PER\_SEC);}
\DoxyCodeLine{00092\ \}}
\DoxyCodeLine{00093\ }
\DoxyCodeLine{00094\ }
\DoxyCodeLine{00095\ int32}
\DoxyCodeLine{00096\ ad\_start\_rec(ad\_rec\_t\ *\ r)}
\DoxyCodeLine{00097\ \{}
\DoxyCodeLine{00098\ \ \ \ \ \textcolor{keywordflow}{if}\ (r-\/>recording)}
\DoxyCodeLine{00099\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ AD\_ERR\_GEN;}
\DoxyCodeLine{00100\ }
\DoxyCodeLine{00101\ \ \ \ \ r-\/>recording\ =\ 1;}
\DoxyCodeLine{00102\ }
\DoxyCodeLine{00103\ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00104\ \}}
\DoxyCodeLine{00105\ }
\DoxyCodeLine{00106\ }
\DoxyCodeLine{00107\ int32}
\DoxyCodeLine{00108\ ad\_stop\_rec(ad\_rec\_t\ *\ r)}
\DoxyCodeLine{00109\ \{}
\DoxyCodeLine{00110\ \ \ \ \ \textcolor{keywordflow}{if}\ (!r-\/>recording)}
\DoxyCodeLine{00111\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ AD\_ERR\_GEN;}
\DoxyCodeLine{00112\ }
\DoxyCodeLine{00113\ \ \ \ \ r-\/>recording\ =\ 0;}
\DoxyCodeLine{00114\ }
\DoxyCodeLine{00115\ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00116\ \}}
\DoxyCodeLine{00117\ }
\DoxyCodeLine{00118\ }
\DoxyCodeLine{00119\ int32}
\DoxyCodeLine{00120\ ad\_read(ad\_rec\_t\ *\ r,\ int16\ *\ buf,\ int32\ max)}
\DoxyCodeLine{00121\ \{}
\DoxyCodeLine{00122\ \ \ \ \ \textcolor{keywordtype}{int}\ error;}
\DoxyCodeLine{00123\ }
\DoxyCodeLine{00124\ \ \ \ \ \textcolor{keywordflow}{if}\ (!r-\/>recording)}
\DoxyCodeLine{00125\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ AD\_EOF;}
\DoxyCodeLine{00126\ \ \ \ \ \ \ \ \ }
\DoxyCodeLine{00127\ \ \ \ \ \textcolor{keywordflow}{if}\ (pa\_simple\_read(r-\/>pa,\ (\textcolor{keywordtype}{void}*)buf,\ max\ *\ 2,\ \&error)\ <\ 0)\ \{}
\DoxyCodeLine{00128\ \ \ \ \ \ \ \ \ fprintf(stderr,\ \textcolor{stringliteral}{"{}Failed\ to\ read\ speech:\ \%s\(\backslash\)n"{}},\ pa\_strerror(error));}
\DoxyCodeLine{00129\ \ \ \ \ \}}
\DoxyCodeLine{00130\ \ \ \ \ }
\DoxyCodeLine{00131\ \ \ \ \ \textcolor{keywordflow}{return}\ max;}
\DoxyCodeLine{00132\ \}}
\DoxyCodeLine{00133\ }
\DoxyCodeLine{00134\ }
\DoxyCodeLine{00135\ int32}
\DoxyCodeLine{00136\ ad\_close(ad\_rec\_t\ *\ r)}
\DoxyCodeLine{00137\ \{}
\DoxyCodeLine{00138\ \ \ \ \ \textcolor{keywordflow}{if}\ (r-\/>pa\ ==\ NULL)}
\DoxyCodeLine{00139\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ AD\_ERR\_NOT\_OPEN;}
\DoxyCodeLine{00140\ }
\DoxyCodeLine{00141\ \ \ \ \ \textcolor{keywordflow}{if}\ (r-\/>recording)\ \{}
\DoxyCodeLine{00142\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (ad\_stop\_rec(r)\ <\ 0)}
\DoxyCodeLine{00143\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ AD\_ERR\_GEN;}
\DoxyCodeLine{00144\ \ \ \ \ \}}
\DoxyCodeLine{00145\ \ \ \ \ pa\_simple\_free(r-\/>pa);}
\DoxyCodeLine{00146\ \ \ \ \ free(r);}
\DoxyCodeLine{00147\ }
\DoxyCodeLine{00148\ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00149\ \}}

\end{DoxyCode}
