-rw-r--r-- | noncore/multimedia/opierec/wavFile.cpp | 34 |
1 files changed, 16 insertions, 18 deletions
diff --git a/noncore/multimedia/opierec/wavFile.cpp b/noncore/multimedia/opierec/wavFile.cpp index fc0130c..360b884 100644 --- a/noncore/multimedia/opierec/wavFile.cpp +++ b/noncore/multimedia/opierec/wavFile.cpp | |||
@@ -91,226 +91,224 @@ bool WavFile::newFile() { | |||
91 | odebug << errorMsg << oendl; | 91 | odebug << errorMsg << oendl; |
92 | QMessageBox::message("Note", "Error opening file.\n" +errorMsg); | 92 | QMessageBox::message("Note", "Error opening file.\n" +errorMsg); |
93 | 93 | ||
94 | return false; | 94 | return false; |
95 | } else { | 95 | } else { |
96 | setWavHeader( track.handle() , &hdr); | 96 | setWavHeader( track.handle() , &hdr); |
97 | } | 97 | } |
98 | return true; | 98 | return true; |
99 | } | 99 | } |
100 | 100 | ||
101 | WavFile::~WavFile() { | 101 | WavFile::~WavFile() { |
102 | 102 | ||
103 | closeFile(); | 103 | closeFile(); |
104 | } | 104 | } |
105 | 105 | ||
106 | void WavFile::closeFile() { | 106 | void WavFile::closeFile() { |
107 | if(track.isOpen()) | 107 | if(track.isOpen()) |
108 | track.close(); | 108 | track.close(); |
109 | } | 109 | } |
110 | 110 | ||
111 | int WavFile::openFile(const QString ¤tFileName) { | 111 | int WavFile::openFile(const QString ¤tFileName) { |
112 | // odebug << "open play file "+currentFileName << oendl; | 112 | // odebug << "open play file "+currentFileName << oendl; |
113 | closeFile(); | 113 | closeFile(); |
114 | 114 | ||
115 | track.setName(currentFileName); | 115 | track.setName(currentFileName); |
116 | 116 | ||
117 | if(!track.open(IO_ReadOnly)) { | 117 | if(!track.open(IO_ReadOnly)) { |
118 | QString errorMsg=(QString)strerror(errno); | 118 | QString errorMsg=(QString)strerror(errno); |
119 | odebug << "<<<<<<<<<<< "+errorMsg+currentFileName << oendl; | 119 | odebug << "<<<<<<<<<<< "+errorMsg+currentFileName << oendl; |
120 | QMessageBox::message("Note", "Error opening file.\n" +errorMsg); | 120 | QMessageBox::message("Note", "Error opening file.\n" +errorMsg); |
121 | return -1; | 121 | return -1; |
122 | } else { | 122 | } else { |
123 | parseWavHeader( track.handle()); | 123 | parseWavHeader( track.handle()); |
124 | } | 124 | } |
125 | return track.handle(); | 125 | return track.handle(); |
126 | } | 126 | } |
127 | 127 | ||
128 | bool WavFile::setWavHeader(int fd, wavhdr *hdr) { | 128 | bool WavFile::setWavHeader(int fd, wavhdr *hdr) { |
129 | 129 | ||
130 | strncpy((*hdr).riffID, "RIFF", 4); // RIFF | 130 | strncpy((*hdr).riffID, "RIFF", 4); // RIFF |
131 | strncpy((*hdr).wavID, "WAVE", 4); //WAVE | 131 | strncpy((*hdr).wavID, "WAVE", 4); //WAVE |
132 | strncpy((*hdr).fmtID, "fmt ", 4); // fmt | 132 | strncpy((*hdr).fmtID, "fmt ", 4); // fmt |
133 | (*hdr).fmtLen = 16; // format length = 16 | 133 | (*hdr).fmtLen = 16; // format length = 16 |
134 | 134 | ||
135 | if( wavFormat == WAVE_FORMAT_PCM) { | 135 | if( wavFormat == WAVE_FORMAT_PCM) { |
136 | (*hdr).fmtTag = 1; // PCM | 136 | (*hdr).fmtTag = 1; // PCM |
137 | // odebug << "set header WAVE_FORMAT_PCM" << oendl; | 137 | // odebug << "set header WAVE_FORMAT_PCM" << oendl; |
138 | } | 138 | } |
139 | else { | 139 | else { |
140 | (*hdr).fmtTag = WAVE_FORMAT_DVI_ADPCM; //intel ADPCM | 140 | (*hdr).fmtTag = WAVE_FORMAT_DVI_ADPCM; //intel ADPCM |
141 | // odebug << "set header WAVE_FORMAT_DVI_ADPCM" << oendl; | 141 | // odebug << "set header WAVE_FORMAT_DVI_ADPCM" << oendl; |
142 | } | 142 | } |
143 | 143 | ||
144 | // (*hdr).nChannels = 1;//filePara.channels;// ? 2 : 1*/; // channels | 144 | // (*hdr).nChannels = 1;//filePara.channels;// ? 2 : 1*/; // channels |
145 | (*hdr).nChannels = wavChannels;// ? 2 : 1*/; // channels | 145 | (*hdr).nChannels = wavChannels;// ? 2 : 1*/; // channels |
146 | 146 | ||
147 | (*hdr).sampleRate = wavSampleRate; //samples per second | 147 | (*hdr).sampleRate = wavSampleRate; //samples per second |
148 | (*hdr).avgBytesPerSec = (wavSampleRate)*( wavChannels*(wavResolution/8)); // bytes per second | 148 | (*hdr).avgBytesPerSec = (wavSampleRate)*( wavChannels*(wavResolution/8)); // bytes per second |
149 | (*hdr).nBlockAlign = wavChannels*( wavResolution/8); //block align | 149 | (*hdr).nBlockAlign = wavChannels*( wavResolution/8); //block align |
150 | (*hdr).bitsPerSample = wavResolution; //bits per sample 8, or 16 | 150 | (*hdr).bitsPerSample = wavResolution; //bits per sample 8, or 16 |
151 | 151 | ||
152 | strncpy((*hdr).dataID, "data", 4); | 152 | strncpy((*hdr).dataID, "data", 4); |
153 | 153 | ||
154 | write( fd,hdr, sizeof(*hdr)); | 154 | write( fd,hdr, sizeof(*hdr)); |
155 | odebug << "writing header: bitrate " << wavResolution | 155 | odebug << "writing header: bitrate " << wavResolution << ", samplerate " << wavSampleRate << ", channels " << wavChannels << oendl; |
156 | << ", samplerate " << wavSampleRate | ||
157 | << ", channels " << wavChannels << oendl; | ||
158 | return true; | 156 | return true; |
159 | } | 157 | } |
160 | 158 | ||
161 | bool WavFile::adjustHeaders(int fd, int total) { | 159 | bool WavFile::adjustHeaders(int fd, int total) { |
162 | lseek(fd, 4, SEEK_SET); | 160 | lseek(fd, 4, SEEK_SET); |
163 | int i = total + 36; | 161 | int i = total + 36; |
164 | write( fd, &i, sizeof(i)); | 162 | write( fd, &i, sizeof(i)); |
165 | lseek( fd, 40, SEEK_SET); | 163 | lseek( fd, 40, SEEK_SET); |
166 | write( fd, &total, sizeof(total)); | 164 | write( fd, &total, sizeof(total)); |
167 | odebug << "adjusting header " << total << "" << oendl; | 165 | odebug << "adjusting header " << total << "" << oendl; |
168 | return true; | 166 | return true; |
169 | } | 167 | } |
170 | 168 | ||
171 | int WavFile::parseWavHeader(int fd) { | 169 | int WavFile::parseWavHeader(int fd) { |
172 | odebug << "Parsing wav header" << oendl; | 170 | odebug << "Parsing wav header" << oendl; |
173 | char string[4]; | 171 | char string[4]; |
174 | int found; | 172 | int found; |
175 | short fmt; | 173 | short fmt; |
176 | unsigned short ch, bitrate; | 174 | unsigned short ch, bitrate; |
177 | unsigned long samplerrate, longdata; | 175 | unsigned long samplerrate, longdata; |
178 | 176 | ||
179 | if (read(fd, string, 4) < 4) { | 177 | if (read(fd, string, 4) < 4) { |
180 | odebug << " Could not read from sound file.\n" << oendl; | 178 | odebug << " Could not read from sound file." << oendl; |
181 | return -1; | 179 | return -1; |
182 | } | 180 | } |
183 | if (strncmp(string, "RIFF", 4)) { | 181 | if (strncmp(string, "RIFF", 4)) { |
184 | odebug << " not a valid WAV file.\n" << oendl; | 182 | odebug << " not a valid WAV file." << oendl; |
185 | return -1; | 183 | return -1; |
186 | } | 184 | } |
187 | lseek(fd, 4, SEEK_CUR); | 185 | lseek(fd, 4, SEEK_CUR); |
188 | if (read(fd, string, 4) < 4) { | 186 | if (read(fd, string, 4) < 4) { |
189 | odebug << "Could not read from sound file.\n" << oendl; | 187 | odebug << "Could not read from sound file." << oendl; |
190 | return -1; | 188 | return -1; |
191 | } | 189 | } |
192 | if (strncmp(string, "WAVE", 4)) { | 190 | if (strncmp(string, "WAVE", 4)) { |
193 | odebug << "not a valid WAV file.\n" << oendl; | 191 | odebug << "not a valid WAV file." << oendl; |
194 | return -1; | 192 | return -1; |
195 | } | 193 | } |
196 | found = 0; | 194 | found = 0; |
197 | 195 | ||
198 | while (!found) { | 196 | while (!found) { |
199 | if (read(fd, string, 4) < 4) { | 197 | if (read(fd, string, 4) < 4) { |
200 | odebug << "Could not read from sound file.\n" << oendl; | 198 | odebug << "Could not read from sound file." << oendl; |
201 | return -1; | 199 | return -1; |
202 | } | 200 | } |
203 | if (strncmp(string, "fmt ", 4)) { | 201 | if (strncmp(string, "fmt ", 4)) { |
204 | if (read(fd, &longdata, 4) < 4) { | 202 | if (read(fd, &longdata, 4) < 4) { |
205 | odebug << "Could not read from sound file.\n" << oendl; | 203 | odebug << "Could not read from sound file." << oendl; |
206 | return -1; | 204 | return -1; |
207 | } | 205 | } |
208 | lseek(fd, longdata, SEEK_CUR); | 206 | lseek(fd, longdata, SEEK_CUR); |
209 | } else { | 207 | } else { |
210 | lseek(fd, 4, SEEK_CUR); | 208 | lseek(fd, 4, SEEK_CUR); |
211 | if (read(fd, &fmt, 2) < 2) { | 209 | if (read(fd, &fmt, 2) < 2) { |
212 | odebug << "Could not read format chunk.\n" << oendl; | 210 | odebug << "Could not read format chunk." << oendl; |
213 | return -1; | 211 | return -1; |
214 | } | 212 | } |
215 | if (fmt != WAVE_FORMAT_PCM && fmt != WAVE_FORMAT_DVI_ADPCM) { | 213 | if (fmt != WAVE_FORMAT_PCM && fmt != WAVE_FORMAT_DVI_ADPCM) { |
216 | odebug << "Wave file contains unknown format. Unable to continue.\n" << oendl; | 214 | odebug << "Wave file contains unknown format. Unable to continue." << oendl; |
217 | return -1; | 215 | return -1; |
218 | } | 216 | } |
219 | wavFormat = fmt; | 217 | wavFormat = fmt; |
220 | // compressionFormat=fmt; | 218 | // compressionFormat=fmt; |
221 | odebug << "compressionFormat is " << fmt << "" << oendl; | 219 | odebug << "compressionFormat is " << fmt << "" << oendl; |
222 | if (read(fd, &ch, 2) < 2) { | 220 | if (read(fd, &ch, 2) < 2) { |
223 | odebug << "Could not read format chunk.\n" << oendl; | 221 | odebug << "Could not read format chunk." << oendl; |
224 | return -1; | 222 | return -1; |
225 | } else { | 223 | } else { |
226 | wavChannels = ch; | 224 | wavChannels = ch; |
227 | odebug << "File has " << ch << " channels" << oendl; | 225 | odebug << "File has " << ch << " channels" << oendl; |
228 | } | 226 | } |
229 | if (read(fd, &samplerrate, 4) < 4) { | 227 | if (read(fd, &samplerrate, 4) < 4) { |
230 | odebug << "Could not read from format chunk.\n" << oendl; | 228 | odebug << "Could not read from format chunk." << oendl; |
231 | return -1; | 229 | return -1; |
232 | } else { | 230 | } else { |
233 | wavSampleRate = samplerrate; | 231 | wavSampleRate = samplerrate; |
234 | // sampleRate = samplerrate; | 232 | // sampleRate = samplerrate; |
235 | odebug << "File has samplerate of " << (int) samplerrate << "" << oendl; | 233 | odebug << "File has samplerate of " << (int) samplerrate << "" << oendl; |
236 | } | 234 | } |
237 | lseek(fd, 6, SEEK_CUR); | 235 | lseek(fd, 6, SEEK_CUR); |
238 | if (read(fd, &bitrate, 2) < 2) { | 236 | if (read(fd, &bitrate, 2) < 2) { |
239 | odebug << "Could not read format chunk.\n" << oendl; | 237 | odebug << "Could not read format chunk." << oendl; |
240 | return -1; | 238 | return -1; |
241 | } else { | 239 | } else { |
242 | wavResolution=bitrate; | 240 | wavResolution=bitrate; |
243 | // resolution = bitrate; | 241 | // resolution = bitrate; |
244 | odebug << "File has bitrate of " << bitrate << "" << oendl; | 242 | odebug << "File has bitrate of " << bitrate << "" << oendl; |
245 | } | 243 | } |
246 | found++; | 244 | found++; |
247 | } | 245 | } |
248 | } | 246 | } |
249 | found = 0; | 247 | found = 0; |
250 | while (!found) { | 248 | while (!found) { |
251 | if (read(fd, string, 4) < 4) { | 249 | if (read(fd, string, 4) < 4) { |
252 | odebug << "Could not read from sound file.\n" << oendl; | 250 | odebug << "Could not read from sound file." << oendl; |
253 | return -1; | 251 | return -1; |
254 | } | 252 | } |
255 | 253 | ||
256 | if (strncmp(string, "data", 4)) { | 254 | if (strncmp(string, "data", 4)) { |
257 | if (read(fd, &longdata, 4)<4) { | 255 | if (read(fd, &longdata, 4)<4) { |
258 | odebug << "Could not read from sound file.\n" << oendl; | 256 | odebug << "Could not read from sound file." << oendl; |
259 | return -1; | 257 | return -1; |
260 | } | 258 | } |
261 | 259 | ||
262 | lseek(fd, longdata, SEEK_CUR); | 260 | lseek(fd, longdata, SEEK_CUR); |
263 | } else { | 261 | } else { |
264 | if (read(fd, &longdata, 4) < 4) { | 262 | if (read(fd, &longdata, 4) < 4) { |
265 | odebug << "Could not read from sound file.\n" << oendl; | 263 | odebug << "Could not read from sound file." << oendl; |
266 | return -1; | 264 | return -1; |
267 | } else { | 265 | } else { |
268 | wavNumberSamples = longdata; | 266 | wavNumberSamples = longdata; |
269 | odebug << "file hase length of " << (int)longdata << "\n" | 267 | odebug << "file hase length of " << (int)longdata << "" |
270 | << "lasting " | 268 | << "lasting " |
271 | << (int)(( longdata / wavSampleRate) / wavChannels) / ( wavChannels*( wavResolution/8)) | 269 | << (int)(( longdata / wavSampleRate) / wavChannels) / ( wavChannels*( wavResolution/8)) |
272 | << " seconds" << oendl; | 270 | << " seconds" << oendl; |
273 | // wavSeconds = (( longdata / wavSampleRate) / wavChannels) / ( wavChannels*( wavResolution/8)); | 271 | // wavSeconds = (( longdata / wavSampleRate) / wavChannels) / ( wavChannels*( wavResolution/8)); |
274 | 272 | ||
275 | return longdata; | 273 | return longdata; |
276 | } | 274 | } |
277 | } | 275 | } |
278 | } | 276 | } |
279 | 277 | ||
280 | lseek(fd, 0, SEEK_SET); | 278 | lseek(fd, 0, SEEK_SET); |
281 | 279 | ||
282 | return 0; | 280 | return 0; |
283 | } | 281 | } |
284 | 282 | ||
285 | QString WavFile::trackName() { | 283 | QString WavFile::trackName() { |
286 | return track.name(); | 284 | return track.name(); |
287 | } | 285 | } |
288 | 286 | ||
289 | int WavFile::wavHandle(){ | 287 | int WavFile::wavHandle(){ |
290 | return track.handle(); | 288 | return track.handle(); |
291 | } | 289 | } |
292 | 290 | ||
293 | int WavFile::getFormat() { | 291 | int WavFile::getFormat() { |
294 | return wavFormat; | 292 | return wavFormat; |
295 | } | 293 | } |
296 | 294 | ||
297 | int WavFile::getResolution() { | 295 | int WavFile::getResolution() { |
298 | return wavResolution; | 296 | return wavResolution; |
299 | } | 297 | } |
300 | 298 | ||
301 | int WavFile::getSampleRate() { | 299 | int WavFile::getSampleRate() { |
302 | return wavSampleRate; | 300 | return wavSampleRate; |
303 | } | 301 | } |
304 | 302 | ||
305 | int WavFile::getNumberSamples() { | 303 | int WavFile::getNumberSamples() { |
306 | return wavNumberSamples; | 304 | return wavNumberSamples; |
307 | } | 305 | } |
308 | 306 | ||
309 | bool WavFile::isTempFile() { | 307 | bool WavFile::isTempFile() { |
310 | return useTmpFile; | 308 | return useTmpFile; |
311 | } | 309 | } |
312 | 310 | ||
313 | int WavFile::getChannels() { | 311 | int WavFile::getChannels() { |
314 | 312 | ||
315 | return wavChannels; | 313 | return wavChannels; |
316 | } | 314 | } |