Debian Codegraph @

sndlib/ddsoundload.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/dd_sndlib/ddsoundload.cpp $
21 * $Revision: 17 $
22 * $Date: 3/20/00 12:23p $
23 * $Author: Matt $
24 *
25 *
26 *
27 * $Log: /DescentIII/Main/dd_sndlib/ddsoundload.cpp $
28 *
29 * 17 3/20/00 12:23p Matt
30 * Merge of Duane's post-1.3 changes.
31 * Fixed bug with sounds not being deleted
32 *
33 * 16 10/23/99 5:12p 3dsmax
34 * use GlobalAlloc for win32 instead of mem_malloc for now (restored
35 * original code)
36 *
37 * 15 10/22/99 8:10a Kevin
38 * Fixed bug introduced from the mac merge
39 *
40 * 14 10/21/99 9:27p Jeff
41 * B.A. Macintosh code merge
42 *
43 * 13 4/17/99 1:53a Jeff
44 * made platform 'independant' so other low-level sound libs can include
45 * without having to double code
46 *
47 * 12 4/06/99 8:29p Samir
48 * added error check system.
49 *
50 * 11 2/16/99 12:42p Kevin
51 * Improvements to the paging data progress indicator
52 *
53 * 10 2/09/99 7:01p Kevin
54 * First work for new and improved progress screen while loading a level.
55 * Note that this is a hack at this point, while I get the details worked
56 * out, then I'll make it cleaner.
57 *
58 * 9 1/14/99 6:10p Samir
59 * added DirectSound buffer duplication code.
60 *
61 * 8 10/22/98 8:31p Chris
62 * Sounds use GlobalAlloc and GlobalFree
63 *
64 * 7 10/19/98 11:57a Chris
65 * Update the sound system to use the import volume
66 *
67 * 6 10/12/98 1:27p Jeff
68 * all mem_free's do set the value to NULL after they are complete
69 *
70 * 5 10/12/98 11:30a Kevin
71 * More memory stuff
72 *
73 * 4 10/08/98 1:18a Chris
74 * Now it is non-Intel platform friendly.
75 *
76 * 3 10/08/98 1:09a Chris
77 * Speed up the sound paging in. -- Fucks up if byte ordering if different
78 * then for the Intel platform.
79 *
80 * 2 7/24/98 5:23p Samir
81 * moved sound variables from soundload to ssl_lib.h (and WAV file stuff)
82 *
83 * 1 7/20/98 2:54p Samir
84 * moved code from sndlib.
85 *
86 * $NoKeywords: $
87 */
88
89 #include <cstdlib>
90 #include <cstring>
91
92 #include "ssl_lib.h"
93 #include "cfile.h"
94 #include "mem.h"
95 #include "pserror.h"
96 #include "byteswap.h"
97 #include "gamesequence.h"
98
99 sound_info Sounds[MAX_SOUNDS];
100 sound_file_info SoundFiles[MAX_SOUND_FILES];
101
102 #define CLIP_ATTENUATION .5f
103
104 #define SOUND_FILE_SAMPLE_ALIGNMENT 4
105
106 #define SND_LOAD_ERROR_NO_FILE 0
107 #define SND_LOAD_ERROR_INVALID 1
108
109 char SoundLoadWaveFile(const char *filename, float percent_volume, int sound_file_index, bool f_high_quality,
110 bool f_load_sample_data, int *e_type) {
111
112 // File pointer to sound file
113 CFILE *cfptr;
114
115 char format_type[80]; // ASCII name of format type
116 uint16_t fmttag = 0; // Numerical format type
117 uint32_t ckid; // Current chunk's ID
118 uint32_t cksize; // Current chunk's size in bytes
119 uint32_t filesize; // Size of the sound file
120 uint32_t nextseek = 0; // Location of the next seek
121
122 uint32_t aligned_size; // Sound files are aligned to SOUND_FILE_SAMPLE_ALIGNMENT samples
123
124 // Sound format information
125 int samples_per_second;
126 int16_t bits_per_sample;
127 int16_t number_channels;
128
129 // Used to read temporary long values
130 uint32_t temp_long;
131
132 // Flags for if we previously read data or a format
133 char f_data, f_fmt = 0;
134
135 // Loop counter
136 int count;
137
138 // If there is an error assume it would be because of an invalid file
139 if (e_type)
140 *e_type = SND_LOAD_ERROR_INVALID;
141
142 // Setup the initial sound_file state
143 SoundFiles[sound_file_index].sample_8bit = NULL;
144 SoundFiles[sound_file_index].sample_16bit = NULL;
145 SoundFiles[sound_file_index].sample_length = 0;
146
147 // Open the wave file
148 if ((cfptr = cfopen(filename, "rb")) == NULL) {
149 if (e_type)
150 *e_type = SND_LOAD_ERROR_NO_FILE;
151
152 mprintf(0, "SOUND LOADER: %s not found\n", filename);
153 goto error_state;
154 }
155
156 if (!f_load_sample_data) {
157 cfclose(cfptr);
158 return 1;
159 }
160 // Used for progress bar when loading the level
161 paged_in_count += cfilelength(cfptr);
162 paged_in_num++;
163 // Make sure that it is a RIFF format
164 temp_long = (uint32_t)cf_ReadInt(cfptr);
165 if (temp_long != 0x46464952) {
166 mprintf(0, "SOUND LOADER: %s is not a RIFF format file\n", filename);
167 goto error_state;
168 }
169
170 // The first item is the filesize minus the RIFF type specifier and filesize (add current location for total filesize)
171 filesize = cf_ReadInt(cfptr);
172 filesize += cftell(cfptr);
173
174 // Make sure it is a wave file
175 temp_long = (uint32_t)cf_ReadInt(cfptr);
176 if (temp_long != 0x45564157) {
177 mprintf(0, "SOUND LOADER: %s is not a WAVE file\n", filename);
178 goto error_state;
179 }
180 nextseek = cftell(cfptr);
181
182 // Read in the chunks
183 while (nextseek < filesize - 9) // Need room for chunk and its header
184 {
185 // Move the the beginning of the next chunck
186 cfseek(cfptr, nextseek, SEEK_SET);
187
188 // Read in the chunk size and type
189 ckid = cf_ReadInt(cfptr);
190 cksize = cf_ReadInt(cfptr);
191 if (cksize <= 0) {
192 mprintf(0, "SOUND LOADER: %s has an invalid block length\n", filename);
193 goto error_state;
194 }
195
196 // Determine where the next chunk begins
197 nextseek = cksize + cftell(cfptr);
198
199 // Determine what to do with the chunk
200 switch (ckid) {
201 case 0x20746D66: // Format Chunk
202 // Make sure that this format was not preceeded by another format without data inbetween them.
203 if (f_fmt) {
204 mprintf(0, "Sound Loader: Found 2 formats in a row in file %s\n", filename);
205 goto error_state;
206 }
207
208 // Read in the format type
209 fmttag = (uint16_t)cf_ReadShort(cfptr);
210
211 switch (fmttag) {
212 // We only support WAVE_FORMAT_PCM currently
213 default:
214 case 0x0000:
215 strcpy(format_type, "WAVE_FORMAT_UNKNOWN");
216 break;
217 case 0x0001:
218 strcpy(format_type, "WAVE_FORMAT_PCM");
219 break;
220 case 0x0002:
221 strcpy(format_type, "WAVE_FORMAT_ADPCM");
222 break;
223 case 0x0005:
224 strcpy(format_type, "WAVE_FORMAT_IBM_CVSD");
225 break;
226 case 0x0006:
227 strcpy(format_type, "WAVE_FORMAT_ALAW");
228 break;
229 case 0x0007:
230 strcpy(format_type, "WAVE_FORMAT_MULAW");
231 break;
232 case 0x0010:
233 strcpy(format_type, "WAVE_FORMAT_OKI_ADPCM");
234 break;
235 case 0x0011:
236 strcpy(format_type, "WAVE_FORMAT_DVI_ADPCM");
237 break;
238 case 0x0015:
239 strcpy(format_type, "WAVE_FORMAT_DIGISTD");
240 break;
241 case 0x0016:
242 strcpy(format_type, "WAVE_FORMAT_DIGIFIX");
243 break;
244 case 0x0020:
245 strcpy(format_type, "WAVE_FORMAT_YAMAHA_ADPCM");
246 break;
247 case 0x0021:
248 strcpy(format_type, "WAVE_FORMAT_SONARC");
249 break;
250 case 0x0022:
251 strcpy(format_type, "WAVE_FORMAT_DSPGROUP_TRUESPEECH");
252 break;
253 case 0x0023:
254 strcpy(format_type, "WAVE_FORMAT_ECHOSC1");
255 break;
256 case 0x0024:
257 strcpy(format_type, "WAVE_FORMAT_AUDIOFILE_AF18");
258 break;
259 case 0x0101:
260 strcpy(format_type, "IBM_FORMAT_MULAW");
261 break;
262 case 0x0102:
263 strcpy(format_type, "IBM_FORMAT_ALAW");
264 break;
265 case 0x0103:
266 strcpy(format_type, "IBM_FORMAT_ADPCM");
267 break;
268 case 0x0200:
269 strcpy(format_type, "WAVE_FORMAT_CREATIVE_ADPCM");
270 break;
271 }
272
273 // Currently, we only support PCM wave files
274 if (fmttag != 0x0001) {
275 mprintf(0, "SOUND LOADER: %s is a type %s wavefile, we only support WAVE_FORMAT_PCM waves.\n", filename,
276 format_type);
277 goto error_state;
278 }
279
280 // Get the number of channels
281 number_channels = cf_ReadShort(cfptr);
282 if (number_channels != 1) {
283 mprintf(0, "SOUND LOADER: Invalid number of channels(%d)in %s, we want mono samples only.\n", number_channels,
284 filename);
285 goto error_state;
286 }
287
288 // Get the number of samples per second
289 samples_per_second = cf_ReadInt(cfptr);
290 if (samples_per_second != 22050) {
291 mprintf(0, "SOUND LOADER: Invalid sample per second(%d)in %s, we want 22k samples only.\n", samples_per_second,
292 filename);
293 goto error_state;
294 }
295
296 // get the Average bytes per second (ignore it)
297 cf_ReadInt(cfptr);
298
299 // Get the Block Alignment (ignore it)
300 cf_ReadShort(cfptr);
301
302 // Get the Bits per sample
303 bits_per_sample = cf_ReadShort(cfptr);
304 if (bits_per_sample != 8 && bits_per_sample != 16) {
305 mprintf(0, "SOUND LOADER: Invalid bits per sample(%d)in %s, we want 8 or 16 bit samples only.\n",
306 bits_per_sample, filename);
307 goto error_state;
308 }
309
310 // Beginning of some support for other wave file formats
311 // if(fmttag!=0x0001)
312 // {
313 // fread(&ra,1,2,fp);
314 // printf(" %u Bytes of extra information\n");
315 // printf(" ***I have not seen a file like this before, hope it goes well***\n");
316 // switch(fmttag)
317 // {
318 // case 0x0002: //MS ADPCM?
319 // fread(&ra,1,2,fp);
320 // printf(" %u Samples Per Block\n",ra);
321 // fread(&ra,1,2,fp);
322 // printf(" %u Coefficients\n",ra);
323 // for(la=0;la<ra;la++)
324 // {
325 // fread(&rb,1,2,fp);
326 // fread(&rc,1,2,fp);
327 // printf(" %u %u\n",rb,rc);
328 // }
329 // break;
330 // case 0x0011: //WAVE_FORMAT_DVI_ADPCM
331 // case 0x0022: //WAVE_FORMAT_DSPGROUP_TRUESPEECH
332 // fread(&ra,1,2,fp);
333 // printf(" %u Samples Per Block\n",ra);
334 // break;
335 // case 0x0021: // WAVE_FORMAT_SONARC
336 // fread(&ra,1,2,fp);
337 // printf(" %u CompType\n",ra);
338 // break;
339 // case 0x0200: //WAVE_FORMAT_CREATIVE_ADPCM
340 // fread(&ra,1,2,fp);
341 // printf(" Revision %u\n",ra);
342 // break;
343 // }
344 // }
345
346 // We did find a new format type
347 f_fmt = 1;
348 f_data = 0;
349 break;
350
351 // Data Chunk
352 case 0x61746164:
353 if (!f_fmt) {
354 mprintf(0, "SOUND LOADER: Format Chunk not defined before Data Chunk\n");
355 goto error_state;
356 }
357
358 // It is either 8 or 16 bit as validated from above
359 if (bits_per_sample == 8) {
360 int num_needed = 0;
361
362 // Force sample alignment
363 if ((cksize - 1) % SOUND_FILE_SAMPLE_ALIGNMENT) {
364 num_needed = SOUND_FILE_SAMPLE_ALIGNMENT - ((cksize - 1) % SOUND_FILE_SAMPLE_ALIGNMENT);
365 aligned_size = cksize + num_needed;
366 } else {
367 aligned_size = cksize;
368 }
369
370 SoundFiles[sound_file_index].sample_length = aligned_size;
371 SoundFiles[sound_file_index].np_sample_length = cksize;
372
373 SoundFiles[sound_file_index].sample_8bit = (uint8_t *)mem_malloc(aligned_size);
374
375 cf_ReadBytes((uint8_t *)SoundFiles[sound_file_index].sample_8bit, cksize, cfptr);
376
377 if (aligned_size != cksize)
378 memset(SoundFiles[sound_file_index].sample_8bit + cksize, 128, num_needed);
379 } else {
380 int num_needed = 0;
381
382 SoundFiles[sound_file_index].sample_length = cksize / 2;
383 SoundFiles[sound_file_index].np_sample_length = cksize / 2;
384
385 if ((SoundFiles[sound_file_index].sample_length - 1) % SOUND_FILE_SAMPLE_ALIGNMENT) {
386 num_needed = (SOUND_FILE_SAMPLE_ALIGNMENT -
387 ((SoundFiles[sound_file_index].sample_length - 1) % SOUND_FILE_SAMPLE_ALIGNMENT)) *
388 2;
389 SoundFiles[sound_file_index].sample_length = cksize / 2 + num_needed / 2;
390 }
391
392 SoundFiles[sound_file_index].sample_16bit = (int16_t *)mem_malloc(cksize + num_needed);
393 cf_ReadBytes((uint8_t *)SoundFiles[sound_file_index].sample_16bit, cksize, cfptr);
394 for (count = 0; count < (int)cksize / 2; count++) {
395 SoundFiles[sound_file_index].sample_16bit[count] =
396 INTEL_SHORT(SoundFiles[sound_file_index].sample_16bit[count]);
397 }
398
399 if (num_needed)
400 memset(SoundFiles[sound_file_index].sample_16bit + (cksize / 2), 0, num_needed);
401 }
402
403 // We found data, clear format flag
404 f_data = 1;
405 f_fmt = 0; // Multiple waveforms, per file, are allowed
406 break;
407
408 // Undefined chunk
409 default:
410 // mprintf(0, "\nSOUND LOADER: Unknown Chunk in %s, attempting to continue.\n", filename);
411 break;
412 }
413 }
414
415 // Close the file
416 cfclose(cfptr);
417
418 if (SoundFiles[sound_file_index].sample_8bit != NULL) {
419 mprintf(0, "Sound file %s is 8bit please make a 16bit version", filename);
420 }
421
422 // Make sure we got data and format information
423 if (SoundFiles[sound_file_index].sample_8bit == NULL && SoundFiles[sound_file_index].sample_16bit == NULL) {
424 mprintf(0, "SOUND LOADER: Did not find data and/or format in %s.\n", filename);
425 goto error_state;
426 } else if (SoundFiles[sound_file_index].sample_8bit != NULL && !f_high_quality) {
427 // Doing the volume clipping with the low quality (8 bit) sound requires some tricky math -- see chris
428 for (count = 0; count < (int)SoundFiles[sound_file_index].sample_length; count++) {
429 SoundFiles[sound_file_index].sample_8bit[count] =
430 ((((int)SoundFiles[sound_file_index].sample_8bit[count]) - 127) * CLIP_ATTENUATION * percent_volume) + 127;
431 }
432 } else if (SoundFiles[sound_file_index].sample_16bit != NULL && f_high_quality) {
433 // Do the volume clipping with the high quality sound
434 for (count = 0; count < (int)SoundFiles[sound_file_index].sample_length; count++) {
435 SoundFiles[sound_file_index].sample_16bit[count] *= CLIP_ATTENUATION * percent_volume;
436 }
437 } else if (SoundFiles[sound_file_index].sample_8bit == NULL && !f_high_quality) {
438
439 SoundFiles[sound_file_index].sample_8bit =
440 (uint8_t *)mem_malloc(SoundFiles[sound_file_index].sample_length);
441
442 // Do the volume clipping with the high quality sound
443 for (count = 0; count < (int)SoundFiles[sound_file_index].sample_length; count++) {
444 SoundFiles[sound_file_index].sample_16bit[count] *= CLIP_ATTENUATION * percent_volume;
445 }
446
447 // NOTE: Interesting note on sound conversion: 16 bit sounds are signed (0 biase). 8 bit sounds are unsigned
448 // (+128 biase).
449 for (count = 0; count < (int)SoundFiles[sound_file_index].sample_length; count++) {
450 SoundFiles[sound_file_index].sample_8bit[count] =
451 (uint8_t)((((int)SoundFiles[sound_file_index].sample_16bit[count]) + 32767) >> 8);
452 }
453
454 mem_free(SoundFiles[sound_file_index].sample_16bit);
455 SoundFiles[sound_file_index].sample_16bit = NULL;
456
457 } else if (SoundFiles[sound_file_index].sample_16bit == NULL && f_high_quality) {
458 SoundFiles[sound_file_index].sample_16bit =
459 (int16_t *)mem_malloc(SoundFiles[sound_file_index].sample_length * sizeof(int16_t));
460
461 // NOTE: Interesting note on sound conversion: 16 bit sounds are signed (0 biase). 8 bit sounds are unsigned
462 // (+128 biase).
463 for (count = 0; count < (int)SoundFiles[sound_file_index].sample_length; count++) {
464 SoundFiles[sound_file_index].sample_16bit[count] =
465 (((int16_t)SoundFiles[sound_file_index].sample_8bit[count]) - 128) * 256;
466 }
467
468 for (count = 0; count < (int)SoundFiles[sound_file_index].sample_length; count++) {
469 SoundFiles[sound_file_index].sample_16bit[count] *= CLIP_ATTENUATION * percent_volume;
470 }
471
472 mem_free(SoundFiles[sound_file_index].sample_8bit);
473 SoundFiles[sound_file_index].sample_8bit = NULL;
474 }
475
476 // reset use count
477 SoundFiles[sound_file_index].use_count = 0;
478 SoundFiles[sound_file_index].used = true;
479
480 return 1;
481
482 error_state:
483
484 // Free any allocations, this file is a bust!
485 if (cfptr)
486 cfclose(cfptr);
487
488 if (SoundFiles[sound_file_index].sample_8bit) {
489 mem_free(SoundFiles[sound_file_index].sample_8bit);
490 SoundFiles[sound_file_index].sample_8bit = NULL;
491 }
492
493 if (SoundFiles[sound_file_index].sample_16bit) {
494 mem_free(SoundFiles[sound_file_index].sample_16bit);
495 SoundFiles[sound_file_index].sample_16bit = NULL;
496 }
497
498 return 0;
499 }
500
501 void SoundLoadFree(int sound_file_index) {
502 int i = sound_file_index;
503
504 if (SoundFiles[i].used != 0) {
505 if (SoundFiles[i].sample_8bit)
506 mem_free(SoundFiles[i].sample_8bit);
507
508 if (SoundFiles[i].sample_16bit)
509 mem_free(SoundFiles[i].sample_16bit);
510 }
511
512 SoundFiles[i].sample_8bit = NULL;
513 SoundFiles[i].sample_16bit = NULL;
514 }