forked from haguvenir/Turkish_TTS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTTSWave.cpp
174 lines (126 loc) · 3.7 KB
/
TTSWave.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
#include "TTSWave.h"
TTSWave::TTSWave() {
data = NULL;
leftPitchPeriod = 0;
rightPitchPeriod = 0;
size = 0;
}
TTSWave::~TTSWave() {
if(data != NULL) {
HeapFree(GetProcessHeap(), 0, data);
}
}
void TTSWave::ReadFile(TCHAR * fileName) {
FILE * fp;
WAVE_CHUNKS head;
int readBytes=0;
//opening file
if((fp = _tfopen(fileName, "rb")) == NULL) {
TTSError::ShowError(TTSError::TTS_FILE_NOT_FOUND_ERR);
return;
// exit(1);
}
fread(&head,1,WAVE_HEADER_SIZE,fp);
size = head.dataChunk.dataLength; //ignore header, take just data
if((data = (char *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size)) == NULL) {
TTSError::ShowError(TTSError::TTS_HEAP_ALLOC_ERR);
return;
// exit(1);
}
size = size - WAVE_HEADER_SIZE - WAVE_TAIL_SIZE;
while(readBytes < size) {
readBytes += fread(data+readBytes, 1, size - readBytes, fp);
}
for(int i = 0; i<size; i++) {
if(data[i] == 'c' && data[i+1] == 'u' && data[i+2] == 'e') {
cout<<"--------------------"<<i<<"---"<<size - i<<endl;
break;
}
}
fclose(fp);
}
/**
* WriteFile: Used for writing wave output file for only specific format of:
* 22050 hz, mono, 8 bit per sample data.
*
*/
void TTSWave::WriteFile(TCHAR *fileName, WAVE_CHUNKS chunks){
FILE * fp;
int written = 0;
chunks.dataChunk.dataLength = size;
//opening file
if((fp = _tfopen(fileName, "wb")) == NULL) {
TTSError::ShowError(TTSError::TTS_UNKNOWN_ERR);
return;
// exit(1);
}
fwrite(&chunks, 1, WAVE_HEADER_SIZE, fp);
while(written < size ) {
written += fwrite(data+written, 1, size - written, fp);
}
fclose(fp);
}
/**
* Concatenates given wav file with this one
*/
TTSWave & TTSWave::Concat(TTSWave & wave) {
char * temp;
int newSize = 0;
if(this->rightPitchPeriod != 0 && wave.GetLeftPitchPeriod() != 0) {
int mergeAmount = this->rightPitchPeriod * MERGE_PERIOD_WIDTH;
if( mergeAmount >= 3*this->size/2 )
mergeAmount = this->size/2;
newSize = this->size + wave.GetSize() - mergeAmount;
if((temp = (char *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, newSize)) == NULL) {
TTSError::ShowError(TTSError::TTS_HEAP_ALLOC_ERR);
return *this;
// exit(1);
}
memcpy(temp, this->data, this->size - mergeAmount);
memcpy(temp + this->size - mergeAmount, wave.GetData(), wave.GetSize());
}
else {
newSize = this->size + wave.GetSize();
if((temp = (char *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, newSize)) == NULL) {
TTSError::ShowError(TTSError::TTS_HEAP_ALLOC_ERR);
return *this;
// exit(1);
}
if(this->data != NULL) {
memcpy(temp, this->data, this->size);
memcpy(temp+this->size, wave.GetData(), wave.GetSize());
}
else {
memcpy(temp, wave.GetData(), wave.GetSize());
}
}
SetData(temp, newSize);
this->rightPitchPeriod = wave.GetRightPitchPeriod();
return *this;
}
void TTSWave::AddWave(TTSWave &wave) {
char * temp;
int newSize = 0;
newSize = this->size + wave.GetSize();
if((temp = (char *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, newSize)) == NULL) {
TTSError::ShowError(TTSError::TTS_HEAP_ALLOC_ERR);
return;
// exit(1);
}
if(this->data != NULL) {
memcpy(temp, this->data, this->size);
memcpy(temp+this->size, wave.GetData(), wave.GetSize());
}
else {
memcpy(temp, wave.GetData(), wave.GetSize());
}
SetData(temp, newSize);
this->rightPitchPeriod = wave.GetRightPitchPeriod();
}
void TTSWave::SetData(char *newData, int newSize) {
if(data!=NULL) {
HeapFree(GetProcessHeap(), 0, this->data);
}
this->data = newData;
this->size = newSize;
}