1 // SONIC ROBO BLAST 2
2 //-----------------------------------------------------------------------------
3 // Copyright (C) 1993-1996 by id Software, Inc.
4 // Copyright (C) 1998-2000 by DooM Legacy Team.
5 // Copyright (C) 1999-2020 by Sonic Team Junior.
6 //
7 // This program is free software distributed under the
8 // terms of the GNU General Public License, version 2.
9 // See the 'LICENSE' file for more details.
10 //-----------------------------------------------------------------------------
11 /// \file  s_sound.h
12 /// \brief The not so system specific sound interface
13 
14 #ifndef __S_SOUND__
15 #define __S_SOUND__
16 
17 #include "i_sound.h" // musictype_t
18 #include "sounds.h"
19 #include "m_fixed.h"
20 #include "command.h"
21 #include "tables.h" // angle_t
22 
23 #ifdef HAVE_OPENMPT
24 #include "libopenmpt/libopenmpt.h"
25 extern openmpt_module *openmpt_mhandle;
26 #endif
27 
28 // mask used to indicate sound origin is player item pickup
29 #define PICKUP_SOUND 0x8000
30 
31 extern consvar_t stereoreverse;
32 extern consvar_t cv_soundvolume, cv_closedcaptioning, cv_digmusicvolume, cv_midimusicvolume;
33 extern consvar_t cv_numChannels;
34 
35 extern consvar_t cv_resetmusic;
36 extern consvar_t cv_resetmusicbyheader;
37 
38 extern consvar_t cv_1upsound;
39 
40 #define RESETMUSIC (!modeattacking && \
41 	(cv_resetmusicbyheader.value ? \
42 		(mapheaderinfo[gamemap-1]->musforcereset != -1 ? mapheaderinfo[gamemap-1]->musforcereset : cv_resetmusic.value) \
43 		: cv_resetmusic.value) \
44 	)
45 
46 extern consvar_t cv_gamedigimusic;
47 extern consvar_t cv_gamemidimusic;
48 extern consvar_t cv_gamesounds;
49 extern consvar_t cv_musicpref;
50 
51 extern consvar_t cv_playmusicifunfocused;
52 extern consvar_t cv_playsoundsifunfocused;
53 
54 #ifdef HAVE_OPENMPT
55 extern consvar_t cv_modfilter;
56 #endif
57 
58 #ifdef HAVE_MIXERX
59 extern consvar_t cv_midiplayer;
60 extern consvar_t cv_midisoundfontpath;
61 extern consvar_t cv_miditimiditypath;
62 #endif
63 
64 extern CV_PossibleValue_t soundvolume_cons_t[];
65 
66 typedef enum
67 {
68 	SF_TOTALLYSINGLE =  1, // Only play one of these sounds at a time...GLOBALLY
69 	SF_NOMULTIPLESOUND =  2, // Like SF_NOINTERRUPT, but doesnt care what the origin is
70 	SF_OUTSIDESOUND  =  4, // Volume is adjusted depending on how far away you are from 'outside'
71 	SF_X4AWAYSOUND   =  8, // Hear it from 4x the distance away
72 	SF_X8AWAYSOUND   = 16, // Hear it from 8x the distance away
73 	SF_NOINTERRUPT   = 32, // Only play this sound if it isn't already playing on the origin
74 	SF_X2AWAYSOUND   = 64, // Hear it from 2x the distance away
75 } soundflags_t;
76 
77 typedef struct {
78 	fixed_t x, y, z;
79 	angle_t angle;
80 } listener_t;
81 
82 typedef struct
83 {
84 	// sound information (if null, channel avail.)
85 	sfxinfo_t *sfxinfo;
86 
87 	// origin of sound
88 	const void *origin;
89 
90 	// initial volume of sound, which is applied after distance and direction
91 	INT32 volume;
92 
93 	// handle of the sound being played
94 	INT32 handle;
95 
96 } channel_t;
97 
98 typedef struct {
99 	channel_t *c;
100 	sfxinfo_t *s;
101 	UINT16 t;
102 	UINT8 b;
103 } caption_t;
104 
105 #define NUMCAPTIONS 8
106 #define MAXCAPTIONTICS (2*TICRATE)
107 #define CAPTIONFADETICS 20
108 
109 extern caption_t closedcaptions[NUMCAPTIONS];
110 void S_StartCaption(sfxenum_t sfx_id, INT32 cnum, UINT16 lifespan);
111 void S_ResetCaptions(void);
112 
113 // register sound vars and commands at game startup
114 void S_RegisterSoundStuff(void);
115 
116 //
117 // Initializes sound stuff, including volume
118 // Sets channels, SFX, allocates channel buffer, sets S_sfx lookup.
119 //
120 void S_InitSfxChannels(INT32 sfxVolume);
121 
122 //
123 // Per level startup code.
124 // Kills playing sounds at start of level, determines music if any, changes music.
125 //
126 void S_StopSounds(void);
127 void S_ClearSfx(void);
128 void S_StartEx(boolean reset);
129 #define S_Start() S_StartEx(false)
130 
131 //
132 // Basically a W_GetNumForName that adds "ds" at the beginning of the string. Returns a lumpnum.
133 //
134 lumpnum_t S_GetSfxLumpNum(sfxinfo_t *sfx);
135 
136 //
137 // Sound Status
138 //
139 
140 boolean S_SoundDisabled(void);
141 
142 //
143 // Start sound for thing at <origin> using <sound_id> from sounds.h
144 //
145 void S_StartSound(const void *origin, sfxenum_t sound_id);
146 
147 // Will start a sound at a given volume.
148 void S_StartSoundAtVolume(const void *origin, sfxenum_t sound_id, INT32 volume);
149 
150 // Stop sound for thing at <origin>
151 void S_StopSound(void *origin);
152 
153 //
154 // Music Status
155 //
156 
157 boolean S_DigMusicDisabled(void);
158 boolean S_MIDIMusicDisabled(void);
159 boolean S_MusicDisabled(void);
160 boolean S_MusicPlaying(void);
161 boolean S_MusicPaused(void);
162 boolean S_MusicNotInFocus(void);
163 musictype_t S_MusicType(void);
164 const char *S_MusicName(void);
165 boolean S_MusicExists(const char *mname, boolean checkMIDI, boolean checkDigi);
166 #define S_DigExists(a) S_MusicExists(a, false, true)
167 #define S_MIDIExists(a) S_MusicExists(a, true, false)
168 
169 // Returns whether the preferred format a (true = MIDI, false = Digital)
170 // exists and is enabled for musicname b
171 #define S_PrefAvailable(a, b) (a ? \
172 	(!S_MIDIMusicDisabled() && S_MIDIExists(b)) : \
173 	(!S_DigMusicDisabled() && S_DigExists(b)))
174 
175 //
176 // Music Effects
177 //
178 
179 // Set Speed of Music
180 boolean S_SpeedMusic(float speed);
181 
182 // Music definitions
183 typedef struct musicdef_s
184 {
185 	char name[7];
186 	char title[32];
187 	char alttitle[64];
188 	char authors[256];
189 	//char usage[256]; -- probably never going to be relevant to vanilla
190 	/*
191 	the trouble here is that kart combines what we call "title"
192 	and "authors" into one string. we need to split it for sound
193 	test reasons. they might split it later like we did, but...
194 	*/
195 	//char source[256];
196 	UINT8 soundtestpage;
197 	INT16 soundtestcond; // +ve for map, -ve for conditionset, 0 for already here
198 	tic_t stoppingtics;
199 	fixed_t bpm;
200 	UINT32 loop_ms;/* override LOOPPOINT/LOOPMS */
201 	boolean allowed; // question marks or listenable on sound test?
202 	struct musicdef_s *next;
203 } musicdef_t;
204 
205 extern musicdef_t soundtestsfx;
206 extern musicdef_t *musicdefstart;
207 extern musicdef_t **soundtestdefs;
208 extern INT32 numsoundtestdefs;
209 extern UINT8 soundtestpage;
210 
211 void S_LoadMusicDefs(UINT16 wadnum);
212 void S_InitMusicDefs(void);
213 
214 boolean S_PrepareSoundTest(void);
215 
216 //
217 // Music Seeking
218 //
219 
220 // Get Length of Music
221 UINT32 S_GetMusicLength(void);
222 
223 // Set LoopPoint of Music
224 boolean S_SetMusicLoopPoint(UINT32 looppoint);
225 
226 // Get LoopPoint of Music
227 UINT32 S_GetMusicLoopPoint(void);
228 
229 // Set Position of Music
230 boolean S_SetMusicPosition(UINT32 position);
231 
232 // Get Position of Music
233 UINT32 S_GetMusicPosition(void);
234 
235 //
236 // Music Stacking (Jingles)
237 //
238 
239 typedef struct musicstack_s
240 {
241 	char musname[7];
242 	UINT16 musflags;
243 	boolean looping;
244 	UINT32 position;
245 	tic_t tic;
246 	UINT16 status;
247 	lumpnum_t mlumpnum;
248 	boolean noposition; // force music stack resuming from zero (like music_stack_noposition)
249 
250     struct musicstack_s *prev;
251     struct musicstack_s *next;
252 } musicstack_t;
253 
254 extern char music_stack_nextmusname[7];
255 extern boolean music_stack_noposition;
256 extern UINT32 music_stack_fadeout;
257 extern UINT32 music_stack_fadein;
258 
259 void S_SetStackAdjustmentStart(void);
260 void S_AdjustMusicStackTics(void);
261 void S_RetainMusic(const char *mname, UINT16 mflags, boolean looping, UINT32 position, UINT16 status);
262 boolean S_RecallMusic(UINT16 status, boolean fromfirst);
263 
264 //
265 // Music Playback
266 //
267 
268 // Start music track, arbitrary, given its name, and set whether looping
269 // note: music flags 12 bits for tracknum (gme, other formats with more than one track)
270 //       13-15 aren't used yet
271 //       and the last bit we ignore (internal game flag for resetting music on reload)
272 void S_ChangeMusicEx(const char *mmusic, UINT16 mflags, boolean looping, UINT32 position, UINT32 prefadems, UINT32 fadeinms);
273 #define S_ChangeMusicInternal(a,b) S_ChangeMusicEx(a,0,b,0,0,0)
274 #define S_ChangeMusic(a,b,c) S_ChangeMusicEx(a,b,c,0,0,0)
275 
276 // Stops the music.
277 void S_StopMusic(void);
278 
279 // Stop and resume music, during game PAUSE.
280 void S_PauseAudio(void);
281 void S_ResumeAudio(void);
282 
283 //
284 // Music Fading
285 //
286 
287 void S_SetInternalMusicVolume(INT32 volume);
288 void S_StopFadingMusic(void);
289 boolean S_FadeMusicFromVolume(UINT8 target_volume, INT16 source_volume, UINT32 ms);
290 #define S_FadeMusic(a, b) S_FadeMusicFromVolume(a, -1, b)
291 #define S_FadeInChangeMusic(a,b,c,d) S_ChangeMusicEx(a,b,c,0,0,d)
292 boolean S_FadeOutStopMusic(UINT32 ms);
293 
294 //
295 // Updates music & sounds
296 //
297 void S_UpdateSounds(void);
298 void S_UpdateClosedCaptions(void);
299 
300 FUNCMATH fixed_t S_CalculateSoundDistance(fixed_t px1, fixed_t py1, fixed_t pz1, fixed_t px2, fixed_t py2, fixed_t pz2);
301 
302 void S_SetSfxVolume(INT32 volume);
303 void S_SetMusicVolume(INT32 digvolume, INT32 seqvolume);
304 #define S_SetDigMusicVolume(a) S_SetMusicVolume(a,-1)
305 #define S_SetMIDIMusicVolume(a) S_SetMusicVolume(-1,a)
306 #define S_InitMusicVolume() S_SetMusicVolume(-1,-1)
307 
308 INT32 S_OriginPlaying(void *origin);
309 INT32 S_IdPlaying(sfxenum_t id);
310 INT32 S_SoundPlaying(void *origin, sfxenum_t id);
311 
312 void S_StartSoundName(void *mo, const  char *soundname);
313 
314 void S_StopSoundByID(void *origin, sfxenum_t sfx_id);
315 void S_StopSoundByNum(sfxenum_t sfxnum);
316 
317 #ifndef HW3SOUND
318 #define S_StartAttackSound S_StartSound
319 #define S_StartScreamSound S_StartSound
320 #endif
321 
322 #ifdef MUSICSLOT_COMPATIBILITY
323 // For compatibility with code/scripts relying on older versions
324 // This is a list of all the "special" slot names and their associated numbers
325 extern const char *compat_special_music_slots[16];
326 #endif
327 
328 #endif
329