CorsixTH engine (the C++ part)
Open source implementation of Theme Hospital
Loading...
Searching...
No Matches
th_sound.h
Go to the documentation of this file.
1/*
2Copyright (c) 2009 Peter "Corsix" Cawley
3
4Permission is hereby granted, free of charge, to any person obtaining a copy of
5this software and associated documentation files (the "Software"), to deal in
6the Software without restriction, including without limitation the rights to
7use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8of the Software, and to permit persons to whom the Software is furnished to do
9so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall be included in all
12copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20SOFTWARE.
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
36 public:
37 bool load_from_th_file(const uint8_t* pData, size_t iDataLength);
38
40 size_t get_number_of_sounds() const;
41
43 const char* get_sound_name(size_t iIndex) const;
44
46 size_t get_sound_duration(size_t iIndex);
47
49
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
66 public:
68 static constexpr uint32_t null_handle = 0u;
69 static constexpr int number_of_channels = 32;
70
72 sound_player(const sound_player&) = delete;
75
77
79
90 uint32_t play(size_t iIndex, double dVolume, int loops);
91
102 uint32_t play_at(size_t iIndex, int iX, int iY, int loops);
103
116 uint32_t play_at(size_t iIndex, double dVolume, int iX, int iY, int loops);
117
123
127 void stop(uint32_t handle);
128
130 bool is_playing(uint32_t handle);
131
133 void set_sound_effect_volume(double dVolume);
134
138
140 void set_camera(int iX, int iY, int iRadius);
141
148 int reserve_channel();
149
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
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
175 std::array<uint32_t, number_of_channels> channels{};
176 uint32_t next_playing_track_handle{0};
177
183 std::recursive_mutex channel_mutex{};
184};
185
186#endif // CORSIX_TH_TH_SOUND_H_
Utility class for accessing Theme Hospital's SOUND-0.DAT.
Definition th_sound.h:35
size_t get_number_of_sounds() const
Returns the number of sounds present in the archive.
Definition th_sound.cpp:90
size_t get_sound_duration(size_t iIndex)
Gets the duration (in milliseconds) of the sound at a given index.
Definition th_sound.cpp:116
SDL_RWops * load_sound(size_t iIndex)
Opens the sound at a given index into an SDL_RWops structure.
Definition th_sound.cpp:192
const char * get_sound_name(size_t iIndex) const
Gets the name of the sound at a given index.
Definition th_sound.cpp:94
bool load_from_th_file(const uint8_t *pData, size_t iDataLength)
Definition th_sound.cpp:46
Definition th_sound.h:65
~sound_player()
Definition th_sound.cpp:226
bool is_playing(uint32_t handle)
Returns whether the sound matching the given handle is playing.
Definition th_sound.cpp:321
int reserve_channel()
Definition th_sound.cpp:333
static constexpr uint32_t null_handle
Definition th_sound.h:68
static sound_player * get_singleton()
Definition th_sound.cpp:240
sound_player(const sound_player &)=delete
static constexpr int number_of_channels
Definition th_sound.h:69
toggle_pause_result toggle_pause(uint32_t handle)
Definition th_sound.cpp:298
void release_channel(int iChannel)
Releases a previously reserved SDL_mixer channel.
Definition th_sound.cpp:349
uint32_t play_at(size_t iIndex, int iX, int iY, int loops)
Definition th_sound.cpp:273
void set_sound_effects_enabled(bool bOn)
Definition th_sound.cpp:329
void set_sound_effect_volume(double dVolume)
Sets the default volume for sound effects.
Definition th_sound.cpp:325
sound_player & operator=(const sound_player &)=delete
uint32_t play(size_t iIndex, double dVolume, int loops)
Definition th_sound.cpp:264
void set_camera(int iX, int iY, int iRadius)
Sets the position of the camera for play_at calculations.
Definition th_sound.cpp:366
toggle_pause_result
Definition th_sound.h:67
void populate_from(sound_archive *pArchive)
Definition th_sound.cpp:242
sound_player()
Definition th_sound.cpp:204
void stop(uint32_t handle)
Definition th_sound.cpp:312