\hypertarget{_audio_dictionary_s_d_8h_source}{}\doxysection{Audio\+Dictionary\+SD.\+h}
\label{_audio_dictionary_s_d_8h_source}\index{src/AudioDictionarySD.h@{src/AudioDictionarySD.h}}

\begin{DoxyCode}{0}
\DoxyCodeLine{00001\ }
\DoxyCodeLine{00002\ \textcolor{preprocessor}{\#pragma\ once}}
\DoxyCodeLine{00003\ }
\DoxyCodeLine{00004\ \textcolor{preprocessor}{\#include\ <WiFi.h>}}
\DoxyCodeLine{00005\ }
\DoxyCodeLine{00006\ \textcolor{preprocessor}{\#include\ "{}SimpleTTSBase.h"{}}}
\DoxyCodeLine{00007\ }
\DoxyCodeLine{00008\ \textcolor{comment}{//\ by\ default\ we\ use\ the\ SdFat\ Library}}
\DoxyCodeLine{00009\ \textcolor{preprocessor}{\#ifdef\ USE\_SD}}
\DoxyCodeLine{00010\ \textcolor{preprocessor}{\#include\ <SD.h>}}
\DoxyCodeLine{00011\ \textcolor{keyword}{typedef}\ File\ AudioFile;}
\DoxyCodeLine{00012\ \textcolor{preprocessor}{\#else}}
\DoxyCodeLine{00013\ \textcolor{preprocessor}{\#include\ <SdFat.h>}}
\DoxyCodeLine{00014\ \textcolor{keyword}{typedef}\ File32\ AudioFile;}
\DoxyCodeLine{00015\ \textcolor{keyword}{typedef}\ SdFat32\ AudioFat;}
\DoxyCodeLine{00016\ \textcolor{preprocessor}{\#ifndef\ SDFAT\_SPEED}}
\DoxyCodeLine{00017\ \textcolor{preprocessor}{\#define\ SDFAT\_SPEED\ 2}}
\DoxyCodeLine{00018\ \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{00019\ \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{00020\ }
\DoxyCodeLine{00021\ \textcolor{keyword}{namespace\ }simple\_tts\ \{}
\DoxyCodeLine{00022\ }
\DoxyCodeLine{00028\ \textcolor{keyword}{template}\ <\textcolor{keyword}{class}\ T>}
\DoxyCodeLine{00029\ \textcolor{keyword}{class\ }\mbox{\hyperlink{classsimple__tts_1_1_audio_stream_file_wrapper}{AudioStreamFileWrapper}}\ :\ \textcolor{keyword}{public}\ AudioStream\ \{}
\DoxyCodeLine{00030\ \ \textcolor{keyword}{public}:}
\DoxyCodeLine{00031\ \ \ \mbox{\hyperlink{classsimple__tts_1_1_audio_stream_file_wrapper}{AudioStreamFileWrapper}}()\ =\ \textcolor{keywordflow}{default};}
\DoxyCodeLine{00032\ \ \ \textcolor{keyword}{virtual}\ \textcolor{keywordtype}{bool}\ begin(T\ \&file)\ \{}
\DoxyCodeLine{00033\ \ \ \ \ p\_file\ =\ \&file;}
\DoxyCodeLine{00034\ \ \ \ \ \textcolor{keywordflow}{return}\ \textcolor{keyword}{true};}
\DoxyCodeLine{00035\ \ \ \}}
\DoxyCodeLine{00036\ \ \ \textcolor{keyword}{virtual}\ \textcolor{keywordtype}{bool}\ begin()\textcolor{keyword}{\ override\ }\{}
\DoxyCodeLine{00037\ \ \ \ \ LOGI(LOG\_METHOD);}
\DoxyCodeLine{00038\ \ \ \ \ p\_file-\/>seek(0);}
\DoxyCodeLine{00039\ \ \ \ \ \textcolor{keywordflow}{return}\ \textcolor{keyword}{true};}
\DoxyCodeLine{00040\ \ \ \}}
\DoxyCodeLine{00041\ \ \ \textcolor{keyword}{virtual}\ \textcolor{keywordtype}{void}\ end()\textcolor{keyword}{\ override\ }\{}
\DoxyCodeLine{00042\ \ \ \ \ LOGI(LOG\_METHOD);}
\DoxyCodeLine{00043\ \ \ \ \ p\_file-\/>close();}
\DoxyCodeLine{00044\ \ \ \}}
\DoxyCodeLine{00045\ }
\DoxyCodeLine{00046\ \ \ \textcolor{keyword}{virtual}\ \textcolor{keywordtype}{size\_t}\ readBytes(uint8\_t\ *buffer,\ \textcolor{keywordtype}{size\_t}\ length)\textcolor{keyword}{\ override\ }\{}
\DoxyCodeLine{00047\ \ \ \ \ \textcolor{keywordflow}{return}\ p\_file-\/>readBytes((\textcolor{keywordtype}{char}\ *)buffer,\ length);}
\DoxyCodeLine{00048\ \ \ \}}
\DoxyCodeLine{00049\ }
\DoxyCodeLine{00050\ \ \ \textcolor{keyword}{virtual}\ \textcolor{keywordtype}{size\_t}\ write(\textcolor{keyword}{const}\ uint8\_t\ *buffer,\ \textcolor{keywordtype}{size\_t}\ length)\textcolor{keyword}{\ override\ }\{}
\DoxyCodeLine{00051\ \ \ \ \ \textcolor{keywordflow}{return}\ p\_file-\/>write(buffer,\ length);}
\DoxyCodeLine{00052\ \ \ \}}
\DoxyCodeLine{00053\ }
\DoxyCodeLine{00054\ \ \ \textcolor{keyword}{virtual}\ \textcolor{keywordtype}{int}\ available()\textcolor{keyword}{\ override\ }\{\ \textcolor{keywordflow}{return}\ p\_file-\/>available();\ \}}
\DoxyCodeLine{00055\ }
\DoxyCodeLine{00056\ \ \ \textcolor{keyword}{virtual}\ \textcolor{keywordtype}{int}\ availableForWrite()\textcolor{keyword}{\ override\ }\{}
\DoxyCodeLine{00057\ \ \ \ \ \textcolor{keywordflow}{return}\ p\_file-\/>availableForWrite();}
\DoxyCodeLine{00058\ \ \ \}}
\DoxyCodeLine{00059\ }
\DoxyCodeLine{00060\ \ \ \textcolor{keyword}{operator}\ bool()\ \{\ \textcolor{keywordflow}{return}\ *p\_file;\ \}}
\DoxyCodeLine{00061\ }
\DoxyCodeLine{00062\ \ \textcolor{keyword}{protected}:}
\DoxyCodeLine{00063\ \ \ T\ *p\_file\ =\ \textcolor{keyword}{nullptr};}
\DoxyCodeLine{00064\ \};}
\DoxyCodeLine{00065\ }
\DoxyCodeLine{00073\ \textcolor{keyword}{class\ }\mbox{\hyperlink{classsimple__tts_1_1_audio_dictionary_s_d}{AudioDictionarySD}}\ :\ \textcolor{keyword}{public}\ \mbox{\hyperlink{classsimple__tts_1_1_audio_dictionary_base}{AudioDictionaryBase}}\ \{}
\DoxyCodeLine{00074\ \ \textcolor{keyword}{public}:}
\DoxyCodeLine{00075\ \ \ \mbox{\hyperlink{classsimple__tts_1_1_audio_dictionary_s_d}{AudioDictionarySD}}(\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *path,\ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *ext,\ \textcolor{keywordtype}{int}\ cs\_pin\ =\ PIN\_CS)\ \{}
\DoxyCodeLine{00076\ \ \ \ \ this-\/>path\ =\ path;}
\DoxyCodeLine{00077\ \ \ \ \ this-\/>ext\ =\ ext;}
\DoxyCodeLine{00078\ \ \ \ \ this-\/>cs\_pin\ =\ cs\_pin;}
\DoxyCodeLine{00079\ \ \ \}}
\DoxyCodeLine{00080\ }
\DoxyCodeLine{00081\ \ \ \mbox{\hyperlink{classsimple__tts_1_1_audio_dictionary_s_d}{\string~AudioDictionarySD}}()\ \{\ file.close();\ \}}
\DoxyCodeLine{00082\ }
\DoxyCodeLine{00084\ \ \ AudioStream\ *\mbox{\hyperlink{classsimple__tts_1_1_audio_dictionary_s_d_a6408d3dded7a71531a53fb4e0b8c48c1}{get}}(\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *word)\ \{}
\DoxyCodeLine{00085\ \ \ \ \ setup();}
\DoxyCodeLine{00086\ \ \ \ \ \textcolor{comment}{//\ provide\ new\ file}}
\DoxyCodeLine{00087\ \ \ \ \ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *file\_name\ =\ getFileWithPath(word);}
\DoxyCodeLine{00088\ \ \ \ \ \textcolor{keywordflow}{if}\ (SD.exists(file\_name))\ \{}
\DoxyCodeLine{00089\ \ \ \ \ \ \ file\ =\ SD.open(file\_name,\ FILE\_READ);}
\DoxyCodeLine{00090\ \ \ \ \ \ \ fileWrapper.begin(file);}
\DoxyCodeLine{00091\ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ \&fileWrapper;}
\DoxyCodeLine{00092\ \ \ \ \ \}}
\DoxyCodeLine{00093\ \ \ \ \ LOGE(\textcolor{stringliteral}{"{}File\ does\ not\ exist:\ \%s"{}},\ file\_name);}
\DoxyCodeLine{00094\ }
\DoxyCodeLine{00095\ \ \ \ \ \textcolor{keywordflow}{return}\ \textcolor{keyword}{nullptr};}
\DoxyCodeLine{00096\ \ \ \}}
\DoxyCodeLine{00097\ }
\DoxyCodeLine{00098\ \ \textcolor{keyword}{protected}:}
\DoxyCodeLine{00099\ \ \ audio\_tools::StrExt\ url\_with\_text;}
\DoxyCodeLine{00100\ \ \ AudioFile\ file;}
\DoxyCodeLine{00101\ \ \ \mbox{\hyperlink{classsimple__tts_1_1_audio_stream_file_wrapper}{AudioStreamFileWrapper<AudioFile>}}\ fileWrapper;}
\DoxyCodeLine{00102\ \textcolor{preprocessor}{\#ifndef\ USE\_SD}}
\DoxyCodeLine{00103\ \ \ AudioFat\ SD;}
\DoxyCodeLine{00104\ \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{00105\ \ \ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *path;}
\DoxyCodeLine{00106\ \ \ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *ext;}
\DoxyCodeLine{00107\ \ \ StrExt\ file\_path\{40\};\ \ \textcolor{comment}{//\ allocate\ 40\ bytes\ as\ a\ typical\ good\ initial\ size}}
\DoxyCodeLine{00108\ \ \ \textcolor{keywordtype}{bool}\ is\_setup\ =\ \textcolor{keyword}{false};}
\DoxyCodeLine{00109\ \ \ \textcolor{keywordtype}{int}\ cs\_pin\ =\ -\/1;}
\DoxyCodeLine{00110\ }
\DoxyCodeLine{00111\ \ \ \textcolor{keywordtype}{void}\ setup()\ \{}
\DoxyCodeLine{00112\ \ \ \ \ \textcolor{keywordflow}{if}\ (!is\_setup)\ \{}
\DoxyCodeLine{00113\ \textcolor{preprocessor}{\#ifdef\ USE\_SD}}
\DoxyCodeLine{00114\ \ \ \ \ \ \ LOGI(\textcolor{stringliteral}{"{}Setup\ SD\ library"{}});}
\DoxyCodeLine{00115\ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (!SD.begin(cs\_pin))\ \{}
\DoxyCodeLine{00116\ \ \ \ \ \ \ \ \ LOGE(\textcolor{stringliteral}{"{}SD.begin\ failed\ for\ cs\_pin:\ \%d"{}},\ cs\_pin);}
\DoxyCodeLine{00117\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return};}
\DoxyCodeLine{00118\ \ \ \ \ \ \ \}}
\DoxyCodeLine{00119\ \textcolor{preprocessor}{\#else}}
\DoxyCodeLine{00120\ \ \ \ \ \ \ LOGI(\textcolor{stringliteral}{"{}Setup\ SdFat\ library"{}});}
\DoxyCodeLine{00121\ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (!SD.begin(}
\DoxyCodeLine{00122\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ SdSpiConfig(cs\_pin,\ DEDICATED\_SPI,\ SD\_SCK\_MHZ(SDFAT\_SPEED))))\ \{}
\DoxyCodeLine{00123\ \ \ \ \ \ \ \ \ LOGE(\textcolor{stringliteral}{"{}SD.begin\ failed\ for\ cs\_pin:\ \%d"{}},\ cs\_pin);}
\DoxyCodeLine{00124\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return};}
\DoxyCodeLine{00125\ \ \ \ \ \ \ \}}
\DoxyCodeLine{00126\ \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{00127\ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (!SD.exists(path))\ \{}
\DoxyCodeLine{00128\ \ \ \ \ \ \ \ \ LOGI(\textcolor{stringliteral}{"{}Creating\ directory:\ \%s"{}},\ path)}
\DoxyCodeLine{00129\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (!SD.mkdir(path))\ \{}
\DoxyCodeLine{00130\ \ \ \ \ \ \ \ \ \ \ LOGE(\textcolor{stringliteral}{"{}Could\ not\ create\ directory:\ \%s"{}},\ path);}
\DoxyCodeLine{00131\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00132\ \ \ \ \ \ \ \}}
\DoxyCodeLine{00133\ \ \ \ \ \ \ is\_setup\ =\ \textcolor{keyword}{true};}
\DoxyCodeLine{00134\ \ \ \ \ \}}
\DoxyCodeLine{00135\ \ \ \}}
\DoxyCodeLine{00136\ }
\DoxyCodeLine{00137\ \ \ \textcolor{comment}{//\ determines\ the\ filename\ for\ the\ word}}
\DoxyCodeLine{00138\ \ \ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *getFileWithPath(\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *name)\ \{}
\DoxyCodeLine{00139\ \ \ \ \ file\_path\ =\ path;}
\DoxyCodeLine{00140\ \ \ \ \ file\_path.add(\textcolor{stringliteral}{"{}/"{}});}
\DoxyCodeLine{00141\ \ \ \ \ file\_path.add(name);}
\DoxyCodeLine{00142\ \ \ \ \ file\_path.add(\textcolor{stringliteral}{"{}."{}});}
\DoxyCodeLine{00143\ \ \ \ \ file\_path.add(ext);}
\DoxyCodeLine{00144\ \ \ \ \ file\_path.toLowerCase();}
\DoxyCodeLine{00145\ \ \ \ \ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *str\ =\ file\_path.c\_str();}
\DoxyCodeLine{00146\ \ \ \ \ LOGI(\textcolor{stringliteral}{"{}\%s\ -\/>\ \%s"{}},\ name,\ str);}
\DoxyCodeLine{00147\ \ \ \ \ \textcolor{keywordflow}{return}\ str;}
\DoxyCodeLine{00148\ \ \ \}}
\DoxyCodeLine{00149\ \};}
\DoxyCodeLine{00150\ }
\DoxyCodeLine{00151\ \}\ \ \textcolor{comment}{//\ namespace\ simple\_tts}}

\end{DoxyCode}
