Skip to content

Commit

Permalink
AVSpeech fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
m1maker committed Aug 29, 2024
1 parent 54476bc commit 0a255d9
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 23 deletions.
12 changes: 2 additions & 10 deletions SRC/AVSpeech.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Thanks Gruia for implementing AVSpeech.
#include "../Include/SRAL.h"
#include "Engine.h"
#include <string>
class object;

class AVSpeech : public Engine {
public:
Expand All @@ -28,14 +29,5 @@ class AVSpeech : public Engine {
const char* GetVoiceName(uint64_t index)override;
bool SetVoice(uint64_t index)override;
private:
float rate;
float volume;
class AVSpeechSynthesizer;
AVSpeechSynthesizer* synth;
class AVSpeechSynthesisVoice;
AVSpeechSynthesisVoice* currentVoice;
class AVSpeechUtterance;
AVSpeechUtterance* utterance;
class NSString;
AVSpeechSynthesisVoice* getVoiceObject(NSString* name);
object* obj;
};
74 changes: 61 additions & 13 deletions SRC/AVSpeech.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,34 @@
#include <stdint.h>
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
class object{
public:
float rate;
float volume;
AVSpeechSynthesizer* synth;
AVSpeechSynthesisVoice* currentVoice;
AVSpeechUtterance* utterance;

AVSpeechSynthesisVoice* AVSpeech::getVoiceObject(NSString* name){
AVSpeechSynthesisVoice* getVoiceObject(NSString* name){
NSArray<AVSpeechSynthesisVoice*>* voices = [AVSpeechSynthesisVoice speechVoices];
for (AVSpeechSynthesisVoice* v in voices) {
if ([v.name isEqualToString : name]) return v;
}
return nil;
}

bool AVSpeech::Initialize() {
bool Initialize() {
currentVoice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"]; //choosing english as a default language
utterance = [[AVSpeechUtterance alloc] initWithString:@""];
rate = utterance.rate;
volume = utterance.volume;
synth = [[AVSpeechSynthesizer alloc] init];
return true;
}
bool AVSpeech::Uninitialize(){
bool Uninitialize(){
return true;
}
bool AVSpeech::Speak(const char* text, bool interrupt){
bool Speak(const char* text, bool interrupt){
if (interrupt && synth.isSpeaking)[synth stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
NSString *nstext = [NSString stringWithUTF8String:text];
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:nstext];
Expand All @@ -33,38 +40,38 @@
[synth speakUtterance:this->utterance];
return synth.isSpeaking;
}
bool AVSpeech::StopSpeech(){
bool StopSpeech(){
if (synth.isSpeaking) return [synth stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
return false;
}
bool AVSpeech::GetActive(){
bool GetActive(){
return synth != nil;
}
void AVSpeech::SetVolume(uint64_t value){
void SetVolume(uint64_t value){
this->volume = value;
}
uint64_t AVSpeech::GetVolume(){
uint64_t GetVolume(){
return this->volume;
}
void AVSpeech::SetRate(uint64_t value){
void SetRate(uint64_t value){
this->rate = value;
}
uint64_t AVSpeech::GetRate(){
uint64_t GetRate(){
return this->rate;
}
uint64_t AVSpeech::GetVoiceCount(){
uint64_t GetVoiceCount(){
NSArray<AVSpeechSynthesisVoice *> *voices = [AVSpeechSynthesisVoice speechVoices];
return voices.count;
}
const char* AVSpeech::GetVoiceName(uint64_t index){
const char* GetVoiceName(uint64_t index){
NSArray<AVSpeechSynthesisVoice *> *voices = [AVSpeechSynthesisVoice speechVoices];
@try {
return [[voices objectAtIndex:index].name UTF8String];
} @catch (NSException *exception) {
return "";
}
}
bool AVSpeech::SetVoice(uint64_t index){
bool SetVoice(uint64_t index){
NSArray<AVSpeechSynthesisVoice *> *voices = [AVSpeechSynthesisVoice speechVoices];
AVSpeechSynthesisVoice *oldVoice = currentVoice;
@try {
Expand All @@ -75,3 +82,44 @@
return false;
}
}
}
bool AVSpeech::Initialize(){
obj = new object;
return obj->Initialize();
}
bool AVSpeech::Uninitialize(){
if(obj == nil)return false;
delete obj;
obj = nil;
}
bool AVSpeech::GetActive(){
return obj != nil && obj->GetActive();
}
bool AVSpeech::Speak(const char* text, bool interrupt){
return obj->Speak(text, interrupt);
}
bool AVSpeech::StopSpeech(){
return obj->StopSpeech();
}
void AVSpeech::SetVolume(uint64_t value){
obj->SetVolume(value);
}
uint64_t AVSpeech::GetVolume(){
return obj->GetVolume();
}
void AVSpeech::SetRate(uint64_t value){
obj->SetRate(value);
}
uint64_t AVSpeech::GetRate(){
return obj->GetRate();
}
uint64_t AVSpeech::GetVoiceCount(){
return obj->GetVoiceCount();
}
const char* AVSpeech::GetVoiceName(uint64_t index){
return obj->GetVoiceName(index);
}
bool AVSpeech::SetVoice(uint64_t index){
return obj->SetVoice(index);
}

0 comments on commit 0a255d9

Please sign in to comment.