Debian Codegraph @

Descent3/audiotaunts.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
19 #include <string.h>
20 #include <stdlib.h>
21
22 #include "mono.h"
23 #include "debug.h"
24 #include "pserror.h"
25 #include "pstypes.h"
26 #include "audiotaunts.h"
27 #include "cfile.h"
28 #include "audio_encode.h"
29 #include "byteswap.h"
30 #include "mem.h"
31 #include "ddio.h"
32 #include "manage.h"
33 #include "streamaudio.h"
34 #include "multi.h"
35 #include "game.h"
36 #include "stringtable.h"
37 #include "game2dll.h"
38 #include "player.h"
39
40 struct tWaveFile {
41 int sample_length;
42 int np_sample_length;
43 int samples_per_second;
44 int16_t bits_per_sample;
45 int16_t number_channels;
46 uint8_t *sample_8bit;
47 int16_t *sample_16bit;
48 };
49
50 // returns:
51 // 0: no error
52 // 1: file not found
53 // 2: not RIFF format
54 // 3: not WAVE file
55 // 4: invalid block length
56 // 5: invalid file/unknown
57 // 6: format not supported
58 // 7: invalid num of channels
59 // 8: invalid samples
60 // 9: invalid bit (8 or 16)
61 // 10: no data
62 static char taunt_LoadWaveFile(char *filename, tWaveFile *wave);
63
64 static int TauntLastError = TAUNTIMPERR_NOERROR;
65 bool Audio_taunts_enabled = true;
66 static float Audio_taunt_delay_time = 5.0f;
67
68 // taunt_Enable
69 //
70 // Enables/Disables audio_taunts for the player
71 void taunt_Enable(bool enable) { Audio_taunts_enabled = enable; }
72
73 // taunt_AreEnabled
74 //
75 // Returns true if taunts are enabled
76 bool taunt_AreEnabled(void) { return Audio_taunts_enabled; }
77
78 // taunt_DelayTime()
79 //
80 // Returns the delay time between taunts
81 float taunt_DelayTime(void) {
82 if (Audio_taunt_delay_time < 0)
83 Audio_taunt_delay_time = 5.0f;
84
85 return Audio_taunt_delay_time;
86 }
87
88 // taunt_SetDelayTime()
89 //
90 // Sets the taunt delay time
91 void taunt_SetDelayTime(float t) { Audio_taunt_delay_time = t; }
92
93 // taunt_PlayTauntFile
94 //
95 // Given a path to an .osf file, it will play it
96 bool taunt_PlayTauntFile(const char *filename) {
97 if (!Audio_taunts_enabled)
98 return false;
99
100 int ret = StreamPlay(filename, (MAX_GAME_VOLUME / 2.0f), 0);
101 return true;
102 }
103
104 // taunt_PlayPlayerTaunt
105 //
106 // Given a playernum, and which taunt (0 or 1) it will play that taunt
107 bool taunt_PlayPlayerTaunt(int pnum, int index) {
108 if (!(Game_mode & GM_MULTI))
109 return false;
110
111 if (index < 0 || index > 3) {
112 mprintf(0, "TAUNT: invalid index %d\n", index);
113 return false;
114 }
115
116 if ((NetPlayers[pnum].flags & NPF_CONNECTED) && (NetPlayers[pnum].sequence == NETSEQ_PLAYING)) {
117 char fullpath[_MAX_PATH];
118 char *file;
119 switch (index) {
120 case 0:
121 file = NetPlayers[pnum].voice_taunt1;
122 break;
123 case 1:
124 file = NetPlayers[pnum].voice_taunt2;
125 break;
126 case 2:
127 file = NetPlayers[pnum].voice_taunt3;
128 break;
129 case 3:
130 file = NetPlayers[pnum].voice_taunt4;
131 break;
132 }
133
134 ddio_MakePath(fullpath, LocalCustomSoundsDir, file, NULL);
135
136 if (!cfexist(fullpath)) {
137 mprintf(0, "TAUNT: file %s doesn't exist (pnum=%d)\n", fullpath, pnum);
138 return false;
139 }
140
141 DLLInfo.me_handle = Objects[Players[pnum].objnum].handle;
142 DLLInfo.it_handle = OBJECT_HANDLE_NONE;
143 CallGameDLL(EVT_CLIENT_PLAYERPLAYSAUDIOTAUNT, &DLLInfo);
144
145 return taunt_PlayTauntFile(fullpath);
146 }
147
148 return false;
149 }
150
151 // taunt_GetError
152 //
153 // Returns more information about a failed import
154 int taunt_GetError(void) { return TauntLastError; }
155
156 // taunt_GetErrorString
157 // Returns a string describing an error code
158 // !!! FIXME: This could just return a string literal and not strcpy it.
159 const char *taunt_GetErrorString(int error) {
160 static char str[256];
161
162 switch (error) {
163 case TAUNTIMPERR_NOTFOUND:
164 strcpy(str, TXT_FILENOTFOUND);
165 break;
166 case TAUNTIMPERR_NOTRIFF:
167 strcpy(str, TXT_NOTRIFF);
168 break;
169 case TAUNTIMPERR_NOTWAVE:
170 strcpy(str, TXT_NOTWAVE);
171 break;
172 case TAUNTIMPERR_INVALIDFILE:
173 strcpy(str, TXT_INVALIDFILE);
174 break;
175 case TAUNTIMPERR_NOTSUPPORTED:
176 strcpy(str, TXT_FORMATNOTSUPPORTED);
177 break;
178 case TAUNTIMPERR_INVALIDCHANNELS:
179 strcpy(str, TXT_MUSTBEMONO);
180 break;
181 case TAUNTIMPERR_INVALIDSAMPLES:
182 strcpy(str, TXT_BADSAMPLERATE);
183 break;
184 case TAUNTIMPERR_INVALIDBITDEPTH:
185 strcpy(str, TXT_BADBITDEPTH);
186 break;
187 case TAUNTIMPERR_NODATA:
188 strcpy(str, TXT_NODATAINWAVE);
189 break;
190 case TAUNTIMPERR_OSFEXISTS:
191 strcpy(str, TXT_OSFALREADYEXISTS);
192 break;
193 case TAUNTIMPERR_INTERNALERR:
194 strcpy(str, TXT_INTERNALERROR);
195 break;
196 case TAUNTIMPERR_COMPRESSIONFAILURE:
197 strcpy(str, TXT_COMPRESSFAIL);
198 break;
199 case TAUNTIMPERR_OUTOFMEM:
200 strcpy(str, TXT_OUTOFMEMORY);
201 break;
202 case TAUNTIMPERR_NOOPENOSF:
203 strcpy(str, TXT_CANTOPENOSF);
204 break;
205 default:
206 strcpy(str, TXT_NOERROR);
207 break;
208 }
209 return str;
210 }
211
212 // taunt_ImportWave
213 // Given a fully qualified wave_filename (location of a .wav) and a fully
214 // qualified outputfilename (where the .osf is to go), it will convert and
215 // compress the wav file.
216 #define FILEBUFFER_LENGTH (4 * 1024)
217 bool taunt_ImportWave(char *wave_filename, char *outputfilename) {
218 ASSERT(wave_filename);
219 ASSERT(outputfilename);
220 bool ret = true;
221 CFILE *file = NULL;
222 int amount_to_flush;
223 tWaveFile wavdata;
224 int samples, rate, chan;
225 char temp_filename[_MAX_PATH];
226 char osftemp_filename[_MAX_PATH];
227 uint8_t *StaticFileBuffer = NULL;
228 *temp_filename = *osftemp_filename = '\0';
229 OSFArchive osf;
230 CFILE *fpin = NULL;
231 bool osfopened;
232 osfopened = false;
233
234 if (!wave_filename || !outputfilename) {
235 TauntLastError = TAUNTIMPERR_NOTFOUND;
236 return false;
237 }
238
239 // first make sure the wave file exists
240 if (!cfexist(wave_filename)) {
241 mprintf(0, "AudioTaunt: Can't Import wav, missing %s\n", wave_filename);
242 TauntLastError = TAUNTIMPERR_NOTFOUND;
243 return false;
244 }
245
246 // make sure the output file doesn't exist so we don't overwrite
247 if (cfexist(outputfilename)) {
248 mprintf(0, "AudioTaunt: Can't Import wav, %s already exists\n", outputfilename);
249 TauntLastError = TAUNTIMPERR_OSFEXISTS;
250 return false;
251 }
252
253 // Open the file and import
254 char wave_ret = taunt_LoadWaveFile(wave_filename, &wavdata);
255 if (wave_ret != 0) {
256 mprintf(0, "TAUNT: Unable to load wave\n");
257 switch (wave_ret) {
258 case 1:
259 TauntLastError = TAUNTIMPERR_NOTFOUND;
260 break;
261 case 2:
262 TauntLastError = TAUNTIMPERR_NOTRIFF;
263 break;
264 case 3:
265 TauntLastError = TAUNTIMPERR_NOTWAVE;
266 break;
267 case 4:
268 case 5:
269 TauntLastError = TAUNTIMPERR_INVALIDFILE;
270 break;
271 case 6:
272 TauntLastError = TAUNTIMPERR_NOTSUPPORTED;
273 break;
274 case 7:
275 TauntLastError = TAUNTIMPERR_INVALIDCHANNELS;
276 break;
277 case 8:
278 TauntLastError = TAUNTIMPERR_INVALIDSAMPLES;
279 break;
280 case 9:
281 TauntLastError = TAUNTIMPERR_INVALIDBITDEPTH;
282 break;
283 case 10:
284 TauntLastError = TAUNTIMPERR_NODATA;
285 break;
286 }
287 ret = false;
288 goto error;
289 }
290
291 // at this point we have a loaded wave file in memory and information about it
292 // now we need to compress it, first it must be written as raw data to a temp
293 // file.
294
295 if (!ddio_GetTempFileName(Descent3_temp_directory, "d3o", temp_filename)) {
296 mprintf(0, "TAUNT: Unable to create temp filename\n");
297 ret = false;
298 TauntLastError = TAUNTIMPERR_INTERNALERR;
299 goto error;
300 }
301
302 file = cfopen(temp_filename, "wb");
303 if (!file) {
304 mprintf(0, "TAUNT: Unable to open temp filename\n");
305 ret = false;
306 TauntLastError = TAUNTIMPERR_INTERNALERR;
307 goto error;
308 }
309
310 /*
311 amount_to_flush = (wavdata.bits_per_sample==8)?wavdata.sample_length:wavdata.sample_length*2;
312
313 if(wavdata.bits_per_sample==8 && wavdata.sample_8bit ){
314 cf_WriteBytes(wavdata.sample_8bit,amount_to_flush,file);
315 }else if(wavdata.bits_per_sample==16 && wavdata.sample_16bit ){
316 //flip bytes(?)..NOTE SPEEDUP WARNING..THIS IS DONE IN WAVE LOAD..REMOVE IT?
317 for(int count = 0; count < (int)wavdata.sample_length; count++)
318 {
319 wavdata.sample_16bit[count] = INTEL_SHORT(wavdata.sample_16bit[count]);
320 }
321
322 cf_WriteBytes((uint8_t *)wavdata.sample_16bit,amount_to_flush,file);
323 }else{
324 mprintf(0,"TAUNT: Confusion to bits per sample (%d)\n",wavdata.bits_per_sample);
325 ret = false;
326 goto error;
327 }
328 mprintf(0,"TAUNT: Wrote to temp file %s\n",temp_filename);
329 cfclose(file); file = NULL;
330 */
331
332 amount_to_flush = (wavdata.sample_8bit) ? wavdata.sample_length : wavdata.sample_length * 2;
333
334 ASSERT(wavdata.sample_16bit != NULL); // only 16 bit should be coming out
335
336 // flip bytes(?)..NOTE SPEEDUP WARNING..THIS IS DONE IN WAVE LOAD..REMOVE IT?
337 int count;
338 for (count = 0; count < (int)wavdata.sample_length; count++) {
339 wavdata.sample_16bit[count] = INTEL_SHORT(wavdata.sample_16bit[count]);
340 }
341
342 cf_WriteBytes((uint8_t *)wavdata.sample_16bit, amount_to_flush, file);
343 cfclose(file);
344 file = NULL;
345
346 samples = wavdata.bits_per_sample;
347 rate = wavdata.samples_per_second;
348 chan = wavdata.number_channels;
349
350 if (!ddio_GetTempFileName(Descent3_temp_directory, "d3o", osftemp_filename)) {
351 mprintf(0, "TAUNT: Unable to create osftemp filename\n");
352 TauntLastError = TAUNTIMPERR_INTERNALERR;
353 ret = false;
354 goto error;
355 }
356
357 if (!aenc_Compress(temp_filename, osftemp_filename, NULL, &samples, &rate, &chan, NULL, NULL)) {
358 // unable to compress
359 mprintf(0, "Unable to compress\n");
360 ret = false;
361 TauntLastError = TAUNTIMPERR_COMPRESSIONFAILURE;
362 goto error;
363 }
364
365 // at this point osftemp_filename is a compressed file, now to convert it
366 // to osf format
367 // start writing out data.
368 tOSFDigiHdr digihdr;
369 uint32_t filelen, nblocks, i;
370 int format;
371
372 StaticFileBuffer = (uint8_t *)mem_malloc(FILEBUFFER_LENGTH);
373 if (!StaticFileBuffer) {
374 ret = false;
375 mprintf(0, "Out of memory\n");
376 TauntLastError = TAUNTIMPERR_OUTOFMEM;
377 goto error;
378 }
379
380 fpin = cfopen(osftemp_filename, "rb");
381 if (!fpin) {
382 mprintf(0, "Unable to open temp file to make osf\n");
383 ret = false;
384 TauntLastError = TAUNTIMPERR_INTERNALERR;
385 goto error;
386 }
387
388 filelen = (uint32_t)cfilelength(fpin);
389 nblocks = filelen / FILEBUFFER_LENGTH;
390
391 if (!osf.Open(outputfilename, true)) {
392 ret = false;
393 mprintf(0, "Unable to open osf to write\n");
394 TauntLastError = TAUNTIMPERR_NOOPENOSF;
395 goto error;
396 }
397 osfopened = true;
398
399 // write out data.
400 for (i = 0; i < nblocks; i++) {
401 if (!cf_ReadBytes(StaticFileBuffer, FILEBUFFER_LENGTH, fpin)) {
402 mprintf(0, "Error reading from osf temp\n");
403 ret = false;
404 TauntLastError = TAUNTIMPERR_INTERNALERR;
405 goto error;
406 }
407 if (!osf.WriteBlock(StaticFileBuffer, FILEBUFFER_LENGTH)) {
408 mprintf(0, "Error writing to osf\n");
409 TauntLastError = TAUNTIMPERR_INTERNALERR;
410 ret = false;
411 goto error;
412 }
413 }
414
415 if (filelen % FILEBUFFER_LENGTH) {
416 if (!cf_ReadBytes(StaticFileBuffer, filelen % FILEBUFFER_LENGTH, fpin)) {
417 mprintf(0, "Error reading from osf temp\n");
418 TauntLastError = TAUNTIMPERR_INTERNALERR;
419 ret = false;
420 goto error;
421 }
422 if (!osf.WriteBlock(StaticFileBuffer, filelen % FILEBUFFER_LENGTH)) {
423 mprintf(0, "Error writing to osf\n");
424 TauntLastError = TAUNTIMPERR_INTERNALERR;
425 ret = false;
426 goto error;
427 }
428 }
429
430 digihdr.measure = 44100;
431
432 format = 0;
433 if (wavdata.number_channels == 1) {
434 format |= SAF_MONO_MASK;
435 } else {
436 format |= SAF_STEREO_MASK;
437 }
438
439 if (wavdata.sample_16bit) {
440 format |= SAF_16BIT_MASK;
441 } else {
442 format |= SAF_8BIT_MASK;
443 }
444
445 // save header info
446 char osftitle[256];
447 ddio_SplitPath(outputfilename, NULL, osftitle, NULL);
448
449 if (!osf.SaveHeader(OSF_DIGITAL_STRM, OSF_DIGIACM_STRM, format, samples, filelen, &digihdr, osftitle)) {
450 mprintf(0, "Failed to write out OSF header info.");
451 TauntLastError = TAUNTIMPERR_INTERNALERR;
452 ret = false;
453 goto error;
454 }
455
456 osf.Close();
457
458 error:
459 if (file) {
460 cfclose(file);
461 }
462
463 if (osfopened)
464 osf.Close();
465
466 if (fpin) {
467 cfclose(fpin);
468 }
469
470 if (cfexist(osftemp_filename)) {
471 ddio_DeleteFile(osftemp_filename);
472 }
473
474 if (cfexist(temp_filename)) {
475 ddio_DeleteFile(temp_filename);
476 }
477
478 if (StaticFileBuffer) {
479 mem_free(StaticFileBuffer);
480 }
481
482 // free memory for wave file
483 if (wavdata.sample_16bit)
484 mem_free(wavdata.sample_16bit);
485 if (wavdata.sample_8bit)
486 mem_free(wavdata.sample_8bit);
487
488 return ret;
489 }
490
491 #define SOUND_FILE_SAMPLE_ALIGNMENT 4
492 char taunt_LoadWaveFile(char *filename, tWaveFile *wave) {
493 // File pointer to sound file
494 CFILE *cfptr;
495
496 wave->sample_16bit = NULL;
497 wave->sample_8bit = NULL;
498 wave->np_sample_length = 0;
499 wave->sample_length = 0;
500
501 cfptr = NULL;
502 char format_type[80]; // ASCII name of format type
503 uint16_t fmttag = 0; // Numerical format type
504 uint32_t ckid; // Current chunk's ID
505 uint32_t cksize; // Current chunk's size in bytes
506 uint32_t filesize; // Size of the sound file
507 uint32_t nextseek = 0; // Location of the next seek
508 uint32_t aligned_size; // Sound files are aligned to SOUND_FILE_SAMPLE_ALIGNMENT samples
509
510 // Sound format information
511 int samples_per_second;
512 int16_t bits_per_sample;
513 int16_t number_channels;
514 char error_code = 0;
515
516 // Used to read temporary long values
517 uint32_t temp_long;
518
519 // Flags for if we previously read data or a format
520 char f_data, f_fmt = 0;
521
522 // Loop counter
523 int count;
524
525 // Open the wave file
526 if ((cfptr = cfopen(filename, "rb")) == NULL) {
527 error_code = 1;
528 mprintf(0, "TAUNT: Wav %s not found\n", filename);
529 goto error_state;
530 }
531
532 // Make sure that it is a RIFF format
533 temp_long = (uint32_t)cf_ReadInt(cfptr);
534 if (temp_long != 0x46464952) {
535 error_code = 2;
536 mprintf(0, "TAUNT: Wav Load: %s is not a RIFF format file\n", filename);
537 goto error_state;
538 }
539
540 // The first item is the filesize minus the RIFF type specifier and filesize (add current location for total filesize)
541 filesize = cf_ReadInt(cfptr);
542 filesize += cftell(cfptr);
543
544 // Make sure it is a wave file
545 temp_long = (uint32_t)cf_ReadInt(cfptr);
546 if (temp_long != 0x45564157) {
547 error_code = 3;
548 mprintf(0, "TAUNT: Wav Load: %s is not a WAVE file\n", filename);
549 goto error_state;
550 }
551 nextseek = cftell(cfptr);
552
553 // Read in the chunks
554 while (nextseek < filesize - 9) // Need room for chunk and its header
555 {
556 // Move the the beginning of the next chunck
557 cfseek(cfptr, nextseek, SEEK_SET);
558
559 // Read in the chunk size and type
560 ckid = cf_ReadInt(cfptr);
561 cksize = cf_ReadInt(cfptr);
562 if (cksize <= 0) {
563 error_code = 4;
564 mprintf(0, "TAUNT: Wav Load: %s has an invalid block length\n", filename);
565 goto error_state;
566 }
567
568 // Determine where the next chunk begins
569 nextseek = cksize + cftell(cfptr);
570
571 // Determine what to do with the chunk
572 switch (ckid) {
573 case 0x20746D66: // Format Chunk
574 // Make sure that this format was not preceeded by another format without data inbetween them.
575 if (f_fmt) {
576 error_code = 5;
577 mprintf(0, "TAUNT: Wav Load: Found 2 formats in a row in file %s\n", filename);
578 goto error_state;
579 }
580
581 // Read in the format type
582 fmttag = (uint16_t)cf_ReadShort(cfptr);
583
584 switch (fmttag) {
585 // We only support WAVE_FORMAT_PCM currently
586 default:
587 case 0x0000:
588 strcpy(format_type, "WAVE_FORMAT_UNKNOWN");
589 break;
590 case 0x0001:
591 strcpy(format_type, "WAVE_FORMAT_PCM");
592 break;
593 case 0x0002:
594 strcpy(format_type, "WAVE_FORMAT_ADPCM");
595 break;
596 case 0x0005:
597 strcpy(format_type, "WAVE_FORMAT_IBM_CVSD");
598 break;
599 case 0x0006:
600 strcpy(format_type, "WAVE_FORMAT_ALAW");
601 break;
602 case 0x0007:
603 strcpy(format_type, "WAVE_FORMAT_MULAW");
604 break;
605 case 0x0010:
606 strcpy(format_type, "WAVE_FORMAT_OKI_ADPCM");
607 break;
608 case 0x0011:
609 strcpy(format_type, "WAVE_FORMAT_DVI_ADPCM");
610 break;
611 case 0x0015:
612 strcpy(format_type, "WAVE_FORMAT_DIGISTD");
613 break;
614 case 0x0016:
615 strcpy(format_type, "WAVE_FORMAT_DIGIFIX");
616 break;
617 case 0x0020:
618 strcpy(format_type, "WAVE_FORMAT_YAMAHA_ADPCM");
619 break;
620 case 0x0021:
621 strcpy(format_type, "WAVE_FORMAT_SONARC");
622 break;
623 case 0x0022:
624 strcpy(format_type, "WAVE_FORMAT_DSPGROUP_TRUESPEECH");
625 break;
626 case 0x0023:
627 strcpy(format_type, "WAVE_FORMAT_ECHOSC1");
628 break;
629 case 0x0024:
630 strcpy(format_type, "WAVE_FORMAT_AUDIOFILE_AF18");
631 break;
632 case 0x0101:
633 strcpy(format_type, "IBM_FORMAT_MULAW");
634 break;
635 case 0x0102:
636 strcpy(format_type, "IBM_FORMAT_ALAW");
637 break;
638 case 0x0103:
639 strcpy(format_type, "IBM_FORMAT_ADPCM");
640 break;
641 case 0x0200:
642 strcpy(format_type, "WAVE_FORMAT_CREATIVE_ADPCM");
643 break;
644 }
645
646 // Currently, we only support PCM wave files
647 if (fmttag != 0x0001) {
648 error_code = 6;
649 mprintf(0, "TAUNT Wav Load: %s is a type %s wavefile, we only support WAVE_FORMAT_PCM waves.\n",
650 filename,format_type);
651 goto error_state;
652 }
653
654 // Get the number of channels
655 number_channels = cf_ReadShort(cfptr);
656 if (number_channels != 1) {
657 error_code = 7;
658 mprintf(0, "TAUNT Wav Load: Invalid number of channels(%d)in %s, we want mono samples only.\n",
659 number_channels, filename);
660 goto error_state;
661 }
662
663 // Get the number of samples per second
664 samples_per_second = cf_ReadInt(cfptr);
665 if (samples_per_second != 22050) {
666 error_code = 8;
667 mprintf(0, "TAUNT Wav Load: Invalid sample per second(%d)in %s, we want 22k samples only.\n",
668 samples_per_second, filename);
669 goto error_state;
670 }
671
672 // get the Average bytes per second (ignore it)
673 cf_ReadInt(cfptr);
674
675 // Get the Block Alignment (ignore it)
676 cf_ReadShort(cfptr);
677
678 // Get the Bits per sample
679 bits_per_sample = cf_ReadShort(cfptr);
680 if (bits_per_sample != 8 && bits_per_sample != 16) {
681 error_code = 9;
682 mprintf(0, "TAUNT Wav Load: Invalid bits per sample(%d)in %s, we want 8 or 16 bit samples only.\n",
683 bits_per_sample, filename);
684 goto error_state;
685 }
686
687 // We did find a new format type
688 f_fmt = 1;
689 f_data = 0;
690 break;
691
692 // Data Chunk
693 case 0x61746164:
694 if (!f_fmt) {
695 error_code = 5;
696 mprintf(0, "TAUNT Wav Load: Format Chunk not defined before Data Chunk\n");
697 goto error_state;
698 }
699
700 // It is either 8 or 16 bit as validated from above
701 if (bits_per_sample == 8) {
702 int num_needed = 0;
703
704 // Force sample alignment
705 if ((cksize - 1) % SOUND_FILE_SAMPLE_ALIGNMENT) {
706 num_needed = SOUND_FILE_SAMPLE_ALIGNMENT - ((cksize - 1) % SOUND_FILE_SAMPLE_ALIGNMENT);
707 aligned_size = cksize + num_needed;
708 } else {
709 aligned_size = cksize;
710 }
711
712 wave->sample_length = aligned_size;
713 wave->np_sample_length = cksize;
714 wave->sample_8bit = (uint8_t *)mem_malloc(aligned_size);
715
716 cf_ReadBytes((uint8_t *)wave->sample_8bit, cksize, cfptr);
717
718 if (aligned_size != cksize)
719 memset(wave->sample_8bit + cksize, 128, num_needed);
720 } else {
721 int num_needed = 0;
722
723 wave->sample_length = cksize / 2;
724 wave->np_sample_length = cksize / 2;
725
726 if ((wave->sample_length - 1) % SOUND_FILE_SAMPLE_ALIGNMENT) {
727 num_needed = (SOUND_FILE_SAMPLE_ALIGNMENT - ((wave->sample_length - 1) % SOUND_FILE_SAMPLE_ALIGNMENT)) * 2;
728 wave->sample_length = cksize / 2 + num_needed / 2;
729 }
730
731 wave->sample_16bit = (int16_t *)mem_malloc(cksize + num_needed);
732
733 cf_ReadBytes((uint8_t *)wave->sample_16bit, cksize, cfptr);
734 for (count = 0; count < (int)cksize / 2; count++) {
735 wave->sample_16bit[count] = INTEL_SHORT(wave->sample_16bit[count]);
736 }
737
738 if (num_needed)
739 memset(wave->sample_16bit + (cksize / 2), 0, num_needed);
740 }
741
742 // We found data, clear format flag
743 f_data = 1;
744 f_fmt = 0; // Multiple waveforms, per file, are allowed
745 break;
746
747 // Undefined chunk
748 default:
749 break;
750 }
751 }
752
753 // Close the file
754 cfclose(cfptr);
755 cfptr = NULL;
756
757 if (wave->sample_16bit == NULL) {
758 ASSERT(wave->sample_8bit);
759
760 wave->sample_16bit = (int16_t *)mem_malloc(wave->sample_length * sizeof(int16_t));
761
762 // NOTE: Interesting note on sound conversion: 16 bit sounds are signed (0 biase). 8 bit sounds are unsigned
763 // (+128 biase).
764 for (count = 0; count < (int)wave->sample_length; count++) {
765 wave->sample_16bit[count] = (((int16_t)wave->sample_8bit[count]) - 128) * 256;
766 }
767
768 mem_free(wave->sample_8bit);
769 wave->sample_8bit = NULL;
770 }
771
772 error_state:
773
774 if (error_code != 0) {
775 // free memory and set to NULL
776 if (wave->sample_16bit)
777 mem_free(wave->sample_16bit);
778 if (wave->sample_8bit)
779 mem_free(wave->sample_8bit);
780 wave->sample_16bit = NULL;
781 wave->sample_8bit = NULL;
782 } else {
783 wave->samples_per_second = samples_per_second;
784 wave->bits_per_sample = bits_per_sample;
785 wave->number_channels = number_channels;
786 }
787
788 if (cfptr)
789 cfclose(cfptr);
790
791 return error_code;
792 }