Public Member Functions | List of all members
cpp3ds::InputSoundFile Class Reference

Provide read access to sound files. More...

#include <InputSoundFile.hpp>

Inheritance diagram for cpp3ds::InputSoundFile:
cpp3ds::NonCopyable

Public Member Functions

 InputSoundFile ()
 Default constructor. More...
 
 ~InputSoundFile ()
 Destructor. More...
 
bool openFromFile (const std::string &filename)
 Open a sound file from the disk for reading. More...
 
bool openFromMemory (const void *data, std::size_t sizeInBytes)
 Open a sound file in memory for reading. More...
 
bool openFromStream (InputStream &stream)
 Open a sound file from a custom stream for reading. More...
 
bool openForWriting (const std::string &filename, unsigned int channelCount, unsigned int sampleRate)
 Open the sound file from the disk for writing. More...
 
Uint64 getSampleCount () const
 Get the total number of audio samples in the file. More...
 
unsigned int getChannelCount () const
 Get the number of channels used by the sound. More...
 
unsigned int getSampleRate () const
 Get the sample rate of the sound. More...
 
Time getDuration () const
 Get the total duration of the sound file. More...
 
void seek (Uint64 sampleOffset)
 Change the current read position to the given sample offset. More...
 
void seek (Time timeOffset)
 Change the current read position to the given time offset. More...
 
Uint64 read (Int16 *samples, Uint64 maxCount)
 Read audio samples from the open file. More...
 

Detailed Description

Provide read access to sound files.

This class decodes audio samples from a sound file.

It is used internally by higher-level classes such as cpp3ds::SoundBuffer and cpp3ds::Music, but can also be useful if you want to process or analyze audio files without playing them, or if you want to implement your own version of cpp3ds::Music with more specific features.

Usage example:

// Open a sound file
if (!file.openFromFile("music.ogg"))
/* error */;
// Print the sound attributes
std::cout << "duration: " << file.getDuration().asSeconds() << std::endl;
std::cout << "channels: " << file.getChannelCount() << std::endl;
std::cout << "sample rate: " << file.getSampleRate() << std::endl;
std::cout << "sample count: " << file.getSampleCount() << std::endl;
// Read and process batches of samples until the end of file is reached
cpp3ds::Int16 samples[1024];
cpp3ds::Uint64 count;
do
{
count = file.read(samples, 1024);
// process, analyze, play, convert, or whatever
// you want to do with the samples...
}
while (count > 0);
See also
cpp3ds::SoundFileReader, cpp3ds::OutputSoundFile

Definition at line 45 of file InputSoundFile.hpp.

Constructor & Destructor Documentation

cpp3ds::InputSoundFile::InputSoundFile ( )

Default constructor.

cpp3ds::InputSoundFile::~InputSoundFile ( )

Destructor.

Member Function Documentation

unsigned int cpp3ds::InputSoundFile::getChannelCount ( ) const

Get the number of channels used by the sound.

Returns
Number of channels (1 = mono, 2 = stereo)
Time cpp3ds::InputSoundFile::getDuration ( ) const

Get the total duration of the sound file.

This function is provided for convenience, the duration is deduced from the other sound file attributes.

Returns
Duration of the sound file
Uint64 cpp3ds::InputSoundFile::getSampleCount ( ) const

Get the total number of audio samples in the file.

Returns
Number of samples
unsigned int cpp3ds::InputSoundFile::getSampleRate ( ) const

Get the sample rate of the sound.

Returns
Sample rate, in samples per second
bool cpp3ds::InputSoundFile::openForWriting ( const std::string &  filename,
unsigned int  channelCount,
unsigned int  sampleRate 
)

Open the sound file from the disk for writing.

Parameters
filenamePath of the sound file to write
channelCountNumber of channels in the sound
sampleRateSample rate of the sound
Returns
True if the file was successfully opened
bool cpp3ds::InputSoundFile::openFromFile ( const std::string &  filename)

Open a sound file from the disk for reading.

The supported audio formats are: WAV, OGG/Vorbis, FLAC.

Parameters
filenamePath of the sound file to load
Returns
True if the file was successfully opened
bool cpp3ds::InputSoundFile::openFromMemory ( const void *  data,
std::size_t  sizeInBytes 
)

Open a sound file in memory for reading.

The supported audio formats are: WAV, OGG/Vorbis, FLAC.

Parameters
dataPointer to the file data in memory
sizeInBytesSize of the data to load, in bytes
Returns
True if the file was successfully opened
bool cpp3ds::InputSoundFile::openFromStream ( InputStream stream)

Open a sound file from a custom stream for reading.

The supported audio formats are: WAV, OGG/Vorbis, FLAC.

Parameters
streamSource stream to read from
Returns
True if the file was successfully opened
Uint64 cpp3ds::InputSoundFile::read ( Int16 *  samples,
Uint64  maxCount 
)

Read audio samples from the open file.

Parameters
samplesPointer to the sample array to fill
maxCountMaximum number of samples to read
Returns
Number of samples actually read (may be less than maxCount)
void cpp3ds::InputSoundFile::seek ( Uint64  sampleOffset)

Change the current read position to the given sample offset.

This function takes a sample offset to provide maximum precision. If you need to jump to a given time, use the other overload.

If the given offset exceeds to total number of samples, this function jumps to the end of the sound file.

Parameters
sampleOffsetIndex of the sample to jump to, relative to the beginning
void cpp3ds::InputSoundFile::seek ( Time  timeOffset)

Change the current read position to the given time offset.

Using a time offset is handy but imprecise. If you need an accurate result, consider using the overload which takes a sample offset.

If the given time exceeds to total duration, this function jumps to the end of the sound file.

Parameters
timeOffsetTime to jump to, relative to the beginning

The documentation for this class was generated from the following file: