Debian Codegraph @

CorsixTH/Src/th_sound.h

raw
1 /*
2 Copyright (c) 2009 Peter "Corsix" Cawley
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23 #ifndef CORSIX_TH_TH_SOUND_H_
24 #define CORSIX_TH_TH_SOUND_H_
25 #include "config.h"
26
27 #include <SDL_mixer.h>
28 #include <SDL_rwops.h>
29
30 #include <array>
31 #include <mutex>
32 #include <vector>
33
34 //! Utility class for accessing Theme Hospital's SOUND-0.DAT
35 class sound_archive {
36 public:
37 bool load_from_th_file(const uint8_t* pData, size_t iDataLength);
38
39 //! Returns the number of sounds present in the archive
40 size_t get_number_of_sounds() const;
41
42 //! Gets the name of the sound at a given index
43 const char* get_sound_name(size_t iIndex) const;
44
45 //! Gets the duration (in milliseconds) of the sound at a given index
46 size_t get_sound_duration(size_t iIndex);
47
48 //! Opens the sound at a given index into an SDL_RWops structure
49 /*!
50 The caller is responsible for closing/freeing the result.
51 */
52 SDL_RWops* load_sound(size_t iIndex);
53
54 private:
55 struct sound_dat_sound_info {
56 std::array<char, 18> sound_name;
57 uint32_t position;
58 uint32_t length;
59 };
60
61 std::vector<uint8_t> data;
62 std::vector<sound_dat_sound_info> sound_files;
63 };
64
65 class sound_player {
66 public:
67 enum class toggle_pause_result { error, paused, resumed };
68 static constexpr uint32_t null_handle = 0u;
69 static constexpr int number_of_channels = 32;
70
71 sound_player();
72 sound_player(const sound_player&) = delete;
73 sound_player& operator=(const sound_player&) = delete;
74 ~sound_player();
75
76 static sound_player* get_singleton();
77
78 void populate_from(sound_archive* pArchive);
79
80 //! Plays the sound effect in the sound_archive with the given index.
81 //!
82 //! \param iIndex Index of the sound effect to play.
83 //! \param dVolume Volume to play the sound effect at, in the range 0.0 to
84 //! 1.0.
85 //! \param loops The number of times to play the sound, -1 for 'practically'
86 //! infinite.
87 //! \return The sound handle
88 //! \see <a
89 //! href="https://wiki.libsdl.org/SDL2_mixer/Mix_PlayChannel">Mix_PlayChannel</a>
90 uint32_t play(size_t iIndex, double dVolume, int loops);
91
92 //! Plays the sound effect in the sound_archive with the given index with
93 //! volume attenuation based on the distance from the given position to the
94 //! camera.
95 //!
96 //! \param iIndex Index of the sound effect to play.
97 //! \param iX X coordinate of the sound effect.
98 //! \param iY Y coordinate of the sound effect.
99 //! \param loops The number of times to play the sound, -1 for 'practically'
100 //! infinite.
101 //! \return The sound handle or 0 on error
102 uint32_t play_at(size_t iIndex, int iX, int iY, int loops);
103
104 //! Plays the sound effect in the sound_archive with the given index with
105 //! volume attenuation based on the distance from the given position to the
106 //! camera.
107 //!
108 //! \param iIndex Index of the sound effect to play.
109 //! \param dVolume Volume to play the sound effect at before attenuation, in
110 //! the range 0.0 to 1.0.
111 //! \param iX X coordinate of the sound effect.
112 //! \param iY Y coordinate of the sound effect.
113 //! \param loops The number of times to play the sound, -1 for 'practically'
114 //! infinite.
115 //! \return The sound handle or 0 on error
116 uint32_t play_at(size_t iIndex, double dVolume, int iX, int iY, int loops);
117
118 //! Pause playback on a given channel if playing, or resume if paused.
119 //!
120 //! \param handle The sound to toggle pause on.
121 //! \return The result of the toggle: paused, resumed, or error.
122 toggle_pause_result toggle_pause(uint32_t handle);
123
124 //! Stops playback on a given channel.
125 //!
126 //! \param handle The sound to stop.
127 void stop(uint32_t handle);
128
129 //! Returns whether the sound matching the given handle is playing
130 bool is_playing(uint32_t handle);
131
132 //! Sets the default volume for sound effects.
133 void set_sound_effect_volume(double dVolume);
134
135 //! Enables or disables sound effects.
136 //! Note: Only affects sounds played via play_at(int, int, int).
137 void set_sound_effects_enabled(bool bOn);
138
139 //! Sets the position of the camera for play_at calculations.
140 void set_camera(int iX, int iY, int iRadius);
141
142 //! Reserves an SDL_mixer channel for exclusive use.
143 //!
144 //! Not necessary to use in conjunction with play() or play_at(), as these
145 //! methods will automatically reserve and release channels.
146 //!
147 //! \return The reserved channel index, or -1 if no channels are available.
148 int reserve_channel();
149
150 //! Releases a previously reserved SDL_mixer channel.
151 void release_channel(int iChannel);
152
153 private:
154 static sound_player* singleton;
155 static void on_channel_finished(int iChannel);
156
157 uint32_t play_raw(size_t iIndex, int iVolume, int loops);
158
159 //! Returns the channel that the handle is playing on or -1 if it is not
160 //! playing.
161 int playing_channel_for_handle(uint32_t handle);
162
163 Mix_Chunk** sounds;
164 size_t sound_count;
165 int camera_x;
166 int camera_y;
167 double camera_radius;
168 double master_volume;
169 double sound_effect_volume;
170 int positionless_volume;
171 bool sound_effects_enabled;
172
173 //! Each channel holds the handle of the track playing on it or null_handle
174 //! if it is free.
175 std::array<uint32_t, number_of_channels> channels{};
176 uint32_t next_playing_track_handle{0};
177
178 /// Mutex to protect access to channels array and next_playing_track_handle.
179 ///
180 /// \remark Do not hold this mutex while calling any SDL_mixer (Mix_*)
181 /// functions as SDL_mixer has its own internal lock which is held while
182 /// calling on_channel_finished and could lead to deadlocks.
183 std::recursive_mutex channel_mutex{};
184 };
185
186 #endif // CORSIX_TH_TH_SOUND_H_