From ad9cf1b0f60a0b8b03e2ef8f7c1b12fbb6c2758b Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 29 Jan 2023 15:23:23 +0530 Subject: [PATCH] Code to set the voice and audio device --- src/calibre/utils/windows/winspeech.cpp | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/calibre/utils/windows/winspeech.cpp b/src/calibre/utils/windows/winspeech.cpp index 063349fd77..df5a688e7c 100644 --- a/src/calibre/utils/windows/winspeech.cpp +++ b/src/calibre/utils/windows/winspeech.cpp @@ -554,14 +554,12 @@ handle_speak(id_type cmd_id, std::vector &parts) { *((wchar_t*)text.data() + text.size()) = 0; // ensure NULL termination output(cmd_id, "synthesizing", {{"ssml", is_ssml}, {"num_marks", marks->entries.size()}, {"text_length", text.size()}}); - bool ok = false; SpeechSynthesisStream stream{nullptr}; if (!run_catching_exceptions([&]() { speech_synthesizer.Options().IncludeSentenceBoundaryMetadata(true); speech_synthesizer.Options().IncludeWordBoundaryMetadata(true); if (is_ssml) stream = speech_synthesizer.SynthesizeSsmlToStreamAsync(text).get(); else stream = speech_synthesizer.SynthesizeTextToStreamAsync(text).get(); - ok = true; }, "Failed to synthesize speech", __LINE__, cmd_id)) return; speak_revoker = {}; // delete any revokers previously installed @@ -710,6 +708,29 @@ static const std::unordered_map handlers = { handle_speak(cmd_id, parts); }}, + {"audio_device", [](id_type cmd_id, std::vector parts, int64_t*) { + if (parts.size()) { + auto di = DeviceInformation::CreateFromIdAsync(parts[0]).get(); + media_player.AudioDevice(di); + } + output(cmd_id, "audio_device", {{"value", media_player.AudioDevice()}}); + }}, + + {"voice", [](id_type cmd_id, std::vector parts, int64_t*) { + bool found = false; + if (parts.size()) { + auto voice_id = winrt::hstring(parts[0]); + for (auto const &candidate : SpeechSynthesizer::AllVoices()) { + if (candidate.Id() == voice_id) { + speech_synthesizer.Voice(candidate); + found = true; + break; + } + } + } + output(cmd_id, "voice", {{"value", speech_synthesizer.Voice()}, {"found", found}}); + }}, + {"volume", [](id_type cmd_id, std::vector parts, int64_t*) { if (parts.size()) { auto vol = parse_double(parts[0].data());