annotate decoders/wav.c @ 183:26996236d790

Updated comments to reflect change from LICENSE to COPYING file, and put a good chunk of the ADPCM decoder in place. Does NOT work, is NOT complete, but is more than halfway there.
author Ryan C. Gordon <icculus@icculus.org>
date Wed, 26 Dec 2001 10:39:16 +0000
parents bdbe09014724
children 6bb68b3bdcf1
rev   line source
17
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
1 /*
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
2 * SDL_sound -- An abstract sound format decoding API.
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
3 * Copyright (C) 2001 Ryan C. Gordon.
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
4 *
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
5 * This library is free software; you can redistribute it and/or
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
7 * License as published by the Free Software Foundation; either
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
8 * version 2.1 of the License, or (at your option) any later version.
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
9 *
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
10 * This library is distributed in the hope that it will be useful,
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
13 * Lesser General Public License for more details.
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
14 *
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
16 * License along with this library; if not, write to the Free Software
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
18 */
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
19
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
20 /*
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
21 * WAV decoder for SDL_sound.
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
22 *
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
23 * This driver handles Microsoft .WAVs, in as many of the thousands of
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
24 * variations as we can.
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
25 *
183
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
26 * Please see the file COPYING in the source's root directory.
17
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
27 *
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
28 * This file written by Ryan C. Gordon. (icculus@clutteredmind.org)
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
29 */
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
30
106
40de367eb59e Changing my include structure to do this right.
Ryan C. Gordon <icculus@icculus.org>
parents: 104
diff changeset
31 #if HAVE_CONFIG_H
40de367eb59e Changing my include structure to do this right.
Ryan C. Gordon <icculus@icculus.org>
parents: 104
diff changeset
32 # include <config.h>
40de367eb59e Changing my include structure to do this right.
Ryan C. Gordon <icculus@icculus.org>
parents: 104
diff changeset
33 #endif
100
6d9fdec2f708 added config.h, added --enable-debug flag, various other changes to the build system
fingolfin
parents: 64
diff changeset
34
104
103cfcb3c014 Updated to fix build system problem.
Ryan C. Gordon <icculus@icculus.org>
parents: 100
diff changeset
35 #ifdef SOUND_SUPPORTS_WAV
103cfcb3c014 Updated to fix build system problem.
Ryan C. Gordon <icculus@icculus.org>
parents: 100
diff changeset
36
17
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
37 #include <stdio.h>
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
38 #include <stdlib.h>
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
39 #include <string.h>
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
40 #include <assert.h>
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
41
106
40de367eb59e Changing my include structure to do this right.
Ryan C. Gordon <icculus@icculus.org>
parents: 104
diff changeset
42 #include "SDL_sound.h"
40de367eb59e Changing my include structure to do this right.
Ryan C. Gordon <icculus@icculus.org>
parents: 104
diff changeset
43
40de367eb59e Changing my include structure to do this right.
Ryan C. Gordon <icculus@icculus.org>
parents: 104
diff changeset
44 #define __SDL_SOUND_INTERNAL__
40de367eb59e Changing my include structure to do this right.
Ryan C. Gordon <icculus@icculus.org>
parents: 104
diff changeset
45 #include "SDL_sound_internal.h"
40de367eb59e Changing my include structure to do this right.
Ryan C. Gordon <icculus@icculus.org>
parents: 104
diff changeset
46
47
ea58bc3b15d7 Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
47 static int WAV_init(void);
ea58bc3b15d7 Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
48 static void WAV_quit(void);
17
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
49 static int WAV_open(Sound_Sample *sample, const char *ext);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
50 static void WAV_close(Sound_Sample *sample);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
51 static Uint32 WAV_read(Sound_Sample *sample);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
52
149
1df5c106504e Decoders can now list multiple file extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 124
diff changeset
53 static const char *extensions_wav[] = { "WAV", NULL };
17
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
54 const Sound_DecoderFunctions __Sound_DecoderFunctions_WAV =
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
55 {
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
56 {
149
1df5c106504e Decoders can now list multiple file extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 124
diff changeset
57 extensions_wav,
17
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
58 "Microsoft WAVE audio format",
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
59 "Ryan C. Gordon <icculus@clutteredmind.org>",
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
60 "http://www.icculus.org/SDL_sound/"
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
61 },
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
62
47
ea58bc3b15d7 Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
63 WAV_init, /* init() method */
ea58bc3b15d7 Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
64 WAV_quit, /* quit() method */
ea58bc3b15d7 Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
65 WAV_open, /* open() method */
ea58bc3b15d7 Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
66 WAV_close, /* close() method */
ea58bc3b15d7 Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
67 WAV_read /* read() method */
17
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
68 };
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
69
124
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
70
183
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
71 /* Better than SDL_ReadLE16, since you can detect i/o errors... */
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
72 static inline int read_le16(SDL_RWops *rw, Uint16 *ui16)
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
73 {
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
74 int rc = SDL_RWread(rw, ui16, sizeof (Uint16), 1);
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
75 BAIL_IF_MACRO(rc != 1, ERR_IO_ERROR, 0);
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
76 *ui16 = SDL_SwapLE16(*ui16);
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
77 return(1);
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
78 } /* read_le16 */
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
79
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
80
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
81 /* Better than SDL_ReadLE32, since you can detect i/o errors... */
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
82 static inline int read_le32(SDL_RWops *rw, Uint32 *ui32)
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
83 {
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
84 int rc = SDL_RWread(rw, ui32, sizeof (Uint32), 1);
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
85 BAIL_IF_MACRO(rc != 1, ERR_IO_ERROR, 0);
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
86 *ui32 = SDL_SwapLE32(*ui32);
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
87 return(1);
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
88 } /* read_le32 */
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
89
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
90
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
91 /* This is just cleaner on the caller's end... */
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
92 static inline int read_uint8(SDL_RWops *rw, Uint8 *ui8)
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
93 {
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
94 int rc = SDL_RWread(rw, ui8, sizeof (Uint8), 1);
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
95 BAIL_IF_MACRO(rc != 1, ERR_IO_ERROR, 0);
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
96 return(1);
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
97 } /* read_uint8 */
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
98
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
99
124
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
100 /* Chunk management code... */
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
101
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
102 #define riffID 0x46464952 /* "RIFF", in ascii. */
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
103 #define waveID 0x45564157 /* "WAVE", in ascii. */
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
104
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
105
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
106 /*****************************************************************************
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
107 * The FORMAT chunk... *
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
108 *****************************************************************************/
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
109
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
110 #define fmtID 0x20746D66 /* "fmt ", in ascii. */
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
111
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
112 #define FMT_NORMAL 0x0001 /* Uncompressed waveform data. */
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
113 #define FMT_ADPCM 0x0002 /* ADPCM compressed waveform data. */
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
114
183
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
115 typedef struct
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
116 {
124
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
117 Uint16 iCoef1;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
118 Uint16 iCoef2;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
119 } ADPCMCOEFSET;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
120
183
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
121 typedef struct
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
122 {
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
123 Uint8 bPredictor;
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
124 Uint16 iDelta;
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
125 Sint16 iSamp1;
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
126 Sint16 iSamp2;
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
127 } ADPCMBLOCKHEADER;
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
128
178
bdbe09014724 Minor tweaks and such.
Ryan C. Gordon <icculus@icculus.org>
parents: 149
diff changeset
129 typedef struct S_WAV_FMT_T
124
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
130 {
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
131 Uint32 chunkID;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
132 Sint32 chunkSize;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
133 Sint16 wFormatTag;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
134 Uint16 wChannels;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
135 Uint32 dwSamplesPerSec;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
136 Uint32 dwAvgBytesPerSec;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
137 Uint16 wBlockAlign;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
138 Uint16 wBitsPerSample;
178
bdbe09014724 Minor tweaks and such.
Ryan C. Gordon <icculus@icculus.org>
parents: 149
diff changeset
139
bdbe09014724 Minor tweaks and such.
Ryan C. Gordon <icculus@icculus.org>
parents: 149
diff changeset
140 void (*free)(struct S_WAV_FMT_T *fmt);
183
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
141 Uint32 (*read_sample)(Sound_Sample *sample);
124
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
142
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
143 union
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
144 {
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
145 struct
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
146 {
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
147 Uint16 cbSize;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
148 Uint16 wSamplesPerBlock;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
149 Uint16 wNumCoef;
183
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
150 ADPCMCOEFSET *aCoef;
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
151 ADPCMBLOCKHEADER *blockheaders;
124
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
152 } adpcm;
178
bdbe09014724 Minor tweaks and such.
Ryan C. Gordon <icculus@icculus.org>
parents: 149
diff changeset
153
bdbe09014724 Minor tweaks and such.
Ryan C. Gordon <icculus@icculus.org>
parents: 149
diff changeset
154 /* put other format-specific data here... */
124
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
155 } fmt;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
156 } fmt_t;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
157
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
158
17
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
159 /*
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
160 * Read in a fmt_t from disk. This makes this process safe regardless of
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
161 * the processor's byte order or how the fmt_t structure is packed.
124
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
162 * Note that the union "fmt" is not read in here; that is handled as
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
163 * needed in the read_fmt_* functions.
17
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
164 */
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
165 static int read_fmt_chunk(SDL_RWops *rw, fmt_t *fmt)
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
166 {
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
167 /* skip reading the chunk ID, since it was already read at this point... */
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
168 fmt->chunkID = fmtID;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
169
183
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
170 BAIL_IF_MACRO(!read_le32(rw, &fmt->chunkSize), NULL, 0);
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
171 BAIL_IF_MACRO(!read_le16(rw, &fmt->wFormatTag), NULL, 0);
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
172 BAIL_IF_MACRO(!read_le16(rw, &fmt->wChannels), NULL, 0);
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
173 BAIL_IF_MACRO(!read_le32(rw, &fmt->dwSamplesPerSec), NULL, 0);
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
174 BAIL_IF_MACRO(!read_le32(rw, &fmt->dwAvgBytesPerSec), NULL, 0);
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
175 BAIL_IF_MACRO(!read_le16(rw, &fmt->wBlockAlign), NULL, 0);
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
176 BAIL_IF_MACRO(!read_le16(rw, &fmt->wBitsPerSample), NULL, 0);
17
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
177
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
178 return(1);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
179 } /* read_fmt_chunk */
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
180
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
181
124
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
182
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
183 /*****************************************************************************
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
184 * The DATA chunk... *
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
185 *****************************************************************************/
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
186
17
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
187 #define dataID 0x61746164 /* "data", in ascii. */
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
188
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
189 typedef struct
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
190 {
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
191 Uint32 chunkID;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
192 Sint32 chunkSize;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
193 /* Then, (chunkSize) bytes of waveform data... */
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
194 } data_t;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
195
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
196
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
197 /*
124
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
198 * Read in a data_t from disk. This makes this process safe regardless of
17
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
199 * the processor's byte order or how the fmt_t structure is packed.
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
200 */
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
201 static int read_data_chunk(SDL_RWops *rw, data_t *data)
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
202 {
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
203 /* skip reading the chunk ID, since it was already read at this point... */
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
204 data->chunkID = dataID;
183
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
205 BAIL_IF_MACRO(!read_le32(rw, &data->chunkSize), NULL, 0);
17
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
206 return(1);
40
c15396fc0e55 Fixed incorrect comment, by Torbj�rn Andersson. :)
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
207 } /* read_data_chunk */
17
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
208
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
209
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
210
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
211
124
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
212 /*****************************************************************************
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
213 * this is what we store in our internal->decoder_private field... *
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
214 *****************************************************************************/
17
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
215
124
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
216 typedef struct
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
217 {
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
218 fmt_t *fmt;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
219 Sint32 bytesLeft;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
220 } wav_t;
17
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
221
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
222
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
223
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
224
124
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
225 /*****************************************************************************
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
226 * Normal, uncompressed waveform handler... *
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
227 *****************************************************************************/
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
228
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
229 static Uint32 read_sample_fmt_normal(Sound_Sample *sample)
17
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
230 {
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
231 Uint32 retval;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
232 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
233 wav_t *w = (wav_t *) internal->decoder_private;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
234 Uint32 max = (internal->buffer_size < (Uint32) w->bytesLeft) ?
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
235 internal->buffer_size : (Uint32) w->bytesLeft;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
236
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
237 assert(max > 0);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
238
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
239 /*
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
240 * We don't actually do any decoding, so we read the wav data
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
241 * directly into the internal buffer...
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
242 */
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
243 retval = SDL_RWread(internal->rw, internal->buffer, 1, max);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
244
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
245 w->bytesLeft -= retval;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
246
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
247 /* Make sure the read went smoothly... */
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
248 if ((retval == 0) || (w->bytesLeft == 0))
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
249 sample->flags |= SOUND_SAMPLEFLAG_EOF;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
250
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
251 else if (retval == -1)
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
252 sample->flags |= SOUND_SAMPLEFLAG_ERROR;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
253
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
254 /* (next call this EAGAIN may turn into an EOF or error.) */
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
255 else if (retval < internal->buffer_size)
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
256 sample->flags |= SOUND_SAMPLEFLAG_EAGAIN;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
257
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
258 return(retval);
124
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
259 } /* read_sample_fmt_normal */
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
260
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
261
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
262
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
263 static int read_fmt_normal(SDL_RWops *rw, fmt_t *fmt)
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
264 {
178
bdbe09014724 Minor tweaks and such.
Ryan C. Gordon <icculus@icculus.org>
parents: 149
diff changeset
265 /* (don't need to read more from the RWops...) */
183
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
266 fmt->free = NULL;
124
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
267 fmt->read_sample = read_sample_fmt_normal;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
268 return(1);
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
269 } /* read_fmt_normal */
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
270
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
271
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
272
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
273 /*****************************************************************************
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
274 * ADPCM compression handler... *
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
275 *****************************************************************************/
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
276
183
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
277 static int read_adpcm_block_headers(SDL_RWops *rw, fmt_t *fmt)
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
278 {
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
279 ADPCMBLOCKHEADER *headers = fmt->fmt.adpcm.blockheaders;
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
280 int i;
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
281 int max = fmt->wChannels;
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
282
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
283 for (i = 0; i < max; i++)
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
284 BAIL_IF_MACRO(!read_uint8(rw, &headers[i].bPredictor), NULL, 0);
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
285
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
286 for (i = 0; i < max; i++)
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
287 BAIL_IF_MACRO(!read_le16(rw, &headers[i].iDelta), NULL, 0);
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
288
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
289 for (i = 0; i < max; i++)
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
290 BAIL_IF_MACRO(!read_le16(rw, &headers[i].iSamp1), NULL, 0);
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
291
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
292 for (i = 0; i < max; i++)
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
293 BAIL_IF_MACRO(!read_le16(rw, &headers[i].iSamp2), NULL, 0);
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
294
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
295 return(1);
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
296 } /* read_adpcm_block_headers */
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
297
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
298
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
299 static int decode_adpcm_block(Sound_Sample *sample)
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
300 {
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
301 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
302 SDL_RWops *rw = internal->rw;
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
303 wav_t *w = (wav_t *) internal->decoder_private;
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
304 fmt_t *fmt = w->fmt;
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
305 ADPCMBLOCKHEADER *headers = fmt->fmt.adpcm.blockheaders;
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
306 int i;
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
307 int max = fmt->wChannels;
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
308
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
309 if (!read_adpcm_block_headers(rw, fmt))
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
310 {
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
311 sample->flags |= SOUND_SAMPLEFLAG_ERROR;
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
312 return(0);
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
313 } /* if */
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
314
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
315 for (i = 0; i < max; i++)
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
316 {
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
317 Uint16 iCoef1 = fmt->fmt.adpcm.aCoef[headers[i].bPredictor].iCoef1;
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
318 Uint16 iCoef2 = fmt->fmt.adpcm.aCoef[headers[i].bPredictor].iCoef2;
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
319 /*
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
320 Sint32 lPredSamp = ((headers[i].iSamp1 * iCoef1) +
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
321 (headers[i].iSamp2 * iCoef2)) /
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
322 FIXED_POINT_COEF_BASE;
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
323 */
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
324 } /* for */
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
325 } /* decode_adpcm_block */
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
326
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
327
124
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
328 static Uint32 read_sample_fmt_adpcm(Sound_Sample *sample)
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
329 {
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
330 /* !!! FIXME: Write this. */
183
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
331 Sound_SetError("WAV: Not implemented. Soon.");
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
332 sample->flags |= SOUND_SAMPLEFLAG_ERROR;
124
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
333 return(0);
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
334 } /* read_sample_fmt_adpcm */
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
335
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
336
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
337 static void free_fmt_adpcm(fmt_t *fmt)
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
338 {
183
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
339 if (fmt->fmt.adpcm.aCoef != NULL)
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
340 free(fmt->fmt.adpcm.aCoef);
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
341
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
342 if (fmt->fmt.adpcm.blockheaders != NULL)
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
343 free(fmt->fmt.adpcm.blockheaders);
124
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
344 } /* free_fmt_adpcm */
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
345
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
346
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
347 /*
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
348 * Read in a the adpcm-specific info from disk. This makes this process
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
349 * safe regardless of the processor's byte order or how the fmt_t
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
350 * structure is packed.
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
351 */
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
352 static int read_fmt_adpcm(SDL_RWops *rw, fmt_t *fmt)
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
353 {
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
354 size_t i;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
355
183
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
356 memset(&fmt->fmt.adpcm, '\0', sizeof (fmt->fmt.adpcm));
124
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
357 fmt->free = free_fmt_adpcm;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
358 fmt->read_sample = read_sample_fmt_adpcm;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
359
183
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
360 BAIL_IF_MACRO(!read_le16(rw, &fmt->fmt.adpcm.cbSize), NULL, 0);
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
361 BAIL_IF_MACRO(!read_le16(rw, &fmt->fmt.adpcm.wSamplesPerBlock), NULL, 0);
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
362 BAIL_IF_MACRO(!read_le16(rw, &fmt->fmt.adpcm.wNumCoef), NULL, 0);
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
363
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
364 /*
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
365 * !!! FIXME: SDL seems nervous about this, so I'll make a debug
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
366 * !!! FIXME: note for the time being...
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
367 */
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
368 if (fmt->fmt.adpcm.wNumCoef != 7)
124
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
369 {
183
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
370 SNDDBG(("WAV: adpcm's wNumCoef is NOT seven (it's %d)!\n",
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
371 fmt->fmt.adpcm.wNumCoef));
124
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
372 } /* if */
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
373
183
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
374 /* fmt->free() is always called, so these malloc()s will be cleaned up. */
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
375 i = sizeof (ADPCMCOEFSET) * fmt->fmt.adpcm.wNumCoef;
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
376 fmt->fmt.adpcm.aCoef = (ADPCMCOEFSET *) malloc(i);
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
377 BAIL_IF_MACRO(fmt->fmt.adpcm.aCoef == NULL, ERR_OUT_OF_MEMORY, 0);
124
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
378
183
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
379 i = sizeof (ADPCMBLOCKHEADER) * fmt->wChannels;
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
380 fmt->fmt.adpcm.blockheaders = (ADPCMBLOCKHEADER *) malloc(i);
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
381 BAIL_IF_MACRO(fmt->fmt.adpcm.blockheaders == NULL, ERR_OUT_OF_MEMORY, 0);
124
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
382
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
383 for (i = 0; i < fmt->fmt.adpcm.wNumCoef; i++)
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
384 {
183
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
385 int rc;
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
386 rc = SDL_RWread(rw, &fmt->fmt.adpcm.aCoef[i].iCoef1,
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
387 sizeof (fmt->fmt.adpcm.aCoef[i].iCoef1), 1);
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
388 BAIL_IF_MACRO(rc != 1, ERR_IO_ERROR, 0);
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
389
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
390 rc = SDL_RWread(rw, &fmt->fmt.adpcm.aCoef[i].iCoef2,
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
391 sizeof (fmt->fmt.adpcm.aCoef[i].iCoef2), 1);
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
392 BAIL_IF_MACRO(rc != 1, ERR_IO_ERROR, 0);
124
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
393 } /* for */
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
394
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
395 return(1);
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
396 } /* read_fmt_adpcm */
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
397
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
398
178
bdbe09014724 Minor tweaks and such.
Ryan C. Gordon <icculus@icculus.org>
parents: 149
diff changeset
399
124
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
400 /*****************************************************************************
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
401 * Everything else... *
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
402 *****************************************************************************/
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
403
178
bdbe09014724 Minor tweaks and such.
Ryan C. Gordon <icculus@icculus.org>
parents: 149
diff changeset
404 static int WAV_init(void)
bdbe09014724 Minor tweaks and such.
Ryan C. Gordon <icculus@icculus.org>
parents: 149
diff changeset
405 {
bdbe09014724 Minor tweaks and such.
Ryan C. Gordon <icculus@icculus.org>
parents: 149
diff changeset
406 return(1); /* always succeeds. */
bdbe09014724 Minor tweaks and such.
Ryan C. Gordon <icculus@icculus.org>
parents: 149
diff changeset
407 } /* WAV_init */
bdbe09014724 Minor tweaks and such.
Ryan C. Gordon <icculus@icculus.org>
parents: 149
diff changeset
408
bdbe09014724 Minor tweaks and such.
Ryan C. Gordon <icculus@icculus.org>
parents: 149
diff changeset
409
bdbe09014724 Minor tweaks and such.
Ryan C. Gordon <icculus@icculus.org>
parents: 149
diff changeset
410 static void WAV_quit(void)
bdbe09014724 Minor tweaks and such.
Ryan C. Gordon <icculus@icculus.org>
parents: 149
diff changeset
411 {
bdbe09014724 Minor tweaks and such.
Ryan C. Gordon <icculus@icculus.org>
parents: 149
diff changeset
412 /* it's a no-op. */
bdbe09014724 Minor tweaks and such.
Ryan C. Gordon <icculus@icculus.org>
parents: 149
diff changeset
413 } /* WAV_quit */
bdbe09014724 Minor tweaks and such.
Ryan C. Gordon <icculus@icculus.org>
parents: 149
diff changeset
414
124
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
415
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
416 static int read_fmt(SDL_RWops *rw, fmt_t *fmt)
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
417 {
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
418 /* if it's in this switch statement, we support the format. */
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
419 switch (fmt->wFormatTag)
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
420 {
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
421 case FMT_NORMAL:
178
bdbe09014724 Minor tweaks and such.
Ryan C. Gordon <icculus@icculus.org>
parents: 149
diff changeset
422 SNDDBG(("WAV: Appears to be uncompressed audio.\n"));
124
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
423 return(read_fmt_normal(rw, fmt));
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
424
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
425 case FMT_ADPCM:
178
bdbe09014724 Minor tweaks and such.
Ryan C. Gordon <icculus@icculus.org>
parents: 149
diff changeset
426 SNDDBG(("WAV: Appears to be ADPCM compressed audio.\n"));
124
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
427 return(read_fmt_adpcm(rw, fmt));
178
bdbe09014724 Minor tweaks and such.
Ryan C. Gordon <icculus@icculus.org>
parents: 149
diff changeset
428
bdbe09014724 Minor tweaks and such.
Ryan C. Gordon <icculus@icculus.org>
parents: 149
diff changeset
429 /* add other types here. */
bdbe09014724 Minor tweaks and such.
Ryan C. Gordon <icculus@icculus.org>
parents: 149
diff changeset
430
bdbe09014724 Minor tweaks and such.
Ryan C. Gordon <icculus@icculus.org>
parents: 149
diff changeset
431 default:
bdbe09014724 Minor tweaks and such.
Ryan C. Gordon <icculus@icculus.org>
parents: 149
diff changeset
432 SNDDBG(("WAV: Format %lu is unknown.\n",
bdbe09014724 Minor tweaks and such.
Ryan C. Gordon <icculus@icculus.org>
parents: 149
diff changeset
433 (unsigned int) fmt->wFormatTag));
bdbe09014724 Minor tweaks and such.
Ryan C. Gordon <icculus@icculus.org>
parents: 149
diff changeset
434 Sound_SetError("WAV: Unsupported format");
bdbe09014724 Minor tweaks and such.
Ryan C. Gordon <icculus@icculus.org>
parents: 149
diff changeset
435 return(0); /* not supported whatsoever. */
124
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
436 } /* switch */
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
437
178
bdbe09014724 Minor tweaks and such.
Ryan C. Gordon <icculus@icculus.org>
parents: 149
diff changeset
438 assert(0); /* shouldn't hit this point. */
bdbe09014724 Minor tweaks and such.
Ryan C. Gordon <icculus@icculus.org>
parents: 149
diff changeset
439 return(0);
124
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
440 } /* read_fmt */
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
441
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
442
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
443 /*
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
444 * Locate a specific chunk in the WAVE file by ID...
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
445 */
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
446 static int find_chunk(SDL_RWops *rw, Uint32 id)
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
447 {
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
448 Sint32 siz = 0;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
449 Uint32 _id = 0;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
450
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
451 while (1)
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
452 {
183
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
453 BAIL_IF_MACRO(!read_le32(rw, &_id), NULL, 0);
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
454 if (_id == id)
124
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
455 return(1);
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
456
183
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
457 /* skip ahead and see what next chunk is... */
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
458 BAIL_IF_MACRO(!read_le32(rw, &siz), NULL, 0);
124
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
459 assert(siz > 0);
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
460 BAIL_IF_MACRO(SDL_RWseek(rw, siz, SEEK_SET) != siz, NULL, 0);
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
461 } /* while */
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
462
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
463 return(0); /* shouldn't hit this, but just in case... */
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
464 } /* find_chunk */
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
465
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
466
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
467 static int WAV_open_internal(Sound_Sample *sample, const char *ext, fmt_t *fmt)
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
468 {
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
469 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
470 SDL_RWops *rw = internal->rw;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
471 data_t d;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
472 wav_t *w;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
473
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
474 BAIL_IF_MACRO(SDL_ReadLE32(rw) != riffID, "WAV: Not a RIFF file.", 0);
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
475 SDL_ReadLE32(rw); /* throw the length away; we get this info later. */
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
476 BAIL_IF_MACRO(SDL_ReadLE32(rw) != waveID, "WAV: Not a WAVE file.", 0);
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
477 BAIL_IF_MACRO(!find_chunk(rw, fmtID), "WAV: No format chunk.", 0);
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
478 BAIL_IF_MACRO(!read_fmt_chunk(rw, fmt), "WAV: Can't read format chunk.", 0);
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
479
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
480 sample->actual.channels = (Uint8) fmt->wChannels;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
481 sample->actual.rate = fmt->dwSamplesPerSec;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
482 if (fmt->wBitsPerSample <= 8)
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
483 sample->actual.format = AUDIO_U8;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
484 else if (fmt->wBitsPerSample <= 16)
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
485 sample->actual.format = AUDIO_S16LSB;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
486 else
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
487 BAIL_MACRO("WAV: Unsupported sample size.", 0);
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
488
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
489 BAIL_IF_MACRO(!read_fmt(rw, fmt), NULL, 0);
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
490 BAIL_IF_MACRO(!find_chunk(rw, dataID), "WAV: No data chunk.", 0);
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
491 BAIL_IF_MACRO(!read_data_chunk(rw, &d), "WAV: Can't read data chunk.", 0);
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
492
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
493 w = (wav_t *) malloc(sizeof(wav_t));
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
494 BAIL_IF_MACRO(w == NULL, ERR_OUT_OF_MEMORY, 0);
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
495 w->fmt = fmt;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
496 w->bytesLeft = d.chunkSize;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
497 internal->decoder_private = (void *) w;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
498
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
499 sample->flags = SOUND_SAMPLEFLAG_NONE;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
500
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
501 SNDDBG(("WAV: Accepting data stream.\n"));
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
502 return(1); /* we'll handle this data. */
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
503 } /* WAV_open_internal */
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
504
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
505
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
506 static int WAV_open(Sound_Sample *sample, const char *ext)
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
507 {
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
508 int rc;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
509
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
510 fmt_t *fmt = (fmt_t *) malloc(sizeof (fmt_t));
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
511 BAIL_IF_MACRO(fmt == NULL, ERR_OUT_OF_MEMORY, 0);
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
512 memset(fmt, '\0', sizeof (fmt_t));
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
513
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
514 rc = WAV_open_internal(sample, ext, fmt);
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
515 if (!rc)
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
516 {
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
517 if (fmt->free != NULL)
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
518 fmt->free(fmt);
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
519 free(fmt);
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
520 } /* if */
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
521
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
522 return(rc);
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
523 } /* WAV_open */
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
524
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
525
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
526 static void WAV_close(Sound_Sample *sample)
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
527 {
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
528 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
529 wav_t *w = (wav_t *) internal->decoder_private;
183
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
530
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
531 if (w->fmt->free != NULL)
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
532 w->fmt->free(w->fmt);
26996236d790 Updated comments to reflect change from LICENSE to COPYING file, and put a
Ryan C. Gordon <icculus@icculus.org>
parents: 178
diff changeset
533
124
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
534 free(w->fmt);
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
535 free(w);
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
536 } /* WAV_close */
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
537
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
538
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
539 static Uint32 WAV_read(Sound_Sample *sample)
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
540 {
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
541 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
542 wav_t *w = (wav_t *) internal->decoder_private;
e46e31fdecfd Restructured to handle multiple formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
543 return(w->fmt->read_sample(sample));
17
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
544 } /* WAV_read */
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
545
64
40006625142a Changes in preparation of autoconf support.
Ryan C. Gordon <icculus@icculus.org>
parents: 62
diff changeset
546 #endif /* SOUND_SUPPORTS_WAV */
17
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
547
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
548 /* end of wav.c ... */
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
549