smk::Sound Class Reference

#include <Sound.hpp>

Represent a sound being played. More...

Description

Represent a sound being played.

See also
SoundBuffer

Example

// Load a sound file.
auto sound_buffer = smk::SoundBuffer(asset::water_mp3);
// Create a sound source.
auto sound = smk::Sound(sound_buffer);
// Start playing.
sound.Play()
A sound file loaded in memory.
Definition: SoundBuffer.hpp:33
Represent a sound being played.
Definition: Sound.hpp:31

Please make sure to init OpenAL in main() by creating a smk::Audio.

Examples
sound.cpp.

Definition at line 31 of file Sound.hpp.

Public Member Functions

 Sound ()
 Create a null Sound. More...
 
 Sound (const SoundBuffer &buffer)
 Create a sound reading data from a SoundBuffer. More...
 
void Play ()
 Start playing the sound. More...
 
void Stop ()
 Stop playing the sound. More...
 
void SetLoop (bool looping)
 Specify whether the sound must restart when it has reached the end. More...
 
bool IsPlaying () const
 
void SetVolume (float volume)
 
 Sound (Sound &&) noexcept
 
 Sound (const Sound &)=delete
 
Soundoperator= (Sound &&) noexcept
 
Soundoperator= (const Sound &)=delete
 

Constructor & Destructor Documentation

◆ Sound() [1/3]

smk::Sound::Sound ( )
default

Create a null Sound.

◆ Sound() [2/3]

smk::Sound::Sound ( const SoundBuffer buffer)

Create a sound reading data from a SoundBuffer.

Parameters
bufferThe SoundBuffer to read the data from.

Definition at line 39 of file Sound.cpp.

39: buffer_(&buffer) {}

◆ ~Sound()

smk::Sound::~Sound ( )

Definition at line 41 of file Sound.cpp.

41 {
42 Stop();
43 if (source_) {
44 alDeleteSources(1, &source_);
45 }
46}
void Stop()
Stop playing the sound.
Definition: Sound.cpp:63

◆ Sound() [3/3]

smk::Sound::Sound ( Sound &&  o)
noexcept

Definition at line 89 of file Sound.cpp.

89 {
90 operator=(std::move(o));
91}

Member Function Documentation

◆ IsPlaying()

bool smk::Sound::IsPlaying ( ) const
Returns
return whether the sound is currently playing something or not.

Definition at line 80 of file Sound.cpp.

80 {
81 if (!source_) {
82 return false;
83 }
84 ALint state = {};
85 alGetSourcei(source_, AL_SOURCE_STATE, &state);
86 return (state == AL_PLAYING);
87}

◆ operator=()

Sound & smk::Sound::operator= ( Sound &&  o)
noexcept

Definition at line 93 of file Sound.cpp.

93 {
94 std::swap(buffer_, o.buffer_);
95 std::swap(source_, o.source_);
96 std::swap(is_playing_, o.is_playing_);
97 return *this;
98}

◆ Play()

void smk::Sound::Play ( )

Start playing the sound.

Definition at line 49 of file Sound.cpp.

49 {
50 if (!buffer_ || !buffer_->buffer()) {
51 return;
52 }
53 if (is_playing_) {
54 Stop();
55 }
56 EnsureSourceIsCreated();
57 alSourcei(source_, AL_BUFFER, ALint(buffer_->buffer()));
58 alSourcePlay(source_);
59 is_playing_ = true;
60}

◆ SetLoop()

void smk::Sound::SetLoop ( bool  looping)

Specify whether the sound must restart when it has reached the end.

Parameters
loopingwhether the sound must restart when it has reached the end.

Definition at line 74 of file Sound.cpp.

74 {
75 EnsureSourceIsCreated();
76 alSourcei(source_, AL_LOOPING, looping);
77}

◆ SetVolume()

void smk::Sound::SetVolume ( float  volume)

Definition at line 100 of file Sound.cpp.

100 {
101 if (!source_) {
102 return;
103 }
104 EnsureSourceIsCreated();
105 alSourcef(source_, AL_GAIN, volume);
106}

◆ Stop()

void smk::Sound::Stop ( )

Stop playing the sound.

Definition at line 63 of file Sound.cpp.

63 {
64 if (!source_ || !buffer_ || !is_playing_) {
65 return;
66 }
67 alSourceStop(source_);
68 alSourcei(source_, AL_BUFFER, 0);
69 is_playing_ = false;
70}

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