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