CUGL 1.3
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUWAVDecoder.h
1 //
2 // CUWAVDecoder.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This is class for decoding WAV files. It supports PCM, IEEE Float, and
6 // ADPCM encoding (both MS and IMA). However, it does not support MP3 data
7 // stored in a WAV file. MP3 data should be stored in an MP3 file.
8 //
9 // This code is heavily adapted from SDL_wave.c in SDL 2.0.5 by Sam Lantinga.
10 // That implementation does not allow WAV files to be streamed. In addition,
11 // it required the audio thread to be initialized before loading assets. This
12 // implementation decouples the decoding process, removing these concerns.
13 //
14 // CUGL MIT License:
15 //
16 // This software is provided 'as-is', without any express or implied
17 // warranty. In no event will the authors be held liable for any damages
18 // arising from the use of this software.
19 //
20 // Permission is granted to anyone to use this software for any purpose,
21 // including commercial applications, and to alter it and redistribute it
22 // freely, subject to the following restrictions:
23 //
24 // 1. The origin of this software must not be misrepresented; you must not
25 // claim that you wrote the original software. If you use this software
26 // in a product, an acknowledgment in the product documentation would be
27 // appreciated but is not required.
28 //
29 // 2. Altered source versions must be plainly marked as such, and must not
30 // be misrepresented as being the original software.
31 //
32 // 3. This notice may not be removed or altered from any source distribution.
33 //
34 // Authors: Sam Lantinga, Walker White
35 // Version: 6/29/17
36 //
37 #ifndef __CU_WAV_DECODER_H__
38 #define __CU_WAV_DECODER_H__
39 #include "CUAudioDecoder.h"
40 #include <SDL/SDL.h>
41 
42 namespace cugl {
43  namespace audio {
44 
45 #pragma mark WAV Header
46 
52 typedef struct WaveFMT {
54  Uint16 encoding;
56  Uint16 channels;
58  Uint32 frequency;
60  Uint32 byterate;
62  Uint16 blockalign;
64  Uint16 bitspersample;
65 } WaveFMT;
66 
67 
68 #pragma mark -
69 #pragma mark ADPCM Decoder
70 
77 class ADPCMDecoder {
78 protected:
82  Uint8* _blkbuffer;
84  Uint16 _blocksize;
85 
86 public:
87 #pragma mark Constructors
88 
94  ADPCMDecoder() : _blocksize(0), _blkbuffer(nullptr) {}
95 
99  virtual ~ADPCMDecoder();
100 
112  virtual bool init(WaveFMT* format);
113 
114 #pragma mark Decoding
115 
126  Uint64 getFrames(Uint64 bytes) const {
127  return (_blocksize*bytes)/_wavefmt.blockalign;
128  }
129 
137  Uint32 getBlockSize() const {
138  return _blocksize;
139  }
140 
153  virtual Sint32 read(SDL_RWops* source, Uint8* buffer) = 0;
154 };
155 
156 
157 #pragma mark -
158 #pragma mark WAV Decoder
159 
178 class WAVDecoder : public AudioDecoder {
179 public:
185  enum class Type : int {
187  PCM_DATA = 0,
189  IEEE_FLOAT = 1,
191  MS_ADPCM = 2,
193  IMA_ADPCM = 3,
195  MP3_DATA = 4,
197  UNKNOWN = 5
198  };
199 
200 protected:
202  SDL_RWops* _source;
204  Uint8* _chunker;
207 
209  Uint32 _sampbits;
211  Uint32 _sampsize;
213  Sint64 _datamark;
214 
216  std::shared_ptr<ADPCMDecoder> _adpcm;
217 
218 public:
219 #pragma mark Constructors
220 
226  WAVDecoder();
227 
231  ~WAVDecoder();
232 
242  bool init(const char* file) override {
243  return init(std::string(file));
244  }
245 
255  bool init(const std::string& file) override;
256 
263  void dispose() override;
264 
265 #pragma mark Static Constructors
266 
276  static std::shared_ptr<AudioDecoder> alloc(const char* file) {
277  return alloc(std::string(file));
278  }
279 
290  static std::shared_ptr<AudioDecoder> alloc(const std::string& file);
291 
292 
293 #pragma mark Decoding
294 
299  Type getType() const { return _datatype; }
300 
314  virtual Sint32 pagein(float* buffer) override;
315 
325  void setPage(Uint64 page) override;
326 
327 private:
333  bool isADPCM() const { return _datatype == Type::MS_ADPCM || _datatype == Type::IMA_ADPCM; }
334 
346  bool bootstrap(const std::string& file);
347 
348 };
349  }
350 }
351 
352 #endif /* __CU_WAV_DECODER_H__ */
cugl::audio::WAVDecoder::dispose
void dispose() override
cugl::audio::WAVDecoder::Type
Type
Definition: CUWAVDecoder.h:185
cugl::audio::WaveFMT::blockalign
Uint16 blockalign
Definition: CUWAVDecoder.h:62
cugl::audio::WAVDecoder::_datamark
Sint64 _datamark
Definition: CUWAVDecoder.h:213
cugl::audio::WAVDecoder::WAVDecoder
WAVDecoder()
cugl::audio::WAVDecoder::_adpcm
std::shared_ptr< ADPCMDecoder > _adpcm
Definition: CUWAVDecoder.h:216
cugl::audio::WAVDecoder::Type::PCM_DATA
cugl::audio::ADPCMDecoder::read
virtual Sint32 read(SDL_RWops *source, Uint8 *buffer)=0
cugl::audio::WAVDecoder::~WAVDecoder
~WAVDecoder()
cugl::audio::ADPCMDecoder::init
virtual bool init(WaveFMT *format)
cugl::audio::WaveFMT::bitspersample
Uint16 bitspersample
Definition: CUWAVDecoder.h:64
cugl::audio::AudioDecoder
Definition: CUAudioDecoder.h:55
cugl::audio::WAVDecoder::_sampsize
Uint32 _sampsize
Definition: CUWAVDecoder.h:211
cugl::audio::WAVDecoder::Type::MP3_DATA
cugl::audio::WAVDecoder::init
bool init(const char *file) override
Definition: CUWAVDecoder.h:242
cugl::audio::ADPCMDecoder::_blkbuffer
Uint8 * _blkbuffer
Definition: CUWAVDecoder.h:82
cugl::audio::WAVDecoder::_datatype
Type _datatype
Definition: CUWAVDecoder.h:206
cugl::audio::WaveFMT
struct cugl::audio::WaveFMT WaveFMT
cugl::audio::ADPCMDecoder::_blocksize
Uint16 _blocksize
Definition: CUWAVDecoder.h:84
cugl::audio::ADPCMDecoder::getFrames
Uint64 getFrames(Uint64 bytes) const
Definition: CUWAVDecoder.h:126
cugl::audio::ADPCMDecoder::ADPCMDecoder
ADPCMDecoder()
Definition: CUWAVDecoder.h:94
cugl::audio::WAVDecoder::Type::UNKNOWN
cugl::audio::WAVDecoder
Definition: CUWAVDecoder.h:178
cugl::audio::WAVDecoder::_chunker
Uint8 * _chunker
Definition: CUWAVDecoder.h:204
cugl::audio::ADPCMDecoder::~ADPCMDecoder
virtual ~ADPCMDecoder()
cugl::audio::WaveFMT::frequency
Uint32 frequency
Definition: CUWAVDecoder.h:58
cugl::audio::ADPCMDecoder::getBlockSize
Uint32 getBlockSize() const
Definition: CUWAVDecoder.h:137
cugl::audio::WAVDecoder::Type::IEEE_FLOAT
cugl::audio::WaveFMT::channels
Uint16 channels
Definition: CUWAVDecoder.h:56
cugl::audio::WAVDecoder::pagein
virtual Sint32 pagein(float *buffer) override
cugl::audio::WAVDecoder::alloc
static std::shared_ptr< AudioDecoder > alloc(const char *file)
Definition: CUWAVDecoder.h:276
cugl::audio::WaveFMT::encoding
Uint16 encoding
Definition: CUWAVDecoder.h:54
cugl::audio::ADPCMDecoder::_wavefmt
WaveFMT _wavefmt
Definition: CUWAVDecoder.h:80
cugl::audio::WAVDecoder::_sampbits
Uint32 _sampbits
Definition: CUWAVDecoder.h:209
cugl::audio::WAVDecoder::setPage
void setPage(Uint64 page) override
cugl::audio::WaveFMT::byterate
Uint32 byterate
Definition: CUWAVDecoder.h:60
cugl::audio::ADPCMDecoder
Definition: CUWAVDecoder.h:77
cugl::audio::WAVDecoder::getType
Type getType() const
Definition: CUWAVDecoder.h:299
cugl::audio::WAVDecoder::_source
SDL_RWops * _source
Definition: CUWAVDecoder.h:202
cugl::audio::WaveFMT
Definition: CUWAVDecoder.h:52
cugl::audio::WAVDecoder::Type::IMA_ADPCM
cugl::audio::WAVDecoder::Type::MS_ADPCM