\doxysection{cont\+\_\+adseg.\+c}
\label{cont__adseg_8c_source}\index{src/sphinx\_adtools/cont\_adseg.c@{src/sphinx\_adtools/cont\_adseg.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-\/2001\ 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\ \textcolor{comment}{/*}}
\DoxyCodeLine{00038\ \textcolor{comment}{\ *\ cont\_adseg.c\ -\/-\/\ Continuously\ listen\ and\ segment\ input\ speech\ into\ utterances.}}
\DoxyCodeLine{00039\ \textcolor{comment}{\ *\ }}
\DoxyCodeLine{00040\ \textcolor{comment}{\ *\ HISTORY}}
\DoxyCodeLine{00041\ \textcolor{comment}{\ *\ }}
\DoxyCodeLine{00042\ \textcolor{comment}{\ *\ 27-\/Jun-\/96\ \ \ \ M\ K\ Ravishankar\ (rkm@cs.cmu.edu)\ at\ Carnegie\ Mellon\ University}}
\DoxyCodeLine{00043\ \textcolor{comment}{\ *\ \ \ \ \ \ \ \ \ \ \ \ \ \ Created.}}
\DoxyCodeLine{00044\ \textcolor{comment}{\ */}}
\DoxyCodeLine{00045\ }
\DoxyCodeLine{00046\ \textcolor{preprocessor}{\#include\ <stdio.h>}}
\DoxyCodeLine{00047\ \textcolor{preprocessor}{\#include\ <stdlib.h>}}
\DoxyCodeLine{00048\ \textcolor{preprocessor}{\#include\ <string.h>}}
\DoxyCodeLine{00049\ \textcolor{preprocessor}{\#include\ <assert.h>}}
\DoxyCodeLine{00050\ \textcolor{preprocessor}{\#include\ <math.h>}}
\DoxyCodeLine{00051\ }
\DoxyCodeLine{00052\ \textcolor{preprocessor}{\#include\ <sphinxbase/prim\_type.h>}}
\DoxyCodeLine{00053\ \textcolor{preprocessor}{\#include\ <sphinxbase/ad.h>}}
\DoxyCodeLine{00054\ \textcolor{preprocessor}{\#include\ <sphinxbase/cont\_ad.h>}}
\DoxyCodeLine{00055\ \textcolor{preprocessor}{\#include\ <sphinxbase/err.h>}}
\DoxyCodeLine{00056\ }
\DoxyCodeLine{00057\ \textcolor{comment}{/*}}
\DoxyCodeLine{00058\ \textcolor{comment}{\ *\ Segment\ raw\ A/D\ input\ data\ into\ utterances\ whenever\ silence\ region\ of\ given}}
\DoxyCodeLine{00059\ \textcolor{comment}{\ *\ duration\ is\ encountered.}}
\DoxyCodeLine{00060\ \textcolor{comment}{\ *\ Utterances\ are\ written\ to\ files\ named\ 0001.raw,\ 0002.raw,\ 0003.raw,\ etc.}}
\DoxyCodeLine{00061\ \textcolor{comment}{\ */}}
\DoxyCodeLine{00062\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00063\ main(int32\ argc,\ \textcolor{keywordtype}{char}\ **argv)}
\DoxyCodeLine{00064\ \{}
\DoxyCodeLine{00065\ \ \ \ \ ad\_rec\_t\ *ad;}
\DoxyCodeLine{00066\ \ \ \ \ cont\_ad\_t\ *cont;}
\DoxyCodeLine{00067\ \ \ \ \ int32\ k,\ uttno,\ ts,\ uttlen,\ sps,\ endsilsamples;}
\DoxyCodeLine{00068\ \ \ \ \ \textcolor{keywordtype}{float}\ endsil;}
\DoxyCodeLine{00069\ \ \ \ \ int16\ buf[4096];}
\DoxyCodeLine{00070\ \ \ \ \ FILE\ *fp;}
\DoxyCodeLine{00071\ \ \ \ \ \textcolor{keywordtype}{char}\ file[1024];}
\DoxyCodeLine{00072\ }
\DoxyCodeLine{00073\ \ \ \ \ \textcolor{keywordflow}{if}\ ((argc\ !=\ 3)\ ||}
\DoxyCodeLine{00074\ \ \ \ \ \ \ \ \ (sscanf(argv[1],\ \textcolor{stringliteral}{"{}\%d"{}},\ \&sps)\ !=\ 1)\ ||}
\DoxyCodeLine{00075\ \ \ \ \ \ \ \ \ (sscanf(argv[2],\ \textcolor{stringliteral}{"{}\%f"{}},\ \&endsil)\ !=\ 1)\ ||\ (endsil\ <=\ 0.0))\ \{}
\DoxyCodeLine{00076\ \ \ \ \ \ \ \ \ E\_FATAL(\textcolor{stringliteral}{"{}Usage:\ \%s\ <sampling-\/rate>\ <utt-\/end-\/sil(sec)>\(\backslash\)n"{}},\ argv[0]);}
\DoxyCodeLine{00077\ \ \ \ \ \}}
\DoxyCodeLine{00078\ }
\DoxyCodeLine{00079\ \ \ \ \ \textcolor{comment}{/*\ Convert\ desired\ min.\ inter-\/utterance\ silence\ duration\ to\ \#samples\ */}}
\DoxyCodeLine{00080\ \ \ \ \ endsilsamples\ =\ (int32)\ (endsil\ *\ sps);}
\DoxyCodeLine{00081\ }
\DoxyCodeLine{00082\ \ \ \ \ \textcolor{comment}{/*\ Open\ raw\ A/D\ device\ */}}
\DoxyCodeLine{00083\ \ \ \ \ \textcolor{keywordflow}{if}\ ((ad\ =\ ad\_open\_sps(sps))\ ==\ NULL)}
\DoxyCodeLine{00084\ \ \ \ \ \ \ \ \ E\_FATAL(\textcolor{stringliteral}{"{}ad\_open\_sps(\%d)\ failed\(\backslash\)n"{}},\ sps);}
\DoxyCodeLine{00085\ }
\DoxyCodeLine{00086\ \ \ \ \ \textcolor{comment}{/*\ Associate\ new\ continuous\ listening\ module\ with\ opened\ raw\ A/D\ device\ */}}
\DoxyCodeLine{00087\ \ \ \ \ \textcolor{keywordflow}{if}\ ((cont\ =\ cont\_ad\_init(ad,\ ad\_read))\ ==\ NULL)}
\DoxyCodeLine{00088\ \ \ \ \ \ \ \ \ E\_FATAL(\textcolor{stringliteral}{"{}cont\_ad\_init\ failed\(\backslash\)n"{}});}
\DoxyCodeLine{00089\ }
\DoxyCodeLine{00090\ \ \ \ \ \textcolor{comment}{/*\ Calibrate\ continuous\ listening\ for\ background\ noise/silence\ level\ */}}
\DoxyCodeLine{00091\ \ \ \ \ printf(\textcolor{stringliteral}{"{}Calibrating\ ..."{}});}
\DoxyCodeLine{00092\ \ \ \ \ fflush(stdout);}
\DoxyCodeLine{00093\ \ \ \ \ ad\_start\_rec(ad);}
\DoxyCodeLine{00094\ \ \ \ \ \textcolor{keywordflow}{if}\ (cont\_ad\_calib(cont)\ <\ 0)}
\DoxyCodeLine{00095\ \ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}\ failed\(\backslash\)n"{}});}
\DoxyCodeLine{00096\ \ \ \ \ \textcolor{keywordflow}{else}}
\DoxyCodeLine{00097\ \ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}\ done\(\backslash\)n"{}});}
\DoxyCodeLine{00098\ }
\DoxyCodeLine{00099\ \ \ \ \ \textcolor{comment}{/*\ Forever\ listen\ for\ utterances\ */}}
\DoxyCodeLine{00100\ \ \ \ \ printf(\textcolor{stringliteral}{"{}You\ may\ speak\ now\(\backslash\)n"{}});}
\DoxyCodeLine{00101\ \ \ \ \ fflush(stdout);}
\DoxyCodeLine{00102\ \ \ \ \ uttno\ =\ 0;}
\DoxyCodeLine{00103\ \ \ \ \ \textcolor{keywordflow}{for}\ (;;)\ \{}
\DoxyCodeLine{00104\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Wait\ for\ beginning\ of\ next\ utterance;\ for\ non-\/silence\ data\ */}}
\DoxyCodeLine{00105\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{while}\ ((k\ =\ cont\_ad\_read(cont,\ buf,\ 4096))\ ==\ 0);}
\DoxyCodeLine{00106\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (k\ <\ 0)}
\DoxyCodeLine{00107\ \ \ \ \ \ \ \ \ \ \ \ \ E\_FATAL(\textcolor{stringliteral}{"{}cont\_ad\_read\ failed\(\backslash\)n"{}});}
\DoxyCodeLine{00108\ }
\DoxyCodeLine{00109\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Non-\/silence\ data\ received;\ open\ and\ write\ to\ new\ logging\ file\ */}}
\DoxyCodeLine{00110\ \ \ \ \ \ \ \ \ uttno++;}
\DoxyCodeLine{00111\ \ \ \ \ \ \ \ \ sprintf(file,\ \textcolor{stringliteral}{"{}\%04d.raw"{}},\ uttno);}
\DoxyCodeLine{00112\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ ((fp\ =\ fopen(file,\ \textcolor{stringliteral}{"{}wb"{}}))\ ==\ NULL)}
\DoxyCodeLine{00113\ \ \ \ \ \ \ \ \ \ \ \ \ E\_FATAL\_SYSTEM(\textcolor{stringliteral}{"{}Failed\ to\ open\ '\%s'\ for\ reading"{}},\ file);}
\DoxyCodeLine{00114\ \ \ \ \ \ \ \ \ fwrite(buf,\ \textcolor{keyword}{sizeof}(int16),\ k,\ fp);}
\DoxyCodeLine{00115\ \ \ \ \ \ \ \ \ uttlen\ =\ k;}
\DoxyCodeLine{00116\ \ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}Utterance\ \%04d,\ logging\ to\ \%s\(\backslash\)n"{}},\ uttno,\ file);}
\DoxyCodeLine{00117\ }
\DoxyCodeLine{00118\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Note\ current\ timestamp\ */}}
\DoxyCodeLine{00119\ \ \ \ \ \ \ \ \ ts\ =\ cont-\/>read\_ts;}
\DoxyCodeLine{00120\ }
\DoxyCodeLine{00121\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Read\ utterance\ data\ until\ a\ gap\ of\ at\ least\ 1\ sec\ observed\ */}}
\DoxyCodeLine{00122\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (;;)\ \{}
\DoxyCodeLine{00123\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ ((k\ =\ cont\_ad\_read(cont,\ buf,\ 4096))\ <\ 0)}
\DoxyCodeLine{00124\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ E\_FATAL(\textcolor{stringliteral}{"{}cont\_ad\_read\ failed\(\backslash\)n"{}});}
\DoxyCodeLine{00125\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (k\ ==\ 0)\ \{}
\DoxyCodeLine{00126\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*}}
\DoxyCodeLine{00127\ \textcolor{comment}{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ *\ No\ speech\ data\ available;\ check\ current\ timestamp.\ \ End\ of}}
\DoxyCodeLine{00128\ \textcolor{comment}{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ *\ utterance\ if\ no\ non-\/silence\ data\ been\ read\ for\ at\ least\ 1\ sec.}}
\DoxyCodeLine{00129\ \textcolor{comment}{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ */}}
\DoxyCodeLine{00130\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ ((cont-\/>read\_ts\ -\/\ ts)\ >\ endsilsamples)}
\DoxyCodeLine{00131\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{break};}
\DoxyCodeLine{00132\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00133\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00134\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Note\ timestamp\ at\ the\ end\ of\ most\ recently\ read\ speech\ data\ */}}
\DoxyCodeLine{00135\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ts\ =\ cont-\/>read\_ts;}
\DoxyCodeLine{00136\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ uttlen\ +=\ k;}
\DoxyCodeLine{00137\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ fwrite(buf,\ \textcolor{keyword}{sizeof}(int16),\ k,\ fp);}
\DoxyCodeLine{00138\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00139\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00140\ \ \ \ \ \ \ \ \ fclose(fp);}
\DoxyCodeLine{00141\ }
\DoxyCodeLine{00142\ \ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}\(\backslash\)tUtterance\ \%04d\ =\ \%d\ samples\ (\%.1fsec)\(\backslash\)n\(\backslash\)n"{}},}
\DoxyCodeLine{00143\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ uttno,\ uttlen,\ (\textcolor{keywordtype}{double})\ uttlen\ /\ (\textcolor{keywordtype}{double})\ sps);}
\DoxyCodeLine{00144\ \ \ \ \ \}}
\DoxyCodeLine{00145\ }
\DoxyCodeLine{00146\ \ \ \ \ ad\_stop\_rec(ad);}
\DoxyCodeLine{00147\ \ \ \ \ cont\_ad\_close(cont);}
\DoxyCodeLine{00148\ \ \ \ \ ad\_close(ad);}
\DoxyCodeLine{00149\ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00150\ \}}

\end{DoxyCode}
