Diffstat (limited to 'core/multimedia/opieplayer/wavplugin') (more/less context) (ignore whitespace changes)
-rw-r--r-- | core/multimedia/opieplayer/wavplugin/wavplugin.cpp | 50 |
1 files changed, 29 insertions, 21 deletions
diff --git a/core/multimedia/opieplayer/wavplugin/wavplugin.cpp b/core/multimedia/opieplayer/wavplugin/wavplugin.cpp index 4e82900..7f3a0a8 100644 --- a/core/multimedia/opieplayer/wavplugin/wavplugin.cpp +++ b/core/multimedia/opieplayer/wavplugin/wavplugin.cpp | |||
@@ -1,52 +1,60 @@ | |||
1 | /********************************************************************** | 1 | /********************************************************************** |
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | 2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. |
3 | ** | 3 | ** |
4 | ** This file is part of Qtopia Environment. | 4 | ** This file is part of Qtopia Environment. |
5 | ** | 5 | ** |
6 | ** This file may be distributed and/or modified under the terms of the | 6 | ** This file may be distributed and/or modified under the terms of the |
7 | ** GNU General Public License version 2 as published by the Free Software | 7 | ** GNU General Public License version 2 as published by the Free Software |
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | 8 | ** Foundation and appearing in the file LICENSE.GPL included in the |
9 | ** packaging of this file. | 9 | ** packaging of this file. |
10 | ** | 10 | ** |
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | 11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | 12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
13 | ** | 13 | ** |
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | 14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. |
15 | ** | 15 | ** |
16 | ** Contact info@trolltech.com if any conditions of this licensing are | 16 | ** Contact info@trolltech.com if any conditions of this licensing are |
17 | ** not clear to you. | 17 | ** not clear to you. |
18 | ** | 18 | ** |
19 | **********************************************************************/ | 19 | **********************************************************************/ |
20 | // L.J.Potter added changes Fri 02-15-2002 | 20 | // L.J.Potter added changes Fri 02-15-2002 |
21 | 21 | ||
22 | |||
23 | #include "wavplugin.h" | ||
24 | |||
25 | /* OPIE */ | ||
26 | #include <opie2/odebug.h> | ||
27 | |||
28 | /* QT */ | ||
29 | #include <qfile.h> | ||
30 | |||
31 | /* STD */ | ||
22 | #include <stdio.h> | 32 | #include <stdio.h> |
23 | #include <stdarg.h> | 33 | #include <stdarg.h> |
24 | #include <stdlib.h> | 34 | #include <stdlib.h> |
25 | #include <errno.h> | 35 | #include <errno.h> |
26 | #include <unistd.h> | 36 | #include <unistd.h> |
27 | #include <qfile.h> | ||
28 | #include "wavplugin.h" | ||
29 | 37 | ||
30 | //#define debugMsg(a) qDebug(a) | 38 | //#define debugMsg(a) qDebug(a) |
31 | #define debugMsg(a) | 39 | #define debugMsg(a) |
32 | 40 | ||
33 | 41 | ||
34 | struct RiffChunk { | 42 | struct RiffChunk { |
35 | char id[4]; | 43 | char id[4]; |
36 | Q_UINT32 size; | 44 | Q_UINT32 size; |
37 | char data[4]; | 45 | char data[4]; |
38 | }; | 46 | }; |
39 | 47 | ||
40 | 48 | ||
41 | struct ChunkData { | 49 | struct ChunkData { |
42 | Q_INT16 formatTag; | 50 | Q_INT16 formatTag; |
43 | Q_INT16 channels; | 51 | Q_INT16 channels; |
44 | Q_INT32 samplesPerSec; | 52 | Q_INT32 samplesPerSec; |
45 | Q_INT32 avgBytesPerSec; | 53 | Q_INT32 avgBytesPerSec; |
46 | Q_INT16 blockAlign; | 54 | Q_INT16 blockAlign; |
47 | Q_INT16 wBitsPerSample; | 55 | Q_INT16 wBitsPerSample; |
48 | }; | 56 | }; |
49 | 57 | ||
50 | 58 | ||
51 | const int sound_buffer_size = 512; // 4096; // you got to be kidding right? | 59 | const int sound_buffer_size = 512; // 4096; // you got to be kidding right? |
52 | 60 | ||
@@ -55,56 +63,56 @@ class WavPluginData { | |||
55 | public: | 63 | public: |
56 | QFile *input; | 64 | QFile *input; |
57 | 65 | ||
58 | int wavedata_remaining; | 66 | int wavedata_remaining; |
59 | ChunkData chunkdata; | 67 | ChunkData chunkdata; |
60 | RiffChunk chunk; | 68 | RiffChunk chunk; |
61 | uchar data[sound_buffer_size+32]; // +32 to handle badly aligned input data | 69 | uchar data[sound_buffer_size+32]; // +32 to handle badly aligned input data |
62 | int out,max; | 70 | int out,max; |
63 | int samples_due; | 71 | int samples_due; |
64 | int samples; | 72 | int samples; |
65 | 73 | ||
66 | WavPluginData() { | 74 | WavPluginData() { |
67 | max = out = sound_buffer_size; | 75 | max = out = sound_buffer_size; |
68 | wavedata_remaining = 0; | 76 | wavedata_remaining = 0; |
69 | samples_due = 0; | 77 | samples_due = 0; |
70 | samples = -1; | 78 | samples = -1; |
71 | } | 79 | } |
72 | 80 | ||
73 | // expands out samples to the frequency of 44kHz //not any more | 81 | // expands out samples to the frequency of 44kHz //not any more |
74 | bool add( short *output, long count, long& done, bool stereo ) | 82 | bool add( short *output, long count, long& done, bool stereo ) |
75 | { | 83 | { |
76 | done = 0; | 84 | done = 0; |
77 | 85 | ||
78 | if ( input == 0 ) { | 86 | if ( input == 0 ) { |
79 | qDebug("no input"); | 87 | odebug << "no input" << oendl; |
80 | return FALSE; | 88 | return FALSE; |
81 | } | 89 | } |
82 | 90 | ||
83 | while ( count ) { | 91 | while ( count ) { |
84 | int l,r; | 92 | int l,r; |
85 | if ( getSample(l, r) == FALSE ) { | 93 | if ( getSample(l, r) == FALSE ) { |
86 | qDebug("didn't get sample"); | 94 | odebug << "didn't get sample" << oendl; |
87 | return FALSE; | 95 | return FALSE; |
88 | } | 96 | } |
89 | samples_due += chunkdata.samplesPerSec; | 97 | samples_due += chunkdata.samplesPerSec; |
90 | printf("samples due %d\r", samples_due); | 98 | printf("samples due %d\r", samples_due); |
91 | fflush(stdout); | 99 | fflush(stdout); |
92 | while ( count && (samples_due > chunkdata.samplesPerSec) ) { | 100 | while ( count && (samples_due > chunkdata.samplesPerSec) ) { |
93 | *output++ = l; | 101 | *output++ = l; |
94 | if ( stereo ) | 102 | if ( stereo ) |
95 | *output++ = r; | 103 | *output++ = r; |
96 | samples_due -= chunkdata.samplesPerSec; | 104 | samples_due -= chunkdata.samplesPerSec; |
97 | count--; | 105 | count--; |
98 | done++; | 106 | done++; |
99 | } | 107 | } |
100 | } | 108 | } |
101 | return TRUE; | 109 | return TRUE; |
102 | } | 110 | } |
103 | 111 | ||
104 | bool initialise() { | 112 | bool initialise() { |
105 | if ( input == 0 ) | 113 | if ( input == 0 ) |
106 | return FALSE; | 114 | return FALSE; |
107 | 115 | ||
108 | wavedata_remaining = -1; | 116 | wavedata_remaining = -1; |
109 | 117 | ||
110 | while ( wavedata_remaining == -1 ) { | 118 | while ( wavedata_remaining == -1 ) { |
@@ -114,59 +122,59 @@ public: | |||
114 | if ( t != n ) { | 122 | if ( t != n ) { |
115 | if ( t == -1 ) | 123 | if ( t == -1 ) |
116 | return FALSE; | 124 | return FALSE; |
117 | return TRUE; | 125 | return TRUE; |
118 | } | 126 | } |
119 | if ( qstrncmp(chunk.id,"data",4) == 0 ) { | 127 | if ( qstrncmp(chunk.id,"data",4) == 0 ) { |
120 | samples = wavedata_remaining = chunk.size; | 128 | samples = wavedata_remaining = chunk.size; |
121 | } else if ( qstrncmp(chunk.id,"RIFF",4) == 0 ) { | 129 | } else if ( qstrncmp(chunk.id,"RIFF",4) == 0 ) { |
122 | char d[4]; | 130 | char d[4]; |
123 | if ( input->readBlock(d,4) != 4 ) { | 131 | if ( input->readBlock(d,4) != 4 ) { |
124 | return FALSE; | 132 | return FALSE; |
125 | } | 133 | } |
126 | if ( qstrncmp(d,"WAVE",4) != 0 ) { | 134 | if ( qstrncmp(d,"WAVE",4) != 0 ) { |
127 | // skip | 135 | // skip |
128 | if ( chunk.size > 1000000000 || !input->at(input->at()+chunk.size-4) ) { | 136 | if ( chunk.size > 1000000000 || !input->at(input->at()+chunk.size-4) ) { |
129 | return FALSE; | 137 | return FALSE; |
130 | } | 138 | } |
131 | } | 139 | } |
132 | } else if ( qstrncmp(chunk.id,"fmt ",4) == 0 ) { | 140 | } else if ( qstrncmp(chunk.id,"fmt ",4) == 0 ) { |
133 | if ( input->readBlock((char*)&chunkdata,sizeof(chunkdata)) != sizeof(chunkdata) ) { | 141 | if ( input->readBlock((char*)&chunkdata,sizeof(chunkdata)) != sizeof(chunkdata) ) { |
134 | return FALSE; | 142 | return FALSE; |
135 | } | 143 | } |
136 | #define WAVE_FORMAT_PCM 1 | 144 | #define WAVE_FORMAT_PCM 1 |
137 | if ( chunkdata.formatTag != WAVE_FORMAT_PCM ) { | 145 | if ( chunkdata.formatTag != WAVE_FORMAT_PCM ) { |
138 | qDebug("WAV file: UNSUPPORTED FORMAT %d",chunkdata.formatTag); | 146 | odebug << "WAV file: UNSUPPORTED FORMAT " << chunkdata.formatTag << "" << oendl; |
139 | return FALSE; | 147 | return FALSE; |
140 | } | 148 | } |
141 | } else { | 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) ) { |
144 | return FALSE; | 152 | return FALSE; |
145 | } | 153 | } |
146 | } | 154 | } |
147 | } // while | 155 | } // while |
148 | qDebug("bits %d", chunkdata.wBitsPerSample); | 156 | odebug << "bits " << chunkdata.wBitsPerSample << "" << oendl; |
149 | return TRUE; | 157 | return TRUE; |
150 | } | 158 | } |
151 | 159 | ||
152 | 160 | ||
153 | // gets a sample from the file | 161 | // gets a sample from the file |
154 | bool getSample(int& l, int& r) | 162 | bool getSample(int& l, int& r) |
155 | { | 163 | { |
156 | l = r = 0; | 164 | l = r = 0; |
157 | 165 | ||
158 | if ( input == 0 ) | 166 | if ( input == 0 ) |
159 | return FALSE; | 167 | return FALSE; |
160 | 168 | ||
161 | if ( (wavedata_remaining < 0) || !max ) | 169 | if ( (wavedata_remaining < 0) || !max ) |
162 | return FALSE; | 170 | return FALSE; |
163 | 171 | ||
164 | if ( out >= max ) { | 172 | if ( out >= max ) { |
165 | max = input->readBlock( (char*)data, (uint)QMIN(sound_buffer_size,wavedata_remaining) ); | 173 | max = input->readBlock( (char*)data, (uint)QMIN(sound_buffer_size,wavedata_remaining) ); |
166 | 174 | ||
167 | wavedata_remaining -= max; | 175 | wavedata_remaining -= max; |
168 | 176 | ||
169 | out = 0; | 177 | out = 0; |
170 | if ( max <= 0 ) { | 178 | if ( max <= 0 ) { |
171 | max = 0; | 179 | max = 0; |
172 | return TRUE; | 180 | return TRUE; |
@@ -186,157 +194,157 @@ public: | |||
186 | } else { | 194 | } else { |
187 | r = ((short*)data)[out/2]; | 195 | r = ((short*)data)[out/2]; |
188 | out += 2; | 196 | out += 2; |
189 | } | 197 | } |
190 | } | 198 | } |
191 | return TRUE; | 199 | return TRUE; |
192 | } // getSample | 200 | } // getSample |
193 | 201 | ||
194 | }; | 202 | }; |
195 | 203 | ||
196 | 204 | ||
197 | WavPlugin::WavPlugin() { | 205 | WavPlugin::WavPlugin() { |
198 | d = new WavPluginData; | 206 | d = new WavPluginData; |
199 | d->input = 0; | 207 | d->input = 0; |
200 | } | 208 | } |
201 | 209 | ||
202 | 210 | ||
203 | WavPlugin::~WavPlugin() { | 211 | WavPlugin::~WavPlugin() { |
204 | close(); | 212 | close(); |
205 | delete d; | 213 | delete d; |
206 | } | 214 | } |
207 | 215 | ||
208 | 216 | ||
209 | bool WavPlugin::isFileSupported( const QString& path ) { | 217 | bool WavPlugin::isFileSupported( const QString& path ) { |
210 | // qDebug( "WavPlugin::isFileSupported" ); | 218 | // odebug << "WavPlugin::isFileSupported" << oendl; |
211 | 219 | ||
212 | char *ext = strrchr( path.latin1(), '.' ); | 220 | char *ext = strrchr( path.latin1(), '.' ); |
213 | 221 | ||
214 | // Test file extension | 222 | // Test file extension |
215 | if ( ext ) { | 223 | if ( ext ) { |
216 | if ( strncasecmp(ext, ".raw", 4) == 0 ) | 224 | if ( strncasecmp(ext, ".raw", 4) == 0 ) |
217 | return TRUE; | 225 | return TRUE; |
218 | if ( strncasecmp(ext, ".wav", 4) == 0 ) | 226 | if ( strncasecmp(ext, ".wav", 4) == 0 ) |
219 | return TRUE; | 227 | return TRUE; |
220 | if ( strncasecmp(ext, ".wave", 4) == 0 ) | 228 | if ( strncasecmp(ext, ".wave", 4) == 0 ) |
221 | return TRUE; | 229 | return TRUE; |
222 | } | 230 | } |
223 | 231 | ||
224 | return FALSE; | 232 | return FALSE; |
225 | } | 233 | } |
226 | 234 | ||
227 | 235 | ||
228 | bool WavPlugin::open( const QString& path ) { | 236 | bool WavPlugin::open( const QString& path ) { |
229 | // qDebug( "WavPlugin::open" ); | 237 | // odebug << "WavPlugin::open" << oendl; |
230 | 238 | ||
231 | d->max = d->out = sound_buffer_size; | 239 | d->max = d->out = sound_buffer_size; |
232 | d->wavedata_remaining = 0; | 240 | d->wavedata_remaining = 0; |
233 | d->samples_due = 0; | 241 | d->samples_due = 0; |
234 | 242 | ||
235 | d->input = new QFile( path ); | 243 | d->input = new QFile( path ); |
236 | if ( d->input->open(IO_ReadOnly) == FALSE ) { | 244 | if ( d->input->open(IO_ReadOnly) == FALSE ) { |
237 | qDebug("couldn't open file"); | 245 | odebug << "couldn't open file" << oendl; |
238 | delete d->input; | 246 | delete d->input; |
239 | d->input = 0; | 247 | d->input = 0; |
240 | return FALSE; | 248 | return FALSE; |
241 | } | 249 | } |
242 | 250 | ||
243 | d->initialise(); | 251 | d->initialise(); |
244 | qApp->processEvents(); | 252 | qApp->processEvents(); |
245 | 253 | ||
246 | return TRUE; | 254 | return TRUE; |
247 | } | 255 | } |
248 | 256 | ||
249 | 257 | ||
250 | bool WavPlugin::close() { | 258 | bool WavPlugin::close() { |
251 | // qDebug( "WavPlugin::close" ); | 259 | // odebug << "WavPlugin::close" << oendl; |
252 | 260 | ||
253 | d->input->close(); | 261 | d->input->close(); |
254 | delete d->input; | 262 | delete d->input; |
255 | d->input = 0; | 263 | d->input = 0; |
256 | return TRUE; | 264 | return TRUE; |
257 | } | 265 | } |
258 | 266 | ||
259 | 267 | ||
260 | bool WavPlugin::isOpen() { | 268 | bool WavPlugin::isOpen() { |
261 | // qDebug( "WavPlugin::isOpen" ); | 269 | // odebug << "WavPlugin::isOpen" << oendl; |
262 | return ( d->input != 0 ); | 270 | return ( d->input != 0 ); |
263 | } | 271 | } |
264 | 272 | ||
265 | 273 | ||
266 | int WavPlugin::audioStreams() { | 274 | int WavPlugin::audioStreams() { |
267 | // qDebug( "WavPlugin::audioStreams" ); | 275 | // odebug << "WavPlugin::audioStreams" << oendl; |
268 | return 1; | 276 | return 1; |
269 | } | 277 | } |
270 | 278 | ||
271 | 279 | ||
272 | int WavPlugin::audioChannels( int ) { | 280 | int WavPlugin::audioChannels( int ) { |
273 | // qDebug( "WavPlugin::audioChannels" ); | 281 | // odebug << "WavPlugin::audioChannels" << oendl; |
274 | return d->chunkdata.channels;// 2; // ### Always scale audio to stereo samples | 282 | return d->chunkdata.channels;// 2; // ### Always scale audio to stereo samples |
275 | } | 283 | } |
276 | 284 | ||
277 | 285 | ||
278 | int WavPlugin::audioFrequency( int ) { | 286 | int WavPlugin::audioFrequency( int ) { |
279 | // qDebug( "WavPlugin::audioFrequency %d", d->chunkdata.samplesPerSec ); | 287 | // odebug << "WavPlugin::audioFrequency " << d->chunkdata.samplesPerSec << "" << oendl; |
280 | return d->chunkdata.samplesPerSec; //44100; // ### Always scale to frequency of 44100 | 288 | return d->chunkdata.samplesPerSec; //44100; // ### Always scale to frequency of 44100 |
281 | } | 289 | } |
282 | 290 | ||
283 | 291 | ||
284 | int WavPlugin::audioSamples( int ) { | 292 | int WavPlugin::audioSamples( int ) { |
285 | // qDebug( "WavPlugin::audioSamples" ); | 293 | // odebug << "WavPlugin::audioSamples" << oendl; |
286 | return d->samples / d->chunkdata.channels/2; // ### Scaled samples will be made stereo, | 294 | return d->samples / d->chunkdata.channels/2; // ### Scaled samples will be made stereo, |
287 | // Therefore if source is mono we will double the number of samples | 295 | // Therefore if source is mono we will double the number of samples |
288 | } | 296 | } |
289 | 297 | ||
290 | 298 | ||
291 | bool WavPlugin::audioSetSample( long, int ) { | 299 | bool WavPlugin::audioSetSample( long, int ) { |
292 | // qDebug( "WavPlugin::audioSetSample" ); | 300 | // odebug << "WavPlugin::audioSetSample" << oendl; |
293 | return FALSE; | 301 | return FALSE; |
294 | } | 302 | } |
295 | 303 | ||
296 | 304 | ||
297 | long WavPlugin::audioGetSample( int ) { | 305 | long WavPlugin::audioGetSample( int ) { |
298 | // qDebug( "WavPlugin::audioGetSample" ); | 306 | // odebug << "WavPlugin::audioGetSample" << oendl; |
299 | return 0; | 307 | return 0; |
300 | } | 308 | } |
301 | 309 | ||
302 | /* | 310 | /* |
303 | bool WavPlugin::audioReadSamples( short *, int, long, int ) { | 311 | bool WavPlugin::audioReadSamples( short *, int, long, int ) { |
304 | debugMsg( "WavPlugin::audioReadSamples" ); | 312 | debugMsg( "WavPlugin::audioReadSamples" ); |
305 | return FALSE; | 313 | return FALSE; |
306 | } | 314 | } |
307 | 315 | ||
308 | 316 | ||
309 | bool WavPlugin::audioReReadSamples( short *, int, long, int ) { | 317 | bool WavPlugin::audioReReadSamples( short *, int, long, int ) { |
310 | debugMsg( "WavPlugin::audioReReadSamples" ); | 318 | debugMsg( "WavPlugin::audioReReadSamples" ); |
311 | return FALSE; | 319 | return FALSE; |
312 | } | 320 | } |
313 | 321 | ||
314 | 322 | ||
315 | bool WavPlugin::audioReadMonoSamples( short *output, long samples, long& samplesMade, int ) { | 323 | bool WavPlugin::audioReadMonoSamples( short *output, long samples, long& samplesMade, int ) { |
316 | debugMsg( "WavPlugin::audioReadMonoSamples" ); | 324 | debugMsg( "WavPlugin::audioReadMonoSamples" ); |
317 | return !d->add( output, samples, samplesMade, FALSE ); | 325 | return !d->add( output, samples, samplesMade, FALSE ); |
318 | } | 326 | } |
319 | 327 | ||
320 | 328 | ||
321 | bool WavPlugin::audioReadStereoSamples( short *output, long samples, long& samplesMade, int ) { | 329 | bool WavPlugin::audioReadStereoSamples( short *output, long samples, long& samplesMade, int ) { |
322 | debugMsg( "WavPlugin::audioReadStereoSamples" ); | 330 | debugMsg( "WavPlugin::audioReadStereoSamples" ); |
323 | return !d->add( output, samples, samplesMade, TRUE ); | 331 | return !d->add( output, samples, samplesMade, TRUE ); |
324 | } | 332 | } |
325 | */ | 333 | */ |
326 | 334 | ||
327 | bool WavPlugin::audioReadSamples( short *output, int channels, long samples, long& samplesMade, int ) { | 335 | bool WavPlugin::audioReadSamples( short *output, int channels, long samples, long& samplesMade, int ) { |
328 | // qDebug( "WavPlugin::audioReadSamples" ); | 336 | // odebug << "WavPlugin::audioReadSamples" << oendl; |
329 | return d->add( output, samples, samplesMade, channels != 1 ); | 337 | return d->add( output, samples, samplesMade, channels != 1 ); |
330 | } | 338 | } |
331 | 339 | ||
332 | double WavPlugin::getTime() { | 340 | double WavPlugin::getTime() { |
333 | // qDebug( "WavPlugin::getTime" ); //this is a stupid hack here!! | 341 | // odebug << "WavPlugin::getTime" << oendl; //this is a stupid hack here!! |
334 | return d->chunkdata.wBitsPerSample; /*0.0*/; | 342 | return d->chunkdata.wBitsPerSample; /*0.0*/; |
335 | } | 343 | } |
336 | 344 | ||
337 | // int WavPlugin::audioBitsPerSample( int ) { | 345 | // int WavPlugin::audioBitsPerSample( int ) { |
338 | // // qDebug( "WavPlugin::audioFormat %d", d->chunkdata.wBitsPerSample ); | 346 | // // odebug << "WavPlugin::audioFormat " << d->chunkdata.wBitsPerSample << "" << oendl; |
339 | // return d->chunkdata.wBitsPerSample; // | 347 | // return d->chunkdata.wBitsPerSample; // |
340 | // } | 348 | // } |
341 | 349 | ||
342 | 350 | ||