1 #ifndef _Sound_h_
2 #define _Sound_h_
3 
4 #include <boost/filesystem/path.hpp>
5 
6 #include <memory>
7 
8 
9 class Sound
10 {
11 public:
12     /** Temporarily disables UI sound effects, saving the old state (on or off), for later restoration upon object
13         destruction.  TempUISoundDisablers should be created at the beginning of any function in which Controls that
14         emit sounds are to be programmatically altered, e.g. the ctor of a window class that contains a ListBox with an
15         initially-selected item.  If this were not done, the list-select sound would be played when the window was
16         constructed, which would make the sound seem to be malfunctioning. */
17     struct TempUISoundDisabler
18     {
19         TempUISoundDisabler();
20         ~TempUISoundDisabler();
21     };
22 
23     class InitializationFailureException : public std::runtime_error {
24     public:
InitializationFailureException(const std::string & s)25         explicit InitializationFailureException(const std::string& s) : std::runtime_error(s) {}
26     };
27 
28     /** Returns the singleton instance of Sound.*/
29     static Sound& GetSound();
30 
31     /** Plays a music file.  The file will be played in an infinitve loop if \a loop is < 0, and it will be played \a
32         loops + 1 times otherwise. */
33     void PlayMusic(const boost::filesystem::path& path, int loops = 0);
34 
35     /** Pauses music play, to be continued from the same position */
36     void PauseMusic();
37 
38     /** Resumes music play */
39     void ResumeMusic();
40 
41     /** Stops playing music. */
42     void StopMusic();
43 
44     /** Plays a sound file. */
45     void PlaySound(const boost::filesystem::path& path, bool is_ui_sound = false);
46 
47     /** Frees the cached sound data associated with the filename. */
48     void FreeSound(const boost::filesystem::path& path);
49 
50     /** Frees all cached sound data. */
51     void FreeAllSounds();
52 
53     /** Sets the music volume from 0 (muted) to 255 (full volume); \a vol is range-adjusted. */
54     void SetMusicVolume(int vol);
55 
56     /** Sets the UI sounds volume from 0 (muted) to 255 (full volume); \a vol is range-adjusted. */
57     void SetUISoundsVolume(int vol);
58 
59     /** Does the work that must be done by the sound system once per frame. */
60     void DoFrame();
61 
62     /** Enables the sound system.  Throws runtime_error on failure. */
63     void Enable();
64 
65     /** Disable the sound system. */
66     void Disable();
67 
68 private:
69     class Impl;
70 
71     std::unique_ptr<Impl> const m_impl;
72 
73     Sound();
74 
75     ~Sound();
76 };
77 
78 #endif // _Sound_h_
79