From 3ae27e38f34ef2aa671081d43f20df09e565e14e Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 19 Jan 2023 10:38:19 +0530 Subject: [PATCH] More work on winspeech --- src/calibre/utils/windows/winspeech.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/calibre/utils/windows/winspeech.cpp b/src/calibre/utils/windows/winspeech.cpp index a2d1628d1d..1174fde0fe 100644 --- a/src/calibre/utils/windows/winspeech.cpp +++ b/src/calibre/utils/windows/winspeech.cpp @@ -127,14 +127,15 @@ ensure_current_thread_has_message_queue(void) { PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE); } +#define PREPARE_METHOD_CALL ensure_current_thread_has_message_queue(); if (GetCurrentThreadId() != self->creation_thread_id) { PyErr_SetString(PyExc_RuntimeError, "Cannot use a Synthesizer object from a thread other than the thread it was created in"); return NULL; } + + static PyObject* Synthesizer_speak(Synthesizer *self, PyObject *args) { + PREPARE_METHOD_CALL; wchar_raii pytext; - PyObject *callback; int is_ssml = 0; - if (!PyArg_ParseTuple(args, "O&O|p", py_to_wchar_no_none, &pytext, &callback, &is_ssml)) return NULL; - if (!PyCallable_Check(callback)) { PyErr_SetString(PyExc_TypeError, "callback must be callable"); return NULL; } - ensure_current_thread_has_message_queue(); + if (!PyArg_ParseTuple(args, "O&|p", py_to_wchar_no_none, &pytext, &is_ssml)) return NULL; SpeechSynthesisStream stream{nullptr}; try { if (is_ssml) stream = self->synth.SynthesizeSsmlToStreamAsync(pytext.as_view()).get(); @@ -151,13 +152,13 @@ Synthesizer_speak(Synthesizer *self, PyObject *args) { static PyObject* Synthesizer_create_recording(Synthesizer *self, PyObject *args) { + PREPARE_METHOD_CALL; wchar_raii pytext; PyObject *callback; int is_ssml = 0; if (!PyArg_ParseTuple(args, "O&O|p", py_to_wchar_no_none, &pytext, &callback, &is_ssml)) return NULL; if (!PyCallable_Check(callback)) { PyErr_SetString(PyExc_TypeError, "callback must be callable"); return NULL; } - ensure_current_thread_has_message_queue(); SpeechSynthesisStream stream{nullptr}; try { if (is_ssml) stream = self->synth.SynthesizeSsmlToStreamAsync(pytext.as_view()).get();