CUGL 1.2
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUAudioDecoder.h
1 //
2 // CUAudioDecoder.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This is the base class for an audio decoder. An audio decoder converts a
6 // a binary file into a pageable PCM data stream. This unifies the API for
7 // all of the supported audio codes (WAV, MP3, OGG, FLAC)
8 //
9 // CUGL MIT License:
10 //
11 // This software is provided 'as-is', without any express or implied
12 // warranty. In no event will the authors be held liable for any damages
13 // arising from the use of this software.
14 //
15 // Permission is granted to anyone to use this software for any purpose,
16 // including commercial applications, and to alter it and redistribute it
17 // freely, subject to the following restrictions:
18 //
19 // 1. The origin of this software must not be misrepresented; you must not
20 // claim that you wrote the original software. If you use this software
21 // in a product, an acknowledgment in the product documentation would be
22 // appreciated but is not required.
23 //
24 // 2. Altered source versions must be plainly marked as such, and must not
25 // be misrepresented as being the original software.
26 //
27 // 3. This notice may not be removed or altered from any source distribution.
28 //
29 // Author: Walker White
30 // Version: 8/20/18
31 //
32 #ifndef __CU_AUDIO_DECODER_H__
33 #define __CU_AUDIO_DECODER_H__
34 #include <SDL/SDL.h>
35 #include <string>
36 
37 namespace cugl {
38  namespace audio {
39 
55 class AudioDecoder {
56 protected:
58  Uint8 _channels;
59 
61  Uint32 _rate;
62 
64  Uint64 _frames;
65 
67  std::string _file;
68 
70  Uint32 _pagesize;
71 
73  Uint64 _currpage;
75  Uint64 _lastpage;
76 
77 public:
84  AudioDecoder();
85 
90 
102  virtual bool init(const char* file) {
103  return init(std::string(file));
104  }
105 
117  virtual bool init(const std::string& file) = 0;
118 
125  virtual void dispose() = 0;
126 
127 
128 #pragma mark Attributes
129 
136  double getDuration() const { return (double)_frames/(double)_rate; }
137 
143  Uint32 getSampleRate() const { return _rate; }
144 
152  Uint64 getLength() const { return _frames; }
153 
165  Uint32 getChannels() const { return _channels; }
166 
174  std::string getFile() const { return _file; }
175 
184  Uint32 getPageSize() const { return _pagesize; }
185 
186 
187 #pragma mark Decoding
188 
195  bool ready() {
196  return (_currpage < getPageCount());
197  }
198 
212  virtual Sint32 pagein(float* buffer) = 0;
213 
221  Uint64 getPage() const { return _currpage; }
222 
232  virtual void setPage(Uint64 page) = 0;
233 
241  Uint64 getPageCount() const {
242  return _frames % _pagesize == 0 ? _lastpage : _lastpage+1;
243  }
244 
248  void rewind() { setPage(0); }
249 
261  Sint32 decode(float* buffer);
262 
263 };
264  }
265 }
266 
267 
268 #endif /* __CU_AUDIO_DECODER_H__ */
double getDuration() const
Definition: CUAudioDecoder.h:136
Sint32 decode(float *buffer)
void rewind()
Definition: CUAudioDecoder.h:248
virtual bool init(const char *file)
Definition: CUAudioDecoder.h:102
Uint64 _currpage
Definition: CUAudioDecoder.h:73
Uint32 getPageSize() const
Definition: CUAudioDecoder.h:184
std::string _file
Definition: CUAudioDecoder.h:67
Uint64 _lastpage
Definition: CUAudioDecoder.h:75
~AudioDecoder()
Definition: CUAudioDecoder.h:89
Definition: CUAudioDecoder.h:55
virtual Sint32 pagein(float *buffer)=0
Uint64 getPageCount() const
Definition: CUAudioDecoder.h:241
Uint32 _pagesize
Definition: CUAudioDecoder.h:70
std::string getFile() const
Definition: CUAudioDecoder.h:174
Uint64 getLength() const
Definition: CUAudioDecoder.h:152
bool ready()
Definition: CUAudioDecoder.h:195
Uint32 getChannels() const
Definition: CUAudioDecoder.h:165
Uint8 _channels
Definition: CUAudioDecoder.h:58
virtual void dispose()=0
Uint32 _rate
Definition: CUAudioDecoder.h:61
Definition: CUAction.h:51
virtual void setPage(Uint64 page)=0
Uint32 getSampleRate() const
Definition: CUAudioDecoder.h:143
Uint64 getPage() const
Definition: CUAudioDecoder.h:221
Uint64 _frames
Definition: CUAudioDecoder.h:64