ScummVM API documentation
mp3.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 SOUND_MP3_PSP_H
23 #define SOUND_MP3_PSP_H
24 
25 #include "common/ptr.h"
26 #include "common/types.h"
27 #include "common/scummsys.h"
28 
29 namespace Common {
30 class SeekableReadStream;
31 }
32 
33 namespace Audio {
34 
35 class AudioStream;
36 class SeekableAudioStream;
37 
39 protected:
40  enum State {
41  MP3_STATE_INIT, // Need to init the decoder
42  MP3_STATE_READY, // ready for processing data
43  MP3_STATE_EOS // end of data reached (may need to loop)
44  };
45 
46  #define MAX_SAMPLES_PER_FRAME 1152 * 2 /* x2 for stereo */
47  int16 _pcmSamples[MAX_SAMPLES_PER_FRAME] __attribute__((aligned(64))); // samples to output PCM data into
48  byte _codecInBuffer[3072] __attribute__((aligned(64))); // the codec always needs alignment
49  unsigned long _codecParams[65]__attribute__((aligned(64))); // TODO: change to struct
50 
52 
53  uint32 _pcmLength; // how many pcm samples we have for this type of file (x2 this for stereo)
54 
55  uint _posInFrame; // position in frame
56  State _state; // what state the stream is in
57 
58  Timestamp _length;
59  uint32 _sampleRate;
60  bool _stereo;
61 
62  mad_timer_t _totalTime;
63  mad_stream _stream; //
64  mad_header _header; // This is all we need from libmad
65 
66  static bool _decoderInit; // has the decoder been initialized
67  static bool _decoderFail; // has the decoder failed to load
68 
69  enum {
70  BUFFER_SIZE = 5 * 8192
71  };
72 
73  // This buffer contains a slab of input data
74  byte _buf[BUFFER_SIZE + MAD_BUFFER_GUARD];
75 
76  void decodeMP3Data();
77  void readMP3DataIntoBuffer();
78 
79  static bool loadStartAudioModule(const char *modname, int partition);
80  int initStream();
81  void findValidHeader();
82  void deinitStream();
83  void updatePcmLength();
84 
85  // to init and uninit ME decoder
86  static bool initDecoder();
87  static bool stopDecoder();
88 
89  // ME functions for stream
90  bool initStreamME();
91  void releaseStreamME();
92 
93 public:
94  Mp3PspStream(Common::SeekableReadStream *inStream, DisposeAfterUse::Flag dispose);
95  ~Mp3PspStream();
96 
97  // This function avoids having to create streams when it's not possible
98  static inline bool isOkToCreateStream() {
99  if (_decoderFail) // fatal failure
100  return false;
101  if (!_decoderInit) // if we're not initialized
102  if (!initDecoder()) // check if we failed init
103  return false;
104  return true;
105  }
106 
107  int readBuffer(int16 *buffer, const int numSamples);
108 
109  bool endOfData() const { return _state == MP3_STATE_EOS; }
110  bool isStereo() const { return _stereo; }
111  int getRate() const { return _sampleRate; }
112 
113  bool seek(const Timestamp &where);
114  Timestamp getLength() const { return _length; }
115 };
116 
117 } // End of namespace Audio
118 
119 #endif // #ifndef SOUND_MP3_PSP_H
Definition: mp3.h:38
int getRate() const
Definition: mp3.h:111
Definition: timestamp.h:83
Definition: stream.h:745
Timestamp getLength() const
Definition: mp3.h:114
Definition: audiostream.h:212
bool endOfData() const
Definition: mp3.h:109
Definition: algorithm.h:29
bool isStereo() const
Definition: mp3.h:110
Definition: system.h:38