Synesis Software STLSoft - ... Robust, Lightweight, Cross-platform, Template Software ...

fastformat/sinks/speech.hpp

Go to the documentation of this file.
00001 /* /////////////////////////////////////////////////////////////////////////
00002  * File:        fastformat/sinks/speech.hpp
00003  *
00004  * Purpose:     A FastFormat sink for the Windows Speech API.
00005  *
00006  * Created:     27th November 2007
00007  * Updated:     30th April 2010
00008  *
00009  * Home:        http://www.fastformat.org/
00010  *
00011  * Copyright (c) 2007-2010, Matthew Wilson and Synesis Software
00012  * All rights reserved.
00013  *
00014  * Redistribution and use in source and binary forms, with or without
00015  * modification, are permitted provided that the following conditions are
00016  * met:
00017  *
00018  * - Redistributions of source code must retain the above copyright notice,
00019  *   this list of conditions and the following disclaimer.
00020  * - Redistributions in binary form must reproduce the above copyright
00021  *   notice, this list of conditions and the following disclaimer in the
00022  *   documentation and/or other materials provided with the distribution.
00023  * - Neither the names of Matthew Wilson and Synesis Software nor the names
00024  *   of any contributors may be used to endorse or promote products derived
00025  *   from this software without specific prior written permission.
00026  *
00027  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00028  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
00029  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00030  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
00031  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00032  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00033  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00034  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00035  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00036  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00037  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00038  *
00039  * ////////////////////////////////////////////////////////////////////// */
00040 
00041 
00047 #ifndef FASTFORMAT_INCL_FASTFORMAT_SINK_HPP_SPEECH
00048 #define FASTFORMAT_INCL_FASTFORMAT_SINK_HPP_SPEECH
00049 
00050 /* /////////////////////////////////////////////////////////////////////////
00051  * Version information
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     17
00059 #endif /* !FASTFORMAT_DOCUMENTATION_SKIP_SECTION */
00060 
00061 /* /////////////////////////////////////////////////////////////////////////
00062  * Language
00063  */
00064 
00065 #ifndef __cplusplus
00066 # error This file can only be included in C++ compilation units
00067 #endif /* !__cplusplus */
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 /* OS */
00073 
00074 /* /////////////////////////////////////////////////////////////////////////
00075  * Includes
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  * Namespace
00090  */
00091 
00092 #if !defined(FASTFORMAT_NO_NAMESPACE)
00093 namespace fastformat
00094 {
00095 namespace sinks
00096 {
00097 #endif /* !FASTFORMAT_NO_NAMESPACE */
00098 
00099 /* /////////////////////////////////////////////////////////////////////////
00100  * Classes
00101  */
00102 
00109 class speech_sink
00110 {
00111 public: // Member Types
00113     typedef speech_sink                     class_type;
00115     typedef stlsoft::ref_ptr<ISpVoice>      voice_type;
00116 
00117 public: // Construction
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: // Accessors
00126     voice_type get() const
00127     {
00128         return m_voice;
00129     }
00130 
00131 public: // Shim Operations
00133     class_type& write(int /* flags */, 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 /* !STLSOFT_CF_THROW_BAD_ALLOC */
00141         {
00142 #if !defined(FASTFORMAT_NO_NAMESPACE)
00143             using ::fastformat::util::concat_slices;
00144 #endif /* !FASTFORMAT_NO_NAMESPACE */
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 // TODO:                throw fastformat::write_exception(
00155             }
00156 #else /* STLSOFT_CF_EXCEPTION_SUPPORT */
00157             STLSOFT_SUPPRESS_UNUSED(hr);
00158 #endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
00159         }
00160 
00161         return *this;
00162     }
00163 
00164 private: // Implementation
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 // TODO:            throw 
00179         }
00180 #else /* STLSOFT_CF_EXCEPTION_SUPPORT */
00181         STLSOFT_SUPPRESS_UNUSED(hr);
00182 #endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
00183 
00184         return voice;
00185     }
00186 
00187 private: // Member Variables
00188     comstl::com_initializer m_coinit;
00189     voice_type              m_voice;
00190     const int               m_flags;
00191 };
00192 
00193 /* /////////////////////////////////////////////////////////////////////////
00194  * Action Shims
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  * Namespace
00207  */
00208 
00209 #if !defined(FASTFORMAT_NO_NAMESPACE)
00210 } /* namespace sinks */
00211 } /* namespace fastformat */
00212 #endif /* !FASTFORMAT_NO_NAMESPACE */
00213 
00214 /* ////////////////////////////////////////////////////////////////////// */
00215 
00216 #endif /* FASTFORMAT_INCL_FASTFORMAT_SINK_HPP_SPEECH */
00217 
00218 /* ///////////////////////////// end of file //////////////////////////// */

FastFormat Library documentation © Matthew Wilson, 2006-2009 SourceForge.net Logo