1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
/*****************************************************************************
* sapi.cpp: Simple text to Speech renderer for Windows, based on SAPI
*****************************************************************************
* Copyright (c) 2015 Moti Zilberman
*
* Authors: Moti Zilberman
* Jean-Baptiste Kempf
*
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
/* VLC core API headers */
#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_filter.h>
#include <vlc_charset.h>
#include <vlc_subpicture.h>
#define INITGUID
#include <windows.h>
#include <sapi.h>
#include <sphelper.h>
static int Create (vlc_object_t *);
static void Destroy(vlc_object_t *);
static int RenderText(filter_t *,
subpicture_region_t *,
subpicture_region_t *,
const vlc_fourcc_t *);
vlc_module_begin ()
set_description(N_("Speech synthesis for Windows"))
set_category(CAT_VIDEO)
set_subcategory(SUBCAT_VIDEO_SUBPIC)
set_capability("text renderer", 0)
set_callbacks(Create, Destroy)
add_integer("sapi-voice", -1, "Voice Index", "Voice index", false)
vlc_module_end ()
struct filter_sys_t
{
ISpVoice* cpVoice;
char* lastString;
};
/* MTA functions */
static int TryEnterMTA(vlc_object_t *obj)
{
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (unlikely(FAILED(hr)))
{
msg_Err (obj, "cannot initialize COM (error 0x%lx)", hr);
return -1;
}
return 0;
}
#define TryEnterMTA(o) TryEnterMTA(VLC_OBJECT(o))
static void EnterMTA(void)
{
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (unlikely(FAILED(hr)))
abort();
}
static void LeaveMTA(void)
{
CoUninitialize();
}
static int Create (vlc_object_t *p_this)
{
filter_t *p_filter = (filter_t *)p_this;
filter_sys_t *p_sys;
HRESULT hr;
if (TryEnterMTA(p_this))
return VLC_EGENERIC;
p_filter->p_sys = p_sys = (filter_sys_t*) malloc(sizeof(filter_sys_t));
if (!p_sys)
goto error;
p_sys->cpVoice = NULL;
p_sys->lastString = NULL;
hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_INPROC_SERVER, IID_ISpVoice, (void**) &p_sys->cpVoice);
if (SUCCEEDED(hr)) {
ISpObjectToken* cpVoiceToken = NULL;
IEnumSpObjectTokens* cpEnum = NULL;
ULONG ulCount = 0;
hr = SpEnumTokens(SPCAT_VOICES, NULL, NULL, &cpEnum);
if (SUCCEEDED(hr))
{
// Get the number of voices.
hr = cpEnum->GetCount(&ulCount);
if (SUCCEEDED (hr))
{
int voiceIndex = var_InheritInteger(p_this, "sapi-voice");
if (voiceIndex > -1)
{
if ((unsigned)voiceIndex < ulCount) {
hr = cpEnum->Item(voiceIndex, &cpVoiceToken);
if (SUCCEEDED(hr)) {
hr = p_sys->cpVoice->SetVoice(cpVoiceToken);
if (SUCCEEDED(hr)) {
msg_Dbg(p_this, "Selected voice %d", voiceIndex);
}
else {
msg_Err(p_this, "Failed to set voice %d", voiceIndex);
}
cpVoiceToken->Release();
cpVoiceToken = NULL;
}
}
else
msg_Err(p_this, "Voice index exceeds available count");
}
}
cpEnum->Release();
/* Set Output */
hr = p_sys->cpVoice->SetOutput(NULL, TRUE);
}
}
else
{
msg_Err(p_filter, "Could not create SpVoice");
goto error;
}
LeaveMTA();
p_filter->pf_render = RenderText;
return VLC_SUCCESS;
error:
LeaveMTA();
free(p_sys);
return VLC_EGENERIC;
}
static void Destroy(vlc_object_t *p_this)
{
filter_t *p_filter = (filter_t *)p_this;
filter_sys_t *p_sys = p_filter->p_sys;
if (p_sys->cpVoice)
p_sys->cpVoice->Release();
free(p_sys->lastString);
free(p_sys);
}
static int RenderText(filter_t *p_filter,
subpicture_region_t *,
subpicture_region_t *p_region_in,
const vlc_fourcc_t *)
{
filter_sys_t *p_sys = p_filter->p_sys;
text_segment_t *p_segment = p_region_in->p_text;
if (!p_segment)
return VLC_EGENERIC;
for (const text_segment_t *s = p_segment; s != NULL; s = s->p_next ) {
if (!s->psz_text)
continue;
if (strlen(s->psz_text) == 0)
continue;
if (p_sys->lastString && !strcmp(p_sys->lastString, s->psz_text))
continue;
if (!strcmp(s->psz_text, "\n"))
continue;
/* */
free(p_sys->lastString);
p_sys->lastString = strdup(s->psz_text);
/* */
if (p_sys->lastString) {
msg_Dbg(p_filter, "Speaking '%s'", s->psz_text);
EnterMTA();
wchar_t* wideText = ToWide(s->psz_text);
HRESULT hr = p_sys->cpVoice->Speak(wideText, SPF_ASYNC, NULL);
free(wideText);
if (!SUCCEEDED(hr)) {
msg_Err(p_filter, "Speak() error");
}
LeaveMTA();
}
}
return VLC_SUCCESS;
}
|