Debian Codegraph @

Descent3/voice.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/voice.cpp $
21 * $Revision: 11 $
22 * $Date: 5/13/99 3:42p $
23 * $Author: Ardussi $
24 *
25 * implementation file for voice system
26 *
27 * $Log: /DescentIII/Main/voice.cpp $
28 *
29 * 11 5/13/99 3:42p Ardussi
30 * changes for compiling on the Mac
31 *
32 * 10 2/10/99 4:45p Jeff
33 * table file parser stuff
34 *
35 * 9 9/28/98 4:36p Jeff
36 * shortcircuit the voices for the demo
37 *
38 * 8 7/24/98 5:19p Samir
39 * took out old stream code.
40 *
41 * 7 7/13/98 6:07p Jeff
42 * handled not removing non-powerup voices on a queue clean up
43 *
44 * 6 7/09/98 8:35p Samir
45 * Changed StreamStop.
46 *
47 * 5 6/19/98 6:04p Jeff
48 * added voice queue
49 *
50 * 4 6/18/98 5:18p Jeff
51 * Use new streamaudio system
52 *
53 * 3 6/17/98 8:08p Jeff
54 * removed DoVoiceFrame
55 *
56 * 2 6/08/98 3:55p Jeff
57 * rough initial creation
58 *
59 * $NoKeywords: $
60 */
61 #include "voice.h"
62 #include "streamaudio.h"
63 #include "hlsoundlib.h"
64 #include "soundload.h"
65 #include <stdlib.h>
66 #include <string.h>
67
68 #define MOTHERLOAD_STRING "Cheater!"
69 #define MOTHERLOAD_FLAGS VF_POWERUP | VF_PLAYTABLE
70
71 struct tVoice {
72 int handle;
73 int flags;
74 int chandle;
75 bool inuse;
76 };
77
78 bool PlayPowerupVoice;
79 bool PlayVoices;
80
81 #define QUEUE_SIZE 3
82
83 class VoiceQueue {
84 public:
85 VoiceQueue();
86 ~VoiceQueue();
87 void AddVoice(char *fn, int flg);
88 bool GetNextVoice(char *fn, int *flg);
89 void Clear(bool onlypowerups = false);
90
91 private:
92 char filenames[QUEUE_SIZE][_MAX_PATH];
93 int flags[QUEUE_SIZE];
94 uint8_t currvoice;
95 uint8_t pos;
96 char motherloadat;
97 bool inuse[QUEUE_SIZE];
98 bool full;
99 };
100
101 tVoice CurrentVoiceHandle;
102 VoiceQueue vq;
103
104 void StartVoice(char *filename, int flags);
105
106 bool InitVoices(void) {
107 mprintf(0, "Voice System: Init\n");
108 CurrentVoiceHandle.handle = -1;
109 CurrentVoiceHandle.flags = 0;
110 CurrentVoiceHandle.chandle = -1;
111 CurrentVoiceHandle.inuse = false;
112 vq.Clear();
113
114 atexit(CloseVoices);
115 return true;
116 }
117
118 void CloseVoices(void) {
119 mprintf(0, "Voice System: Shutdown\n");
120 StopVoice();
121 }
122
123 void PlayVoice(char *filename, int flags) {
124 #ifdef DEMO
125 return;
126 #endif
127 if ((!PlayPowerupVoice) && (flags & VF_POWERUP) && (!(flags & VF_FORCE)))
128 return;
129 if ((CurrentVoiceHandle.inuse) && (flags & VF_INTERUPT)) {
130 StopVoice();
131 vq.Clear();
132 }
133 vq.AddVoice(filename, flags);
134 }
135
136 void StartVoice(char *filename, int flags) {
137 #ifdef DEMO
138 return;
139 #endif
140 if ((!PlayVoices) && (!(flags & VF_FORCE)))
141 return;
142
143 if (flags & VF_PLAYTABLE) {
144 CurrentVoiceHandle.flags = flags;
145 CurrentVoiceHandle.inuse = true;
146 CurrentVoiceHandle.chandle = -1;
147 int index = FindSoundName(IGNORE_TABLE(filename));
148 CurrentVoiceHandle.handle = Sound_system.Play2dSound(index);
149 return;
150 }
151
152 bool compressed = (flags & VF_COMPRESSED) ? true : false;
153 bool bit8 = (flags & VF_8BIT) ? true : false;
154 bool stereo = (flags & VF_STEREO) ? true : false;
155
156 int vf = 0;
157
158 // if(bit8){
159 // if(stereo)
160 // vf = SAF_8BIT_S;
161 // else
162 // vf = SAF_8BIT_M;
163 // }else{
164 // if(stereo)
165 // vf = SAF_16BIT_S;
166 // else
167 // vf = SAF_16BIT_M;
168 // }
169 // if(compressed)
170 // vf |= SAF_COMPRESSED;
171
172 CurrentVoiceHandle.chandle = StreamPlay(filename, (MAX_GAME_VOLUME / 2.0), vf);
173 if (CurrentVoiceHandle.chandle != -1)
174 CurrentVoiceHandle.handle = StreamGetSoundHandle(CurrentVoiceHandle.chandle);
175
176 CurrentVoiceHandle.flags = flags;
177 CurrentVoiceHandle.inuse = true;
178 }
179
180 void StopVoice(void) {
181 if (CurrentVoiceHandle.inuse) {
182 if (CurrentVoiceHandle.flags & VF_PLAYTABLE) {
183 if (CurrentVoiceHandle.handle != -1)
184 Sound_system.StopSoundImmediate(CurrentVoiceHandle.handle);
185 } else {
186 if (CurrentVoiceHandle.chandle != -1)
187 StreamStop(CurrentVoiceHandle.chandle);
188 }
189 }
190 CurrentVoiceHandle.handle = -1;
191 CurrentVoiceHandle.flags = 0;
192 CurrentVoiceHandle.chandle = -1;
193 CurrentVoiceHandle.inuse = false;
194 }
195
196 void UpdateVoices(void) {
197 char filename[_MAX_PATH];
198 int flags;
199
200 if (!CurrentVoiceHandle.inuse) {
201 // see if we have something waiting
202 if (vq.GetNextVoice(filename, &flags)) {
203 mprintf(0, "Playing queued voice %s\n", filename);
204 StartVoice(filename, flags);
205 return;
206 }
207 return;
208 }
209
210 // according to the handle, the current voice is inuse, lets make sure
211 if (Sound_system.IsSoundPlaying(CurrentVoiceHandle.handle))
212 return; // it is
213
214 // it isn't, so stop it and play the next voice in the queue if any
215 StopVoice();
216 if (vq.GetNextVoice(filename, &flags)) {
217 mprintf(0, "Playing queued voice %s\n", filename);
218 StartVoice(filename, flags);
219 }
220 }
221
222 //////////////////////
223 // Voice Queue Implementation
224
225 VoiceQueue::VoiceQueue() {
226 currvoice = pos = 0;
227 motherloadat = -1;
228 full = false;
229 for (int i = 0; i < QUEUE_SIZE; i++)
230 inuse[i] = false;
231 }
232
233 VoiceQueue::~VoiceQueue() { Clear(); }
234
235 void VoiceQueue::AddVoice(char *fn, int flg) {
236 if (motherloadat != -1)
237 return;
238 if (full) {
239 // since the queue is full, remove everything and add just the "mother load" voice
240 Clear(true);
241 strcpy(filenames[pos], MOTHERLOAD_STRING);
242 flags[pos] = MOTHERLOAD_FLAGS;
243 motherloadat = pos;
244 inuse[pos] = true;
245 return;
246 }
247
248 // the queue isn't full yet, so add it in
249 int nextpos = (pos + 1) % QUEUE_SIZE;
250 if ((nextpos == currvoice + 1) && (inuse[nextpos])) {
251 // this really shouldn't be since it was reported that the queue isn't full
252 mprintf(0, "Voice Queue full, missed full bool (this shouldn't happen)\n");
253 // since the queue is full, remove everything and add just the "mother load" voice
254 Clear();
255 pos = 1;
256 strcpy(filenames[pos], MOTHERLOAD_STRING);
257 flags[pos] = MOTHERLOAD_FLAGS;
258 motherloadat = pos;
259 inuse[pos] = true;
260 return;
261 }
262 pos = nextpos;
263 strcpy(filenames[pos], fn);
264 flags[pos] = flg;
265 inuse[pos] = true;
266 for (int i = 0; i < QUEUE_SIZE; i++) {
267 // check to see if the queue is full now
268 if (inuse[i] == false)
269 return;
270 }
271 // queue is full
272 full = true;
273 }
274
275 bool VoiceQueue::GetNextVoice(char *fn, int *flg) {
276 int p = (++currvoice) % QUEUE_SIZE;
277 if (!inuse[p])
278 return false;
279 inuse[p] = false;
280 strcpy(fn, filenames[p]);
281 *flg = flags[p];
282 currvoice = p;
283 if (currvoice == motherloadat)
284 motherloadat = -1;
285 full = false;
286 return true;
287 }
288
289 void VoiceQueue::Clear(bool onlypowerups) {
290 int i;
291
292 for (i = 0; i < QUEUE_SIZE; i++) {
293 if ((!onlypowerups) || (flags[i] & VF_POWERUP))
294 inuse[i] = false;
295 }
296 if (onlypowerups) {
297 // we need to adjust all the buffers so what is left (non-powerup voices) are pushed to the
298 // front
299 int realp = 0;
300 for (i = 0; i < QUEUE_SIZE; i++) {
301 if (inuse[i] && (i != realp)) {
302 // we'll need to move this guy
303 inuse[i] = false;
304 inuse[realp] = true;
305 flags[realp] = flags[i];
306 strcpy(filenames[realp], filenames[i]);
307 // adjust pointers so they are pointing to the newly moved pointers
308 if (motherloadat == i)
309 motherloadat = realp;
310 if (currvoice == i)
311 currvoice = realp;
312 if (pos == i)
313 pos = realp;
314 realp++;
315 } // endif
316 } // end for
317 if (realp == QUEUE_SIZE) {
318 // we're still full :(
319 full = true;
320 mprintf(0, "Voice Warning: Buffer still full after clean (All Non-powerups voices in queue?)\n");
321 }
322 } else {
323 motherloadat = -1;
324 full = false;
325 currvoice = pos = 0;
326 }
327 }