forked from haguvenir/Turkish_TTS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTTSSound.cpp
280 lines (223 loc) · 6.67 KB
/
TTSSound.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
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
/**
* TTSSound.cpp: Includes TTSSound class that provides sound playback utilies for TTS engine.
*
*
*/
#include "TTSSound.h"
TTSSound::TTSSound() {
InitSound();
}
TTSSound::TTSSound(int bPS, int sPS, int nOC) {
InitSound(bPS, sPS, nOC);
}
TTSSound::~TTSSound() {
printf("Delete TTSSound \n");
ExitSound();
}
/**
* SoundInit():
* Initializes necessary data, opens the wave output device by calling OpenSndDevice function.
* Returns: TTS_NO_ERROR value when initialization is successful,
* error value when initialization fails.
*/
int TTSSound::InitSound(int bitsPerSample , int samplesPerSecond, int numOfChannels) {
int result = TTSError::TTS_NO_ERROR;
isStopped = false;
isBusy = 0; //not busy
InitializeCriticalSection(&waveCriticalSection);
result = OpenSndDevice(bitsPerSample, samplesPerSecond, numOfChannels);
return result;
}
/**
* SoundExit():
* Closes the wave output device by calling CloseSndDevice function.
* Returns: TTS_NO_ERROR value when initialization is successful,
* error value when initialization fails.
*/
int TTSSound::ExitSound() {
printf("Exit sound \n");;
int result ;
while(IsBusy());
DeleteCriticalSection(&waveCriticalSection);
result = CloseSndDevice();
return result;
} // ExitSound
int TTSSound::Stop() {
int result = TTSError::TTS_NO_ERROR;
isStopped = true;
result = waveOutReset(HWaveOut);
if(result != MMSYSERR_NOERROR)
return result;
return result;
} // Stop
int TTSSound::Pause() {
int result = TTSError::TTS_NO_ERROR;
result = waveOutPause(HWaveOut);
if(result != MMSYSERR_NOERROR)
return result;
return result;
} // Pause
int TTSSound::Restart() {
int result = TTSError::TTS_NO_ERROR;
result = waveOutRestart(HWaveOut);
if(result != MMSYSERR_NOERROR)
return result;
return result;
} // Restart
unsigned long int TTSSound::GetPitch() {
int error = TTSError::TTS_NO_ERROR;
unsigned long int result = 0;
error = waveOutGetPitch(HWaveOut, &result);
if(error != MMSYSERR_NOERROR) {
return error;
}
return result;
} // GetPitch
int TTSSound::SetPitch(unsigned long int newPitch) {
int result = TTSError::TTS_NO_ERROR;
result = waveOutSetPitch(HWaveOut, newPitch);
if(result != MMSYSERR_NOERROR)
return result;
return result;
} // SetPitch
unsigned long int TTSSound::GetVolume() {
int error = TTSError::TTS_NO_ERROR;
unsigned long int result = 0;
error = waveOutGetVolume(HWaveOut, &result);
if(error != MMSYSERR_NOERROR) {
printf("hata get");
return error;
}
return result;
} //GetVolume
int TTSSound::SetVolume(unsigned long int newVol) {
int result = TTSError::TTS_NO_ERROR;
result = waveOutSetVolume(HWaveOut, newVol);
if(result != MMSYSERR_NOERROR)
{
return result;
}
return result;
}
unsigned long int TTSSound::GetPlaybackRate() {
int error = TTSError::TTS_NO_ERROR;
unsigned long int result = 0;
error = waveOutGetPlaybackRate(HWaveOut, &result);
if(error != MMSYSERR_NOERROR) {
return error;
}
return result;
}
int TTSSound::SetPlaybackRate(unsigned long int newPRate) {
int result = TTSError::TTS_NO_ERROR;
result = waveOutSetPlaybackRate(HWaveOut, newPRate);
if(result != MMSYSERR_NOERROR)
return result;
return result;
}
void TTSSound::PrintError_Exit(int errNum) {
int code = 0;
memset(errorStr, 0, 100);
code = waveOutGetErrorText(errNum, errorStr, 100);
if( code == MMSYSERR_NOERROR) printf("ERROR CODE: %s\n", errorStr);
else {
if(code != MMSYSERR_BADERRNUM) printf("Unknown Error\n");
}
ExitSound();
//ExitProcess(1);
}
void TTSSound::PrintError(int errNum) {
int code = 0;
memset(errorStr, 0, 100);
code = waveOutGetErrorText(errNum, errorStr, 100);
if( code == MMSYSERR_NOERROR) printf("ERROR CODE: %s\n", errorStr);
else {
if(code != MMSYSERR_BADERRNUM) printf("Unknown Error\n");
}
}
/**
*
*/
int TTSSound::WriteSndData(/*WAVEHDR * waveHdr,*/ char * block, int size) {
int result = TTSError::TTS_NO_ERROR;
WAVEHDR * waveHeader = new WAVEHDR;
// while(isBusy == 1); //white until sound device finishes playing current
EnterCriticalSection(&waveCriticalSection);
isBusy = 1;
LeaveCriticalSection(&waveCriticalSection);
ZeroMemory(waveHeader, sizeof(WAVEHDR));
waveHeader->lpData = block;
waveHeader->dwBufferLength = size;
//preaparing data and writing to sound device
result = waveOutPrepareHeader(HWaveOut, waveHeader, sizeof(WAVEHDR));
if(result != MMSYSERR_NOERROR) return result;
result = waveOutWrite(HWaveOut, waveHeader, sizeof(WAVEHDR));
if(result != MMSYSERR_NOERROR) return result;
while(isBusy == 1);
delete waveHeader;
return result;
}
/**
* OpenSndDevice():
* Opens wave output device
* Returns: TTS_NO_ERROR value on success,
* error value on failure.
*/
int TTSSound::OpenSndDevice(int bitsPerSample, int samplesPerSecond, int numOfChannels) {
WAVEFORMATEX wfx; //wave format structure
int result = TTSError::TTS_NO_ERROR; //mm function result
//initializing wave format with defined values in the header
wfx.wFormatTag = WAVE_FORMAT_PCM;
wfx.nSamplesPerSec = samplesPerSecond;
wfx.nChannels = numOfChannels;
wfx.wBitsPerSample = bitsPerSample ;
wfx.nAvgBytesPerSec = samplesPerSecond * numOfChannels * (bitsPerSample/8);
wfx.nBlockAlign = (bitsPerSample/8) * numOfChannels;
wfx.cbSize = 0;
//opening the device with callback function
result = waveOutOpen(&HWaveOut,
WAVE_MAPPER,
&wfx,
(long int)SoundCallback,
(long int)&isBusy,
CALLBACK_FUNCTION );
if(result != MMSYSERR_NOERROR)return result;
return result;
}
/**
* CloseSndDevice():
* Closes wave output device
* Returns: TTS_NO_ERROR value on success,
* error value on failure.
*/
int TTSSound::CloseSndDevice() {
int result = TTSError::TTS_NO_ERROR;
//if device is still playing do not close device
while( (result = waveOutClose(HWaveOut)) == WAVERR_STILLPLAYING ) {
Stop();
}
if(result != MMSYSERR_NOERROR) return result;
return result;
}
bool TTSSound::IsBusy() {
bool busy;
EnterCriticalSection(&waveCriticalSection);
busy = (isBusy == 1);
LeaveCriticalSection(&waveCriticalSection);
return busy;
}
/**
* SoundCallback():
*
*/
static void CALLBACK SoundCallback(HWAVEOUT HWaveOut,
UINT uMsg,
DWORD dwInstance,
DWORD dwParam1,
DWORD dwParam2 ) {
int * isBusy = (int *)dwInstance;
//if playback complete for any block
if(uMsg == WOM_DONE) {
(*isBusy) = 0;
}//endif
}