Skip to content

Commit

Permalink
SRAL_GetEngineName: Move parameter retrieving to output argument.
Browse files Browse the repository at this point in the history
  • Loading branch information
m1maker committed Feb 20, 2025
1 parent e5df6df commit fce4bb2
Show file tree
Hide file tree
Showing 14 changed files with 113 additions and 109 deletions.
36 changes: 20 additions & 16 deletions Examples/C/SRALExample.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ int main(void) {

SRAL_RegisterKeyboardHooks();
// Speak some text
bool result = *(bool*)SRAL_GetEngineParameter(ENGINE_NVDA, NVDA_IS_CONTROL_EX);
bool result;
SRAL_GetEngineParameter(ENGINE_NVDA, NVDA_IS_CONTROL_EX, &result);
printf("NVDA extended: %d\n", result);
if (SRAL_GetEngineFeatures(0) & SUPPORTS_SPEECH) {
printf("Enter the text you want to be spoken:\n");
Expand All @@ -70,29 +71,32 @@ int main(void) {

// Set voice
if (SRAL_GetEngineFeatures(0) & SUPPORTS_SELECT_VOICE) {
int voice_count = *(int*)SRAL_GetEngineParameter(0, VOICE_COUNT);
int voice_count;
SRAL_GetEngineParameter(ENGINE_NONE, VOICE_COUNT, &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);
}
char* voices[256];
for (unsigned int i = 0; i < voice_count; i++) {
voices[i] = (char*)malloc(64);
}
SRAL_GetEngineParameter(0, VOICE_LIST, voices);
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(voices[i]);
}
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) {

int rate = *(int*)SRAL_GetEngineParameter(ENGINE_NONE, SPEECH_RATE);
int rate;
SRAL_GetEngineParameter(ENGINE_NONE, SPEECH_RATE, &rate);

const uint64_t max_rate = rate + 10;
for (rate; rate < max_rate; rate++) {
Expand Down
8 changes: 5 additions & 3 deletions Include/SRAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,18 +203,20 @@ extern "C" {
* @return true if the parameter was set successfully, false otherwise.
*/

SRAL_API bool SRAL_SetEngineParameter(int engine, int param, void* value);
SRAL_API bool SRAL_SetEngineParameter(int engine, int param, const void* value);




/**
* @brief Get the parameter for the specified speech engine.
* @param engine The engine to get the param for.
* @return an address to value if the parameter was get successfully, NULL otherwise.
* @param value An out pointer to write value.
* @return true if the parameter was retrieved successfully, false otherwise.
*/

SRAL_API void* SRAL_GetEngineParameter(int engine, int param);
SRAL_API bool SRAL_GetEngineParameter(int engine, int param, void* value);



Expand Down
6 changes: 2 additions & 4 deletions SRC/AVSpeech.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ Thanks Gruia for implementing AVSpeech.
#include "../Include/SRAL.h"
#include "Engine.h"
#include <string>
#include <vector>
class AVSpeechSynthesizerWrapper;

class AVSpeech : public Engine {
Expand All @@ -18,9 +17,9 @@ class AVSpeech : public Engine {
return nullptr;
}

bool SetParameter(int param, void* value)override;
bool SetParameter(int param, const void* value)override;

void* GetParameter(int param) override;
bool GetParameter(int param, void* value) override;

bool Braille(const char* text)override { return false; }
bool StopSpeech()override;
Expand Down Expand Up @@ -48,5 +47,4 @@ class AVSpeech : public Engine {

private:
AVSpeechSynthesizerWrapper* obj = nullptr;
std::vector<const char*> voices;
};
35 changes: 16 additions & 19 deletions SRC/AVSpeech.mm
Original file line number Diff line number Diff line change
Expand Up @@ -139,45 +139,42 @@ bool SetVoice(uint64_t index){
}


bool AVSpeech::SetParameter(int param, void* value) {
bool AVSpeech::SetParameter(int param, const void* value) {
switch (param) {
case SPEECH_RATE:
obj->SetRate(*static_cast<int*>(value));
obj->SetRate(*reinterpret_cast<const int*>(value));
return true;
case SPEECH_VOLUME:
obj->SetVolume(*static_cast<int*>(value));
obj->SetVolume(*reinterpret_cast<const int*>(value));
return true;
case VOICE_INDEX:
return obj->SetVoice(*static_cast<int*>(value));
return obj->SetVoice(*reinterpret_cast<const int*>(value));
default:
return false;
}
return true;
}

void* AVSpeech::GetParameter(int param) {
int voice_count = 0;
bool AVSpeech::GetParameter(int param, void* value) {
switch (param) {
case SPEECH_RATE: {
int rate = obj->GetRate();
return new int(rate);
*(int*)value = obj->GetRate();
return true;
}
case SPEECH_VOLUME: {
int volume = obj->GetVolume();
return new int(volume);
*(int*)value = obj->GetVolume();
return true;
}
case VOICE_LIST:
if (!voices.empty())
voices.clear();
voice_count = obj->GetVoiceCount();
voices.resize(voice_count);
case VOICE_LIST: {
int voice_count = obj->GetVoiceCount();
for (int i = 0; i < voice_count; ++i) {
voices[i] = obj->GetVoiceName(i);
(const char**)value[i] = obj->GetVoiceName(i);
}
return static_cast<void*>(voices.data());
return true;
}
default:
return nullptr;
return false;
}
return nullptr;
return true;
}

4 changes: 2 additions & 2 deletions SRC/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class Engine {
virtual const char* GetVoiceName(uint64_t index) = 0;
virtual bool SetVoice(uint64_t index) = 0;
virtual int GetKeyFlags() = 0;
virtual bool SetParameter(int param, void* value) = 0;
virtual void* GetParameter(int param) = 0;
virtual bool SetParameter(int param, const void* value) = 0;
virtual bool GetParameter(int param, void* value) = 0;

bool paused;
};
Expand Down
6 changes: 3 additions & 3 deletions SRC/Jaws.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ class Jaws : public Engine {
void* SpeakToMemory(const char* text, uint64_t* buffer_size, int*channels, int* sample_rate, int* bits_per_sample)override {
return nullptr;
}
bool SetParameter(int param, void* value)override {
bool SetParameter(int param, const void* value)override {
return false;
}

void* GetParameter(int param) override {
return nullptr;
bool GetParameter(int param, void* value) override {
return false;
}

bool Braille(const char* text)override;
Expand Down
26 changes: 15 additions & 11 deletions SRC/NVDA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,38 +79,42 @@ bool NVDA::SpeakSsml(const char* ssml, bool interrupt) {
return nvdaController_speakSsml(out.c_str(), this->symbolLevel, 0, true) == 0;
}

bool NVDA::SetParameter(int param, void* value) {
bool NVDA::SetParameter(int param, const void* value) {
switch (param) {
case SYMBOL_LEVEL:
this->symbolLevel = *static_cast<int*>(value);
this->symbolLevel = *reinterpret_cast<const int*>(value);
break;
case ENABLE_SPELLING:
this->enable_spelling = *static_cast<bool*>(value);
this->enable_spelling = *reinterpret_cast<const bool*>(value);
break;
case USE_CHARACTER_DESCRIPTIONS:
this->use_character_descriptions = *static_cast<bool*>(value);
this->use_character_descriptions = *reinterpret_cast<const bool*>(value);
break;
default:
return false;
}
return true;
}

void* NVDA::GetParameter(int param) {
bool NVDA::GetParameter(int param, void* value) {
switch (param) {
case SYMBOL_LEVEL:
return new int(this->symbolLevel);
*(int*)value = this->symbolLevel;
return true;
case ENABLE_SPELLING:
return new bool(this->enable_spelling);
*(bool*)value = this->enable_spelling;
return true;
case USE_CHARACTER_DESCRIPTIONS:
return new bool(this->use_character_descriptions);
*(bool*)value = this->use_character_descriptions;
return true;
case NVDA_IS_CONTROL_EX:
this->extended = nvda_active() == 0;
return new bool(this->extended);
*(bool*)value = this->extended;
return true;
default:
return nullptr;
return false;
}
return nullptr;
return false;
}

bool NVDA::Braille(const char* text) {
Expand Down
4 changes: 2 additions & 2 deletions SRC/NVDA.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class NVDA : public Engine {
return nullptr;
}

bool SetParameter(int param, void* value)override;
void* GetParameter(int param) override;
bool SetParameter(int param, const void* value)override;
bool GetParameter(int param, void* value) override;

bool Braille(const char* text)override;
bool StopSpeech()override;
Expand Down
50 changes: 24 additions & 26 deletions SRC/SAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ struct PCMData {
unsigned long size;
};

std::vector<PCMData> g_dataQueue;
bool g_threadStarted = false;
static std::vector<PCMData> g_dataQueue;
static bool g_threadStarted = false;
static void sapi_thread() {
if (g_player == nullptr) {
g_threadStarted = false;
Expand Down Expand Up @@ -171,61 +171,59 @@ void* SAPI::SpeakToMemory(const char* text, uint64_t* buffer_size, int*channels,
return final;
}

bool SAPI::SetParameter(int param, void* value) {
bool SAPI::SetParameter(int param, const void* value) {
if (instance == nullptr)
return false;

switch (param) {
case SAPI_TRIM_THRESHOLD:
this->trimThreshold = *static_cast<int*>(value);
this->trimThreshold = *reinterpret_cast<const int*>(value);
break;
case SPEECH_RATE:
blastspeak_set_voice_rate(instance, *static_cast<int*>(value));
blastspeak_set_voice_rate(instance, *reinterpret_cast<const long*>(value));
return true;
case SPEECH_VOLUME:
blastspeak_set_voice_volume(instance, *static_cast<int*>(value));
blastspeak_set_voice_volume(instance, *reinterpret_cast<const long*>(value));
return true;
case VOICE_INDEX:
blastspeak_set_voice(instance, *static_cast<int*>(value));
blastspeak_set_voice(instance, *reinterpret_cast<const int*>(value));
default:
return false;
}
return true;
}

void* SAPI::GetParameter(int param) {
bool SAPI::GetParameter(int param, void* value) {
if (instance == nullptr)
return nullptr;
return false;

switch (param) {
case SAPI_TRIM_THRESHOLD:
return new long(this->trimThreshold);
*(int*)value = this->trimThreshold;
return true;
case SPEECH_RATE: {
long* val = new long;
blastspeak_get_voice_rate(instance, val);
return static_cast<void*>(val);
blastspeak_get_voice_rate(instance, (long*)value);
return true;
}
case SPEECH_VOLUME: {
long* val = new long;
blastspeak_get_voice_volume(instance, val);
return static_cast<void*>(val);
blastspeak_get_voice_volume(instance, (long*)value);
return true;
}
case VOICE_LIST:
if (voices)
delete[] voices;
voices = new const char*[instance->voice_count];
case VOICE_LIST: {
char** voices = (char**)value;
for (int i = 0; i < instance->voice_count; ++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);
strcpy(voices[i], voice_desc);
}
return voices;
return true;
}
case VOICE_COUNT:
return new int(instance->voice_count);
*(int*)value = instance->voice_count;
return true;
default:
return nullptr;
return false;
}
return nullptr;
return false;
}

bool SAPI::StopSpeech() {
Expand Down
5 changes: 2 additions & 3 deletions SRC/SAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class SAPI : public Engine {
return false;
}
void* SpeakToMemory(const char* text, uint64_t* buffer_size, int*channels, int* sample_rate, int* bits_per_sample)override;
bool SetParameter(int param, void* value)override;
void* GetParameter(int param) override;
bool SetParameter(int param, const void* value)override;
bool GetParameter(int param, void* value) override;

bool Braille(const char* text)override { return false; }
bool StopSpeech()override;
Expand Down Expand Up @@ -45,6 +45,5 @@ class SAPI : public Engine {
blastspeak* instance = nullptr;
WAVEFORMATEX wfx;
int trimThreshold = 20;
const char** voices = nullptr;
};
#endif
Loading

0 comments on commit fce4bb2

Please sign in to comment.