Basics of SFML sound
Anything audio related falls into one of two categories within SFML: sf::Sound
that represents short sound effects, or sf::Music
that is used to play longer audio tracks. It's prudent that we understand how these two classes are used before continuing further. Let's talk about each one individually.
Playing sounds
The sf::Sound
class is extremely lightweight and should only ever be used to play short sound effects that don't take up a lot of memory. The way it stores and utilizes actual audio files is by using a sf::SoundBuffer
instance. It is analogous to sf::Sprite
and the way it uses an instance of sf::Texture
for drawing. The sf::SoundBuffer
is used to hold audio data in memory, which the sf::Sound
class then reads and plays from. It can be used as follows:
sf::SoundBuffer buffer; buffer.loadFromFile("SomeSound.ogg"); sf::Sound sound(buffer); sound.setBuffer(buffer); // Alternative.
As you can see, a sound buffer can be attached to an instance...