Debian Codegraph @

common/main/digi.h

raw
1 /*
2 * Portions of this file are copyright Rebirth contributors and licensed as
3 * described in COPYING.txt.
4 * Portions of this file are copyright Parallax Software and licensed
5 * according to the Parallax license below.
6 * See COPYING.txt for license details.
7
8 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
9 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
10 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
11 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
12 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
13 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
14 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
15 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
16 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
17 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
18 */
19
20 /*
21 *
22 * Include file for sound hardware.
23 *
24 */
25
26 #pragma once
27
28 #include <SDL_version.h>
29 #include <type_traits>
30 #include "pstypes.h"
31
32 #include "dxxsconf.h"
33 #include "dsx-ns.h"
34 #include "fwd-object.h"
35 #include "fwd-segment.h"
36 #include "fwd-piggy.h"
37 #include <memory>
38 #include <span>
39 #include <utility>
40
41 namespace dcx {
42
43 #ifdef _WIN32
44 // Windows native-MIDI stuff.
45 void digi_win32_pause_midi_song();
46 void digi_win32_resume_midi_song();
47 void digi_win32_stop_midi_song();
48 void digi_win32_set_midi_volume(int mvolume);
49 int digi_win32_play_midi_song(const char * filename, int loop);
50 #endif
51
52 }
53
54 #ifdef DXX_BUILD_DESCENT
55 namespace dcx {
56
57 enum class sound_pan : int
58 {
59 };
60
61 enum class sound_channel : uint8_t
62 {
63 None = UINT8_MAX,
64 };
65
66 struct sound_object;
67 extern int digi_volume;
68
69 enum class sound_stack : bool
70 {
71 allow_stacking,
72 cancel_previous,
73 };
74
75 enum class sound_sample_rate : uint16_t
76 {
77 _11k = 11025,
78 _22k = 22050,
79 _44k = 44100,
80 };
81
82 extern sound_channel SoundQ_channel;
83
84 /* These values are written to a file as integers, so they must not be
85 * renumbered.
86 */
87 enum class music_type : uint8_t
88 {
89 None,
90 Builtin,
91 #if DXX_USE_SDL_REDBOOK_AUDIO
92 Redbook,
93 #endif
94 Custom = 3,
95 };
96
97 }
98
99 namespace dsx {
100
101 extern int digi_init();
102 extern void digi_close();
103
104 // Volume is max at F1_0.
105 void digi_play_sample(sound_effect sndnum, fix max_volume);
106 void digi_play_sample_once(sound_effect sndnum, fix max_volume);
107 #ifdef DXX_BUILD_DESCENT
108 void digi_link_sound_to_object(sound_effect soundnum, vcobjptridx_t objnum, uint8_t forever, fix max_volume, sound_stack once);
109 void digi_kill_sound_linked_to_segment(vmsegidx_t segnum, sidenum_t sidenum, sound_effect soundnum);
110 void digi_link_sound_to_pos(sound_effect soundnum, vcsegptridx_t segnum, sidenum_t sidenum, const vms_vector &pos, int forever, fix max_volume);
111 // Same as above, but you pass the max distance sound can be heard. The old way uses f1_0*256 for max_distance.
112 void digi_link_sound_to_object2(sound_effect soundnum, vcobjptridx_t objnum, uint8_t forever, fix max_volume, sound_stack once, vm_distance max_distance);
113 void digi_link_sound_to_object3(sound_effect soundnum, vcobjptridx_t objnum, uint8_t forever, fix max_volume, sound_stack once, vm_distance max_distance, int loop_start, int loop_end);
114 void digi_kill_sound_linked_to_object(vcobjptridx_t);
115 #endif
116
117 void digi_play_sample_3d(sound_effect soundno, sound_pan angle, int volume); // Volume from 0-0x7fff
118
119 extern void digi_init_sounds();
120 extern void digi_sync_sounds();
121
122 extern void digi_set_digi_volume( int dvolume );
123
124 extern void digi_pause_digi_sounds();
125 extern void digi_resume_digi_sounds();
126
127 sound_effect digi_xlat_sound(sound_effect soundno);
128
129 void digi_stop_sound(sound_channel channel);
130
131 // Volume 0-F1_0
132 constexpr sound_object *sound_object_none = nullptr;
133 sound_channel digi_start_sound(sound_effect soundnum, fix volume, sound_pan pan, int looping, int loop_start, int loop_end, sound_object *);
134
135 // Stops all sounds that are playing
136 void digi_stop_all_channels();
137
138 void digi_stop_digi_sounds();
139
140 void digi_end_sound(sound_channel channel);
141 void digi_set_channel_pan(sound_channel channel, sound_pan pan);
142 void digi_set_channel_volume(sound_channel channel, int volume);
143 int digi_is_channel_playing(sound_channel channel);
144
145 void digi_play_sample_looping(sound_effect soundno, fix max_volume,int loop_start, int loop_end );
146 extern void digi_change_looping_volume( fix volume );
147 extern void digi_stop_looping_sound();
148
149 // Plays a queued voice sound.
150 void digi_start_sound_queued(sound_effect soundnum, fix volume);
151
152 // Following declarations are for the runtime switching system
153
154 #define SOUND_MAX_VOLUME F1_0 / 2
155
156 extern int Dont_start_sound_objects;
157 void digi_select_system();
158
159 void digi_end_soundobj(sound_object &);
160 void SoundQ_end();
161 #ifndef NDEBUG
162 void verify_sound_channel_free(sound_channel channel);
163 #endif
164
165 class RAIIdigi_sound
166 {
167 static constexpr std::integral_constant<sound_channel, sound_channel::None> invalid_channel{};
168 sound_channel channel = invalid_channel;
169 static void stop(const sound_channel channel)
170 {
171 if (channel != invalid_channel)
172 digi_stop_sound(channel);
173 }
174 public:
175 ~RAIIdigi_sound()
176 {
177 stop(channel);
178 }
179 void reset(const sound_channel c = invalid_channel)
180 {
181 stop(std::exchange(channel, c));
182 }
183 operator int() const = delete;
184 explicit operator bool() const
185 {
186 return channel != invalid_channel;
187 }
188 };
189
190 }
191 #endif
192
193 namespace dcx {
194
195 struct digi_sound_deleter : std::default_delete<uint8_t[]>
196 {
197 game_sound_offset offset{};
198 constexpr digi_sound_deleter() = default;
199 constexpr digi_sound_deleter(const game_sound_offset offset) :
200 offset{offset}
201 {
202 }
203 constexpr bool must_free_buffer() const
204 {
205 return static_cast<std::underlying_type<game_sound_offset>::type>(offset) <= 0;
206 }
207 void operator()(uint8_t *const p) const
208 {
209 if (must_free_buffer())
210 this->std::default_delete<uint8_t[]>::operator()(p);
211 /* Else, this pointer was not owned by the unique_ptr, and should not
212 * be freed. This happens for some sounds that are stored in a single
213 * large allocation containing all the sounds, rather than individual
214 * per-sound allocations.
215 */
216 }
217 };
218
219 struct digi_sound
220 {
221 struct allocated_data : private std::unique_ptr<uint8_t[], digi_sound_deleter>
222 {
223 using base_type = std::unique_ptr<uint8_t[], digi_sound_deleter>;
224 using base_type::get;
225 using base_type::get_deleter;
226 using base_type::operator bool;
227 constexpr allocated_data() :
228 base_type{}
229 {
230 }
231 allocated_data(const base_type::pointer p, const game_sound_offset o) :
232 base_type{p, o}
233 {
234 }
235 /* This is only used in the Descent 1 build. */
236 explicit allocated_data(std::unique_ptr<uint8_t[]> p, const game_sound_offset o) :
237 allocated_data{p.release(), o}
238 {
239 }
240 /* Define reset() instead of inheriting via `using base_type::reset`,
241 * because only the zero-argument form of reset() should be exposed.
242 * The one-argument form should be hidden.
243 */
244 void reset()
245 {
246 this->base_type::reset();
247 }
248 allocated_data &operator=(allocated_data &&rhs)
249 {
250 auto &d = rhs.get_deleter();
251 if (d.must_free_buffer())
252 /* If the other object exclusively owns the data it controls,
253 * then use the standard move semantics of unique_ptr.
254 */
255 return static_cast<allocated_data &>(this->base_type::operator=(std::move(rhs)));
256 /* Otherwise, the source object does not free its data, so assume
257 * that it is safe, and sometimes necessary, to copy from the
258 * source instead of moving from it.
259 */
260 if (this == &rhs)
261 return *this;
262 /* Free the old data, if needed. */
263 this->base_type::reset();
264 /* Mark this deleter as not requiring a free. */
265 get_deleter() = d;
266 /* Change this pointer to share ownership with the other object. */
267 this->base_type::reset(rhs.get());
268 return *this;
269 }
270 };
271 std::size_t length;
272 int freq;
273 allocated_data data;
274 std::span<const uint8_t> span() const
275 {
276 return {data.get(), length};
277 }
278 };
279
280 }