This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathosxspeech.cpp
More file actions
311 lines (264 loc) · 7.19 KB
/
osxspeech.cpp
File metadata and controls
311 lines (264 loc) · 7.19 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/* Copyright (C) 2003-2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include <Carbon/Carbon.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <math.h>
#include "revspeech.h"
#include "osxspeech.h"
INarrator *InstantiateNarrator(NarratorProvider p_provider)
{
return new OSXSpeechNarrator();
}
INarrator::~INarrator(void)
{
// Nothing required.
}
OSXSpeechNarrator::OSXSpeechNarrator(void)
{
spchannel = NULL;
strcpy(speechvoice, "empty");
speechtext = NULL;
speechspeed = 0;
speechpitch = 0;
}
OSXSpeechNarrator::~OSXSpeechNarrator(void)
{
// Nothing required.
}
bool OSXSpeechNarrator::Initialize(void)
{
// Nothing required.
return true;
}
bool OSXSpeechNarrator::Finalize(void)
{
if (SpeechStop(true))
{
return true;
}
return false;
}
bool OSXSpeechNarrator::Start(const char* p_string, bool p_is_utf8)
{
if (SpeechStart(true) == false)
{
return false;
}
if (speechtext != nil)
CFRelease(speechtext);
speechtext = CFStringCreateWithCString(kCFAllocatorDefault, p_string, p_is_utf8 ? kCFStringEncodingUTF8 : kCFStringEncodingMacRoman);
SpeakCFString(spchannel, speechtext, nil);
return true;
}
bool OSXSpeechNarrator::Stop(void)
{
if (spchannel != nil)
StopSpeech(spchannel);
return true;
}
bool OSXSpeechNarrator::Busy(void)
{
if (spchannel != nil)
{
if (SpeechBusy())
{
return true;
}
else
{
return false;
}
}
return false;
}
bool OSXSpeechNarrator::ListVoices(NarratorGender p_gender, NarratorListVoicesCallback p_callback, void* p_context)
{
short NumVoices;
if (CountVoices(&NumVoices) != noErr)
return false;
VoiceSpec tVoice;
short t_gender = -1;
if (p_gender == kNarratorGenderNeuter)
{
t_gender = 0;
}
if (p_gender == kNarratorGenderMale)
{
t_gender = 1;
}
if (p_gender == kNarratorGenderFemale)
{
t_gender = 2;
}
VoiceDescription myInfo;
for (short count = 1; count<=NumVoices; count++)
{
if ((GetIndVoice(count, &tVoice) == noErr)
&&(GetVoiceDescription(&tVoice, &myInfo, sizeof(myInfo)) == noErr))
{
if ((t_gender != -1) && (myInfo.gender != t_gender)) continue;
char cvoice[256];
strncpy(cvoice, (const char*)&myInfo.name[1], myInfo.name[0]);
// PM-2016-02-15: [[ Bug 16929 ]] Make sure the copied strings are null-terminated
cvoice[myInfo.name[0]]='\0';
p_callback(p_context, p_gender, cvoice);
}
}
return true;
}
bool OSXSpeechNarrator::SetVoice(const char* p_string)
{
strcpy(speechvoice, p_string);
if (spchannel != nil)
{
SpeechStop(false);
SpeechStart(false);
}
return true;
}
bool OSXSpeechNarrator::SetSpeed(int p_speed)
{
speechspeed = Long2Fix(p_speed);
if (spchannel != nil)
SetSpeechRate (spchannel, speechspeed);
return true;
}
bool OSXSpeechNarrator::GetSpeed(int& p_speed)
{
p_speed = Fix2Long(speechspeed);
return true;
}
bool OSXSpeechNarrator::SetPitch(int p_pitch)
{
speechpitch = Long2Fix(p_pitch);
if (spchannel != nil)
SetSpeechPitch(spchannel, speechpitch);
return true;
}
bool OSXSpeechNarrator::GetPitch(int& p_pitch)
{
p_pitch = Fix2Long(speechpitch);
return true;
}
// Parameters
// p_volume : the value to set the volume to. Note, this is assumed to be between 0 and 100
// and is not checked. A value outside this range may cause unexpected volume or failure to set volume.
// Returns
// true if setting the volume suceeded, false otherwise.
// Description
// Sets the speech volume. If a speech channel is not already open, one is created and the volume of it set. Setting the volume
// will not take affect until after the current text being spoken is finished.
bool OSXSpeechNarrator::SetVolume(int p_volume)
{
// Convert t_volume to the format expected by the speech manager.
Fixed t_volume;
t_volume = (p_volume * 65536) / 100;
// As the volume can only be set with an open channel, we check if a channel is open
// and if not, create a speech channel here using FindAndSelect().
if (spchannel == NULL)
FindAndSelect();
if (spchannel == NULL)
return false;
if (SetSpeechInfo(spchannel, soVolume, &t_volume) != noErr)
{
return false;
}
return true;
}
bool OSXSpeechNarrator::GetVolume(int& p_volume)
{
// If no speech channel has been created then we create one via FindAndSelect() first.
Fixed t_volume;
t_volume = -1;
if (spchannel == NULL)
{
FindAndSelect();
}
// Get the volume of the current channel and check that the operation suceeded.
if (GetSpeechInfo(spchannel, soVolume, &t_volume) != noErr)
return false;
// Convert the volume to a standardised value and return.
p_volume = (((t_volume + 654) * 100) / 65536);
return true;
}
bool OSXSpeechNarrator::SpeechStart(bool StartInit)
{
if (spchannel == nil)
{
FindAndSelect();
if (spchannel == nil) return false;
if (speechspeed != 0)
SetSpeechRate (spchannel, speechspeed);
if (speechpitch != 0)
SetSpeechPitch(spchannel, speechpitch);
}
return true;
}
bool OSXSpeechNarrator::SpeechStop(bool ReleaseInit)
{
if (spchannel == nil)
return true;
// OK-2007-04-16: Fix for bug 3611 and 3591.
StopSpeech(spchannel);
do
{
// Run an inner main loop until the speech has finished processing
EventRef t_event;
OSStatus t_status = ReceiveNextEvent(0, NULL, 1, true, &t_event);
if (t_status == noErr)
{
SendEventToEventTarget(t_event, GetEventDispatcherTarget());
ReleaseEvent(t_event);
}
}
while(SpeechBusy() > 0);
DisposeSpeechChannel(spchannel);
spchannel = nil;
if (speechtext != nil)
{
CFRelease(speechtext);
speechtext = nil;
}
return true;
}
void OSXSpeechNarrator::FindAndSelect()
{
VoiceSpec *FoundVoice = nil;
VoiceSpec tVoice;
char cvoice[256];
short NumVoices;
if (CountVoices(&NumVoices) == noErr) {
for (short count = 1;count<=NumVoices;count++)
{
if (GetIndVoice(count, &tVoice) == noErr)
{
VoiceDescription myInfo;
if (GetVoiceDescription(&tVoice, &myInfo, sizeof(myInfo)) == noErr) {
strncpy(cvoice, (const char*)&myInfo.name[1], myInfo.name[0]);
// PM-2016-02-15: [[ Bug 16929 ]] Make sure the copied strings are null-terminated
cvoice[myInfo.name[0]]='\0';
if (strcmp(cvoice,speechvoice) == 0)
{
FoundVoice = &tVoice;
break;
}
}
}
}
}
if (FoundVoice != nil)
NewSpeechChannel(FoundVoice, &spchannel);
else
NewSpeechChannel(nil, &spchannel);
}