\doxysection{ad\+\_\+jack.\+c}
\label{ad__jack_8c_source}\index{src/libsphinxad/ad\_jack.c@{src/libsphinxad/ad\_jack.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)\ 2011\ Glenn\ Pierce.\ \ All\ rights\ reserved.}}
\DoxyCodeLine{00004\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00005\ \textcolor{comment}{\ *\ Redistribution\ and\ use\ in\ source\ and\ binary\ forms,\ with\ or\ without}}
\DoxyCodeLine{00006\ \textcolor{comment}{\ *\ modification,\ are\ permitted\ provided\ that\ the\ following\ conditions}}
\DoxyCodeLine{00007\ \textcolor{comment}{\ *\ are\ met:}}
\DoxyCodeLine{00008\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00009\ \textcolor{comment}{\ *\ 1.\ Redistributions\ of\ source\ code\ must\ retain\ the\ above\ copyright}}
\DoxyCodeLine{00010\ \textcolor{comment}{\ *\ \ \ \ notice,\ this\ list\ of\ conditions\ and\ the\ following\ disclaimer.\ }}
\DoxyCodeLine{00011\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00012\ \textcolor{comment}{\ *\ 2.\ Redistributions\ in\ binary\ form\ must\ reproduce\ the\ above\ copyright}}
\DoxyCodeLine{00013\ \textcolor{comment}{\ *\ \ \ \ notice,\ this\ list\ of\ conditions\ and\ the\ following\ disclaimer\ in}}
\DoxyCodeLine{00014\ \textcolor{comment}{\ *\ \ \ \ the\ documentation\ and/or\ other\ materials\ provided\ with\ the}}
\DoxyCodeLine{00015\ \textcolor{comment}{\ *\ \ \ \ distribution.}}
\DoxyCodeLine{00016\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00017\ \textcolor{comment}{\ *\ THIS\ SOFTWARE\ IS\ PROVIDED\ BY\ GLENN\ PIERCE\ \`{}\`{}AS\ IS''\ AND\ }}
\DoxyCodeLine{00018\ \textcolor{comment}{\ *\ ANY\ EXPRESSED\ OR\ IMPLIED\ WARRANTIES,\ INCLUDING,\ BUT\ NOT\ LIMITED\ TO,\ }}
\DoxyCodeLine{00019\ \textcolor{comment}{\ *\ THE\ IMPLIED\ WARRANTIES\ OF\ MERCHANTABILITY\ AND\ FITNESS\ FOR\ A\ PARTICULAR}}
\DoxyCodeLine{00020\ \textcolor{comment}{\ *\ PURPOSE\ ARE\ DISCLAIMED.\ \ IN\ NO\ EVENT\ SHALL\ CARNEGIE\ MELLON\ UNIVERSITY}}
\DoxyCodeLine{00021\ \textcolor{comment}{\ *\ NOR\ ITS\ EMPLOYEES\ BE\ LIABLE\ FOR\ ANY\ DIRECT,\ INDIRECT,\ INCIDENTAL,}}
\DoxyCodeLine{00022\ \textcolor{comment}{\ *\ SPECIAL,\ EXEMPLARY,\ OR\ CONSEQUENTIAL\ DAMAGES\ (INCLUDING,\ BUT\ NOT\ }}
\DoxyCodeLine{00023\ \textcolor{comment}{\ *\ LIMITED\ TO,\ PROCUREMENT\ OF\ SUBSTITUTE\ GOODS\ OR\ SERVICES;\ LOSS\ OF\ USE,\ }}
\DoxyCodeLine{00024\ \textcolor{comment}{\ *\ DATA,\ OR\ PROFITS;\ OR\ BUSINESS\ INTERRUPTION)\ HOWEVER\ CAUSED\ AND\ ON\ ANY\ }}
\DoxyCodeLine{00025\ \textcolor{comment}{\ *\ THEORY\ OF\ LIABILITY,\ WHETHER\ IN\ CONTRACT,\ STRICT\ LIABILITY,\ OR\ TORT\ }}
\DoxyCodeLine{00026\ \textcolor{comment}{\ *\ (INCLUDING\ NEGLIGENCE\ OR\ OTHERWISE)\ ARISING\ IN\ ANY\ WAY\ OUT\ OF\ THE\ USE\ }}
\DoxyCodeLine{00027\ \textcolor{comment}{\ *\ OF\ THIS\ SOFTWARE,\ EVEN\ IF\ ADVISED\ OF\ THE\ POSSIBILITY\ OF\ SUCH\ DAMAGE.}}
\DoxyCodeLine{00028\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00029\ \textcolor{comment}{\ *\ ====================================================================}}
\DoxyCodeLine{00030\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00031\ \textcolor{comment}{\ */}}
\DoxyCodeLine{00032\ \textcolor{comment}{/*\ Sphinx\ II\ libad\ (Linux)}}
\DoxyCodeLine{00033\ \textcolor{comment}{\ *\ \string^\string^\string^\string^\string^\string^\string^\string^\string^\string^\string^\string^\string^\string^\string^\string^\string^\string^\string^\string^\string^\string^\string^}}
\DoxyCodeLine{00034\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00035\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00036\ \textcolor{comment}{\ *\ Glenn\ Pierce\ (glennpierce@gmail.com)}}
\DoxyCodeLine{00037\ \textcolor{comment}{\ *\ ********************************************************************}}
\DoxyCodeLine{00038\ \textcolor{comment}{\ */}}
\DoxyCodeLine{00039\ }
\DoxyCodeLine{00040\ \textcolor{preprocessor}{\#include\ <fcntl.h>}}
\DoxyCodeLine{00041\ \textcolor{preprocessor}{\#include\ <stdio.h>}}
\DoxyCodeLine{00042\ \textcolor{preprocessor}{\#include\ <stdlib.h>}}
\DoxyCodeLine{00043\ \textcolor{preprocessor}{\#include\ <string.h>}}
\DoxyCodeLine{00044\ \textcolor{preprocessor}{\#include\ <errno.h>}}
\DoxyCodeLine{00045\ \textcolor{preprocessor}{\#include\ <unistd.h>}}
\DoxyCodeLine{00046\ \textcolor{preprocessor}{\#include\ <config.h>}}
\DoxyCodeLine{00047\ \textcolor{preprocessor}{\#include\ <limits.h>}}
\DoxyCodeLine{00048\ }
\DoxyCodeLine{00049\ \textcolor{preprocessor}{\#include\ "{}prim\_type.h"{}}}
\DoxyCodeLine{00050\ \textcolor{preprocessor}{\#include\ "{}ad.h"{}}}
\DoxyCodeLine{00051\ }
\DoxyCodeLine{00052\ \textcolor{preprocessor}{\#include\ <jack/jack.h>}}
\DoxyCodeLine{00053\ }
\DoxyCodeLine{00054\ \textcolor{preprocessor}{\#define\ DEFAULT\_DEVICE\ "{}system:capture\_1"{}}}
\DoxyCodeLine{00055\ \textcolor{preprocessor}{\#define\ BUFFER\_SIZE\ 352800}}
\DoxyCodeLine{00056\ }
\DoxyCodeLine{00057\ \textcolor{comment}{//\#define\ MIC\_SPEAKER\_PASSTHROUGH\_DEBUG}}
\DoxyCodeLine{00058\ }
\DoxyCodeLine{00059\ \textcolor{keyword}{const}\ \textcolor{keywordtype}{size\_t}\ sample\_size\ =\ \textcolor{keyword}{sizeof}(jack\_default\_audio\_sample\_t);}
\DoxyCodeLine{00060\ \textcolor{keyword}{const}\ \textcolor{keywordtype}{size\_t}\ int16\_range\_over\_two\ =\ (-\/SHRT\_MIN\ +\ SHRT\_MAX)\ /\ 2.0;}
\DoxyCodeLine{00061\ }
\DoxyCodeLine{00062\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00063\ process\ (jack\_nframes\_t\ nframes,\ \textcolor{keywordtype}{void}\ *arg)}
\DoxyCodeLine{00064\ \{}
\DoxyCodeLine{00065\ \ \ \ \ ad\_rec\_t\ *handle\ =\ (ad\_rec\_t\ *)\ arg;}
\DoxyCodeLine{00066\ }
\DoxyCodeLine{00067\ \ \ \ \ \textcolor{keywordtype}{size\_t}\ buffer\_size\ =\ jack\_ringbuffer\_write\_space\ (handle-\/>rbuffer);\ }
\DoxyCodeLine{00068\ }
\DoxyCodeLine{00069\ \ \ \ \ jack\_default\_audio\_sample\_t\ *in\ =\ (jack\_default\_audio\_sample\_t\ *)\ jack\_port\_get\_buffer\ (handle-\/>input\_port,\ nframes);}
\DoxyCodeLine{00070\ \ \ \ \ }
\DoxyCodeLine{00071\ \ \ \ \ \textcolor{keywordflow}{if}\ (buffer\_size\ <=\ 0)\ \{}
\DoxyCodeLine{00072\ \ \ \ \ \ \ \ \ fprintf(stderr,\ \textcolor{stringliteral}{"{}JACK:\ buffer\ is\ full.\ Deactivating\ JACK\ client.\(\backslash\)n"{}});}
\DoxyCodeLine{00073\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 1;}
\DoxyCodeLine{00074\ \ \ \ \ \}}
\DoxyCodeLine{00075\ }
\DoxyCodeLine{00076\ \ \ \ \ \textcolor{comment}{//\ Write\ to\ jack\ ringbuffer\ which\ should\ be\ thread\ safe}}
\DoxyCodeLine{00077\ \ \ \ \ jack\_ringbuffer\_write\ (handle-\/>rbuffer,\ (\textcolor{keywordtype}{char}*)\ in,\ sample\_size\ *\ nframes);}
\DoxyCodeLine{00078\ }
\DoxyCodeLine{00079\ \textcolor{preprocessor}{\#ifdef\ MIC\_SPEAKER\_PASSTHROUGH\_DEBUG}}
\DoxyCodeLine{00080\ }
\DoxyCodeLine{00081\ \ \ \ \ jack\_default\_audio\_sample\_t\ *out\ =\ (jack\_default\_audio\_sample\_t\ *)\ jack\_port\_get\_buffer\ (handle-\/>output\_port,\ nframes);}
\DoxyCodeLine{00082\ }
\DoxyCodeLine{00083\ \ \ \ \ \textcolor{comment}{//\ Output\ mic\ output\ to\ speakers\ (Just\ for\ testing)}}
\DoxyCodeLine{00084\ \ \ \ \ memcpy\ (out,\ in,\ sample\_size\ *\ nframes);}
\DoxyCodeLine{00085\ }
\DoxyCodeLine{00086\ \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{00087\ }
\DoxyCodeLine{00088\ \ \ \ \ \textcolor{keywordflow}{return}\ 0;\ \ \ \ \ \ }
\DoxyCodeLine{00089\ \}}
\DoxyCodeLine{00090\ }
\DoxyCodeLine{00091\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00092\ error\ (\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *desc)}
\DoxyCodeLine{00093\ \{}
\DoxyCodeLine{00094\ \ \ \ \ fprintf\ (stderr,\ \textcolor{stringliteral}{"{}JACK\ error:\ \%s\(\backslash\)n"{}},\ desc);}
\DoxyCodeLine{00095\ \}}
\DoxyCodeLine{00096\ }
\DoxyCodeLine{00097\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00098\ jack\_shutdown\ (\textcolor{keywordtype}{void}\ *arg)}
\DoxyCodeLine{00099\ \{}
\DoxyCodeLine{00100\ \ \ \ \ exit(1);}
\DoxyCodeLine{00101\ \}}
\DoxyCodeLine{00102\ }
\DoxyCodeLine{00103\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00104\ srate\ (jack\_nframes\_t\ nframes,\ \textcolor{keywordtype}{void}\ *arg)}
\DoxyCodeLine{00105\ }
\DoxyCodeLine{00106\ \{}
\DoxyCodeLine{00107\ \ \ \ \ printf\ (\textcolor{stringliteral}{"{}JACK:\ The\ sample\ rate\ is\ now\ \%u/sec\(\backslash\)n"{}},\ nframes);}
\DoxyCodeLine{00108\ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00109\ \}}
\DoxyCodeLine{00110\ }
\DoxyCodeLine{00111\ ad\_rec\_t\ *}
\DoxyCodeLine{00112\ ad\_open\_dev(\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *dev,\ int32\ sps)}
\DoxyCodeLine{00113\ \{}
\DoxyCodeLine{00114\ \ \ \ \ ad\_rec\_t\ *handle;}
\DoxyCodeLine{00115\ \ \ \ \ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ **ports;}
\DoxyCodeLine{00116\ }
\DoxyCodeLine{00117\ \textcolor{preprocessor}{\#ifdef\ HAVE\_SAMPLERATE\_H}}
\DoxyCodeLine{00118\ \ \ \ \ \textcolor{keywordtype}{int}\ resample\_error;}
\DoxyCodeLine{00119\ \ \ \ \ \textcolor{keywordtype}{double}\ samplerates\_ratio;}
\DoxyCodeLine{00120\ \ \ \ \ jack\_nframes\_t\ jack\_samplerate;}
\DoxyCodeLine{00121\ \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{00122\ }
\DoxyCodeLine{00123\ \ \ \ \ \textcolor{keywordflow}{if}\ (dev\ ==\ NULL)\ \{}
\DoxyCodeLine{00124\ \ \ \ \ \ \ \ \ dev\ =\ DEFAULT\_DEVICE;}
\DoxyCodeLine{00125\ \ \ \ \ \}}
\DoxyCodeLine{00126\ }
\DoxyCodeLine{00127\ \ \ \ \ printf(\textcolor{stringliteral}{"{}JACK:\ Setting\ default\ device:\ \%s\(\backslash\)n"{}},\ dev);}
\DoxyCodeLine{00128\ }
\DoxyCodeLine{00129\ \ \ \ \ \textcolor{keywordflow}{if}\ ((handle\ =\ (ad\_rec\_t\ *)\ calloc(1,\ \textcolor{keyword}{sizeof}(ad\_rec\_t)))\ ==\ NULL)\ \{}
\DoxyCodeLine{00130\ \ \ \ \ \ \ \ \ fprintf(stderr,\ \textcolor{stringliteral}{"{}calloc(\%d)\ failed\(\backslash\)n"{}},\ (\textcolor{keywordtype}{int})\textcolor{keyword}{sizeof}(ad\_rec\_t));}
\DoxyCodeLine{00131\ \ \ \ \ \ \ \ \ abort();}
\DoxyCodeLine{00132\ \ \ \ \ \}}
\DoxyCodeLine{00133\ \ }
\DoxyCodeLine{00134\ \ \ \ \ \textcolor{comment}{/*\ Tell\ the\ JACK\ server\ to\ call\ error()\ whenever\ it}}
\DoxyCodeLine{00135\ \textcolor{comment}{\ \ \ \ \ \ \ experiences\ an\ error.\ \ }}
\DoxyCodeLine{00136\ \textcolor{comment}{\ \ \ \ */}}
\DoxyCodeLine{00137\ \ \ \ \ jack\_set\_error\_function\ (error);}
\DoxyCodeLine{00138\ }
\DoxyCodeLine{00139\ \ \ \ \ \textcolor{comment}{/*\ Try\ to\ become\ a\ client\ of\ the\ JACK\ server\ */}}
\DoxyCodeLine{00140\ \ \ \ \ \textcolor{keywordflow}{if}\ ((handle-\/>client\ =\ jack\_client\_open\ (\textcolor{stringliteral}{"{}jack\_ad"{}},\ (jack\_options\_t)0,\ NULL))\ ==\ 0)\ \{}
\DoxyCodeLine{00141\ \ \ \ \ \ \ \ \ fprintf\ (stderr,\ \textcolor{stringliteral}{"{}jack\ server\ not\ running?\(\backslash\)n"{}});}
\DoxyCodeLine{00142\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00143\ \ \ \ \ \}}
\DoxyCodeLine{00144\ }
\DoxyCodeLine{00145\ \textcolor{preprocessor}{\#ifdef\ HAVE\_SAMPLERATE\_H}}
\DoxyCodeLine{00146\ \ \ \ \ handle-\/>resample\_buffer\ =\ malloc(BUFFER\_SIZE);}
\DoxyCodeLine{00147\ }
\DoxyCodeLine{00148\ \ \ \ \ jack\_samplerate\ =\ jack\_get\_sample\_rate(handle-\/>client);}
\DoxyCodeLine{00149\ \ \ \ \ samplerates\_ratio\ =\ (double)((\textcolor{keywordtype}{double})jack\_samplerate\ /\ (double)sps);}
\DoxyCodeLine{00150\ \ \ \ \ }
\DoxyCodeLine{00151\ \ \ \ \ handle-\/>rbuffer\ =\ jack\_ringbuffer\_create((\textcolor{keywordtype}{int})((\textcolor{keywordtype}{double})BUFFER\_SIZE\ *\ samplerates\_ratio));}
\DoxyCodeLine{00152\ \ \ \ \ handle-\/>sample\_buffer\ =\ malloc((\textcolor{keywordtype}{int})((\textcolor{keywordtype}{double})BUFFER\_SIZE\ *\ samplerates\_ratio));}
\DoxyCodeLine{00153\ \textcolor{preprocessor}{\#else}}
\DoxyCodeLine{00154\ \ \ \ \ handle-\/>rbuffer\ =\ jack\_ringbuffer\_create(BUFFER\_SIZE);}
\DoxyCodeLine{00155\ \ \ \ \ handle-\/>sample\_buffer\ =\ malloc(BUFFER\_SIZE);}
\DoxyCodeLine{00156\ \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{00157\ }
\DoxyCodeLine{00158\ \ \ \ \ \textcolor{keywordflow}{if}(handle-\/>rbuffer\ ==\ NULL)\ \{}
\DoxyCodeLine{00159\ \ \ \ \ \ \ \ \ fprintf\ (stderr,\ \textcolor{stringliteral}{"{}Failed\ to\ create\ jack\ ringbuffer\(\backslash\)n"{}});}
\DoxyCodeLine{00160\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00161\ \ \ \ \ \}}
\DoxyCodeLine{00162\ }
\DoxyCodeLine{00163\ \ \ \ \ \textcolor{comment}{/*\ Tell\ the\ JACK\ server\ to\ call\ \`{}process()'\ whenever}}
\DoxyCodeLine{00164\ \textcolor{comment}{\ \ \ \ \ \ \ there\ is\ work\ to\ be\ done.}}
\DoxyCodeLine{00165\ \textcolor{comment}{\ \ \ \ */}}
\DoxyCodeLine{00166\ \ \ \ \ jack\_set\_process\_callback\ (handle-\/>client,\ process,\ handle);}
\DoxyCodeLine{00167\ }
\DoxyCodeLine{00168\ \ \ \ \ \textcolor{comment}{/*\ Tell\ the\ JACK\ server\ to\ call\ \`{}srate()'\ whenever}}
\DoxyCodeLine{00169\ \textcolor{comment}{\ \ \ \ \ \ \ the\ sample\ rate\ of\ the\ system\ changes.}}
\DoxyCodeLine{00170\ \textcolor{comment}{\ \ \ \ */}}
\DoxyCodeLine{00171\ \ \ \ \ jack\_set\_sample\_rate\_callback\ (handle-\/>client,\ srate,\ 0);}
\DoxyCodeLine{00172\ }
\DoxyCodeLine{00173\ \ \ \ \ \textcolor{comment}{/*\ Tell\ the\ JACK\ server\ to\ call\ \`{}jack\_shutdown()'\ if}}
\DoxyCodeLine{00174\ \textcolor{comment}{\ \ \ \ \ \ \ it\ ever\ shuts\ down,\ either\ entirely,\ or\ if\ it}}
\DoxyCodeLine{00175\ \textcolor{comment}{\ \ \ \ \ \ \ just\ decides\ to\ stop\ calling\ us.}}
\DoxyCodeLine{00176\ \textcolor{comment}{\ \ \ \ */}}
\DoxyCodeLine{00177\ \ \ \ \ jack\_on\_shutdown\ (handle-\/>client,\ jack\_shutdown,\ 0);}
\DoxyCodeLine{00178\ }
\DoxyCodeLine{00179\ }
\DoxyCodeLine{00180\ \ \ \ \ \textcolor{comment}{/*\ Create\ the\ input\ port\ */}}
\DoxyCodeLine{00181\ \ \ \ \ \textcolor{keywordflow}{if}((handle-\/>input\_port\ =\ jack\_port\_register\ (handle-\/>client,\ \textcolor{stringliteral}{"{}jack\_ad\_input"{}},\ JACK\_DEFAULT\_AUDIO\_TYPE,\ JackPortIsInput,\ 0))\ ==\ 0)\ \{}
\DoxyCodeLine{00182\ \ \ \ \ \ \ \ \ fprintf\ (stderr,\ \textcolor{stringliteral}{"{}cannot\ register\ input\ port!\(\backslash\)n"{}});}
\DoxyCodeLine{00183\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00184\ \ \ \ \ \}}
\DoxyCodeLine{00185\ \ \ \ }
\DoxyCodeLine{00186\ \ \ \ \ \textcolor{keywordflow}{if}((handle-\/>output\_port\ =\ jack\_port\_register\ (handle-\/>client,\ \textcolor{stringliteral}{"{}jack\_ad\_output"{}},\ JACK\_DEFAULT\_AUDIO\_TYPE,\ JackPortIsPhysical|JackPortIsOutput,\ 0))\ ==\ 0)\ \{}
\DoxyCodeLine{00187\ \ \ \ \ \ \ \ \ fprintf\ (stderr,\ \textcolor{stringliteral}{"{}cannot\ register\ output\ port!\(\backslash\)n"{}});}
\DoxyCodeLine{00188\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00189\ \ \ \ \ \}}
\DoxyCodeLine{00190\ \ }
\DoxyCodeLine{00191\ \ \ \ \ \textcolor{comment}{/*\ Tell\ the\ JACK\ server\ that\ we\ are\ ready\ to\ start\ */}}
\DoxyCodeLine{00192\ \ \ \ \ \textcolor{keywordflow}{if}\ (jack\_activate\ (handle-\/>client))\ \{}
\DoxyCodeLine{00193\ \ \ \ \ \ \ \ \ fprintf\ (stderr,\ \textcolor{stringliteral}{"{}cannot\ activate\ client"{}});}
\DoxyCodeLine{00194\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00195\ \ \ \ \ \}}
\DoxyCodeLine{00196\ }
\DoxyCodeLine{00197\ \ \ \ \ \textcolor{comment}{/*\ Connect\ the\ ports.\ Note:\ you\ can't\ do\ this\ before}}
\DoxyCodeLine{00198\ \textcolor{comment}{\ \ \ \ \ \ \ the\ client\ is\ activated,\ because\ we\ can't\ allow}}
\DoxyCodeLine{00199\ \textcolor{comment}{\ \ \ \ \ \ \ connections\ to\ be\ made\ to\ clients\ that\ aren't}}
\DoxyCodeLine{00200\ \textcolor{comment}{\ \ \ \ \ \ \ running.}}
\DoxyCodeLine{00201\ \textcolor{comment}{\ \ \ \ */}}
\DoxyCodeLine{00202\ }
\DoxyCodeLine{00203\ \ \ \ \ \textcolor{keywordflow}{if}\ ((ports\ =\ jack\_get\_ports\ (handle-\/>client,\ dev,\ NULL,\ JackPortIsOutput))\ ==\ NULL)\ \{}
\DoxyCodeLine{00204\ \ \ \ \ \ \ \ \ fprintf(stderr,\ \textcolor{stringliteral}{"{}Cannot\ find\ any\ physical\ capture\ ports\(\backslash\)n"{}});}
\DoxyCodeLine{00205\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00206\ \ \ \ \ \}}
\DoxyCodeLine{00207\ }
\DoxyCodeLine{00208\ \ \ \ \ \textcolor{keywordflow}{if}\ (jack\_connect\ (handle-\/>client,\ ports[0],\ jack\_port\_name\ (handle-\/>input\_port)))\ \{}
\DoxyCodeLine{00209\ \ \ \ \ \ \ \ \ fprintf\ (stderr,\ \textcolor{stringliteral}{"{}cannot\ connect\ input\ ports\(\backslash\)n"{}});}
\DoxyCodeLine{00210\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00211\ \ \ \ \ \}}
\DoxyCodeLine{00212\ }
\DoxyCodeLine{00213\ \ \ \ \ free\ (ports);}
\DoxyCodeLine{00214\ }
\DoxyCodeLine{00215\ \textcolor{preprocessor}{\#ifdef\ MIC\_SPEAKER\_PASSTHROUGH\_DEBUG}}
\DoxyCodeLine{00216\ \ \ \ \ \textcolor{keywordtype}{int}\ i;}
\DoxyCodeLine{00217\ \ \ \ \ \textcolor{keywordflow}{if}\ ((ports\ =\ jack\_get\_ports\ (handle-\/>client,\ \textcolor{stringliteral}{"{}system:playback"{}},\ NULL,}
\DoxyCodeLine{00218\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ JackPortIsPhysical|JackPortIsInput))\ ==\ NULL)\ \{}
\DoxyCodeLine{00219\ \ \ \ \ \ \ \ \ fprintf(stderr,\ \textcolor{stringliteral}{"{}Cannot\ find\ any\ physical\ playback\ ports\(\backslash\)n"{}});}
\DoxyCodeLine{00220\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00221\ \ \ \ \ \}}
\DoxyCodeLine{00222\ }
\DoxyCodeLine{00223\ \ \ \ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;\ ports[i]\ !=\ NULL;\ i++)\ \{}
\DoxyCodeLine{00224\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (jack\_connect\ (handle-\/>client,\ jack\_port\_name\ (handle-\/>output\_port),\ ports[i]))\ \{}
\DoxyCodeLine{00225\ \ \ \ \ \ \ \ \ \ \ \ \ fprintf\ (stderr,\ \textcolor{stringliteral}{"{}cannot\ connect\ output\ ports\(\backslash\)n"{}});}
\DoxyCodeLine{00226\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00227\ \ \ \ \ \}}
\DoxyCodeLine{00228\ }
\DoxyCodeLine{00229\ \ \ \ \ free\ (ports);}
\DoxyCodeLine{00230\ \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{00231\ }
\DoxyCodeLine{00232\ \textcolor{preprocessor}{\#ifdef\ HAVE\_SAMPLERATE\_H}}
\DoxyCodeLine{00233\ \ \ \ \ \textcolor{comment}{//\ Initialize\ the\ sample\ rate\ converter.}}
\DoxyCodeLine{00234\ \ \ \ \ \textcolor{keywordflow}{if}\ ((handle-\/>resample\_state\ =\ src\_new\ (SRC\_SINC\_BEST\_QUALITY,\ 1,\ \&resample\_error))\ ==\ NULL)\ \{}
\DoxyCodeLine{00235\ \ \ \ \ \ \ \ \ fprintf\ (stderr,\ \textcolor{stringliteral}{"{}Error\ :\ src\_new()\ failed:\ \%s\(\backslash\)n"{}},\ src\_strerror(resample\_error));}
\DoxyCodeLine{00236\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00237\ \ \ \ \ \}}
\DoxyCodeLine{00238\ \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{00239\ }
\DoxyCodeLine{00240\ \ \ \ \ handle-\/>recording\ =\ 0;}
\DoxyCodeLine{00241\ \ \ \ \ handle-\/>sps\ =\ sps;}
\DoxyCodeLine{00242\ \ \ \ \ handle-\/>bps\ =\ \textcolor{keyword}{sizeof}(int16);}
\DoxyCodeLine{00243\ }
\DoxyCodeLine{00244\ \ \ \ \ \textcolor{comment}{//\ Give\ the\ jack\ process\ callback\ time\ to\ run\ ?}}
\DoxyCodeLine{00245\ \ \ \ \ sleep\ (1);}
\DoxyCodeLine{00246\ }
\DoxyCodeLine{00247\ \ \ \ \ \textcolor{keywordflow}{return}\ (ad\_rec\_t\ *)\ handle;}
\DoxyCodeLine{00248\ \}}
\DoxyCodeLine{00249\ }
\DoxyCodeLine{00250\ ad\_rec\_t\ *}
\DoxyCodeLine{00251\ ad\_open\_sps(int32\ sps)}
\DoxyCodeLine{00252\ \{}
\DoxyCodeLine{00253\ \ \ \ \ \textcolor{comment}{//\ Ignored\ the\ sps\ has\ to\ set\ for\ the\ jackd\ server}}
\DoxyCodeLine{00254\ \ \ \ \ \textcolor{keywordflow}{return}\ ad\_open\_dev(DEFAULT\_DEVICE,\ sps);}
\DoxyCodeLine{00255\ \}}
\DoxyCodeLine{00256\ }
\DoxyCodeLine{00257\ ad\_rec\_t\ *}
\DoxyCodeLine{00258\ ad\_open(\textcolor{keywordtype}{void})}
\DoxyCodeLine{00259\ \{}
\DoxyCodeLine{00260\ \ \ \ \ \textcolor{keywordflow}{return}\ ad\_open\_sps(DEFAULT\_SAMPLES\_PER\_SEC);}
\DoxyCodeLine{00261\ \}}
\DoxyCodeLine{00262\ }
\DoxyCodeLine{00263\ int32}
\DoxyCodeLine{00264\ ad\_close(ad\_rec\_t\ *\ handle)}
\DoxyCodeLine{00265\ \{}
\DoxyCodeLine{00266\ \ \ \ \ free\ (handle-\/>sample\_buffer);}
\DoxyCodeLine{00267\ \textcolor{preprocessor}{\#ifdef\ HAVE\_SAMPLERATE\_H}}
\DoxyCodeLine{00268\ \ \ \ \ free\ (handle-\/>resample\_buffer);}
\DoxyCodeLine{00269\ \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{00270\ \ \ \ \ jack\_ringbuffer\_free\ (handle-\/>rbuffer);\ \ \ \ \ }
\DoxyCodeLine{00271\ \ \ \ \ jack\_client\_close\ (handle-\/>client);}
\DoxyCodeLine{00272\ \ \ \ \ free(handle);}
\DoxyCodeLine{00273\ }
\DoxyCodeLine{00274\ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00275\ \}}
\DoxyCodeLine{00276\ }
\DoxyCodeLine{00277\ int32}
\DoxyCodeLine{00278\ ad\_start\_rec(ad\_rec\_t\ *\ handle)}
\DoxyCodeLine{00279\ \{}
\DoxyCodeLine{00280\ \ \ \ \ \textcolor{keywordflow}{if}\ (handle-\/>recording)}
\DoxyCodeLine{00281\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ AD\_ERR\_GEN;}
\DoxyCodeLine{00282\ }
\DoxyCodeLine{00283\ \ \ \ \ handle-\/>recording\ =\ 1;}
\DoxyCodeLine{00284\ }
\DoxyCodeLine{00285\ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00286\ \}}
\DoxyCodeLine{00287\ }
\DoxyCodeLine{00288\ int32}
\DoxyCodeLine{00289\ ad\_stop\_rec(ad\_rec\_t\ *\ handle)}
\DoxyCodeLine{00290\ \{}
\DoxyCodeLine{00291\ \ \ \ \ handle-\/>recording\ =\ 0;}
\DoxyCodeLine{00292\ }
\DoxyCodeLine{00293\ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00294\ \}}
\DoxyCodeLine{00295\ }
\DoxyCodeLine{00296\ int32}
\DoxyCodeLine{00297\ ad\_read(ad\_rec\_t\ *\ handle,\ int16\ *\ buf,\ int32\ max)}
\DoxyCodeLine{00298\ \{}
\DoxyCodeLine{00299\ \textcolor{preprocessor}{\#ifdef\ HAVE\_SAMPLERATE\_H}}
\DoxyCodeLine{00300\ \ \ \ \ \textcolor{keywordtype}{int}\ resample\_error;}
\DoxyCodeLine{00301\ \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{00302\ }
\DoxyCodeLine{00303\ \ \ \ \textcolor{keywordflow}{if}\ (!handle-\/>recording)}
\DoxyCodeLine{00304\ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ AD\_EOF;}
\DoxyCodeLine{00305\ }
\DoxyCodeLine{00306\ \ \ \ \textcolor{keywordtype}{size\_t}\ length\ =\ sample\_size\ *\ max;}
\DoxyCodeLine{00307\ }
\DoxyCodeLine{00308\ \textcolor{preprocessor}{\#ifdef\ HAVE\_SAMPLERATE\_H}}
\DoxyCodeLine{00309\ }
\DoxyCodeLine{00310\ \ \ \ \textcolor{comment}{//\ Resample\ the\ data\ from\ the\ sample\ rate\ set\ in\ the\ jack\ server\ to\ that\ required\ }}
\DoxyCodeLine{00311\ \ \ \ \textcolor{comment}{//\ by\ sphinx}}
\DoxyCodeLine{00312\ }
\DoxyCodeLine{00313\ \ \ \ length\ =\ jack\_ringbuffer\_peek\ (handle-\/>rbuffer,\ (\textcolor{keywordtype}{char}*)\ handle-\/>sample\_buffer,\ length);}
\DoxyCodeLine{00314\ \ \ \ \textcolor{keywordtype}{size\_t}\ length\_in\_samples\ =\ length\ /\ sample\_size;}
\DoxyCodeLine{00315\ }
\DoxyCodeLine{00316\ \ \ \ \textcolor{keywordflow}{if}(handle-\/>resample\_state\ ==\ NULL)}
\DoxyCodeLine{00317\ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ AD\_EOF;}
\DoxyCodeLine{00318\ }
\DoxyCodeLine{00319\ \ \ \ \textcolor{comment}{//\ We\ will\ try\ to\ downsample\ if\ jackd\ is\ running\ at\ a\ higher\ sample\ rate}}
\DoxyCodeLine{00320\ \ \ \ jack\_nframes\_t\ jack\_samplerate\ =\ jack\_get\_sample\_rate(handle-\/>client);}
\DoxyCodeLine{00321\ }
\DoxyCodeLine{00322\ \ \ \ SRC\_DATA\ data;}
\DoxyCodeLine{00323\ }
\DoxyCodeLine{00324\ \ \ \ data.data\_in\ =\ handle-\/>sample\_buffer;}
\DoxyCodeLine{00325\ \ \ \ data.input\_frames\ =\ length\_in\_samples;}
\DoxyCodeLine{00326\ \ \ \ data.data\_out\ =\ handle-\/>resample\_buffer;}
\DoxyCodeLine{00327\ \ \ \ data.output\_frames\ =\ BUFFER\_SIZE\ /\ sample\_size;}
\DoxyCodeLine{00328\ \ \ \ data.src\_ratio\ =\ (float)\ handle-\/>sps\ /\ jack\_samplerate;}
\DoxyCodeLine{00329\ \ \ \ data.end\_of\_input\ =\ 0;}
\DoxyCodeLine{00330\ }
\DoxyCodeLine{00331\ \ \ \ \textcolor{keywordflow}{if}\ ((resample\_error\ =\ src\_process(handle-\/>resample\_state,\ \&data))\ !=\ 0)\ \{}
\DoxyCodeLine{00332\ \ \ \ \ \ \ \ fprintf\ (stderr,\ \textcolor{stringliteral}{"{}resample\ error\ \%s\(\backslash\)n"{}},\ src\_strerror\ (resample\_error));}
\DoxyCodeLine{00333\ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 1;}
\DoxyCodeLine{00334\ \ \ \ \}}
\DoxyCodeLine{00335\ }
\DoxyCodeLine{00336\ \ \ \ \textcolor{keywordflow}{for}(\textcolor{keywordtype}{int}\ i=0;\ i<data.output\_frames\_gen;\ i++)\ \{}
\DoxyCodeLine{00337\ \ \ \ \ \ \ \ buf[i]\ =\ (int16)\ (int16\_range\_over\_two\ *\ (handle-\/>resample\_buffer[i]\ +\ 1.0)\ +\ SHRT\_MIN);}
\DoxyCodeLine{00338\ \ \ \ \}}
\DoxyCodeLine{00339\ }
\DoxyCodeLine{00340\ \ \ \ jack\_ringbuffer\_read\_advance(handle-\/>rbuffer,\ data.input\_frames\_used\ *\ sample\_size);\ }
\DoxyCodeLine{00341\ }
\DoxyCodeLine{00342\ \ \ \ \textcolor{keywordflow}{if}(length\ ==\ 0\ \&\&\ (!handle-\/>recording))\ \{}
\DoxyCodeLine{00343\ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ AD\_EOF;}
\DoxyCodeLine{00344\ \ \ \ \}}
\DoxyCodeLine{00345\ }
\DoxyCodeLine{00346\ \ \ \ \textcolor{keywordflow}{return}\ data.output\_frames\_gen;}
\DoxyCodeLine{00347\ }
\DoxyCodeLine{00348\ \textcolor{preprocessor}{\#else}}
\DoxyCodeLine{00349\ }
\DoxyCodeLine{00350\ \ \ \ length\ =\ jack\_ringbuffer\_read\ (handle-\/>rbuffer,\ (\textcolor{keywordtype}{char}*)\ handle-\/>sample\_buffer,\ length);}
\DoxyCodeLine{00351\ \ \ \ \textcolor{keywordtype}{size\_t}\ length\_in\_samples\ =\ length\ /\ sample\_size;}
\DoxyCodeLine{00352\ }
\DoxyCodeLine{00353\ \ \ \ \textcolor{keywordflow}{for}(\textcolor{keywordtype}{int}\ i=0;\ i<length\_in\_samples;\ i++)\ \{}
\DoxyCodeLine{00354\ \ \ \ \ \ \ \ buf[i]\ =\ (int16)\ (int16\_range\_over\_two\ *\ (handle-\/>sample\_buffer[i]\ +\ 1.0)\ +\ SHRT\_MIN);}
\DoxyCodeLine{00355\ \ \ \ \}}
\DoxyCodeLine{00356\ }
\DoxyCodeLine{00357\ \ \ \ \textcolor{keywordflow}{if}(length\ ==\ 0\ \&\&\ (!handle-\/>recording))\ \{}
\DoxyCodeLine{00358\ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ AD\_EOF;}
\DoxyCodeLine{00359\ \ \ \ \}}
\DoxyCodeLine{00360\ }
\DoxyCodeLine{00361\ \ \ \ \textcolor{keywordflow}{return}\ length\_in\_samples;}
\DoxyCodeLine{00362\ }
\DoxyCodeLine{00363\ \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{00364\ \}}

\end{DoxyCode}
