CUGL 2.3
Cornell University Game Library
|
#include <CUAudioSample.h>
Public Member Functions | |
AudioSample () | |
~AudioSample () | |
bool | init (const std::string file, bool stream=false) |
bool | init (Uint8 channels, Uint32 rate, Uint32 frames) |
bool | initWithData (const std::shared_ptr< JsonValue > &data) |
virtual void | dispose () override |
bool | isStreamed () const |
AudioType | getType () const |
virtual Sint64 | getLength () const override |
virtual double | getDuration () const override |
float * | getBuffer () |
std::shared_ptr< AudioDecoder > | getDecoder () |
virtual std::shared_ptr< audio::AudioNode > | createNode () override |
Public Member Functions inherited from cugl::Sound | |
Sound () | |
~Sound () | |
virtual void | dispose () |
Uint32 | getRate () const |
Uint32 | getChannels () const |
virtual Sint64 | getLength () const |
virtual double | getDuration () const |
const std::string | getFile () const |
const std::string | getSuffix () const |
float | getVolume () const |
void | setVolume (float volume) |
virtual std::shared_ptr< audio::AudioNode > | createNode () |
Static Public Member Functions | |
static std::shared_ptr< AudioSample > | alloc (const std::string file, bool stream=false) |
static std::shared_ptr< AudioSample > | alloc (Uint8 channels, Uint32 rate, Uint32 frames) |
static std::shared_ptr< AudioSample > | allocWithData (const std::shared_ptr< JsonValue > &data) |
Protected Attributes | |
Uint64 | _frames |
AudioType | _type |
bool | _stream |
float * | _buffer |
Protected Attributes inherited from cugl::Sound | |
Uint8 | _channels |
Uint32 | _rate |
std::string | _file |
float | _volume |
This class represents a sample that can be played by a audio::AudioPlayer
.
This class provides support for both in-memory audio samples and streaming audio. The former is ideal for sound effects, but not long-playing music. The latter introduces some latency and is only ideal for long-playing music.
The choice of buffered or streaming is independent of the file type.
Currently, we support four file types: WAV (including ADPCM encodings), MP3, Ogg (Vorbis), and Flac. As a general rule, we prefer WAV for sound effects and Ogg for music.
All audio samples consist of float-formated PCM data. We assume channels are interleaved. We support up to 32 channels, though it is unlikely for that many channels to be encoded in a sound file. SDL itself only supports 8 channels for (7.1 surround) playback.
cugl::AudioSample::AudioSample | ( | ) |
Creates a degenerate audio sample with no buffer.
NEVER USE A CONSTRUCTOR WITH NEW. If you want to allocate an asset on the heap, use one of the static constructors instead.
|
inline |
Deletes this audio sample, disposing of all resources.
|
inlinestatic |
Returns a newly allocated audio sample for the given file.
The choice of buffered or streaming is independent of the file type. If the file is streamed, it will not be loaded into memory. Otherwise, this initializer will allocate memory to read the asset into memory.
file | The source file for the audio sample |
stream | Wether to stream the audio from the file. |
|
inlinestatic |
Returns an empty audio sample of the given size.
The audio sample will be in-memory (not streamed). The contents of the buffer will all be 0s. Use the getBuffer()
method to write data to this buffer.
channels | the number of audio channels |
rate | the sampling rate of this source |
frames | the number of frames in this source |
|
inlinestatic |
Returns a newly allocated audio sample with the given JSON specificaton.
This initializer is designed to receive the "data" object from the JSON passed to Scene2Loader
. This JSON format supports the following attribute values:
"file": The path to the source, relative to the asset directory "stream": A boolean, indicating whether to stream the sample "volume": A float, representing the volume
All attributes are optional. There are no required attributes. By default, audio samples are not streamed, meaning they are fully loaded into memory. This is recommended for sound effects, but not for music.
data | The JSON object specifying the audio sample |
|
overridevirtual |
Returns a playble audio node for this asset.
This audio node may be attached to an audio::AudioOutput
for immediate playback. Nodes are distinct. Each call to this method allocates a new audio node.
Reimplemented from cugl::Sound.
|
overridevirtual |
Deletes the sample resources and resets all attributes.
This will delete the file reference and any allocated buffers. You must reinitialize the sound data to use the object.
Reimplemented from cugl::Sound.
|
inline |
Returns the underlying PCM data buffer.
This pointer will be null if the sample is streamed. Otherwise, the the buffer will contain channels * frames many elements. It is okay to write data to the buffer, but it cannot be resized or reassigned.
std::shared_ptr< AudioDecoder > cugl::AudioSample::getDecoder | ( | ) |
Returns a new decoder for this audio sample
A decoder is used to extract the sound data into a PCM buffer. It should not be accessed directly. Instead it is used by the audio graph to acquire playback data.
|
inlineoverridevirtual |
Returns the length of this audio sample in seconds.
The accuracy of this method depends on the specific implementation.
Reimplemented from cugl::Sound.
|
inlineoverridevirtual |
Returns the frame length of this audio sample.
The frame length is the duration times the sample rate.
Reimplemented from cugl::Sound.
|
inline |
Returns the encoding type for this audio sample
The type should be one of WAV, MP3, OGG, or FLAC.
bool cugl::AudioSample::init | ( | const std::string | file, |
bool | stream = false |
||
) |
Initializes a new audio sample for the given file.
The choice of buffered or streaming is independent of the file type. If the file is streamed, it will not be loaded into memory. Otherwise, this initializer will allocate memory to read the asset into memory.
file | The source file for the audio sample |
stream | Wether to stream the audio from the file. |
bool cugl::AudioSample::init | ( | Uint8 | channels, |
Uint32 | rate, | ||
Uint32 | frames | ||
) |
Initializes an empty audio sample of the given size.
The audio sample will be in-memory (not streamed). The contents of the buffer will all be 0s. Use the getBuffer()
method to write data to this buffer.
channels | the number of audio channels |
rate | the sampling rate of this source |
frames | the number of frames in this source |
bool cugl::AudioSample::initWithData | ( | const std::shared_ptr< JsonValue > & | data | ) |
Initializes an audio sample with the given JSON specificaton.
This initializer is designed to receive the "data" object from the JSON passed to Scene2Loader
. This JSON format supports the following attribute values:
"file": The path to the source, relative to the asset directory "stream": A boolean, indicating whether to stream the sample "volume": A float, representing the volume
All attributes are optional. There are no required attributes. By default, audio samples are not streamed, meaning they are fully loaded into memory. This is recommended for sound effects, but not for music.
data | The JSON object specifying the audio sample |
|
inline |
Returns true if this is an streaming audio asset.
This method is to prevent the overhead of run-time typing.
|
protected |
The in-memory sound buffer for this sound source (OPTIONAL)
|
protected |
The number of frames in this audio sample
|
protected |
Whether or not this sample is streamed or in-memory
|
protected |
The encoding type (WAV, MP3, OGG, FLAC) of this source