Storage for audio samples defining a sound. More...
#include <SoundBuffer.hpp>
Public Member Functions | |
SoundBuffer () | |
Default constructor. | |
SoundBuffer (const SoundBuffer ©) | |
Copy constructor. | |
~SoundBuffer () | |
Destructor. | |
bool | LoadFromFile (const std::string &filename) |
Load the sound buffer from a file. | |
bool | LoadFromMemory (const void *data, std::size_t sizeInBytes) |
Load the sound buffer from a file in memory. | |
bool | LoadFromSamples (const Int16 *samples, std::size_t samplesCount, unsigned int channelsCount, unsigned int sampleRate) |
Load the sound buffer from an array of audio samples. | |
bool | SaveToFile (const std::string &filename) const |
Save the sound buffer to an audio file. | |
const Int16 * | GetSamples () const |
Get the array of audio samples stored in the buffer. | |
std::size_t | GetSamplesCount () const |
Get the number of samples stored in the buffer. | |
unsigned int | GetSampleRate () const |
Get the sample rate of the sound. | |
unsigned int | GetChannelsCount () const |
Get the number of channels used by the sound. | |
float | GetDuration () const |
Get the total duration of the sound. | |
SoundBuffer & | operator= (const SoundBuffer &right) |
Overload of assignment operator. | |
Friends | |
class | Sound |
Storage for audio samples defining a sound.
A sound buffer holds the data of a sound, which is an array of audio samples.
A sample is a 16 bits signed integer that defines the amplitude of the sound at a given time. The sound is then restituted by playing these samples at a high rate (for example, 44100 samples per second is the standard rate used for playing CDs). In short, audio samples are like image pixels, and a sf::SoundBuffer is similar to a sf::Image.
A sound buffer can be loaded from a file (see LoadFromFile() for the complete list of supported formats), from memory or directly from an array of samples. It can also be saved back to a file.
Sound buffers alone are not very useful: they hold the audio data but cannot be played. To do so, you need to use the sf::Sound class, which provides functions to play/pause/stop the sound as well as changing the way it is outputted (volume, pitch, 3D position, ...). This separation allows more flexibility and better performances: indeed a sf::SoundBuffer is a heavy resource, and any operation on it is slow (often too slow for real-time applications). On the other side, a sf::Sound is a lightweight object, which can use the audio data of a sound buffer and change the way it is played without actually modifying that data. Note that it is also possible to bind several sf::Sound instances to the same sf::SoundBuffer.
It is important to note that the sf::Sound instance doesn't copy the buffer that it uses, it only keeps a reference to it. Thus, a sf::SoundBuffer must not be destructed while it is used by a sf::Sound (i.e. never write a function that uses a local sf::SoundBuffer instance for loading a sound).
Usage example:
// Declare a new sound buffer sf::SoundBuffer buffer; // Load it from a file if (!buffer.LoadFromFile("sound.wav")) { // error... } // Create a sound source and bind it to the buffer sf::Sound sound1; sound1.SetBuffer(buffer); // Play the sound sound1.Play(); // Create another sound source bound to the same buffer sf::Sound sound2; sound2.SetBuffer(buffer); // Play it with a higher pitch -- the first sound remains unchanged sound2.SetPitch(2); sound2.Play();
Definition at line 46 of file SoundBuffer.hpp.
sf::SoundBuffer::SoundBuffer | ( | ) |
Default constructor.
Definition at line 40 of file SoundBuffer.cpp.
sf::SoundBuffer::SoundBuffer | ( | const SoundBuffer & | copy | ) |
sf::SoundBuffer::~SoundBuffer | ( | ) |
Destructor.
Definition at line 67 of file SoundBuffer.cpp.
unsigned int sf::SoundBuffer::GetChannelsCount | ( | ) | const |
Get the number of channels used by the sound.
If the sound is mono then the number ofchannels will be 1, 2 for stereo, etc.
Definition at line 211 of file SoundBuffer.cpp.
float sf::SoundBuffer::GetDuration | ( | ) | const |
Get the total duration of the sound.
Definition at line 221 of file SoundBuffer.cpp.
unsigned int sf::SoundBuffer::GetSampleRate | ( | ) | const |
Get the sample rate of the sound.
The sample rate is the number of samples played per second. The higher, the better the quality (for example, 44100 samples/s is CD quality).
Definition at line 201 of file SoundBuffer.cpp.
const Int16 * sf::SoundBuffer::GetSamples | ( | ) | const |
Get the array of audio samples stored in the buffer.
The format of the returned samples is 16 bits signed integer (sf::Int16). The total number of samples in this array is given by the GetSamplesCount() function.
Definition at line 187 of file SoundBuffer.cpp.
std::size_t sf::SoundBuffer::GetSamplesCount | ( | ) | const |
Get the number of samples stored in the buffer.
The array of samples can be accessed with the GetSamples() function.
Definition at line 194 of file SoundBuffer.cpp.
bool sf::SoundBuffer::LoadFromFile | ( | const std::string & | filename | ) |
Load the sound buffer from a file.
Here is a complete list of all the supported audio formats: ogg, wav, flac, aiff, au, raw, paf, svx, nist, voc, ircam, w64, mat4, mat5 pvf, htk, sds, avr, sd2, caf, wve, mpc2k, rf64.
filename | Path of the sound file to load |
Definition at line 80 of file SoundBuffer.cpp.
bool sf::SoundBuffer::LoadFromMemory | ( | const void * | data, | |
std::size_t | sizeInBytes | |||
) |
Load the sound buffer from a file in memory.
Here is a complete list of all the supported audio formats: ogg, wav, flac, aiff, au, raw, paf, svx, nist, voc, ircam, w64, mat4, mat5 pvf, htk, sds, avr, sd2, caf, wve, mpc2k, rf64.
data | Pointer to the file data in memory | |
sizeInBytes | Size of the data to load, in bytes |
Definition at line 111 of file SoundBuffer.cpp.
bool sf::SoundBuffer::LoadFromSamples | ( | const Int16 * | samples, | |
std::size_t | samplesCount, | |||
unsigned int | channelsCount, | |||
unsigned int | sampleRate | |||
) |
Load the sound buffer from an array of audio samples.
The assumed format of the audio samples is 16 bits signed integer (sf::Int16).
samples | Pointer to the array of samples in memory | |
samplesCount | Number of samples in the array | |
channelsCount | Number of channels (1 = mono, 2 = stereo, ...) | |
sampleRate | Sample rate (number of samples to play per second) |
Definition at line 142 of file SoundBuffer.cpp.
SoundBuffer & sf::SoundBuffer::operator= | ( | const SoundBuffer & | right | ) |
Overload of assignment operator.
right | Instance to assign |
Definition at line 228 of file SoundBuffer.cpp.
bool sf::SoundBuffer::SaveToFile | ( | const std::string & | filename | ) | const |
Save the sound buffer to an audio file.
Here is a complete list of all the supported audio formats: ogg, wav, flac, aiff, au, raw, paf, svx, nist, voc, ircam, w64, mat4, mat5 pvf, htk, sds, avr, sd2, caf, wve, mpc2k, rf64.
filename | Path of the sound file to write |
Definition at line 168 of file SoundBuffer.cpp.