Skip to content

Commit 57af291

Browse files
committed
SAPI: Do not interrupt speech message when uninitializing.
1 parent 9f3e695 commit 57af291

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

SRC/SAPI.cpp

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
#include<thread>
66

77

8+
WasapiPlayer* g_player = nullptr; // Make it global to avoid multiple voices when reinitializing
9+
10+
811
static char* trim(char* data, unsigned long* size, WAVEFORMATEX* wfx, int threshold) {
912
int channels = wfx->nChannels;
1013
int bytesPerSample = wfx->wBitsPerSample / 8;
@@ -56,24 +59,30 @@ struct PCMData {
5659

5760
std::vector<PCMData> g_dataQueue;
5861
bool g_threadStarted = false;
59-
static void sapi_thread(WasapiPlayer* player) {
60-
if (player == nullptr) {
62+
static void sapi_thread() {
63+
if (g_player == nullptr) {
6164
g_threadStarted = false;
6265
}
6366
HRESULT hr;
6467
while (g_threadStarted) {
6568
Sleep(1);
6669
for (uint64_t i = 0; i < g_dataQueue.size(); ++i) {
6770

68-
hr = player->feed(g_dataQueue[i].data, g_dataQueue[i].size, nullptr);
71+
hr = g_player->feed(g_dataQueue[i].data, g_dataQueue[i].size, nullptr);
6972
if (FAILED(hr))continue;
70-
hr = player->sync();
73+
hr = g_player->sync();
7174
if (FAILED(hr))continue;
7275
}
7376
if (g_dataQueue.size() > 0) g_dataQueue.clear();
7477
}
78+
delete g_player;
79+
g_player = nullptr;
7580
}
7681
bool SAPI::Initialize() {
82+
if (g_player) {
83+
g_threadStarted = false;
84+
g_player->stop();
85+
}
7786
instance = new blastspeak;
7887

7988
if (blastspeak_initialize(instance) == 0) {
@@ -92,34 +101,31 @@ bool SAPI::Initialize() {
92101
wfx.nBlockAlign = (wfx.nChannels * wfx.wBitsPerSample) / 8;
93102
wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
94103
wfx.cbSize = 0;
95-
player = new WasapiPlayer(device, wfx, callback);
96-
HRESULT hr = player->open();
104+
g_player = new WasapiPlayer(device, wfx, callback);
105+
HRESULT hr = g_player->open();
97106
if (FAILED(hr)) {
98-
delete player;
99-
player = nullptr;
107+
delete g_player;
108+
g_player = nullptr;
100109
return false;
101110
}
102111
g_threadStarted = true;
103-
std::thread t(sapi_thread, player);
112+
std::thread t(sapi_thread);
104113
t.detach();
105114
return true;
106115
}
107116
bool SAPI::Uninitialize() {
108-
if (instance == nullptr || player == nullptr)return false;
109-
g_threadStarted = false;
117+
if (instance == nullptr || g_player == nullptr)return false;
118+
g_threadStarted = false; // SAPI thread will be stopped when all messages was spoken
110119
blastspeak_destroy(instance);
111120
delete instance;
112121
instance = nullptr;
113-
StopSpeech();
114-
delete player;
115-
player = nullptr;
116122
return true;
117123
}
118124
bool SAPI::GetActive() {
119-
return instance != nullptr;
125+
return instance != nullptr && g_player != nullptr;
120126
}
121127
bool SAPI::Speak(const char* text, bool interrupt) {
122-
if (instance == nullptr || player == nullptr)
128+
if (instance == nullptr || g_player == nullptr)
123129
return false;
124130
if (interrupt) {
125131
StopSpeech();
@@ -145,7 +151,7 @@ bool SAPI::Speak(const char* text, bool interrupt) {
145151
if (this->paused) {
146152
this->paused = false;
147153
if (!interrupt)
148-
player->resume();
154+
g_player->resume();
149155
}
150156
g_dataQueue.push_back(dat);
151157
return true;
@@ -176,19 +182,19 @@ bool SAPI::SetParameter(int param, int value) {
176182

177183

178184
bool SAPI::StopSpeech() {
179-
if (player == nullptr)return false;
185+
if (g_player == nullptr)return false;
180186
g_dataQueue.clear();
181-
player->stop();
187+
g_player->stop();
182188
this->paused = false;
183189
return true;
184190
}
185191
bool SAPI::PauseSpeech() {
186192
paused = true;
187-
return SUCCEEDED(player->pause());
193+
return SUCCEEDED(g_player->pause());
188194
}
189195
bool SAPI::ResumeSpeech() {
190196
paused = false;
191-
return SUCCEEDED(player->resume());
197+
return SUCCEEDED(g_player->resume());
192198
}
193199
void SAPI::SetVolume(uint64_t value) {
194200
if (instance == nullptr)return;

SRC/SAPI.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ class SAPI : public Engine {
4141

4242
private:
4343
blastspeak* instance = nullptr;
44-
WasapiPlayer* player = nullptr;
4544
WAVEFORMATEX wfx;
4645
int trimThreshold = 20;
4746
};

0 commit comments

Comments
 (0)