1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* $URL$
* $Id$
*
*/
#include "common/debug.h"
#include "common/endian.h"
#include "common/file.h"
#include "common/queue.h"
#include "common/util.h"
#include "sound/audiostream.h"
#include "sound/decoders/flac.h"
#include "sound/mixer.h"
#include "sound/decoders/mp3.h"
#include "sound/decoders/raw.h"
#include "sound/decoders/vorbis.h"
namespace Audio {
struct StreamFileFormat {
/** Decodername */
const char *decoderName;
const char *fileExtension;
/**
* Pointer to a function which tries to open a file of type StreamFormat.
* Return NULL in case of an error (invalid/nonexisting file).
*/
SeekableAudioStream *(*openStreamFile)(Common::SeekableReadStream *stream, DisposeAfterUse::Flag disposeAfterUse);
};
static const StreamFileFormat STREAM_FILEFORMATS[] = {
/* decoderName, fileExt, openStreamFuntion */
#ifdef USE_FLAC
{ "FLAC", ".flac", makeFLACStream },
{ "FLAC", ".fla", makeFLACStream },
#endif
#ifdef USE_VORBIS
{ "Ogg Vorbis", ".ogg", makeVorbisStream },
#endif
#ifdef USE_MAD
{ "MPEG Layer 3", ".mp3", makeMP3Stream },
#endif
{ NULL, NULL, NULL } // Terminator
};
SeekableAudioStream *SeekableAudioStream::openStreamFile(const Common::String &basename) {
SeekableAudioStream *stream = NULL;
Common::File *fileHandle = new Common::File();
for (int i = 0; i < ARRAYSIZE(STREAM_FILEFORMATS)-1 && stream == NULL; ++i) {
Common::String filename = basename + STREAM_FILEFORMATS[i].fileExtension;
fileHandle->open(filename);
if (fileHandle->isOpen()) {
// Create the stream object
stream = STREAM_FILEFORMATS[i].openStreamFile(fileHandle, DisposeAfterUse::YES);
fileHandle = 0;
break;
}
}
delete fileHandle;
if (stream == NULL) {
debug(1, "SeekableAudioStream::openStreamFile: Could not open compressed AudioFile %s", basename.c_str());
}
return stream;
}
#pragma mark -
#pragma mark --- LoopingAudioStream ---
#pragma mark -
LoopingAudioStream::LoopingAudioStream(RewindableAudioStream *stream, uint loops, DisposeAfterUse::Flag disposeAfterUse)
: _parent(stream), _disposeAfterUse(disposeAfterUse), _loops(loops), _completeIterations(0) {
}
LoopingAudioStream::~LoopingAudioStream() {
if (_disposeAfterUse == DisposeAfterUse::YES)
delete _parent;
}
int LoopingAudioStream::readBuffer(int16 *buffer, const int numSamples) {
int samplesRead = _parent->readBuffer(buffer, numSamples);
if (_parent->endOfStream()) {
++_completeIterations;
if (_completeIterations == _loops)
return samplesRead;
int remainingSamples = numSamples - samplesRead;
if (!_parent->rewind()) {
// TODO: Properly indicate error
_loops = _completeIterations = 1;
return samplesRead;
}
samplesRead += _parent->readBuffer(buffer + samplesRead, remainingSamples);
}
return samplesRead;
}
bool LoopingAudioStream::endOfData() const {
return (_loops != 0 && (_completeIterations == _loops));
}
AudioStream *makeLoopingAudioStream(RewindableAudioStream *stream, uint loops) {
if (loops != 1)
return new LoopingAudioStream(stream, loops);
else
return stream;
}
AudioStream *makeLoopingAudioStream(SeekableAudioStream *stream, Timestamp start, Timestamp end, uint loops) {
if (!start.totalNumberOfFrames() && (!end.totalNumberOfFrames() || end == stream->getLength())) {
return makeLoopingAudioStream(stream, loops);
} else {
if (!end.totalNumberOfFrames())
end = stream->getLength();
if (start >= end) {
warning("makeLoopingAudioStream: start (%d) >= end (%d)", start.msecs(), end.msecs());
delete stream;
return 0;
}
return makeLoopingAudioStream(new SubSeekableAudioStream(stream, start, end), loops);
}
}
#pragma mark -
#pragma mark --- SubLoopingAudioStream ---
#pragma mark -
SubLoopingAudioStream::SubLoopingAudioStream(SeekableAudioStream *stream,
uint loops,
const Timestamp loopStart,
const Timestamp loopEnd,
DisposeAfterUse::Flag disposeAfterUse)
: _parent(stream), _disposeAfterUse(disposeAfterUse), _loops(loops),
_pos(0, getRate() * (isStereo() ? 2 : 1)),
_loopStart(convertTimeToStreamPos(loopStart, getRate(), isStereo())),
_loopEnd(convertTimeToStreamPos(loopEnd, getRate(), isStereo())),
_done(false) {
if (!_parent->rewind())
_done = true;
}
SubLoopingAudioStream::~SubLoopingAudioStream() {
if (_disposeAfterUse == DisposeAfterUse::YES)
delete _parent;
}
int SubLoopingAudioStream::readBuffer(int16 *buffer, const int numSamples) {
int framesLeft = MIN(_loopEnd.frameDiff(_pos), numSamples);
int framesRead = _parent->readBuffer(buffer, framesLeft);
_pos = _pos.addFrames(framesRead);
if (framesLeft < numSamples || framesRead < framesLeft) {
if (_loops != 0) {
--_loops;
if (!_loops) {
_done = true;
return framesRead;
}
}
if (!_parent->seek(_loopStart)) {
_done = true;
return framesRead;
}
_pos = _loopStart;
framesLeft = numSamples - framesLeft;
framesRead += _parent->readBuffer(buffer + framesRead, framesLeft);
if (_parent->endOfStream())
_done = true;
}
return framesRead;
}
#pragma mark -
#pragma mark --- SubSeekableAudioStream ---
#pragma mark -
SubSeekableAudioStream::SubSeekableAudioStream(SeekableAudioStream *parent, const Timestamp start, const Timestamp end, DisposeAfterUse::Flag disposeAfterUse)
: _parent(parent), _disposeAfterUse(disposeAfterUse),
_start(convertTimeToStreamPos(start, getRate(), isStereo())),
_pos(0, getRate() * (isStereo() ? 2 : 1)),
_length(convertTimeToStreamPos(end - start, getRate(), isStereo())) {
assert(_length.totalNumberOfFrames() % (isStereo() ? 2 : 1) == 0);
_parent->seek(_start);
}
SubSeekableAudioStream::~SubSeekableAudioStream() {
if (_disposeAfterUse)
delete _parent;
}
int SubSeekableAudioStream::readBuffer(int16 *buffer, const int numSamples) {
int framesLeft = MIN(_length.frameDiff(_pos), numSamples);
int framesRead = _parent->readBuffer(buffer, framesLeft);
_pos = _pos.addFrames(framesRead);
return framesRead;
}
bool SubSeekableAudioStream::seek(const Timestamp &where) {
_pos = convertTimeToStreamPos(where, getRate(), isStereo());
if (_pos > _length) {
_pos = _length;
return false;
}
if (_parent->seek(_pos + _start)) {
return true;
} else {
_pos = _length;
return false;
}
}
#pragma mark -
#pragma mark --- Queueing audio stream ---
#pragma mark -
void QueuingAudioStream::queueBuffer(byte *data, uint32 size, DisposeAfterUse::Flag disposeAfterUse, byte flags) {
AudioStream *stream = makeRawStream(data, size, getRate(), flags, disposeAfterUse);
queueAudioStream(stream, DisposeAfterUse::YES);
}
class QueuingAudioStreamImpl : public QueuingAudioStream {
private:
/**
* We queue a number of (pointers to) audio stream objects.
* In addition, we need to remember for each stream whether
* to dispose it after all data has been read from it.
* Hence, we don't store pointers to stream objects directly,
* but rather StreamHolder structs.
*/
struct StreamHolder {
AudioStream *_stream;
DisposeAfterUse::Flag _disposeAfterUse;
StreamHolder(AudioStream *stream, DisposeAfterUse::Flag disposeAfterUse)
: _stream(stream),
_disposeAfterUse(disposeAfterUse) {}
};
/**
* The sampling rate of this audio stream.
*/
const int _rate;
/**
* Whether this audio stream is mono (=false) or stereo (=true).
*/
const int _stereo;
/**
* This flag is set by the finish() method only. See there for more details.
*/
bool _finished;
/**
* A mutex to avoid access problems (causing e.g. corruption of
* the linked list) in thread aware environments.
*/
Common::Mutex _mutex;
/**
* The queue of audio streams.
*/
Common::Queue<StreamHolder> _queue;
public:
QueuingAudioStreamImpl(int rate, bool stereo)
: _rate(rate), _stereo(stereo), _finished(false) {}
~QueuingAudioStreamImpl();
// Implement the AudioStream API
virtual int readBuffer(int16 *buffer, const int numSamples);
virtual bool isStereo() const { return _stereo; }
virtual int getRate() const { return _rate; }
virtual bool endOfData() const {
//Common::StackLock lock(_mutex);
return _queue.empty();
}
virtual bool endOfStream() const { return _finished && _queue.empty(); }
// Implement the QueuingAudioStream API
virtual void queueAudioStream(AudioStream *stream, DisposeAfterUse::Flag disposeAfterUse);
virtual void finish() { _finished = true; }
uint32 numQueuedStreams() const {
//Common::StackLock lock(_mutex);
return _queue.size();
}
};
QueuingAudioStreamImpl::~QueuingAudioStreamImpl() {
while (!_queue.empty()) {
StreamHolder tmp = _queue.pop();
if (tmp._disposeAfterUse == DisposeAfterUse::YES)
delete tmp._stream;
}
}
void QueuingAudioStreamImpl::queueAudioStream(AudioStream *stream, DisposeAfterUse::Flag disposeAfterUse) {
if ((stream->getRate() != getRate()) || (stream->isStereo() != isStereo()))
error("QueuingAudioStreamImpl::queueAudioStream: stream has mismatched parameters");
Common::StackLock lock(_mutex);
_queue.push(StreamHolder(stream, disposeAfterUse));
}
int QueuingAudioStreamImpl::readBuffer(int16 *buffer, const int numSamples) {
Common::StackLock lock(_mutex);
int samplesDecoded = 0;
while (samplesDecoded < numSamples && !_queue.empty()) {
AudioStream *stream = _queue.front()._stream;
samplesDecoded += stream->readBuffer(buffer + samplesDecoded, numSamples - samplesDecoded);
if (stream->endOfData() ) {
StreamHolder tmp = _queue.pop();
if (tmp._disposeAfterUse == DisposeAfterUse::YES)
delete stream;
}
}
return samplesDecoded;
}
QueuingAudioStream *makeQueuingAudioStream(int rate, bool stereo) {
return new QueuingAudioStreamImpl(rate, stereo);
}
Timestamp convertTimeToStreamPos(const Timestamp &where, int rate, bool isStereo) {
Timestamp result(where.convertToFramerate(rate * (isStereo ? 2 : 1)));
// When the Stream is a stereo stream, we have to assure
// that the sample position is an even number.
if (isStereo && (result.totalNumberOfFrames() & 1))
return result.addFrames(-1); // We cut off one sample here.
else
return result;
}
} // End of namespace Audio
|