ScummVM API documentation
audiostream.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef AUDIO_AUDIOSTREAM_H
23 #define AUDIO_AUDIOSTREAM_H
24 
25 #include "common/ptr.h"
26 #include "common/scummsys.h"
27 #include "common/types.h"
28 
29 #include "audio/timestamp.h"
30 
31 namespace Common {
32 class Path;
33 class SeekableReadStream;
34 }
35 
36 namespace Audio {
37 
50 class AudioStream {
51 public:
52  virtual ~AudioStream() {}
53 
70  virtual int readBuffer(int16 *buffer, const int numSamples) = 0;
71 
73  virtual bool isStereo() const = 0;
74 
76  virtual int getRate() const = 0;
77 
87  virtual bool endOfData() const = 0;
88 
99  virtual bool endOfStream() const { return endOfData(); }
100 };
101 
109 class RewindableAudioStream : public virtual AudioStream {
110 public:
116  virtual bool rewind() = 0;
117 };
118 
123 class LoopableAudioStream : public virtual AudioStream {
124 public:
125 
129  virtual uint getCompleteIterations() const = 0;
130 
135  virtual void setRemainingIterations(uint loops) = 0;
136 };
137 
145 public:
158  LoopingAudioStream(RewindableAudioStream *stream, uint loops, DisposeAfterUse::Flag disposeAfterUse = DisposeAfterUse::YES, bool rewind = true);
159 
172  LoopingAudioStream(Common::DisposablePtr<RewindableAudioStream>&& stream, uint loops, bool rewind = true);
173 
174  int readBuffer(int16 *buffer, const int numSamples);
175  bool endOfData() const;
176  bool endOfStream() const;
177 
178  bool isStereo() const { return _parent->isStereo(); }
179  int getRate() const { return _parent->getRate(); }
180 
181  uint getCompleteIterations() const { return _completeIterations; }
182  void setRemainingIterations(uint loops) { _loops = _completeIterations + loops; }
183 private:
185 
186  uint _loops;
187  uint _completeIterations;
188 };
189 
204 
213 public:
225  static SeekableAudioStream *openStreamFile(const Common::Path &basename);
226 
234  bool seek(uint32 where) {
235  return seek(Timestamp(where, getRate()));
236  }
237 
245  virtual bool seek(const Timestamp &where) = 0;
246 
252  virtual Timestamp getLength() const = 0;
253 
254  virtual bool rewind() { return seek(0); }
255 };
256 
276 
288 public:
302  SubLoopingAudioStream(SeekableAudioStream *stream, uint loops,
303  const Timestamp loopStart,
304  const Timestamp loopEnd,
305  DisposeAfterUse::Flag disposeAfterUse = DisposeAfterUse::YES);
306 
307  int readBuffer(int16 *buffer, const int numSamples);
308  bool endOfData() const;
309  bool endOfStream() const;
310 
311  bool isStereo() const { return _parent->isStereo(); }
312  int getRate() const { return _parent->getRate(); }
313 
314  uint getCompleteIterations() const { return _completeIterations; }
315  void setRemainingIterations(uint loops) { _loops = _completeIterations + loops; }
316 private:
318 
319  uint _loops;
320  uint _completeIterations;
321  Timestamp _pos;
322  Timestamp _loopStart, _loopEnd;
323 };
324 
325 
335 public:
344  SubSeekableAudioStream(SeekableAudioStream *parent, const Timestamp start, const Timestamp end, DisposeAfterUse::Flag disposeAfterUse = DisposeAfterUse::YES);
345 
346  int readBuffer(int16 *buffer, const int numSamples);
347 
348  bool isStereo() const { return _parent->isStereo(); }
349 
350  int getRate() const { return _parent->getRate(); }
351 
352  bool endOfData() const { return (_pos >= _length) || _parent->endOfData(); }
353  bool endOfStream() const { return (_pos >= _length) || _parent->endOfStream(); }
354 
355  bool seek(const Timestamp &where);
356 
357  Timestamp getLength() const { return _length; }
358 private:
360 
361  const Timestamp _start;
362  const Timestamp _length;
363  Timestamp _pos;
364 };
365 
371 public:
372 
380  virtual void queueAudioStream(Audio::AudioStream *audStream,
381  DisposeAfterUse::Flag disposeAfterUse = DisposeAfterUse::YES) = 0;
382 
397  void queueBuffer(byte *data, uint32 size, DisposeAfterUse::Flag disposeAfterUse, byte flags);
398 
405  virtual void finish() = 0;
406 
411  virtual uint32 numQueuedStreams() const = 0;
412 };
413 
417 QueuingAudioStream *makeQueuingAudioStream(int rate, bool stereo);
418 
427 Timestamp convertTimeToStreamPos(const Timestamp &where, int rate, bool isStereo);
428 
437 AudioStream *makeLimitingAudioStream(AudioStream *parentStream, const Timestamp &length, DisposeAfterUse::Flag disposeAfterUse = DisposeAfterUse::YES);
438 
446 class PacketizedAudioStream : public virtual AudioStream {
447 public:
448  virtual ~PacketizedAudioStream() {}
449 
453  virtual void queuePacket(Common::SeekableReadStream *data) = 0;
454 
461  virtual void finish() = 0;
462 };
463 
472 public:
477  StatelessPacketizedAudioStream(uint rate, uint channels) :
478  _rate(rate), _channels(channels), _stream(makeQueuingAudioStream(rate, channels == 2)) {}
479  virtual ~StatelessPacketizedAudioStream() {}
480 
481  // AudioStream API
482  bool isStereo() const { return _channels == 2; }
483  int getRate() const { return _rate; }
484  int readBuffer(int16 *data, const int numSamples) { return _stream->readBuffer(data, numSamples); }
485  bool endOfData() const { return _stream->endOfData(); }
486  bool endOfStream() const { return _stream->endOfStream(); }
487 
488  // PacketizedAudioStream API
489  void queuePacket(Common::SeekableReadStream *data) { _stream->queueAudioStream(makeStream(data)); }
490  void finish() { _stream->finish(); }
491 
495  uint getChannels() const { return _channels; }
496 
497 protected:
501  virtual AudioStream *makeStream(Common::SeekableReadStream *data) = 0;
502 
503 private:
504  uint _rate;
505  uint _channels;
507 };
508 
514 
518 AudioStream *makeSilentAudioStream(int rate, bool stereo);
519 
521 } // End of namespace Audio
522 
523 #endif
bool endOfData() const
Definition: audiostream.h:485
Definition: audiostream.h:123
int readBuffer(int16 *data, const int numSamples)
Definition: audiostream.h:484
bool endOfStream() const
Definition: audiostream.h:486
bool isStereo() const
Definition: audiostream.h:311
AudioStream * makeSilentAudioStream(int rate, bool stereo)
Definition: audiostream.h:334
QueuingAudioStream * makeQueuingAudioStream(int rate, bool stereo)
Definition: path.h:52
bool endOfStream() const
Definition: audiostream.h:353
Definition: timestamp.h:83
virtual bool endOfStream() const
Definition: audiostream.h:99
Definition: stream.h:745
Definition: audiostream.h:446
Definition: ptr.h:572
Definition: audiostream.h:212
AudioStream * makeLimitingAudioStream(AudioStream *parentStream, const Timestamp &length, DisposeAfterUse::Flag disposeAfterUse=DisposeAfterUse::YES)
StatelessPacketizedAudioStream(uint rate, uint channels)
Definition: audiostream.h:477
int getRate() const
Definition: audiostream.h:350
void finish()
Definition: audiostream.h:490
bool endOfData() const
Definition: audiostream.h:352
Path
Definition: game.h:75
Definition: audiostream.h:144
uint getChannels() const
Definition: audiostream.h:495
int getRate() const
Definition: audiostream.h:483
bool seek(uint32 where)
Definition: audiostream.h:234
Definition: algorithm.h:29
Definition: audiostream.h:50
uint getCompleteIterations() const
Definition: audiostream.h:181
bool isStereo() const
Definition: audiostream.h:178
Definition: audiostream.h:109
int getRate() const
Definition: audiostream.h:179
Definition: audiostream.h:287
Definition: audiostream.h:471
Definition: audiostream.h:370
int getRate() const
Definition: audiostream.h:312
void setRemainingIterations(uint loops)
Definition: audiostream.h:315
Timestamp convertTimeToStreamPos(const Timestamp &where, int rate, bool isStereo)
virtual bool rewind()
Definition: audiostream.h:254
void queuePacket(Common::SeekableReadStream *data)
Definition: audiostream.h:489
Definition: ptr.h:655
bool isStereo() const
Definition: audiostream.h:348
bool isStereo() const
Definition: audiostream.h:482
AudioStream * makeNullAudioStream()
Definition: system.h:37
AudioStream * makeLoopingAudioStream(SeekableAudioStream *stream, Timestamp start, Timestamp end, uint loops)
Timestamp getLength() const
Definition: audiostream.h:357
void setRemainingIterations(uint loops)
Definition: audiostream.h:182
uint getCompleteIterations() const
Definition: audiostream.h:314