Skip to content

Commit

Permalink
SAPI: Fix getting voice list.
Browse files Browse the repository at this point in the history
  • Loading branch information
m1maker committed Feb 16, 2025
1 parent 4792492 commit 794083e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
22 changes: 22 additions & 0 deletions Examples/C/SRALExample.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,28 @@ int main(void) {
scanf("%s", text);

SRAL_StopSpeech(); // Stops the delay thread

// Set voice
if (SRAL_GetEngineFeatures(0) & SUPPORTS_SELECT_VOICE) {
int voice_count = *(int*)SRAL_GetEngineParameter(0, VOICE_COUNT);
if (voice_count > 0) {
const char** voices = (const char**)SRAL_GetEngineParameter(0, VOICE_LIST);
if (voices != NULL) {
printf("Enter voice index to select\nVoice list:\n");
for (int i = 0; i < voice_count; i++) {
printf("%d: %s\n", i, voices[i]);
free((void*)voices[i]);
}
free(voices);
int index = 0;
scanf("%d", &index);
if (index >= 0 && index < voice_count) {
SRAL_SetEngineParameter(0, VOICE_INDEX, &index);
}
}
}
}

// Speech rate
if (SRAL_GetEngineFeatures(0) & SUPPORTS_SPEECH_RATE) {

Expand Down
1 change: 1 addition & 0 deletions Include/SRAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ extern "C" {
SPEECH_VOLUME,
VOICE_INDEX,
VOICE_LIST,
VOICE_COUNT,
SYMBOL_LEVEL,
SAPI_TRIM_THRESHOLD,
ENABLE_SPELLING,
Expand Down
16 changes: 10 additions & 6 deletions SRC/SAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include<string>
#include<thread>


static WasapiPlayer* g_player = nullptr; // Make it global to avoid multiple voices when reinitializing


Expand Down Expand Up @@ -209,13 +208,18 @@ void* SAPI::GetParameter(int param) {
blastspeak_get_voice_volume(instance, val);
return static_cast<void*>(val);
case VOICE_LIST:
if (!voices.empty())
voices.clear();
voices.resize(instance->voice_count);
if (voices)
delete[] voices;
voices = new const char*[instance->voice_count];
for (int i = 0; i < instance->voice_count; ++i) {
voices[i] = blastspeak_get_voice_description(instance, i);
const char* voice_desc = blastspeak_get_voice_description(instance, i);
voices[i] = new char[strlen(voice_desc) + 1];
strcpy(const_cast<char*>(voices[i]), voice_desc);
}
return static_cast<void*>(voices.data());
return voices;
case VOICE_COUNT:
*val = instance->voice_count;
return static_cast<void*>(val);
default:
return nullptr;
}
Expand Down
3 changes: 1 addition & 2 deletions SRC/SAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "../Dep/wasapi.h"
#include "../Include/SRAL.h"
#include "Engine.h"
#include <vector>

class SAPI : public Engine {
public:
Expand Down Expand Up @@ -46,6 +45,6 @@ class SAPI : public Engine {
blastspeak* instance = nullptr;
WAVEFORMATEX wfx;
int trimThreshold = 20;
std::vector<const char*> voices;
const char** voices = nullptr;
};
#endif

0 comments on commit 794083e

Please sign in to comment.