xoreos  0.0.5
audiostream.h
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 /* Based on ScummVM (<http://scummvm.org>) code, which is released
26  * under the terms of version 2 or later of the GNU General Public
27  * License.
28  *
29  * The original copyright note in ScummVM reads as follows:
30  *
31  * ScummVM is the legal property of its developers, whose names
32  * are too numerous to list here. Please refer to the COPYRIGHT
33  * file distributed with this source distribution.
34  *
35  * This program is free software; you can redistribute it and/or
36  * modify it under the terms of the GNU General Public License
37  * as published by the Free Software Foundation; either version 2
38  * of the License, or (at your option) any later version.
39  *
40  * This program is distributed in the hope that it will be useful,
41  * but WITHOUT ANY WARRANTY; without even the implied warranty of
42  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43  * GNU General Public License for more details.
44  *
45  * You should have received a copy of the GNU General Public License
46  * along with this program; if not, write to the Free Software
47  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
48  */
49 
50 #ifndef SOUND_AUDIOSTREAM_H
51 #define SOUND_AUDIOSTREAM_H
52 
53 #include <boost/noncopyable.hpp>
54 
55 #include "src/common/util.h"
56 #include "src/common/types.h"
58 #include "src/common/scopedptr.h"
59 
60 namespace Common {
61 class SeekableReadStream;
62 }
63 
64 namespace Sound {
65 
70 class AudioStream : boost::noncopyable {
71 public:
72  static const size_t kSizeInvalid = SIZE_MAX;
73 
74  virtual ~AudioStream() {}
75 
92  virtual size_t readBuffer(int16 *buffer, const size_t numSamples) = 0;
93 
95  virtual int getChannels() const = 0;
96 
98  virtual int getRate() const = 0;
99 
107  virtual bool endOfData() const = 0;
108 
117  virtual bool endOfStream() const { return endOfData(); }
118 };
119 
126 public:
127  static const uint64 kInvalidLength = UINT64_C(0xFFFFFFFFFFFFFFFF);
128 
134  virtual bool rewind() = 0;
135 
140  virtual uint64 getLength() const { return kInvalidLength; }
141 
146  virtual uint64 getDuration() const {
147  if ((getLength() == kInvalidLength) || (getRate() <= 0))
148  return kInvalidLength;
149 
150  return (getLength() * 1000) / getRate();
151  }
152 };
153 
156 public:
158 
159  size_t readBuffer(int16 *UNUSED(buffer), const size_t UNUSED(numSamples)) { return 0; }
160 
161  int getChannels() const { return 1; }
162 
163  int getRate() const { return 44100; }
164 
165  bool endOfData() const { return true; }
166  bool rewind() { return true; }
167 
168  uint64 getLength() const { return 0; }
169 };
170 
176 public:
186  LoopingAudioStream(RewindableAudioStream *stream, size_t loops, bool disposeAfterUse = true);
188 
189  size_t readBuffer(int16 *buffer, const size_t numSamples);
190  bool endOfData() const;
191 
192  int getChannels() const { return _parent->getChannels(); }
193  int getRate() const { return _parent->getRate(); }
194 
196  size_t getCompleteIterations() const { return _completeIterations; }
197 
198  bool rewind();
199 
200  uint64 getLength() const;
201  uint64 getDuration() const;
202 
204  uint64 getLengthOnce() const;
206  uint64 getDurationOnce() const;
207 
208 private:
210 
211  size_t _loops;
213 };
214 
228 
230 public:
231 
238  virtual void queueAudioStream(AudioStream *audStream, bool disposeAfterUse = true) = 0;
239 
245  virtual void finish() = 0;
246 
250  virtual bool isFinished() const = 0;
251 
256  virtual size_t numQueuedStreams() const = 0;
257 };
258 
262 QueuingAudioStream *makeQueuingAudioStream(int rate, int channels);
263 
271 class PacketizedAudioStream : public virtual AudioStream {
272 public:
274 
278  virtual void queuePacket(Common::SeekableReadStream *data) = 0;
279 
285  virtual void finish() = 0;
286 
290  virtual bool isFinished() const = 0;
291 };
292 
301 public:
302  StatelessPacketizedAudioStream(int rate, int channels) :
303  _rate(rate), _channels(channels), _stream(makeQueuingAudioStream(rate, channels)) {}
305 
306  // AudioStream API
307  int getChannels() const { return _channels; }
308  int getRate() const { return _rate; }
309  size_t readBuffer(int16 *data, const size_t numSamples) { return _stream->readBuffer(data, numSamples); }
310  bool endOfData() const { return _stream->endOfData(); }
311  bool endOfStream() const { return _stream->endOfStream(); }
312 
313  // PacketizedAudioStream API
314  void queuePacket(Common::SeekableReadStream *data) { _stream->queueAudioStream(makeStream(data)); }
315  void finish() { _stream->finish(); }
316  bool isFinished() const { return _stream->isFinished(); }
317 
318 protected:
323 
324 private:
325  int _rate;
328 };
329 
330 } // End of namespace Sound
331 
332 #endif // SOUND_AUDIOSTREAM_H
int getRate() const
Sample rate of the stream.
Definition: audiostream.h:308
virtual uint64 getDuration() const
Estimate the total duration of the stream in milliseconds.
Definition: audiostream.h:146
uint64 getDuration() const
bool endOfStream() const
End of stream reached? If this returns true, it means that all data in this stream is used up and no ...
Definition: audiostream.h:311
QueuingAudioStream * makeQueuingAudioStream(int rate, int channels)
Factory function for an QueuingAudioStream.
virtual AudioStream * makeStream(Common::SeekableReadStream *data)=0
Make the AudioStream for a given packet.
Definition: 2dafile.h:39
virtual bool endOfStream() const
End of stream reached? If this returns true, it means that all data in this stream is used up and no ...
Definition: audiostream.h:117
bool endOfData() const
End of data reached? If this returns true, it means that at this time there is no data available in t...
Definition: audiostream.h:310
int getChannels() const
Return the number channels in this stream.
Definition: audiostream.h:161
uint64_t uint64
Definition: types.h:206
uint64 getDurationOnce() const
Return the duration of one loop.
void finish()
Mark this stream as finished.
Definition: audiostream.h:315
virtual int getChannels() const =0
Return the number channels in this stream.
virtual void finish()=0
Mark this stream as finished.
virtual int getRate() const =0
Sample rate of the stream.
A looping audio stream.
Definition: audiostream.h:175
A PacketizedAudioStream that works closer to a QueuingAudioStream.
Definition: audiostream.h:300
bool isFinished() const
Is the stream marked as finished?
Definition: audiostream.h:316
int16_t int16
Definition: types.h:201
Common::DisposablePtr< RewindableAudioStream > _parent
Definition: audiostream.h:209
A simple scoped smart pointer template.
bool endOfData() const
End of data reached? If this returns true, it means that at this time there is no data available in t...
Definition: audiostream.cpp:97
virtual ~AudioStream()
Definition: audiostream.h:74
size_t getCompleteIterations() const
Returns number of loops the stream has played.
Definition: audiostream.h:196
int getRate() const
Sample rate of the stream.
Definition: audiostream.h:193
virtual void queueAudioStream(AudioStream *audStream, bool disposeAfterUse=true)=0
Queue an audio stream for playback.
Definition: game.h:37
virtual bool isFinished() const =0
Is the stream marked as finished?
LoopingAudioStream(RewindableAudioStream *stream, size_t loops, bool disposeAfterUse=true)
Creates a looping audio stream object.
Definition: audiostream.cpp:59
int getChannels() const
Return the number channels in this stream.
Definition: audiostream.h:192
void queuePacket(Common::SeekableReadStream *data)
Queue the next packet to be decoded.
Definition: audiostream.h:314
virtual uint64 getLength() const
Estimate the total number of samples per channel in this stream.
Definition: audiostream.h:140
A rewindable audio stream.
Definition: audiostream.h:125
int getChannels() const
Return the number channels in this stream.
Definition: audiostream.h:307
#define UNUSED(x)
Definition: system.h:170
size_t readBuffer(int16 *buffer, const size_t numSamples)
Fill the given buffer with up to numSamples samples.
Definition: audiostream.cpp:66
Utility templates and functions.
virtual size_t readBuffer(int16 *buffer, const size_t numSamples)=0
Fill the given buffer with up to numSamples samples.
static const size_t kSizeInvalid
Definition: audiostream.h:72
#define UINT64_C(c)
Definition: types.h:220
An AudioStream designed to work in terms of packets.
Definition: audiostream.h:271
static const uint64 kInvalidLength
Definition: audiostream.h:127
virtual size_t numQueuedStreams() const =0
Return the number of streams still queued for playback (including the currently playing stream)...
virtual bool rewind()=0
Rewinds the stream to its start.
Common::ScopedPtr< QueuingAudioStream > _stream
Definition: audiostream.h:327
Low-level type definitions to handle fixed width types portably.
A scoped plain pointer, allowing pointer-y access and normal deletion.
Definition: scopedptr.h:120
size_t readBuffer(int16 *data, const size_t numSamples)
Fill the given buffer with up to numSamples samples.
Definition: audiostream.h:309
A disposable plain pointer, allowing pointer-y access and normal deletion.
bool rewind()
Rewinds the stream to its start.
Definition: audiostream.h:166
Generic audio input stream.
Definition: audiostream.h:70
A smart pointer with a deletion flag.
An empty audio stream that plays nothing.
Definition: audiostream.h:155
virtual void queuePacket(Common::SeekableReadStream *data)=0
Queue the next packet to be decoded.
bool endOfData() const
End of data reached? If this returns true, it means that at this time there is no data available in t...
Definition: audiostream.h:165
uint64 getLength() const
Estimate the total number of samples per channel in this stream.
Definition: audiostream.h:168
#define SIZE_MAX
Definition: types.h:172
virtual bool isFinished() const =0
Is the stream marked as finished?
AudioStream * makeLoopingAudioStream(RewindableAudioStream *stream, size_t loops)
Wrapper functionality to efficiently create a stream, which might be looped.
virtual void finish()=0
Mark this stream as finished.
uint64 getLengthOnce() const
Return the length of one loop.
size_t readBuffer(int16 *buffer, const size_t numSamples)
Fill the given buffer with up to numSamples samples.
Definition: audiostream.h:159
Interface for a seekable & readable data stream.
Definition: readstream.h:265
virtual bool endOfData() const =0
End of data reached? If this returns true, it means that at this time there is no data available in t...
int getRate() const
Sample rate of the stream.
Definition: audiostream.h:163
StatelessPacketizedAudioStream(int rate, int channels)
Definition: audiostream.h:302