00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00047 #ifndef FASTFORMAT_INCL_FASTFORMAT_SINK_HPP_SPEECH
00048 #define FASTFORMAT_INCL_FASTFORMAT_SINK_HPP_SPEECH
00049
00050
00051
00052
00053
00054 #ifndef FASTFORMAT_DOCUMENTATION_SKIP_SECTION
00055 # define FASTFORMAT_VER_FASTFORMAT_SINK_HPP_SPEECH_MAJOR 1
00056 # define FASTFORMAT_VER_FASTFORMAT_SINK_HPP_SPEECH_MINOR 2
00057 # define FASTFORMAT_VER_FASTFORMAT_SINK_HPP_SPEECH_REVISION 2
00058 # define FASTFORMAT_VER_FASTFORMAT_SINK_HPP_SPEECH_EDIT 15
00059 #endif
00060
00061
00062
00063
00064
00065 #ifndef __cplusplus
00066 # error This file can only be included in C++ compilation units
00067 #endif
00068
00069 #if !defined(WIN32) && \
00070 !defined(WIN64)
00071 # error The speech_sink component is currently implemented only on Windows, using SAPI; contributions for Mac, Linux and others are welcomed
00072 #endif
00073
00074
00075
00076
00077
00078 #include <fastformat/fastformat.h>
00079 #include <fastformat/util/sinks/helpers.hpp>
00080 #include <fastformat/format/standard_flags.hpp>
00081
00082 #include <comstl/speech/sapi_util.hpp>
00083 #include <comstl/util/initialisers.hpp>
00084
00085 #include <stlsoft/smartptr/ref_ptr.hpp>
00086 #include <stlsoft/memory/auto_buffer.hpp>
00087
00088
00089
00090
00091
00092 #if !defined(FASTFORMAT_NO_NAMESPACE)
00093 namespace fastformat
00094 {
00095 namespace sinks
00096 {
00097 #endif
00098
00099
00100
00101
00102
00109 class speech_sink
00110 {
00111 public:
00112
00113 typedef speech_sink class_type;
00115 typedef stlsoft::ref_ptr<ISpVoice> voice_type;
00116
00117 public:
00118
00119 explicit speech_sink(int flags = 0)
00120 : m_coinit()
00121 , m_voice(init_voice_())
00122 , m_flags(flags & ~disallowed_flags_())
00123 {}
00124
00125 public:
00126 voice_type get() const
00127 {
00128 return m_voice;
00129 }
00130
00131 public:
00132
00133 class_type& write(int , size_t cchTotal, size_t numResults, ff_string_slice_t const* results)
00134 {
00135 stlsoft::auto_buffer<ff_char_t> buff(1 + cchTotal);
00136
00137 #ifndef STLSOFT_CF_THROW_BAD_ALLOC
00138 if( !m_voice.empty() &&
00139 !buff.empty())
00140 #endif
00141 {
00142 #if !defined(FASTFORMAT_NO_NAMESPACE)
00143 using ::fastformat::util::concat_slices;
00144 #endif
00145
00146 concat_slices(buff, numResults, results);
00147 buff[cchTotal] = '\0';
00148
00149 HRESULT hr = comstl::sapi_speak(m_voice, buff.data(), static_cast<DWORD>(m_flags));
00150
00151 #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
00152 if(FAILED(hr))
00153 {
00154
00155 }
00156 #else
00157 STLSOFT_SUPPRESS_UNUSED(hr);
00158 #endif
00159 }
00160
00161 return *this;
00162 }
00163
00164 private:
00165 static int disallowed_flags_()
00166 {
00167 return SPF_IS_FILENAME;
00168 }
00169
00170 static voice_type init_voice_()
00171 {
00172 voice_type voice;
00173 HRESULT hr = comstl::sapi_create(voice);
00174
00175 #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
00176 if(FAILED(hr))
00177 {
00178
00179 }
00180 #else
00181 STLSOFT_SUPPRESS_UNUSED(hr);
00182 #endif
00183
00184 return voice;
00185 }
00186
00187 private:
00188 comstl::com_initializer m_coinit;
00189 voice_type m_voice;
00190 const int m_flags;
00191 };
00192
00193
00194
00195
00196
00200 inline speech_sink& fmt_slices(speech_sink& sink, int flags, size_t cchTotal, size_t numResults, ff_string_slice_t const* results)
00201 {
00202 return sink.write(flags, cchTotal, numResults, results);
00203 }
00204
00205
00206
00207
00208
00209 #if !defined(FASTFORMAT_NO_NAMESPACE)
00210 }
00211 }
00212 #endif
00213
00214
00215
00216 #endif
00217
00218