-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsound.cpp
105 lines (78 loc) · 2.79 KB
/
sound.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "speech.h"
#define READ_STR_FORMAT "read %.*s"
#define READ_SIZE_BUFF 256
#define MAX_TO_READ 250
char phraseToRead[READ_SIZE_BUFF];
int sbtts_run_command(char * command, bool redirectStdout){
if(redirectStdout){
// Send stdout to nul
freopen("nul", "r", stdout);
}
int returnCode = system(command);
if(redirectStdout){
fflush(stdout);
//Restore the stdout
freopen("con", "a", stdout);
}
return returnCode;
}
bool sbtts_init(){
sbtts_run_command("SBTALKER /dBLASTER", false);
if(DetectSpeech()){
ResetSpeech();
return true;
} else {
return false;
}
}
void sbtts_end(){
ResetSpeech();
sbtts_run_command("REMOVE", false);
}
void sbtts_read_this_phrase(char * phrase, int length, bool redirectStdout){
//Clear buffer before starting every new phrase
ResetSpeech();
//Set appropriate speed
SetGlobals(0, 0, 5, 5, 3);
memset(phraseToRead, 0, READ_SIZE_BUFF);
memcpy(phraseToRead, phrase, length);
Say(phraseToRead);
}
void sbtts_read_str(char * str_to_read, int length, bool redirectStdout){
int currentStartPointer = 0;
if(length < MAX_TO_READ){
sbtts_read_this_phrase(str_to_read, length, redirectStdout);
} else {
//Look for first break
for(int i = 0; i < length; i++){
char currentChar = str_to_read[i];
int currentLength = i - currentStartPointer;
if(currentLength > MAX_TO_READ){
//Since we reach the end before any punctuation, we should backtrack to find a space to avoid breaking up word.
int j;
for(j = i; i > currentStartPointer; j--){
char candidateSpace = str_to_read[j];
if(candidateSpace == ' '){
break;
}
}
i = j;
sbtts_read_this_phrase(str_to_read + currentStartPointer, i - currentStartPointer, redirectStdout);
currentStartPointer = i;
} else {
// Split along the punctuation
if('!' <= currentChar && currentChar <= '/'
|| ':' <= currentChar && currentChar <= '@'
|| '[' <= currentChar && currentChar <= '`'
|| '{' <= currentChar && currentChar <= '~'){
//printf("start %d length %d stop at %c\n", currentStartPointer, i - currentStartPointer, currentChar);
sbtts_read_this_phrase(str_to_read + currentStartPointer, i - currentStartPointer, redirectStdout);
currentStartPointer = i;
}
}
}
}
}