CUGL 1.3
Cornell University Game Library
CUAudioSample.h
1 //
2 // CUAudioSample.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides the base class for representing an audio sample, or
6 // a file with prerecorded audio. An audio sample is not a node in the audio
7 // graph. Instead, a sample is provided to an AudioPlayer for playback.
8 // Multiple AudioPlayers can share the same sample, allowing copies of the
9 // sound to be played simultaneously
10 //
11 // This module provides support for both in-memory audio samples and streaming
12 // audio. The former is ideal for sound effects, but not long-playing music.
13 // The latter introduces some latency and is only ideal for long-playing music.
14 //
15 // CUGL MIT License:
16 //
17 // This software is provided 'as-is', without any express or implied
18 // warranty. In no event will the authors be held liable for any damages
19 // arising from the use of this software.
20 //
21 // Permission is granted to anyone to use this software for any purpose,
22 // including commercial applications, and to alter it and redistribute it
23 // freely, subject to the following restrictions:
24 //
25 // 1. The origin of this software must not be misrepresented; you must not
26 // claim that you wrote the original software. If you use this software
27 // in a product, an acknowledgment in the product documentation would be
28 // appreciated but is not required.
29 //
30 // 2. Altered source versions must be plainly marked as such, and must not
31 // be misrepresented as being the original software.
32 //
33 // 3. This notice may not be removed or altered from any source distribution.
34 //
35 // Author: Walker White
36 // Version: 11/20/18
37 //
38 #ifndef __CU_AUDIO_SAMPLE_H__
39 #define __CU_AUDIO_SAMPLE_H__
40 #include <SDL/SDL.h>
41 #include <cugl/assets/CUJsonValue.h>
42 #include "CUSound.h"
43 #include <string>
44 #include <atomic>
45 
46 namespace cugl {
48  namespace audio {
49  class AudioDecoder;
50  }
51 
69 class AudioSample : public Sound {
70 public:
71 #pragma mark File Types
72 
79  enum class Type : int {
81  UNKNOWN = -1,
83  WAV_FILE = 0,
85  MP3_FILE = 1,
87  OGG_FILE = 2,
89  FLAC_FILE = 3,
91  IN_MEMORY = 4
92  };
93 
94 protected:
96  Uint64 _frames;
97 
100 
102  bool _stream;
103 
105  float* _buffer;
106 
107 public:
108 #pragma mark Constructors
109 
115  AudioSample();
116 
121 
134  bool init(const char* file, bool stream=false);
135 
148  bool init(const std::string& file, bool stream=false) {
149  return init(file.c_str(),stream);
150  }
151 
165  bool init(Uint8 channels, Uint32 rate, Uint32 frames);
166 
173  virtual void dispose() override;
174 
185  static Type guessType(const char* file);
186 
197  static Type guessType(const std::string& file) {
198  return guessType(file.c_str());
199  }
200 
201 #pragma mark Static Constructors
202 
214  static std::shared_ptr<AudioSample> alloc(const char* file, bool stream=false) {
215  std::shared_ptr<AudioSample> result = std::make_shared<AudioSample>();
216  return (result->init(file,stream) ? result : nullptr);
217  }
218 
231  static std::shared_ptr<AudioSample> alloc(const std::string& file, bool stream=false) {
232  return alloc(file.c_str(), stream);
233  }
234 
248  static std::shared_ptr<AudioSample> alloc(Uint8 channels, Uint32 rate, Uint32 frames) {
249  std::shared_ptr<AudioSample> result = std::make_shared<AudioSample>();
250  return (result->init(channels,rate,frames) ? result : nullptr);
251  }
252 
272  static std::shared_ptr<AudioSample> allocWithData(const std::shared_ptr<JsonValue>& data);
273 
274 
275 #pragma mark Attributes
276 
283  bool isStreamed() const { return _stream; }
284 
292  Type getType() const { return _type; }
293 
301  virtual Sint64 getLength() const override { return _frames; }
302 
310  virtual double getDuration() const override { return (double)_frames/(double)_rate; }
311 
312 #pragma mark Playback Support
313 
322  float* getBuffer() { return _buffer; }
323 
333  std::shared_ptr<audio::AudioDecoder> getDecoder();
334 
344  virtual std::shared_ptr<audio::AudioNode> createNode() override;
345 };
346 
347 }
348 
349 
350 #endif /* __CU_AUDIO_SAMPLE_H__ */
cugl::AudioSample
Definition: CUAudioSample.h:69
cugl::Sound::_rate
Uint32 _rate
Definition: CUSound.h:79
cugl::AudioSample::Type::IN_MEMORY
cugl::AudioSample::getLength
virtual Sint64 getLength() const override
Definition: CUAudioSample.h:301
cugl::AudioSample::guessType
static Type guessType(const std::string &file)
Definition: CUAudioSample.h:197
cugl::AudioSample::alloc
static std::shared_ptr< AudioSample > alloc(Uint8 channels, Uint32 rate, Uint32 frames)
Definition: CUAudioSample.h:248
cugl::AudioSample::AudioSample
AudioSample()
cugl::AudioSample::Type::UNKNOWN
cugl::AudioSample::_stream
bool _stream
Definition: CUAudioSample.h:102
cugl::AudioSample::getBuffer
float * getBuffer()
Definition: CUAudioSample.h:322
cugl::AudioSample::_buffer
float * _buffer
Definition: CUAudioSample.h:105
cugl::AudioSample::init
bool init(const std::string &file, bool stream=false)
Definition: CUAudioSample.h:148
cugl::AudioSample::Type
Type
Definition: CUAudioSample.h:79
cugl::AudioSample::_frames
Uint64 _frames
Definition: CUAudioSample.h:96
cugl::Sound
Definition: CUSound.h:69
cugl::AudioSample::guessType
static Type guessType(const char *file)
cugl::AudioSample::dispose
virtual void dispose() override
cugl::AudioSample::getDecoder
std::shared_ptr< audio::AudioDecoder > getDecoder()
cugl::AudioSample::Type::WAV_FILE
cugl::AudioSample::Type::FLAC_FILE
cugl::AudioSample::alloc
static std::shared_ptr< AudioSample > alloc(const char *file, bool stream=false)
Definition: CUAudioSample.h:214
cugl::AudioSample::getType
Type getType() const
Definition: CUAudioSample.h:292
cugl::AudioSample::isStreamed
bool isStreamed() const
Definition: CUAudioSample.h:283
cugl::AudioSample::alloc
static std::shared_ptr< AudioSample > alloc(const std::string &file, bool stream=false)
Definition: CUAudioSample.h:231
cugl::AudioSample::Type::MP3_FILE
cugl::AudioSample::getDuration
virtual double getDuration() const override
Definition: CUAudioSample.h:310
cugl::AudioSample::_type
Type _type
Definition: CUAudioSample.h:99
cugl::AudioSample::createNode
virtual std::shared_ptr< audio::AudioNode > createNode() override
cugl::AudioSample::allocWithData
static std::shared_ptr< AudioSample > allocWithData(const std::shared_ptr< JsonValue > &data)
cugl::AudioSample::init
bool init(const char *file, bool stream=false)
cugl::AudioSample::~AudioSample
~AudioSample()
Definition: CUAudioSample.h:120
cugl::AudioSample::Type::OGG_FILE