SoundBuffer

Storage for audio samples defining a sound.

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 texture pixels, and a SoundBuffer is similar to a Texture.

A sound buffer can be loaded from a file (see loadFromFile() for the complete list of supported formats), from memory, from a custom stream (see InputStream) 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 Sound instances to the same SoundBuffer.

It is important to note that the Sound instance doesn't copy the buffer that it uses, it only keeps a reference to it. Thus, a SoundBuffer must not be destructed while it is used by a Sound (i.e. never write a function that uses a local SoundBuffer instance for loading a sound).

Constructors

this
this()
Undocumented in source.

Destructor

~this
~this()
Undocumented in source.

Members

Functions

getChannelCount
uint getChannelCount()

Get the number of channels used by the sound.

getDuration
Duration getDuration()

Get the total duration of the sound.

getSampleRate
uint getSampleRate()

Get the sample rate of the sound.

getSamples
const(short[]) getSamples()

Get the array of audio samples stored in the buffer.

loadFromFile
bool loadFromFile(string filename)

Load the sound buffer from a file.

loadFromMemory
bool loadFromMemory(const(void)[] data)

Load the sound buffer from a file in memory.

loadFromSamples
bool loadFromSamples(const(short[]) samples, uint channelCount, uint sampleRate)

Load the sound buffer from an array of audio samples.

loadFromStream
bool loadFromStream(InputStream stream)
Undocumented in source. Be warned that the author may not have intended to support it.
saveToFile
bool saveToFile(string filename)

Save the sound buffer to an audio file.

Variables

sfPtr
sfSoundBuffer* sfPtr;
Undocumented in source.

See Also

Meta

Authors

Laurent Gomila, Jeremy DeHaan