annotate decoders/wav.c @ 119:254916e602c6

Updated.
author Ryan C. Gordon <icculus@icculus.org>
date Sat, 06 Oct 2001 22:11:09 +0000
parents 40de367eb59e
children e46e31fdecfd
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 *
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
26 * Please see the file LICENSE in the source's root directory.
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
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
53 const Sound_DecoderFunctions __Sound_DecoderFunctions_WAV =
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
54 {
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 "WAV",
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
57 "Microsoft WAVE audio format",
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
58 "Ryan C. Gordon <icculus@clutteredmind.org>",
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
59 "http://www.icculus.org/SDL_sound/"
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
60 },
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
61
47
ea58bc3b15d7 Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
62 WAV_init, /* init() method */
ea58bc3b15d7 Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
63 WAV_quit, /* quit() method */
ea58bc3b15d7 Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
64 WAV_open, /* open() method */
ea58bc3b15d7 Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
65 WAV_close, /* close() method */
ea58bc3b15d7 Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
66 WAV_read /* read() method */
17
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
67 };
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
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
70 /* this is what we store in our internal->decoder_private field... */
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
71 typedef struct
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
72 {
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
73 Sint32 bytesLeft;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
74 } wav_t;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
75
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
76
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
77 /* Chunk management code... */
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
78
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
79 #define riffID 0x46464952 /* "RIFF", in ascii. */
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
80 #define waveID 0x45564157 /* "WAVE", in ascii. */
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
81
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
82
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
83 #define fmtID 0x20746D66 /* "fmt ", in ascii. */
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
84
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
85 #define FMT_NORMAL 1 /* Uncompressed waveform data. */
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
86
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
87 typedef struct
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
88 {
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
89 Uint32 chunkID;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
90 Sint32 chunkSize;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
91 Sint16 wFormatTag;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
92 Uint16 wChannels;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
93 Uint32 dwSamplesPerSec;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
94 Uint32 dwAvgBytesPerSec;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
95 Uint16 wBlockAlign;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
96 Uint16 wBitsPerSample;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
97 } fmt_t;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
98
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
99
47
ea58bc3b15d7 Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
100 static int WAV_init(void)
ea58bc3b15d7 Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
101 {
ea58bc3b15d7 Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
102 return(1); /* always succeeds. */
ea58bc3b15d7 Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
103 } /* WAV_init */
ea58bc3b15d7 Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
104
ea58bc3b15d7 Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
105
ea58bc3b15d7 Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
106 static void WAV_quit(void)
ea58bc3b15d7 Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
107 {
ea58bc3b15d7 Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
108 /* it's a no-op. */
ea58bc3b15d7 Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
109 } /* WAV_quit */
ea58bc3b15d7 Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
110
ea58bc3b15d7 Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents: 40
diff changeset
111
17
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
112 /*
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
113 * 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
114 * 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
115 */
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
116 static int read_fmt_chunk(SDL_RWops *rw, fmt_t *fmt)
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
117 {
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
118 /* 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
119 fmt->chunkID = fmtID;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
120
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
121 if (SDL_RWread(rw, &fmt->chunkSize, sizeof (fmt->chunkSize), 1) != 1)
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
122 return(0);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
123 fmt->chunkSize = SDL_SwapLE32(fmt->chunkSize);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
124
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
125 if (SDL_RWread(rw, &fmt->wFormatTag, sizeof (fmt->wFormatTag), 1) != 1)
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
126 return(0);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
127 fmt->wFormatTag = SDL_SwapLE16(fmt->wFormatTag);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
128
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
129 if (SDL_RWread(rw, &fmt->wChannels, sizeof (fmt->wChannels), 1) != 1)
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
130 return(0);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
131 fmt->wChannels = SDL_SwapLE16(fmt->wChannels);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
132
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
133 if (SDL_RWread(rw, &fmt->dwSamplesPerSec,
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
134 sizeof (fmt->dwSamplesPerSec), 1) != 1)
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
135 return(0);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
136 fmt->dwSamplesPerSec = SDL_SwapLE32(fmt->dwSamplesPerSec);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
137
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
138 if (SDL_RWread(rw, &fmt->dwAvgBytesPerSec,
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
139 sizeof (fmt->dwAvgBytesPerSec), 1) != 1)
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
140 return(0);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
141 fmt->dwAvgBytesPerSec = SDL_SwapLE32(fmt->dwAvgBytesPerSec);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
142
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
143 if (SDL_RWread(rw, &fmt->wBlockAlign, sizeof (fmt->wBlockAlign), 1) != 1)
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
144 return(0);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
145 fmt->wBlockAlign = SDL_SwapLE16(fmt->wBlockAlign);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
146
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
147 if (SDL_RWread(rw, &fmt->wBitsPerSample,
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
148 sizeof (fmt->wBitsPerSample), 1) != 1)
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
149 return(0);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
150 fmt->wBitsPerSample = SDL_SwapLE16(fmt->wBitsPerSample);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
151
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
152 return(1);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
153 } /* read_fmt_chunk */
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
154
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
155
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
156 #define dataID 0x61746164 /* "data", in ascii. */
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
157
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
158 typedef struct
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 Uint32 chunkID;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
161 Sint32 chunkSize;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
162 /* Then, (chunkSize) bytes of waveform data... */
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
163 } data_t;
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
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 * 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
168 * 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
169 */
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
170 static int read_data_chunk(SDL_RWops *rw, data_t *data)
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
171 {
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
172 /* 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
173 data->chunkID = dataID;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
174
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
175 if (SDL_RWread(rw, &data->chunkSize, sizeof (data->chunkSize), 1) != 1)
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
176 return(0);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
177 data->chunkSize = SDL_SwapLE32(data->chunkSize);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
178
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
179 return(1);
40
c15396fc0e55 Fixed incorrect comment, by Torbj�rn Andersson. :)
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
180 } /* read_data_chunk */
17
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
181
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
182
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
183 static int find_chunk(SDL_RWops *rw, Uint32 id)
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
184 {
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
185 Sint32 siz = 0;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
186 Uint32 _id = 0;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
187
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
188 while (1)
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
189 {
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
190 BAIL_IF_MACRO(SDL_RWread(rw, &_id, sizeof (_id), 1) != 1, NULL, 0);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
191 if (SDL_SwapLE32(_id) == id)
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
192 return(1);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
193
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
194 BAIL_IF_MACRO(SDL_RWread(rw, &siz, sizeof (siz), 1) != 1, NULL, 0);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
195 siz = SDL_SwapLE32(siz);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
196 assert(siz > 0);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
197 BAIL_IF_MACRO(SDL_RWseek(rw, siz, SEEK_SET) != siz, NULL, 0);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
198 } /* while */
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
199
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
200 return(0); /* shouldn't hit this, but just in case... */
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
201 } /* find_chunk */
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
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
204 static int WAV_open(Sound_Sample *sample, const char *ext)
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
205 {
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
206 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
207 SDL_RWops *rw = internal->rw;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
208 fmt_t f;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
209 data_t d;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
210 wav_t *w;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
211
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
212 BAIL_IF_MACRO(SDL_ReadLE32(rw) != riffID, "WAV: Not a RIFF file.", 0);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
213 SDL_ReadLE32(rw); /* throw the length away; we get this info later. */
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
214 BAIL_IF_MACRO(SDL_ReadLE32(rw) != waveID, "WAV: Not a WAVE file.", 0);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
215 BAIL_IF_MACRO(!find_chunk(rw, fmtID), "WAV: No format chunk.", 0);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
216 BAIL_IF_MACRO(!read_fmt_chunk(rw, &f), "WAV: Can't read format chunk.", 0);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
217
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
218 /* !!! FIXME: This will have to change for compression types... */
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
219 BAIL_IF_MACRO(f.wFormatTag != FMT_NORMAL, "WAV: Unsupported encoding.", 0);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
220
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
221 sample->actual.channels = (Uint8) f.wChannels;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
222 sample->actual.rate = f.dwSamplesPerSec;
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 if (f.wBitsPerSample <= 8)
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
225 sample->actual.format = AUDIO_U8;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
226 else if (f.wBitsPerSample <= 16)
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
227 sample->actual.format = AUDIO_S16LSB;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
228 else
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
229 BAIL_MACRO("WAV: Unsupported sample size.", 0);
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 BAIL_IF_MACRO(!find_chunk(rw, dataID), "WAV: No data chunk.", 0);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
232 BAIL_IF_MACRO(!read_data_chunk(rw, &d), "WAV: Can't read data chunk.", 0);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
233
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
234 w = (wav_t *) malloc(sizeof(wav_t));
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
235 BAIL_IF_MACRO(w == NULL, ERR_OUT_OF_MEMORY, 0);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
236 w->bytesLeft = d.chunkSize;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
237 internal->decoder_private = (void *) w;
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 sample->flags = SOUND_SAMPLEFLAG_NONE;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
240
62
b13fafb976be Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents: 47
diff changeset
241 SNDDBG(("WAV: Accepting data stream.\n"));
17
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
242 return(1); /* we'll handle this data. */
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
243 } /* WAV_open */
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
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
246 static void WAV_close(Sound_Sample *sample)
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
247 {
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
248 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
249 free(internal->decoder_private);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
250 } /* WAV_close */
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
251
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
252
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
253 static Uint32 WAV_read(Sound_Sample *sample)
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
254 {
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
255 Uint32 retval;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
256 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
257 wav_t *w = (wav_t *) internal->decoder_private;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
258 Uint32 max = (internal->buffer_size < (Uint32) w->bytesLeft) ?
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
259 internal->buffer_size : (Uint32) w->bytesLeft;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
260
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
261 assert(max > 0);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
262
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
263 /*
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
264 * 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
265 * directly into the internal buffer...
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
266 */
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
267 retval = SDL_RWread(internal->rw, internal->buffer, 1, max);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
268
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
269 w->bytesLeft -= retval;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
270
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
271 /* Make sure the read went smoothly... */
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
272 if ((retval == 0) || (w->bytesLeft == 0))
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
273 sample->flags |= SOUND_SAMPLEFLAG_EOF;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
274
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
275 else if (retval == -1)
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
276 sample->flags |= SOUND_SAMPLEFLAG_ERROR;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
277
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
278 /* (next call this EAGAIN may turn into an EOF or error.) */
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
279 else if (retval < internal->buffer_size)
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
280 sample->flags |= SOUND_SAMPLEFLAG_EAGAIN;
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
281
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
282 return(retval);
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
283 } /* WAV_read */
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
284
64
40006625142a Changes in preparation of autoconf support.
Ryan C. Gordon <icculus@icculus.org>
parents: 62
diff changeset
285 #endif /* SOUND_SUPPORTS_WAV */
17
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
286
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
287 /* end of wav.c ... */
102cf61c0816 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
288