CUGL 2.5
Cornell University Game Library
|
#include <CUAudioDecoder.h>
Public Member Functions | |
AudioDecoder () | |
~AudioDecoder () | |
bool | init (const std::string file) |
bool | init (const std::string file, AudioType type) |
void | dispose () |
double | getDuration () const |
Uint32 | getSampleRate () const |
Uint64 | getLength () const |
Uint32 | getChannels () const |
std::string | getFile () const |
Uint32 | getPageSize () const |
bool | ready () |
Sint32 | pagein (float *buffer) |
Uint32 | getPage () const |
void | setPage (Uint32 page) |
Uint32 | getPageCount () const |
void | rewind () |
Sint64 | decode (float *buffer) |
Static Public Member Functions | |
static std::shared_ptr< AudioDecoder > | alloc (const std::string file) |
static std::shared_ptr< AudioDecoder > | alloc (const std::string file, AudioType type) |
Protected Attributes | |
std::string | _file |
AudioType | _type |
Uint8 | _channels |
Uint32 | _rate |
Uint64 | _frames |
Uint32 | _pagesize |
Uint32 | _currpage |
Uint32 | _lastpage |
ATK_AudioSource * | _source |
This class represents an audio file decoder.
An audio file decoder takes an audio file and converts into a linear-PCM stream. This stream is used by the classes AudioSample
(to read the audio data into memory) and audio::AudioPlayer
(to play an audio stream directly from the file).
This decoder supports all the file types in AudioType
, with the exception of AudioType#IN_MEMORY
. The restrictions for the various file types are described in the enumeration for that type.
This class ensures that all memory pages are uniform in size. When the page size is variable, this decoder tries to balance memory requirements paging efficiency.
The decoder always interleaves the audio channels. MP3 and WAV ADPCM only support mono or stereo. But all other formats can support more channels. SDL supports up to 8 channels (7.1 stereo) in general. Note that the channel layout for OGG data is nonstandard (e.g. channels > 3 are not stereo compatible), so this decoder standardizes the channel layout to agree with FLAC and other data encodings.
A decoder is NOT thread safe. If a decoder is used by an audio thread, then it should not be accessed directly in the main thread, and vice versa.
cugl::AudioDecoder::AudioDecoder | ( | ) |
Creates an initialized audio decoder
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 decoder, disposing of all resources.
|
inlinestatic |
Creates a newly allocated decoder for the given file.
The AudioType
of the file will be inferred from the file suffix. If this audio type is not correct, this allocator will fail and return nullptr.
file | the source file for the decoder |
|
inlinestatic |
Creates a newly allocated decoder for the given file.
If the audio type is not correct for this file, this allocator will fail and return nullptr.
file | the source file for the decoder |
type | the codec type for this file |
Sint64 cugl::AudioDecoder::decode | ( | float * | buffer | ) |
Decodes the entire audio file, storing its value in buffer.
The buffer should be able to hold channels * frames many elements. The data is interpretted as floats and channels are all interleaved. If the method returns -1, then an error occurred during reading.
buffer | The buffer to store the audio data |
void cugl::AudioDecoder::dispose | ( | ) |
Deletes the decoder resources and resets all attributes.
This will close the associated file. You must reinitialize the decoder to use it.
|
inline |
Returns the number of channels used by this sound source
A value of 1 means mono, while 2 means stereo. Depending on the file format, other channels are possible. For example, 6 channels means support for 5.1 surround sound.
We support up to 32 possible channels.
|
inline |
Returns the length of this sound source in seconds.
The accuracy of this method depends on the specific implementation.
|
inline |
Returns the file for this audio source
This value is the empty string if there was no source file.
|
inline |
Returns the frame length of this sound source.
The frame length is the duration times the sample rate.
|
inline |
Returns the current page of this decoder
This value is the next page to be read in with the pagein()
command.
|
inline |
Returns the total number of pages in this decoder
This value is the maximum value for the setPage
command.
|
inline |
Returns the number of frames in a single page of data
When multiplied by the number of channels, this gives the number of samples read per page
|
inline |
Returns the sample rate of this sound source.
bool cugl::AudioDecoder::init | ( | const std::string | file | ) |
Initializes a new decoder for the given file.
The AudioType
of the file will be inferred from the file suffix. If this audio type is not correct, this initializer will fail and return false.
file | the source file for the decoder |
bool cugl::AudioDecoder::init | ( | const std::string | file, |
AudioType | type | ||
) |
Initializes a new decoder for the given file and type.
If the audio type is not correct for this file, this initializer will fail and return false.
file | the source file for the decoder |
type | the codec type for this file |
Sint32 cugl::AudioDecoder::pagein | ( | float * | buffer | ) |
Reads a page of data into the provided buffer.
The buffer should be able to hold channels * page size many elements. The data is interpretted as floats and channels are all interleaved. If a full page is read, this method should return the page size. If it reads less, it will return the number of frames read. It will return -1 on a processing error.
buffer | The buffer to store the audio data |
|
inline |
Returns true if there are still data to be read by the decoder
This value will return false if the decoder is at the end of the file
|
inline |
Rewinds this decoder back the beginning of the stream
void cugl::AudioDecoder::setPage | ( | Uint32 | page | ) |
Sets the current page of this decoder
This value is the next page to be read in with the pagein()
command. If the page is greater than the total number of pages, it will be set just beyond the last page.
page | The new page of this decoder |
|
protected |
The number of channels in this sound source (max 32)
|
protected |
The current page in the stream
|
protected |
The source for this decoder
|
protected |
The number of frames in this sounds source
|
protected |
The final page in the stream
|
protected |
The size of a decoder chunk
|
protected |
The sampling rate (frequency) of this sound source
|
protected |
The underlying decoder from SDL_codec
|
protected |
The codec type for the audio file