summaryrefslogtreecommitdiff
path: root/core/multimedia/opieplayer/wavplugin/wavplugin.cpp
Unidiff
Diffstat (limited to 'core/multimedia/opieplayer/wavplugin/wavplugin.cpp') (more/less context) (show whitespace changes)
-rw-r--r--core/multimedia/opieplayer/wavplugin/wavplugin.cpp217
1 files changed, 185 insertions, 32 deletions
diff --git a/core/multimedia/opieplayer/wavplugin/wavplugin.cpp b/core/multimedia/opieplayer/wavplugin/wavplugin.cpp
index 7f63b3b..0be667f 100644
--- a/core/multimedia/opieplayer/wavplugin/wavplugin.cpp
+++ b/core/multimedia/opieplayer/wavplugin/wavplugin.cpp
@@ -18,6 +18,4 @@
18** 18**
19**********************************************************************/ 19**********************************************************************/
20// L.J.Potter added changes Fri 02-15-2002
21
22#include <stdio.h> 20#include <stdio.h>
23#include <stdarg.h> 21#include <stdarg.h>
@@ -28,5 +26,4 @@
28#include "wavplugin.h" 26#include "wavplugin.h"
29 27
30
31//#define debugMsg(a) qDebug(a) 28//#define debugMsg(a) qDebug(a)
32#define debugMsg(a) 29#define debugMsg(a)
@@ -50,5 +47,5 @@ struct ChunkData {
50 47
51 48
52const int sound_buffer_size = 512; // 4096; // you got to be kidding right? 49const int sound_buffer_size = 4096;
53 50
54 51
@@ -64,4 +61,8 @@ public:
64 int samples_due; 61 int samples_due;
65 int samples; 62 int samples;
63 int freq;
64 int chan;
65 int sampleRate;
66 int resolution;
66 67
67 WavPluginData() { 68 WavPluginData() {
@@ -75,6 +76,6 @@ public:
75 bool add( short *output, long count, long& done, bool stereo ) 76 bool add( short *output, long count, long& done, bool stereo )
76 { 77 {
78 qDebug("add");
77 done = 0; 79 done = 0;
78 qApp->processEvents();
79 80
80 if ( input == 0 ) { 81 if ( input == 0 ) {
@@ -89,5 +90,5 @@ public:
89 return FALSE; 90 return FALSE;
90 } 91 }
91 samples_due += chunkdata.samplesPerSec; 92 samples_due += 44100;
92 while ( count && (samples_due > chunkdata.samplesPerSec) ) { 93 while ( count && (samples_due > chunkdata.samplesPerSec) ) {
93 *output++ = l; 94 *output++ = l;
@@ -99,8 +100,10 @@ public:
99 } 100 }
100 } 101 }
102
101 return TRUE; 103 return TRUE;
102 } 104 }
103 105
104 bool initialise() { 106 bool initialise() {
107 qDebug("initialize");
105 if ( input == 0 ) 108 if ( input == 0 )
106 return FALSE; 109 return FALSE;
@@ -139,5 +142,10 @@ public:
139 return FALSE; 142 return FALSE;
140 } 143 }
141 } else { 144 }
145
146
147
148
149 else {
142 // ignored chunk 150 // ignored chunk
143 if ( chunk.size > 1000000000 || !input->at(input->at()+chunk.size) ) { 151 if ( chunk.size > 1000000000 || !input->at(input->at()+chunk.size) ) {
@@ -146,5 +154,5 @@ public:
146 } 154 }
147 } // while 155 } // while
148 qDebug("bits %d", chunkdata.wBitsPerSample); 156
149 return TRUE; 157 return TRUE;
150 } 158 }
@@ -173,5 +181,5 @@ public:
173 } 181 }
174 } 182 }
175 if ( chunkdata.wBitsPerSample == 8 ) { 183 if ( resolution == 8 ) {
176 l = (data[out++] - 128) * 128; 184 l = (data[out++] - 128) * 128;
177 } else { 185 } else {
@@ -179,8 +187,8 @@ public:
179 out += 2; 187 out += 2;
180 } 188 }
181 if ( chunkdata.channels == 1 ) { 189 if ( chan == 1 ) {
182 r = l; 190 r = l;
183 } else { 191 } else {
184 if ( chunkdata.wBitsPerSample == 8 ) { 192 if ( resolution == 8 ) {
185 r = (data[out++] - 128) * 128; 193 r = (data[out++] - 128) * 128;
186 } else { 194 } else {
@@ -192,4 +200,152 @@ public:
192 } // getSample 200 } // getSample
193 201
202//////////////////////////////////////////////////////
203int getWavSettings(int fd)
204{ //this came from wmrecord
205
206 char t1[4];
207 unsigned long l1;
208 int found;
209 short fmt;
210 unsigned short ch, brate;
211 unsigned long srate;
212
213
214 /* First read in the RIFF identifier. If this is missing then the
215 * file is not a valid WAVE file.
216 */
217 if (read(fd, t1, 4)<4) {
218 qDebug(" Could not read from sound file.\n");
219 return -1;
220 }
221 if (strncmp(t1, "RIFF", 4)) {
222 qDebug(" not a valid WAV file.\n");
223 return -1;
224 }
225 /* Advance the file pointer to the next relevant field. */
226 lseek(fd, 4, SEEK_CUR);
227 /* Read in the WAVE identifier. */
228 if (read(fd, t1, 4)<4) {
229 qDebug("Could not read from sound file.\n");
230 return -1;
231 }
232 if (strncmp(t1, "WAVE", 4)) {
233 qDebug("not a valid WAV file.\n");
234 return -1;
235 }
236
237 /* Search through the file for the format chunk. If the end of the
238 * file is reached without finding the chunk, then the file is not a
239 * valid WAVE file.
240 */
241 found = 0;
242 while (!found) {
243 if (read(fd, t1, 4)<4) {
244 qDebug("Could not read from sound file.\n");
245 return -1;
246 }
247 if (strncmp(t1, "fmt ", 4)) {
248 /* Determine the length of the chunk found and skip to the next
249 * chunk. The chunk length is always stored in the four bytes
250 * following the chunk id.
251 */
252 if (read(fd, &l1, 4)<4) {
253 qDebug("Could not read from sound file.\n");
254 return -1;
255 }
256 lseek(fd, l1, SEEK_CUR);
257 }
258 else {
259 /* This is the format chunk, which stores the playback settings
260 * for the recording.
261 */
262 /* Skip the length field, since we don't really need it. */
263 lseek(fd, 4, SEEK_CUR);
264 /* Read in the format tag. If it has a value of 1, then there is
265 * no compression and we can attempt to play the file
266 * back. Otherwise, return.
267 */
268 if (read(fd, &fmt, 2)<2) {
269 qDebug("Could not read from format chunk.\n");
270 return -1;
271 }
272 if (fmt != 1) {
273 qDebug("Wave file contains compressed data."
274 " Unable to continue.\n");
275 return -1;
276 }
277 /* Get the stereo mode. */
278 if (read(fd, &ch, 2)<2) {
279 qDebug("Could not read from format chunk.\n");
280 return -1;
281 }
282 else {
283 chan = ch;
284 qDebug("File has %d channels", chan);
285 }
286 /* Get the sample rate. */
287 if (read(fd, &srate, 4)<4) {
288 qDebug("Could not read from format chunk.\n");
289 return -1;
290 }
291 else {
292 sampleRate = srate;
293 qDebug("File has samplerate of %d", sampleRate);
294 }
295 /* Get the bit rate. This is at the end of the format chunk. */
296 lseek(fd, 6, SEEK_CUR);
297 if (read(fd, &brate, 2)<2) {
298 qDebug("Could not read from format chunk.\n");
299 return -1;
300 }
301 else {
302 resolution = brate;
303 qDebug("File has bitrate of %d", resolution);
304 }
305
306 found++;
307 }
308 }
309
310 /* Search through the file for the data chunk. If the end of the
311 * file is reached without finding the chunk, then the file is not a
312 * valid WAVE file.
313 */
314 found = 0;
315 while (!found) {
316 if (read(fd, t1, 4)<4) {
317 qDebug("Could not read from sound file.\n");
318 return -1;
319 }
320 if (strncmp(t1, "data", 4)) {
321 /* Determine the length of the chunk found and skip to the next
322 * chunk. The chunk length is always stored in the four bytes
323 * following the chunk id.
324 */
325 if (read(fd, &l1, 4)<4) {
326 qDebug("Could not read from sound file.\n");
327 return -1;
328 }
329 lseek(fd, l1, SEEK_CUR);
330 }
331 else {
332 /* This is the data chunk, which stores the recording. */
333 /* Get the length field. */
334 if (read(fd, &l1, 4)<4) {
335 qDebug("Could not read from sound file.\n");
336 return -1;
337 }
338 else {
339 samples =l1;
340 qDebug("file has length of %d\nlasting %d seconds",l1, (( l1 / sampleRate) / chan) / 2 ); // ????
341 return l1;
342 }
343 }
344 }
345
346 return 0;
347}
348
349//////////////////////////////////////////////////
194}; 350};
195 351
@@ -208,5 +364,5 @@ WavPlugin::~WavPlugin() {
208 364
209bool WavPlugin::isFileSupported( const QString& path ) { 365bool WavPlugin::isFileSupported( const QString& path ) {
210// qDebug( "WavPlugin::isFileSupported" ); 366 debugMsg( "WavPlugin::isFileSupported" );
211 367
212 char *ext = strrchr( path.latin1(), '.' ); 368 char *ext = strrchr( path.latin1(), '.' );
@@ -227,5 +383,5 @@ bool WavPlugin::isFileSupported( const QString& path ) {
227 383
228bool WavPlugin::open( const QString& path ) { 384bool WavPlugin::open( const QString& path ) {
229// qDebug( "WavPlugin::open" ); 385 qDebug( "WavPlugin::open" );
230 386
231 d->max = d->out = sound_buffer_size; 387 d->max = d->out = sound_buffer_size;
@@ -241,4 +397,5 @@ bool WavPlugin::open( const QString& path ) {
241 } 397 }
242 398
399// d->getWavSettings( d->input.handle());
243 d->initialise(); 400 d->initialise();
244 401
@@ -248,5 +405,5 @@ bool WavPlugin::open( const QString& path ) {
248 405
249bool WavPlugin::close() { 406bool WavPlugin::close() {
250// qDebug( "WavPlugin::close" ); 407 qDebug( "WavPlugin::close" );
251 408
252 d->input->close(); 409 d->input->close();
@@ -258,5 +415,5 @@ bool WavPlugin::close() {
258 415
259bool WavPlugin::isOpen() { 416bool WavPlugin::isOpen() {
260// qDebug( "WavPlugin::isOpen" ); 417 qDebug( "WavPlugin::isOpen" );
261 return ( d->input != 0 ); 418 return ( d->input != 0 );
262} 419}
@@ -264,5 +421,5 @@ bool WavPlugin::isOpen() {
264 421
265int WavPlugin::audioStreams() { 422int WavPlugin::audioStreams() {
266// qDebug( "WavPlugin::audioStreams" ); 423 qDebug( "WavPlugin::audioStreams" );
267 return 1; 424 return 1;
268} 425}
@@ -270,24 +427,25 @@ int WavPlugin::audioStreams() {
270 427
271int WavPlugin::audioChannels( int ) { 428int WavPlugin::audioChannels( int ) {
272// qDebug( "WavPlugin::audioChannels" ); 429 debugMsg( "WavPlugin::audioChannels" );
273 return d->chunkdata.channels;// 2; // ### Always scale audio to stereo samples 430 return d->chan;
274} 431}
275 432
276 433
277int WavPlugin::audioFrequency( int ) { 434int WavPlugin::audioFrequency( int ) {
278// qDebug( "WavPlugin::audioFrequency %d", d->chunkdata.samplesPerSec ); 435 qDebug( "WavPlugin::audioFrequency" );
279 return d->chunkdata.samplesPerSec; //44100; // ### Always scale to frequency of 44100 436 return d->freq;
280} 437}
281 438
282 439
283int WavPlugin::audioSamples( int ) { 440int WavPlugin::audioSamples( int ) {
284// qDebug( "WavPlugin::audioSamples" ); 441 qDebug( "WavPlugin::audioSamples" );
285 return d->samples / d->chunkdata.channels/2; // ### Scaled samples will be made stereo, 442 return d->samples;
286 // Therefore if source is mono we will double the number of samples 443// return d->samples * 2 / d->chunkdata.channels; // ### Scaled samples will be made stereo,
444// // Therefore if source is mono we will double the number of samples
287} 445}
288 446
289 447
290bool WavPlugin::audioSetSample( long, int ) { 448bool WavPlugin::audioSetSample( long, int ) {
291// qDebug( "WavPlugin::audioSetSample" ); 449 qDebug( "WavPlugin::audioSetSample" );
292 return FALSE; 450 return FALSE;
293} 451}
@@ -295,5 +453,5 @@ bool WavPlugin::audioSetSample( long, int ) {
295 453
296long WavPlugin::audioGetSample( int ) { 454long WavPlugin::audioGetSample( int ) {
297// qDebug( "WavPlugin::audioGetSample" ); 455 qDebug( "WavPlugin::audioGetSample" );
298 return 0; 456 return 0;
299} 457}
@@ -325,17 +483,12 @@ bool WavPlugin::audioReadStereoSamples( short *output, long samples, long& sampl
325 483
326bool WavPlugin::audioReadSamples( short *output, int channels, long samples, long& samplesMade, int ) { 484bool WavPlugin::audioReadSamples( short *output, int channels, long samples, long& samplesMade, int ) {
327// qDebug( "WavPlugin::audioReadSamples" ); 485 qDebug( "WavPlugin::audioReadSamples" );
328 return d->add( output, samples, samplesMade, channels != 1 ); 486 return d->add( output, samples, samplesMade, channels != 1 );
329} 487}
330 488
331double WavPlugin::getTime() { 489double WavPlugin::getTime() {
332// qDebug( "WavPlugin::getTime" ); 490 qDebug( "WavPlugin::getTime" );
333 return 0.0; 491 return 0.0;
334} 492}
335 493
336int WavPlugin::audioBitsPerSample( int ) {
337// qDebug( "WavPlugin::audioFormat %d", d->chunkdata.wBitsPerSample );
338 return d->chunkdata.wBitsPerSample; //
339}
340
341 494