Sound.hpp
1// Copyright 2019 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4
5#ifndef SMK_SOUND_HPP
6#define SMK_SOUND_HPP
7
8#include <smk/SoundBuffer.hpp>
9
10namespace smk {
11
12/// @example sound.cpp
13
14/// @brief Represent a sound being played.
15/// @see SoundBuffer
16///
17/// Example
18/// -------
19/// ~~~cpp
20/// // Load a sound file.
21/// auto sound_buffer = smk::SoundBuffer(asset::water_mp3);
22///
23/// // Create a sound source.
24/// auto sound = smk::Sound(sound_buffer);
25///
26/// // Start playing.
27/// sound.Play()
28/// ~~~
29///
30/// Please make sure to init OpenAL in main() by creating a smk::Audio.
31class Sound {
32 public:
34 Sound(const SoundBuffer& buffer);
35 ~Sound();
36
37 void Play();
38 void Stop();
39 void SetLoop(bool looping);
40 bool IsPlaying() const;
41
42 // The gain applied to the source. Default is 1.
43 void SetVolume(float volume);
44
45 // -- Move-only resource ---
46 Sound(Sound&&) noexcept;
47 Sound(const Sound&) = delete;
48 Sound& operator=(Sound&&) noexcept;
49 Sound& operator=(const Sound&) = delete;
50
51 private:
52 const SoundBuffer* buffer_ = nullptr;
53 unsigned int source_ = 0;
54 bool is_playing_ = false;
55
56 void EnsureSourceIsCreated();
57};
58
59} // namespace smk
60
61#endif /* end of include guard: SMK_SOUND_HPP */
A sound file loaded in memory.
Definition: SoundBuffer.hpp:33
Represent a sound being played.
Definition: Sound.hpp:31
void Stop()
Stop playing the sound.
Definition: Sound.cpp:63
bool IsPlaying() const
Definition: Sound.cpp:80
void SetLoop(bool looping)
Specify whether the sound must restart when it has reached the end.
Definition: Sound.cpp:74
void Play()
Start playing the sound.
Definition: Sound.cpp:49
Sound()
Create a null Sound.