annotate SDL_sound.c @ 20:d4ac6ce1360e

Added WAV entries.
author Ryan C. Gordon <icculus@icculus.org>
date Wed, 19 Sep 2001 08:30:14 +0000
parents cc2c32349380
children ddc3614c9042
rev   line source
4
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
1 /*
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
2 * SDL_sound -- An abstract sound format decoding API.
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
3 * Copyright (C) 2001 Ryan C. Gordon.
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
4 *
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
5 * This library is free software; you can redistribute it and/or
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
7 * License as published by the Free Software Foundation; either
341cea3e13c6 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.
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
9 *
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
10 * This library is distributed in the hope that it will be useful,
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
13 * Lesser General Public License for more details.
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
14 *
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
16 * License along with this library; if not, write to the Free Software
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
18 */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
19
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
20 /**
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
21 * This file implements the core API, which is relatively simple.
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
22 * The real meat of SDL_sound is in the decoders directory.
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
23 *
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
24 * Documentation is in SDL_sound.h ... It's verbose, honest. :)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
25 *
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
26 * Please see the file LICENSE in the source's root directory.
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
27 *
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
28 * This file written by Ryan C. Gordon. (icculus@clutteredmind.org)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
29 */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
30
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
31
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
32 #include <stdio.h>
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
33 #include <stdlib.h>
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
34 #include <string.h>
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
35 #include <assert.h>
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
36 #include <ctype.h>
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
37
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
38 #include "SDL.h"
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
39 #include "SDL_sound.h"
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
40
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
41 #define __SDL_SOUND_INTERNAL__
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
42 #include "SDL_sound_internal.h"
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
43
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
44
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
45 /* The various decoder drivers... */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
46
10
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
47 #if (defined SOUND_SUPPORTS_MP3)
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
48 extern const Sound_DecoderFunctions __Sound_DecoderFunctions_MP3;
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
49 #endif
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
50
20
d4ac6ce1360e Added WAV entries.
Ryan C. Gordon <icculus@icculus.org>
parents: 10
diff changeset
51 #if (defined SOUND_SUPPORTS_WAV)
d4ac6ce1360e Added WAV entries.
Ryan C. Gordon <icculus@icculus.org>
parents: 10
diff changeset
52 extern const Sound_DecoderFunctions __Sound_DecoderFunctions_WAV;
d4ac6ce1360e Added WAV entries.
Ryan C. Gordon <icculus@icculus.org>
parents: 10
diff changeset
53 #endif
d4ac6ce1360e Added WAV entries.
Ryan C. Gordon <icculus@icculus.org>
parents: 10
diff changeset
54
4
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
55 #if (defined SOUND_SUPPORTS_VOC)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
56 extern const Sound_DecoderFunctions __Sound_DecoderFunctions_VOC;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
57 #endif
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
58
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
59 #if (defined SOUND_SUPPORTS_RAW)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
60 extern const Sound_DecoderFunctions __Sound_DecoderFunctions_RAW;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
61 #endif
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
62
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
63 static const Sound_DecoderFunctions *decoderFuncs[] =
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
64 {
10
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
65 #if (defined SOUND_SUPPORTS_MP3)
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
66 &__Sound_DecoderFunctions_MP3,
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
67 #endif
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
68
20
d4ac6ce1360e Added WAV entries.
Ryan C. Gordon <icculus@icculus.org>
parents: 10
diff changeset
69 #if (defined SOUND_SUPPORTS_WAV)
d4ac6ce1360e Added WAV entries.
Ryan C. Gordon <icculus@icculus.org>
parents: 10
diff changeset
70 &__Sound_DecoderFunctions_WAV,
d4ac6ce1360e Added WAV entries.
Ryan C. Gordon <icculus@icculus.org>
parents: 10
diff changeset
71 #endif
d4ac6ce1360e Added WAV entries.
Ryan C. Gordon <icculus@icculus.org>
parents: 10
diff changeset
72
4
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
73 #if (defined SOUND_SUPPORTS_VOC)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
74 &__Sound_DecoderFunctions_VOC,
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
75 #endif
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
76
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
77 #if (defined SOUND_SUPPORTS_RAW)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
78 &__Sound_DecoderFunctions_RAW,
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
79 #endif
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
80
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
81 NULL
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
82 };
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
83
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
84
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
85
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
86 /* General SDL_sound state ... */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
87
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
88 static int initialized = 0;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
89 static Sound_Sample *samplesList = NULL; /* this is a linked list. */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
90 static const Sound_DecoderInfo **available_decoders = NULL;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
91
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
92
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
93 /* functions ... */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
94
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
95 void Sound_GetLinkedVersion(Sound_Version *ver)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
96 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
97 if (ver != NULL)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
98 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
99 ver->major = SOUND_VER_MAJOR;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
100 ver->minor = SOUND_VER_MINOR;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
101 ver->patch = SOUND_VER_PATCH;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
102 } /* if */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
103 } /* Sound_GetLinkedVersion */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
104
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
105
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
106 int Sound_Init(void)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
107 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
108 size_t i;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
109 BAIL_IF_MACRO(initialized, ERR_IS_INITIALIZED, 0);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
110 samplesList = NULL;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
111
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
112 SDL_Init(SDL_INIT_AUDIO);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
113
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
114 for (i = 0; decoderFuncs[i] != NULL; i++)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
115 ; /* do nothing. */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
116
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
117 i++;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
118 available_decoders = (const Sound_DecoderInfo **)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
119 malloc(i * sizeof (Sound_DecoderInfo *));
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
120 BAIL_IF_MACRO(available_decoders == NULL, ERR_OUT_OF_MEMORY, 0);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
121
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
122 for (i = 0; decoderFuncs[i] != NULL; i++)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
123 available_decoders[i] = &decoderFuncs[i]->info;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
124
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
125 initialized = 1;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
126 return(1);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
127 } /* Sound_Init */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
128
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
129
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
130 int Sound_Quit(void)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
131 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
132 BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
133
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
134 while (((volatile Sound_Sample *) samplesList) != NULL)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
135 Sound_FreeSample(samplesList);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
136
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
137 if (available_decoders != NULL)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
138 free(available_decoders);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
139 available_decoders = NULL;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
140
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
141 initialized = 0;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
142
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
143 return(1);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
144 } /* Sound_Quit */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
145
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
146
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
147 const Sound_DecoderInfo **Sound_AvailableDecoders(void)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
148 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
149 return(available_decoders); /* READ. ONLY. */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
150 } /* Sound_AvailableDecoders */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
151
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
152
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
153 const char *Sound_GetError(void)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
154 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
155 return(SDL_GetError());
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
156 } /* Sound_GetError */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
157
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
158
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
159 void Sound_ClearError(void)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
160 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
161 SDL_ClearError();
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
162 } /* Sound_ClearError */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
163
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
164
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
165 /*
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
166 * This is declared in the internal header.
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
167 */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
168 void Sound_SetError(const char *err)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
169 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
170 if (err != NULL)
10
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
171 {
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
172 _D(("Sound_SetError(\"%s\");\n", err));
4
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
173 SDL_SetError(err);
10
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
174 } /* if */
4
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
175 } /* Sound_SetError */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
176
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
177
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
178 /*
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
179 * -ansi and -pedantic flags prevent use of strcasecmp() on Linux, and
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
180 * I honestly don't want to mess around with figuring out if a given
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
181 * platform has "strcasecmp", "stricmp", or
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
182 * "compare_two_damned_strings_case_insensitive", which I hear is in the
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
183 * next release of Carbon. :) This is exported so decoders may use it if
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
184 * they like.
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
185 */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
186 int __Sound_strcasecmp(const char *x, const char *y)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
187 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
188 int ux, uy;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
189
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
190 do
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
191 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
192 ux = toupper((int) *x);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
193 uy = toupper((int) *y);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
194 if (ux > uy)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
195 return(1);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
196 else if (ux < uy)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
197 return(-1);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
198 x++;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
199 y++;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
200 } while ((ux) && (uy));
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
201
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
202 return(0);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
203 } /* __Sound_strcasecmp */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
204
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
205
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
206 /*
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
207 * Allocate a Sound_Sample, and fill in most of its fields. Those that need
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
208 * to be filled in later, by a decoder, will be initialized to zero.
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
209 */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
210 static Sound_Sample *alloc_sample(SDL_RWops *rw, Sound_AudioInfo *desired,
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
211 Uint32 bufferSize)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
212 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
213 Sound_Sample *retval = malloc(sizeof (Sound_Sample));
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
214 Sound_SampleInternal *internal = malloc(sizeof (Sound_SampleInternal));
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
215 if ((retval == NULL) || (internal == NULL))
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
216 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
217 Sound_SetError(ERR_OUT_OF_MEMORY);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
218 if (retval)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
219 free(retval);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
220 if (internal)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
221 free(internal);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
222
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
223 return(NULL);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
224 } /* if */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
225
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
226 memset(retval, '\0', sizeof (Sound_Sample));
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
227 memset(internal, '\0', sizeof (Sound_SampleInternal));
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
228
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
229 assert(bufferSize > 0);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
230 retval->buffer = malloc(bufferSize); /* pure ugly. */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
231 if (!retval->buffer)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
232 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
233 Sound_SetError(ERR_OUT_OF_MEMORY);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
234 free(internal);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
235 free(retval);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
236 return(NULL);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
237 } /* if */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
238 memset(retval->buffer, '\0', bufferSize);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
239 retval->buffer_size = bufferSize;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
240
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
241 if (desired != NULL)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
242 memcpy(&retval->desired, desired, sizeof (Sound_AudioInfo));
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
243
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
244 internal->rw = rw;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
245 retval->opaque = internal;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
246 return(retval);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
247 } /* alloc_sample */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
248
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
249
10
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
250 #if (defined DEBUG_CHATTER)
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
251 static __inline__ const char *fmt_to_str(Uint16 fmt)
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
252 {
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
253 switch(fmt)
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
254 {
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
255 case AUDIO_U8:
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
256 return("U8");
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
257 case AUDIO_S8:
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
258 return("S8");
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
259 case AUDIO_U16LSB:
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
260 return("U16LSB");
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
261 case AUDIO_S16LSB:
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
262 return("S16LSB");
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
263 case AUDIO_U16MSB:
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
264 return("U16MSB");
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
265 case AUDIO_S16MSB:
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
266 return("S16MSB");
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
267 } /* switch */
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
268
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
269 return("Unknown");
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
270 } /* fmt_to_str */
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
271 #endif
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
272
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
273
4
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
274 /*
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
275 * The bulk of the Sound_NewSample() work is done here...
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
276 * Ask the specified decoder to handle the data in (rw), and if
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
277 * so, construct the Sound_Sample. Otherwise, try to wind (rw)'s stream
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
278 * back to where it was, and return false.
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
279 *
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
280 * !!! FIXME: This is big, ugly, nasty, and smelly.
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
281 */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
282 static int init_sample(const Sound_DecoderFunctions *funcs,
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
283 Sound_Sample *sample, const char *ext,
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
284 Sound_AudioInfo *_desired)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
285 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
286 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
287 int pos = SDL_RWtell(internal->rw); /* !!! FIXME: Int? Really? */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
288 Sound_AudioInfo desired;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
289
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
290 /* fill in the funcs for this decoder... */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
291 sample->decoder = &funcs->info;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
292 internal->funcs = funcs;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
293 if (!funcs->open(sample, ext))
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
294 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
295 SDL_RWseek(internal->rw, pos, SEEK_SET); /* set for next try... */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
296 return(0);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
297 } /* if */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
298
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
299 /* success; we've got a decoder! */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
300
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
301 /* Now we need to set up the conversion buffer... */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
302
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
303 memcpy(&desired, (_desired != NULL) ? _desired : &sample->actual,
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
304 sizeof (Sound_AudioInfo));
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
305
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
306 if (SDL_BuildAudioCVT(&internal->sdlcvt,
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
307 sample->actual.format,
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
308 sample->actual.channels,
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
309 (int) sample->actual.rate, /* !!! FIXME: Int? Really? */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
310 desired.format,
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
311 desired.channels,
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
312 (int) desired.rate) == -1) /* !!! FIXME: Int? Really? */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
313 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
314 Sound_SetError(SDL_GetError());
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
315 funcs->close(sample);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
316 SDL_RWseek(internal->rw, pos, SEEK_SET); /* set for next try... */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
317 return(0);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
318 } /* if */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
319
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
320 if (internal->sdlcvt.len_mult > 1)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
321 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
322 void *rc = realloc(sample->buffer,
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
323 sample->buffer_size * internal->sdlcvt.len_mult);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
324 if (rc == NULL)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
325 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
326 funcs->close(sample);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
327 SDL_RWseek(internal->rw, pos, SEEK_SET); /* set for next try... */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
328 return(0);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
329 } /* if */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
330
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
331 sample->buffer = rc;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
332 } /* if */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
333
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
334 /* these pointers are all one and the same. */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
335 memcpy(&sample->desired, &desired, sizeof (Sound_AudioInfo));
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
336 internal->sdlcvt.buf = internal->buffer = sample->buffer;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
337 internal->buffer_size = sample->buffer_size / internal->sdlcvt.len_mult;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
338 internal->sdlcvt.len = internal->buffer_size;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
339
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
340 /* Prepend our new Sound_Sample to the samplesList... */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
341 if (samplesList != NULL)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
342 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
343 internal->next = samplesList;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
344 if (samplesList != NULL)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
345 internal->prev = sample;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
346 } /* if */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
347 samplesList = sample;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
348
10
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
349 _D(("New sample DESIRED format: %s format, %d rate, %d channels.\n",
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
350 fmt_to_str(sample->desired.format),
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
351 sample->desired.rate,
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
352 sample->desired.channels));
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
353
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
354 _D(("New sample ACTUAL format: %s format, %d rate, %d channels.\n",
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
355 fmt_to_str(sample->actual.format),
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
356 sample->actual.rate,
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
357 sample->actual.channels));
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
358
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
359 _D(("On-the-fly conversion: %s.\n",
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
360 internal->sdlcvt.needed ? "ENABLED" : "DISABLED"));
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
361
4
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
362 return(1);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
363 } /* init_sample */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
364
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
365
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
366 Sound_Sample *Sound_NewSample(SDL_RWops *rw, const char *ext,
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
367 Sound_AudioInfo *desired, Uint32 bSize)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
368 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
369 size_t i;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
370 Sound_Sample *retval;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
371
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
372 /* sanity checks. */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
373 BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, NULL);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
374 BAIL_IF_MACRO(rw == NULL, ERR_INVALID_ARGUMENT, NULL);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
375
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
376 retval = alloc_sample(rw, desired, bSize);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
377 if (!retval)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
378 return(NULL); /* alloc_sample() sets error message... */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
379
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
380 if (ext != NULL)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
381 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
382 for (i = 0; decoderFuncs[i] != NULL; i++)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
383 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
384 const char *decoderExt = decoderFuncs[i]->info.extension;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
385 if (__Sound_strcasecmp(decoderExt, ext) == 0)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
386 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
387 if (init_sample(decoderFuncs[i], retval, ext, desired))
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
388 return(retval);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
389 } /* if */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
390 } /* for */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
391 } /* if */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
392
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
393 /* no direct extension match? Try everything we've got... */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
394 for (i = 0; decoderFuncs[i] != NULL; i++)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
395 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
396 if (init_sample(decoderFuncs[i], retval, ext, desired))
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
397 return(retval);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
398 } /* for */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
399
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
400 /* nothing could handle the sound data... */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
401 free(retval->opaque);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
402 if (retval->buffer != NULL)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
403 free(retval->buffer);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
404 free(retval);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
405 SDL_RWclose(rw);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
406 Sound_SetError(ERR_UNSUPPORTED_FORMAT);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
407 return(NULL);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
408 } /* Sound_NewSample */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
409
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
410
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
411 Sound_Sample *Sound_NewSampleFromFile(const char *filename,
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
412 Sound_AudioInfo *desired,
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
413 Uint32 bufferSize)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
414 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
415 const char *ext;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
416 SDL_RWops *rw;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
417
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
418 BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, NULL);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
419 BAIL_IF_MACRO(filename == NULL, ERR_INVALID_ARGUMENT, NULL);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
420
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
421 ext = strrchr(filename, '.');
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
422 rw = SDL_RWFromFile(filename, "rb");
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
423 BAIL_IF_MACRO(rw == NULL, SDL_GetError(), NULL);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
424
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
425 if (ext != NULL)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
426 ext++;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
427
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
428 return(Sound_NewSample(rw, ext, desired, bufferSize));
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
429 } /* Sound_NewSampleFromFile */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
430
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
431
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
432 void Sound_FreeSample(Sound_Sample *sample)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
433 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
434 Sound_SampleInternal *internal;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
435
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
436 if (!initialized)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
437 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
438 Sound_SetError(ERR_NOT_INITIALIZED);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
439 return;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
440 } /* if */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
441
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
442 if (sample == NULL)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
443 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
444 Sound_SetError(ERR_INVALID_ARGUMENT);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
445 return;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
446 } /* if */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
447
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
448 internal = (Sound_SampleInternal *) sample->opaque;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
449
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
450 internal->funcs->close(sample);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
451
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
452 /* update the samplesList... */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
453 if (internal->prev != NULL)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
454 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
455 Sound_SampleInternal *prevInternal;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
456 prevInternal = (Sound_SampleInternal *) internal->prev->opaque;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
457 prevInternal->next = internal->next;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
458 } /* if */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
459 else
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
460 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
461 assert(samplesList == sample);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
462 samplesList = internal->next;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
463 } /* else */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
464
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
465 if (internal->next != NULL)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
466 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
467 Sound_SampleInternal *nextInternal;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
468 nextInternal = (Sound_SampleInternal *) internal->next->opaque;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
469 nextInternal->prev = internal->prev;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
470 } /* if */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
471
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
472 /* nuke it... */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
473 if (internal->rw != NULL) /* this condition is a "just in case" thing. */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
474 SDL_RWclose(internal->rw);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
475 assert(internal->buffer != NULL);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
476 if (internal->buffer != sample->buffer)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
477 free(internal->buffer);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
478 free(internal);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
479 if (sample->buffer != NULL)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
480 free(sample->buffer);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
481 free(sample);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
482 } /* Sound_FreeSample */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
483
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
484
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
485 int Sound_SetBufferSize(Sound_Sample *sample, Uint32 newSize)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
486 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
487 void *newBuf = NULL;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
488 Sound_SampleInternal *internal = NULL;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
489
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
490 BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
491 BAIL_IF_MACRO(sample == NULL, ERR_INVALID_ARGUMENT, 0);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
492 internal = ((Sound_SampleInternal *) sample->opaque);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
493 newBuf = realloc(sample->buffer, newSize * internal->sdlcvt.len_mult);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
494 BAIL_IF_MACRO(newBuf == NULL, ERR_OUT_OF_MEMORY, 0);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
495
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
496 internal->sdlcvt.buf = internal->buffer = sample->buffer = newBuf;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
497 sample->buffer_size = newSize;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
498 internal->buffer_size = newSize / internal->sdlcvt.len_mult;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
499 internal->sdlcvt.len = internal->buffer_size;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
500
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
501 return(1);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
502 } /* Sound_SetBufferSize */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
503
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
504
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
505 Uint32 Sound_Decode(Sound_Sample *sample)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
506 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
507 Sound_SampleInternal *internal = NULL;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
508 Uint32 retval = 0;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
509
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
510 /* a boatload of sanity checks... */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
511 BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
512 BAIL_IF_MACRO(sample == NULL, ERR_INVALID_ARGUMENT, 0);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
513 BAIL_IF_MACRO(sample->flags & SOUND_SAMPLEFLAG_ERROR, ERR_PREV_ERROR, 0);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
514 BAIL_IF_MACRO(sample->flags & SOUND_SAMPLEFLAG_EOF, ERR_PREV_EOF, 0);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
515
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
516 internal = (Sound_SampleInternal *) sample->opaque;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
517
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
518 assert(sample->buffer != NULL);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
519 assert(sample->buffer_size > 0);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
520 assert(internal->buffer != NULL);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
521 assert(internal->buffer_size > 0);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
522
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
523 /* reset EAGAIN. Decoder can flip it back on if it needs to. */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
524 sample->flags &= !SOUND_SAMPLEFLAG_EAGAIN;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
525
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
526 retval = internal->funcs->read(sample);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
527 if (internal->sdlcvt.needed)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
528 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
529 internal->sdlcvt.len = retval;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
530 SDL_ConvertAudio(&internal->sdlcvt);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
531 retval *= internal->sdlcvt.len_mult;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
532 } /* if */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
533
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
534 return(retval);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
535 } /* Sound_Decode */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
536
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
537
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
538 Uint32 Sound_DecodeAll(Sound_Sample *sample)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
539 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
540 Sound_SampleInternal *internal = NULL;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
541 void *buf = NULL;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
542 Uint32 newBufSize = 0;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
543
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
544 BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
545
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
546 internal = (Sound_SampleInternal *) sample->opaque;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
547
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
548 while ( ((sample->flags & SOUND_SAMPLEFLAG_EOF) == 0) &&
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
549 ((sample->flags & SOUND_SAMPLEFLAG_ERROR) == 0) )
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
550 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
551 void *ptr = realloc(buf, newBufSize + sample->buffer_size);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
552 if (ptr == NULL)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
553 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
554 sample->flags |= SOUND_SAMPLEFLAG_ERROR;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
555 Sound_SetError(ERR_OUT_OF_MEMORY);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
556 } /* if */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
557 else
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
558 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
559 Uint32 br = Sound_Decode(sample);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
560 memcpy( ((char *) buf) + newBufSize, sample->buffer, br );
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
561 newBufSize += br;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
562 } /* else */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
563 } /* while */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
564
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
565 free(sample->buffer);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
566 sample->buffer = internal->buffer = internal->sdlcvt.buf = buf;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
567 sample->buffer_size = newBufSize;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
568 internal->buffer_size = newBufSize / internal->sdlcvt.len_mult;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
569 internal->sdlcvt.len_mult = internal->buffer_size;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
570
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
571 return(newBufSize);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
572 } /* Sound_DecodeAll */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
573
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
574
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
575 /* end of SDL_sound.c ... */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
576