annotate SDL_sound.c @ 46:6e13fcc178da

No longer grabs non-MP3 streams. Added init() and quit() methods.
author Ryan C. Gordon <icculus@icculus.org>
date Sat, 22 Sep 2001 14:41:10 +0000
parents f27bcbcaeab1
children c4b8c39a9798
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
37
f27bcbcaeab1 Added AIFF entry, and (for now) some multiple-streams-in-one-rwops support.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
55 #if (defined SOUND_SUPPORTS_AIFF)
f27bcbcaeab1 Added AIFF entry, and (for now) some multiple-streams-in-one-rwops support.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
56 extern const Sound_DecoderFunctions __Sound_DecoderFunctions_AIFF;
f27bcbcaeab1 Added AIFF entry, and (for now) some multiple-streams-in-one-rwops support.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
57 #endif
f27bcbcaeab1 Added AIFF entry, and (for now) some multiple-streams-in-one-rwops support.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
58
26
ddc3614c9042 Bugfix (thanks Tsuyoshi Iguchi!), and Ogg Vorbis decoder entry.
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
59 #if (defined SOUND_SUPPORTS_OGG)
ddc3614c9042 Bugfix (thanks Tsuyoshi Iguchi!), and Ogg Vorbis decoder entry.
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
60 extern const Sound_DecoderFunctions __Sound_DecoderFunctions_OGG;
ddc3614c9042 Bugfix (thanks Tsuyoshi Iguchi!), and Ogg Vorbis decoder entry.
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
61 #endif
ddc3614c9042 Bugfix (thanks Tsuyoshi Iguchi!), and Ogg Vorbis decoder entry.
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
62
4
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
63 #if (defined SOUND_SUPPORTS_VOC)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
64 extern const Sound_DecoderFunctions __Sound_DecoderFunctions_VOC;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
65 #endif
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
66
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
67 #if (defined SOUND_SUPPORTS_RAW)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
68 extern const Sound_DecoderFunctions __Sound_DecoderFunctions_RAW;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
69 #endif
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
70
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
71 static const Sound_DecoderFunctions *decoderFuncs[] =
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
72 {
10
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
73 #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
74 &__Sound_DecoderFunctions_MP3,
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
75 #endif
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
76
20
d4ac6ce1360e Added WAV entries.
Ryan C. Gordon <icculus@icculus.org>
parents: 10
diff changeset
77 #if (defined SOUND_SUPPORTS_WAV)
d4ac6ce1360e Added WAV entries.
Ryan C. Gordon <icculus@icculus.org>
parents: 10
diff changeset
78 &__Sound_DecoderFunctions_WAV,
d4ac6ce1360e Added WAV entries.
Ryan C. Gordon <icculus@icculus.org>
parents: 10
diff changeset
79 #endif
d4ac6ce1360e Added WAV entries.
Ryan C. Gordon <icculus@icculus.org>
parents: 10
diff changeset
80
37
f27bcbcaeab1 Added AIFF entry, and (for now) some multiple-streams-in-one-rwops support.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
81 #if (defined SOUND_SUPPORTS_AIFF)
f27bcbcaeab1 Added AIFF entry, and (for now) some multiple-streams-in-one-rwops support.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
82 &__Sound_DecoderFunctions_AIFF,
f27bcbcaeab1 Added AIFF entry, and (for now) some multiple-streams-in-one-rwops support.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
83 #endif
f27bcbcaeab1 Added AIFF entry, and (for now) some multiple-streams-in-one-rwops support.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
84
26
ddc3614c9042 Bugfix (thanks Tsuyoshi Iguchi!), and Ogg Vorbis decoder entry.
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
85 #if (defined SOUND_SUPPORTS_OGG)
ddc3614c9042 Bugfix (thanks Tsuyoshi Iguchi!), and Ogg Vorbis decoder entry.
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
86 &__Sound_DecoderFunctions_OGG,
ddc3614c9042 Bugfix (thanks Tsuyoshi Iguchi!), and Ogg Vorbis decoder entry.
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
87 #endif
ddc3614c9042 Bugfix (thanks Tsuyoshi Iguchi!), and Ogg Vorbis decoder entry.
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
88
4
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
89 #if (defined SOUND_SUPPORTS_VOC)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
90 &__Sound_DecoderFunctions_VOC,
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
91 #endif
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 #if (defined SOUND_SUPPORTS_RAW)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
94 &__Sound_DecoderFunctions_RAW,
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
95 #endif
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 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
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
100
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
101
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
102 /* General SDL_sound state ... */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
103
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
104 static int initialized = 0;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
105 static Sound_Sample *samplesList = NULL; /* this is a linked list. */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
106 static const Sound_DecoderInfo **available_decoders = NULL;
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
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
109 /* functions ... */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
110
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
111 void Sound_GetLinkedVersion(Sound_Version *ver)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
112 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
113 if (ver != NULL)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
114 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
115 ver->major = SOUND_VER_MAJOR;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
116 ver->minor = SOUND_VER_MINOR;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
117 ver->patch = SOUND_VER_PATCH;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
118 } /* if */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
119 } /* Sound_GetLinkedVersion */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
120
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 int Sound_Init(void)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
123 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
124 size_t i;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
125 BAIL_IF_MACRO(initialized, ERR_IS_INITIALIZED, 0);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
126 samplesList = NULL;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
127
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
128 SDL_Init(SDL_INIT_AUDIO);
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 for (i = 0; decoderFuncs[i] != NULL; i++)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
131 ; /* do nothing. */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
132
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
133 available_decoders = (const Sound_DecoderInfo **)
26
ddc3614c9042 Bugfix (thanks Tsuyoshi Iguchi!), and Ogg Vorbis decoder entry.
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
134 malloc((i + 1) * sizeof (Sound_DecoderInfo *));
4
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
135 BAIL_IF_MACRO(available_decoders == NULL, ERR_OUT_OF_MEMORY, 0);
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 for (i = 0; decoderFuncs[i] != NULL; i++)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
138 available_decoders[i] = &decoderFuncs[i]->info;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
139
26
ddc3614c9042 Bugfix (thanks Tsuyoshi Iguchi!), and Ogg Vorbis decoder entry.
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
140 available_decoders[i] = NULL;
ddc3614c9042 Bugfix (thanks Tsuyoshi Iguchi!), and Ogg Vorbis decoder entry.
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
141
4
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
142 initialized = 1;
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_Init */
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 int Sound_Quit(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 BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
150
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
151 while (((volatile Sound_Sample *) samplesList) != NULL)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
152 Sound_FreeSample(samplesList);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
153
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
154 if (available_decoders != NULL)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
155 free(available_decoders);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
156 available_decoders = NULL;
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 initialized = 0;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
159
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
160 return(1);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
161 } /* Sound_Quit */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
162
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 const Sound_DecoderInfo **Sound_AvailableDecoders(void)
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 return(available_decoders); /* READ. ONLY. */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
167 } /* Sound_AvailableDecoders */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
168
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 const char *Sound_GetError(void)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
171 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
172 return(SDL_GetError());
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
173 } /* Sound_GetError */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
174
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
175
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
176 void Sound_ClearError(void)
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 SDL_ClearError();
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
179 } /* Sound_ClearError */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
180
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
181
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
182 /*
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
183 * This is declared in the internal header.
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
184 */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
185 void Sound_SetError(const char *err)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
186 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
187 if (err != NULL)
10
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
188 {
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
189 _D(("Sound_SetError(\"%s\");\n", err));
4
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
190 SDL_SetError(err);
10
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
191 } /* if */
4
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
192 } /* Sound_SetError */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
193
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
194
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
195 /*
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
196 * -ansi and -pedantic flags prevent use of strcasecmp() on Linux, and
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
197 * 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
198 * platform has "strcasecmp", "stricmp", or
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
199 * "compare_two_damned_strings_case_insensitive", which I hear is in the
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
200 * 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
201 * they like.
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
202 */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
203 int __Sound_strcasecmp(const char *x, const char *y)
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 int ux, uy;
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 do
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
208 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
209 ux = toupper((int) *x);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
210 uy = toupper((int) *y);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
211 if (ux > uy)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
212 return(1);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
213 else if (ux < uy)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
214 return(-1);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
215 x++;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
216 y++;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
217 } while ((ux) && (uy));
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
218
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
219 return(0);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
220 } /* __Sound_strcasecmp */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
221
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 /*
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
224 * 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
225 * 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
226 */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
227 static Sound_Sample *alloc_sample(SDL_RWops *rw, Sound_AudioInfo *desired,
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
228 Uint32 bufferSize)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
229 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
230 Sound_Sample *retval = malloc(sizeof (Sound_Sample));
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
231 Sound_SampleInternal *internal = malloc(sizeof (Sound_SampleInternal));
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
232 if ((retval == NULL) || (internal == NULL))
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
233 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
234 Sound_SetError(ERR_OUT_OF_MEMORY);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
235 if (retval)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
236 free(retval);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
237 if (internal)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
238 free(internal);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
239
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
240 return(NULL);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
241 } /* if */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
242
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
243 memset(retval, '\0', sizeof (Sound_Sample));
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
244 memset(internal, '\0', sizeof (Sound_SampleInternal));
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
245
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
246 assert(bufferSize > 0);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
247 retval->buffer = malloc(bufferSize); /* pure ugly. */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
248 if (!retval->buffer)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
249 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
250 Sound_SetError(ERR_OUT_OF_MEMORY);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
251 free(internal);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
252 free(retval);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
253 return(NULL);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
254 } /* if */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
255 memset(retval->buffer, '\0', bufferSize);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
256 retval->buffer_size = bufferSize;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
257
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
258 if (desired != NULL)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
259 memcpy(&retval->desired, desired, sizeof (Sound_AudioInfo));
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
260
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
261 internal->rw = rw;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
262 retval->opaque = internal;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
263 return(retval);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
264 } /* alloc_sample */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
265
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
266
10
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
267 #if (defined DEBUG_CHATTER)
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
268 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
269 {
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
270 switch(fmt)
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
271 {
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
272 case AUDIO_U8:
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
273 return("U8");
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
274 case AUDIO_S8:
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
275 return("S8");
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
276 case AUDIO_U16LSB:
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
277 return("U16LSB");
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
278 case AUDIO_S16LSB:
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
279 return("S16LSB");
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
280 case AUDIO_U16MSB:
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
281 return("U16MSB");
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
282 case AUDIO_S16MSB:
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
283 return("S16MSB");
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
284 } /* switch */
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
285
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
286 return("Unknown");
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
287 } /* fmt_to_str */
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
288 #endif
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
289
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
290
4
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
291 /*
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
292 * The bulk of the Sound_NewSample() work is done here...
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
293 * Ask the specified decoder to handle the data in (rw), and if
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
294 * so, construct the Sound_Sample. Otherwise, try to wind (rw)'s stream
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
295 * back to where it was, and return false.
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
296 *
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
297 * !!! FIXME: This is big, ugly, nasty, and smelly.
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 static int init_sample(const Sound_DecoderFunctions *funcs,
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
300 Sound_Sample *sample, const char *ext,
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
301 Sound_AudioInfo *_desired)
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 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
37
f27bcbcaeab1 Added AIFF entry, and (for now) some multiple-streams-in-one-rwops support.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
304 Sound_AudioInfo desired;
4
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
305 int pos = SDL_RWtell(internal->rw); /* !!! FIXME: Int? Really? */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
306
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
307 /* fill in the funcs for this decoder... */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
308 sample->decoder = &funcs->info;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
309 internal->funcs = funcs;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
310 if (!funcs->open(sample, ext))
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
311 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
312 SDL_RWseek(internal->rw, pos, SEEK_SET); /* set for next try... */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
313 return(0);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
314 } /* if */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
315
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
316 /* success; we've got a decoder! */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
317
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
318 /* Now we need to set up the conversion buffer... */
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 memcpy(&desired, (_desired != NULL) ? _desired : &sample->actual,
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
321 sizeof (Sound_AudioInfo));
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
322
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
323 if (SDL_BuildAudioCVT(&internal->sdlcvt,
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
324 sample->actual.format,
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
325 sample->actual.channels,
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
326 (int) sample->actual.rate, /* !!! FIXME: Int? Really? */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
327 desired.format,
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
328 desired.channels,
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
329 (int) desired.rate) == -1) /* !!! FIXME: Int? Really? */
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 Sound_SetError(SDL_GetError());
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
332 funcs->close(sample);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
333 SDL_RWseek(internal->rw, pos, SEEK_SET); /* set for next try... */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
334 return(0);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
335 } /* if */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
336
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
337 if (internal->sdlcvt.len_mult > 1)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
338 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
339 void *rc = realloc(sample->buffer,
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
340 sample->buffer_size * internal->sdlcvt.len_mult);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
341 if (rc == 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 funcs->close(sample);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
344 SDL_RWseek(internal->rw, pos, SEEK_SET); /* set for next try... */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
345 return(0);
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
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
348 sample->buffer = rc;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
349 } /* if */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
350
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
351 /* these pointers are all one and the same. */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
352 memcpy(&sample->desired, &desired, sizeof (Sound_AudioInfo));
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
353 internal->sdlcvt.buf = internal->buffer = sample->buffer;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
354 internal->buffer_size = sample->buffer_size / internal->sdlcvt.len_mult;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
355 internal->sdlcvt.len = internal->buffer_size;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
356
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
357 /* Prepend our new Sound_Sample to the samplesList... */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
358 if (samplesList != NULL)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
359 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
360 internal->next = samplesList;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
361 if (samplesList != NULL)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
362 internal->prev = sample;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
363 } /* if */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
364 samplesList = sample;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
365
37
f27bcbcaeab1 Added AIFF entry, and (for now) some multiple-streams-in-one-rwops support.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
366 #if (defined MULTIPLE_STREAMS_PER_RWOPS)
f27bcbcaeab1 Added AIFF entry, and (for now) some multiple-streams-in-one-rwops support.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
367 internal->pos = SDL_RWtell(internal->rw);
f27bcbcaeab1 Added AIFF entry, and (for now) some multiple-streams-in-one-rwops support.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
368 #endif
f27bcbcaeab1 Added AIFF entry, and (for now) some multiple-streams-in-one-rwops support.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
369
10
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
370 _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
371 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
372 sample->desired.rate,
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
373 sample->desired.channels));
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
374
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
375 _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
376 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
377 sample->actual.rate,
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
378 sample->actual.channels));
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
379
cc2c32349380 Some debugging output, and MP3 and VOC entries, added.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
380 _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
381 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
382
4
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
383 return(1);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
384 } /* init_sample */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
385
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 Sound_Sample *Sound_NewSample(SDL_RWops *rw, const char *ext,
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
388 Sound_AudioInfo *desired, Uint32 bSize)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
389 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
390 size_t i;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
391 Sound_Sample *retval;
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 /* sanity checks. */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
394 BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, NULL);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
395 BAIL_IF_MACRO(rw == NULL, ERR_INVALID_ARGUMENT, NULL);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
396
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
397 retval = alloc_sample(rw, desired, bSize);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
398 if (!retval)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
399 return(NULL); /* alloc_sample() sets error message... */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
400
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
401 if (ext != NULL)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
402 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
403 for (i = 0; decoderFuncs[i] != NULL; i++)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
404 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
405 const char *decoderExt = decoderFuncs[i]->info.extension;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
406 if (__Sound_strcasecmp(decoderExt, ext) == 0)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
407 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
408 if (init_sample(decoderFuncs[i], retval, ext, desired))
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
409 return(retval);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
410 } /* if */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
411 } /* for */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
412 } /* if */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
413
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
414 /* no direct extension match? Try everything we've got... */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
415 for (i = 0; decoderFuncs[i] != NULL; i++)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
416 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
417 if (init_sample(decoderFuncs[i], retval, ext, desired))
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
418 return(retval);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
419 } /* for */
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 /* nothing could handle the sound data... */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
422 free(retval->opaque);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
423 if (retval->buffer != NULL)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
424 free(retval->buffer);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
425 free(retval);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
426 SDL_RWclose(rw);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
427 Sound_SetError(ERR_UNSUPPORTED_FORMAT);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
428 return(NULL);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
429 } /* Sound_NewSample */
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 Sound_Sample *Sound_NewSampleFromFile(const char *filename,
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
433 Sound_AudioInfo *desired,
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
434 Uint32 bufferSize)
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 const char *ext;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
437 SDL_RWops *rw;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
438
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
439 BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, NULL);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
440 BAIL_IF_MACRO(filename == NULL, ERR_INVALID_ARGUMENT, NULL);
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 ext = strrchr(filename, '.');
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
443 rw = SDL_RWFromFile(filename, "rb");
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
444 BAIL_IF_MACRO(rw == NULL, SDL_GetError(), NULL);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
445
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
446 if (ext != NULL)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
447 ext++;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
448
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
449 return(Sound_NewSample(rw, ext, desired, bufferSize));
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
450 } /* Sound_NewSampleFromFile */
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
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
453 void Sound_FreeSample(Sound_Sample *sample)
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 *internal;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
456
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
457 if (!initialized)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
458 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
459 Sound_SetError(ERR_NOT_INITIALIZED);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
460 return;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
461 } /* if */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
462
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
463 if (sample == NULL)
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 Sound_SetError(ERR_INVALID_ARGUMENT);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
466 return;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
467 } /* if */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
468
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
469 internal = (Sound_SampleInternal *) sample->opaque;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
470
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
471 internal->funcs->close(sample);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
472
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
473 /* update the samplesList... */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
474 if (internal->prev != NULL)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
475 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
476 Sound_SampleInternal *prevInternal;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
477 prevInternal = (Sound_SampleInternal *) internal->prev->opaque;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
478 prevInternal->next = internal->next;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
479 } /* if */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
480 else
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
481 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
482 assert(samplesList == sample);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
483 samplesList = internal->next;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
484 } /* else */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
485
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
486 if (internal->next != NULL)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
487 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
488 Sound_SampleInternal *nextInternal;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
489 nextInternal = (Sound_SampleInternal *) internal->next->opaque;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
490 nextInternal->prev = internal->prev;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
491 } /* if */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
492
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
493 /* nuke it... */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
494 if (internal->rw != NULL) /* this condition is a "just in case" thing. */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
495 SDL_RWclose(internal->rw);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
496 assert(internal->buffer != NULL);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
497 if (internal->buffer != sample->buffer)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
498 free(internal->buffer);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
499 free(internal);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
500 if (sample->buffer != NULL)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
501 free(sample->buffer);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
502 free(sample);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
503 } /* Sound_FreeSample */
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
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
506 int Sound_SetBufferSize(Sound_Sample *sample, Uint32 newSize)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
507 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
508 void *newBuf = NULL;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
509 Sound_SampleInternal *internal = NULL;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
510
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 internal = ((Sound_SampleInternal *) sample->opaque);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
514 newBuf = realloc(sample->buffer, newSize * internal->sdlcvt.len_mult);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
515 BAIL_IF_MACRO(newBuf == NULL, ERR_OUT_OF_MEMORY, 0);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
516
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
517 internal->sdlcvt.buf = internal->buffer = sample->buffer = newBuf;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
518 sample->buffer_size = newSize;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
519 internal->buffer_size = newSize / internal->sdlcvt.len_mult;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
520 internal->sdlcvt.len = internal->buffer_size;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
521
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
522 return(1);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
523 } /* Sound_SetBufferSize */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
524
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 Uint32 Sound_Decode(Sound_Sample *sample)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
527 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
528 Sound_SampleInternal *internal = NULL;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
529 Uint32 retval = 0;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
530
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
531 /* a boatload of sanity checks... */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
532 BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
533 BAIL_IF_MACRO(sample == NULL, ERR_INVALID_ARGUMENT, 0);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
534 BAIL_IF_MACRO(sample->flags & SOUND_SAMPLEFLAG_ERROR, ERR_PREV_ERROR, 0);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
535 BAIL_IF_MACRO(sample->flags & SOUND_SAMPLEFLAG_EOF, ERR_PREV_EOF, 0);
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 internal = (Sound_SampleInternal *) sample->opaque;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
538
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
539 assert(sample->buffer != NULL);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
540 assert(sample->buffer_size > 0);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
541 assert(internal->buffer != NULL);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
542 assert(internal->buffer_size > 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 /* reset EAGAIN. Decoder can flip it back on if it needs to. */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
545 sample->flags &= !SOUND_SAMPLEFLAG_EAGAIN;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
546
37
f27bcbcaeab1 Added AIFF entry, and (for now) some multiple-streams-in-one-rwops support.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
547 #if (defined MULTIPLE_STREAMS_PER_RWOPS)
f27bcbcaeab1 Added AIFF entry, and (for now) some multiple-streams-in-one-rwops support.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
548 if (SDL_RWseek(internal->rw, internal->pos, SEEK_SET) == -1)
f27bcbcaeab1 Added AIFF entry, and (for now) some multiple-streams-in-one-rwops support.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
549 {
f27bcbcaeab1 Added AIFF entry, and (for now) some multiple-streams-in-one-rwops support.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
550 sample->flags |= SOUND_SAMPLEFLAG_ERROR;
f27bcbcaeab1 Added AIFF entry, and (for now) some multiple-streams-in-one-rwops support.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
551 return(0);
f27bcbcaeab1 Added AIFF entry, and (for now) some multiple-streams-in-one-rwops support.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
552 } /* if */
f27bcbcaeab1 Added AIFF entry, and (for now) some multiple-streams-in-one-rwops support.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
553 #endif
f27bcbcaeab1 Added AIFF entry, and (for now) some multiple-streams-in-one-rwops support.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
554
4
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
555 retval = internal->funcs->read(sample);
37
f27bcbcaeab1 Added AIFF entry, and (for now) some multiple-streams-in-one-rwops support.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
556
f27bcbcaeab1 Added AIFF entry, and (for now) some multiple-streams-in-one-rwops support.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
557 #if (defined MULTIPLE_STREAMS_PER_RWOPS)
f27bcbcaeab1 Added AIFF entry, and (for now) some multiple-streams-in-one-rwops support.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
558 internal->pos = SDL_RWtell(internal->rw);
f27bcbcaeab1 Added AIFF entry, and (for now) some multiple-streams-in-one-rwops support.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
559 #endif
f27bcbcaeab1 Added AIFF entry, and (for now) some multiple-streams-in-one-rwops support.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
560
4
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
561 if (internal->sdlcvt.needed)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
562 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
563 internal->sdlcvt.len = retval;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
564 SDL_ConvertAudio(&internal->sdlcvt);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
565 retval *= internal->sdlcvt.len_mult;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
566 } /* if */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
567
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
568 return(retval);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
569 } /* Sound_Decode */
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
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
572 Uint32 Sound_DecodeAll(Sound_Sample *sample)
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 Sound_SampleInternal *internal = NULL;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
575 void *buf = NULL;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
576 Uint32 newBufSize = 0;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
577
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
578 BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
579
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
580 internal = (Sound_SampleInternal *) sample->opaque;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
581
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
582 while ( ((sample->flags & SOUND_SAMPLEFLAG_EOF) == 0) &&
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
583 ((sample->flags & SOUND_SAMPLEFLAG_ERROR) == 0) )
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
584 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
585 void *ptr = realloc(buf, newBufSize + sample->buffer_size);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
586 if (ptr == NULL)
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
587 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
588 sample->flags |= SOUND_SAMPLEFLAG_ERROR;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
589 Sound_SetError(ERR_OUT_OF_MEMORY);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
590 } /* if */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
591 else
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
592 {
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
593 Uint32 br = Sound_Decode(sample);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
594 memcpy( ((char *) buf) + newBufSize, sample->buffer, br );
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
595 newBufSize += br;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
596 } /* else */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
597 } /* while */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
598
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
599 free(sample->buffer);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
600 sample->buffer = internal->buffer = internal->sdlcvt.buf = buf;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
601 sample->buffer_size = newBufSize;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
602 internal->buffer_size = newBufSize / internal->sdlcvt.len_mult;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
603 internal->sdlcvt.len_mult = internal->buffer_size;
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
604
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
605 return(newBufSize);
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
606 } /* Sound_DecodeAll */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
607
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
608
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
609 /* end of SDL_sound.c ... */
341cea3e13c6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
610