Debian Codegraph @

engine/sound/wav.cpp

raw
1 // ____ _ __
2 // / __ )____ _____ | | / /___ ___________
3 // / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/
4 // / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ )
5 // /_____/\____/____/ |__/|__/\__,_/_/ /____/
6 //
7 // A futuristic real-time strategy game.
8 // This file is part of Bos Wars.
9 //
10 /**@name wav.cpp - wav support */
11 //
12 // (c) Copyright 2003-2008 by Lutz Sammer, Fabrice Rossi and Nehal Mistry
13 //
14 // This program is free software; you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation; only version 2 of the License.
17 //
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 // 02111-1307, USA.
27 //
28
29 //@{
30
31 /*----------------------------------------------------------------------------
32 -- Includes
33 ----------------------------------------------------------------------------*/
34
35 #include "stratagus.h"
36
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <string.h>
40
41 #include "SDL.h"
42 #include "SDL_endian.h"
43
44 #include "iolib.h"
45 #include "sound_server.h"
46 #include "wav.h"
47
48 /*----------------------------------------------------------------------------
49 -- Declaration
50 ----------------------------------------------------------------------------*/
51
52 /**
53 ** Private wav data structure to handle wav streaming.
54 */
55 struct WavData {
56 CFile *WavFile; /// Wav file handle
57 int ChunkRem; /// Bytes remaining in chunk
58 };
59
60 class CSampleWav : public CSample
61 {
62 public:
63 ~CSampleWav();
64 int Read(void *buf, int len);
65
66 WavData Data;
67 };
68
69 class CSampleWavStream : public CSample
70 {
71 public:
72 ~CSampleWavStream();
73 int Read(void *buf, int len);
74
75 WavData Data;
76 };
77
78 /*----------------------------------------------------------------------------
79 -- Functions
80 ----------------------------------------------------------------------------*/
81
82 int CSampleWavStream::Read(void *buf, int len)
83 {
84 WavChunk chunk;
85 unsigned char *sndbuf;
86 int comp; // number of compressed bytes actually read
87 int i;
88 int read;
89 int bufrem;
90
91 if (this->Pos > SOUND_BUFFER_SIZE / 2) {
92 memcpy(this->Buffer, this->Buffer + this->Pos, this->Len);
93 this->Pos = 0;
94 }
95
96 while (this->Len < SOUND_BUFFER_SIZE / 4) {
97 if (!this->Data.ChunkRem) {
98 // read next chunk
99 comp = this->Data.WavFile->read(&chunk, sizeof(chunk));
100
101 if (!comp) {
102 // EOF
103 this->Data.ChunkRem = 0;
104 break;
105 }
106
107 chunk.Magic = SDL_SwapLE32(chunk.Magic);
108 chunk.Length = SDL_SwapLE32(chunk.Length);
109 if (chunk.Magic != DATA) {
110 this->Data.WavFile->seek(chunk.Length, SEEK_CUR);
111 continue;
112 }
113 this->Data.ChunkRem = chunk.Length;
114 }
115
116 bufrem = SOUND_BUFFER_SIZE - (this->Pos + this->Len);
117 if (this->Data.ChunkRem > bufrem) {
118 read = bufrem;
119 } else {
120 read = this->Data.ChunkRem;
121 }
122 this->Data.ChunkRem -= read;
123
124 sndbuf = this->Buffer + this->Pos + this->Len;
125
126 comp = this->Data.WavFile->read(sndbuf, read);
127 if (!comp) {
128 break;
129 }
130
131 read >>= 1;
132 for (i = 0; i < read; ++i) {
133 ((unsigned short *)sndbuf)[i] = SDL_SwapLE16(((unsigned short *)sndbuf)[i]);
134 }
135
136 this->Len += comp;
137 }
138
139 if (this->Len < len) {
140 len = this->Len;
141 }
142
143 memcpy(buf, this->Buffer + this->Pos, len);
144 this->Pos += len;
145 this->Len -= len;
146
147 return len;
148 }
149
150 CSampleWavStream::~CSampleWavStream()
151 {
152 this->Data.WavFile->close();
153 delete this->Data.WavFile;
154 delete[] this->Buffer;
155 }
156
157 int CSampleWav::Read(void *buf, int len)
158 {
159 if (len > this->Len) {
160 len = this->Len;
161 }
162
163 memcpy(buf, this->Buffer + this->Pos, len);
164 this->Pos += len;
165 this->Len -= len;
166
167 return len;
168 }
169
170 CSampleWav::~CSampleWav()
171 {
172 delete[] this->Buffer;
173 }
174
175
176 /**
177 ** Load wav.
178 **
179 ** @param name File name.
180 ** @param flags Load flags.
181 **
182 ** @return Returns the loaded sample.
183 **
184 ** @todo Add ADPCM loading support!
185 */
186 CSample *LoadWav(const std::string &name, int flags)
187 {
188 CSample *sample;
189 WavData *data;
190 CFile *f;
191 WavChunk chunk;
192 WavFMT wavfmt;
193 unsigned int t;
194
195 f = new CFile;
196 if (f->open(name.c_str(), CL_OPEN_READ) == -1) {
197 printf("Can't open file `%s'\n", name.c_str());
198 delete f;
199 return NULL;
200 }
201 f->read(&chunk, sizeof(chunk));
202
203 // Convert to native format
204
205 chunk.Magic = SDL_SwapLE32(chunk.Magic);
206 chunk.Length = SDL_SwapLE32(chunk.Length);
207
208 if (chunk.Magic != RIFF) {
209 f->close();
210 delete f;
211 return NULL;
212 }
213
214 f->read(&t, sizeof(t));
215 t = SDL_SwapLE32(t);
216 if (t != WAVE) {
217 printf("Wrong magic %x (not %x)\n", t, WAVE);
218 f->close();
219 delete f;
220 return NULL;
221 }
222
223 f->read(&wavfmt, sizeof(wavfmt));
224
225 // Convert to native format
226
227 wavfmt.FMTchunk = SDL_SwapLE32(wavfmt.FMTchunk);
228 wavfmt.FMTlength = SDL_SwapLE32(wavfmt.FMTlength);
229 wavfmt.Encoding = SDL_SwapLE16(wavfmt.Encoding);
230 wavfmt.Channels = SDL_SwapLE16(wavfmt.Channels);
231 wavfmt.Frequency = SDL_SwapLE32(wavfmt.Frequency);
232 wavfmt.ByteRate = SDL_SwapLE32(wavfmt.ByteRate);
233 wavfmt.SampleSize = SDL_SwapLE16(wavfmt.SampleSize);
234 wavfmt.BitsPerSample = SDL_SwapLE16(wavfmt.BitsPerSample);
235
236 if (wavfmt.FMTchunk != FMT) {
237 printf("Wrong magic %x (not %x)\n", wavfmt.FMTchunk, FMT);
238 f->close();
239 delete f;
240 return NULL;
241 }
242 if (wavfmt.FMTlength != 16 && wavfmt.FMTlength != 18) {
243 printf("Wrong length %d (not %d)\n", wavfmt.FMTlength, 16);
244 f->close();
245 delete f;
246 return NULL;
247 }
248
249 if (wavfmt.FMTlength == 18) {
250 if (f->read(&chunk, 2) != 2) {
251 f->close();
252 delete f;
253 return NULL;
254 }
255 }
256
257 //
258 // Check if supported
259 //
260 if (wavfmt.Encoding != WAV_PCM_CODE) {
261 printf("Unsupported encoding %d\n", wavfmt.Encoding);
262 f->close();
263 delete f;
264 return NULL;
265 }
266 if (wavfmt.Channels != WAV_MONO && wavfmt.Channels != WAV_STEREO) {
267 printf("Unsupported channels %d\n", wavfmt.Channels);
268 f->close();
269 delete f;
270 return NULL;
271 }
272 if (wavfmt.SampleSize != 1 && wavfmt.SampleSize != 2 && wavfmt.SampleSize != 4) {
273 printf("Unsupported sample size %d\n", wavfmt.SampleSize);
274 f->close();
275 delete f;
276 return NULL;
277 }
278 if (wavfmt.BitsPerSample != 8 && wavfmt.BitsPerSample != 16) {
279 printf("Unsupported bits per sample %d\n", wavfmt.BitsPerSample);
280 f->close();
281 delete f;
282 return NULL;
283 }
284 Assert(wavfmt.Frequency == 44100 || wavfmt.Frequency == 22050 ||
285 wavfmt.Frequency == 11025);
286
287 //
288 // Read sample
289 //
290 if (flags & PlayAudioStream) {
291 sample = new CSampleWavStream;
292 ((CSampleWavStream *)sample)->Data.WavFile = f;
293 data = &((CSampleWavStream *)sample)->Data;
294 } else {
295 sample = new CSampleWav;
296 ((CSampleWav *)sample)->Data.WavFile = f;
297 data = &((CSampleWav *)sample)->Data;
298 }
299 sample->Channels = (unsigned char)wavfmt.Channels;
300 sample->SampleSize = wavfmt.SampleSize * 8 / sample->Channels;
301 sample->Frequency = wavfmt.Frequency;
302 sample->BitsPerSample = wavfmt.BitsPerSample;
303 sample->Len = 0;
304 sample->Pos = 0;
305
306 if (flags & PlayAudioStream) {
307 data->ChunkRem = 0;
308 sample->Buffer = new unsigned char[SOUND_BUFFER_SIZE];
309 } else {
310 int comp; // number of compressed bytes actually read
311 int i;
312 int rem;
313 int read;
314 int bufrem;
315 char *sndbuf = new char[SOUND_BUFFER_SIZE];
316
317 sample->Buffer = NULL;
318 read = 0;
319 rem = 0;
320 while (1) {
321 if (!rem) {
322 // read next chunk
323 comp = f->read(&chunk, sizeof(chunk));
324
325 if (!comp) {
326 // EOF
327 break;
328 }
329
330 chunk.Magic = SDL_SwapLE32(chunk.Magic);
331 chunk.Length = SDL_SwapLE32(chunk.Length);
332 if (chunk.Magic != DATA) {
333 f->seek(chunk.Length, SEEK_CUR);
334 continue;
335 }
336 rem = chunk.Length;
337 }
338
339 bufrem = SOUND_BUFFER_SIZE;
340 if (rem > bufrem) {
341 read = bufrem;
342 } else {
343 read = rem;
344 }
345 rem -= read;
346
347 unsigned char *b = new unsigned char[sample->Len + read];
348 Assert(b);
349 memcpy(b, sample->Buffer, sample->Len);
350 delete[] sample->Buffer;
351 sample->Buffer = b;
352
353 comp = data->WavFile->read(sndbuf, read);
354 Assert(comp == read);
355
356 if (sample->SampleSize == 16) {
357 read >>= 1;
358 for (i = 0; i < read; ++i) {
359 ((unsigned short *)(sample->Buffer + sample->Pos + sample->Len))[i] =
360 SDL_SwapLE16(((unsigned short *)sndbuf)[i]);
361 }
362 } else {
363 memcpy((sample->Buffer + sample->Pos + sample->Len), sndbuf, comp);
364 }
365
366 sample->Len += comp;
367 }
368
369 delete[] sndbuf;
370
371 data->WavFile->close();
372 delete data->WavFile;
373 }
374
375 return sample;
376 }
377
378 //@}