Skip to content

Latest commit

 

History

History
246 lines (209 loc) · 7.35 KB

load_aiff.c

File metadata and controls

246 lines (209 loc) · 7.35 KB
 
Dec 31, 2011
Dec 31, 2011
2
SDL_mixer: An audio mixer library based on the SDL library
Mar 1, 2018
Mar 1, 2018
3
Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
Dec 31, 2011
Dec 31, 2011
5
6
7
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Dec 31, 2011
Dec 31, 2011
9
10
11
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
Dec 31, 2011
Dec 31, 2011
13
14
15
16
17
18
19
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Dec 31, 2011
Dec 31, 2011
21
22
23
24
This is the source needed to decode an AIFF file into a waveform.
It's pretty straightforward once you get going. The only
externally-callable function is Mix_LoadAIFF_RW(), which is meant to
act as identically to SDL_LoadWAV_RW() as possible.
Dec 31, 2011
Dec 31, 2011
26
27
28
This file by Torbjörn Andersson (torbjorn.andersson@eurotime.se)
8SVX file support added by Marc Le Douarain (mavati@club-internet.fr)
in december 2002.
Dec 17, 2001
Dec 17, 2001
31
#include "SDL_endian.h"
32
33
34
35
36
37
#include "SDL_mixer.h"
#include "load_aiff.h"
/*********************************************/
/* Define values for AIFF (IFF audio) format */
/*********************************************/
May 22, 2013
May 22, 2013
38
#define FORM 0x4d524f46 /* "FORM" */
Feb 21, 2003
Feb 21, 2003
39
May 22, 2013
May 22, 2013
40
41
42
#define AIFF 0x46464941 /* "AIFF" */
#define SSND 0x444e5353 /* "SSND" */
#define COMM 0x4d4d4f43 /* "COMM" */
May 22, 2013
May 22, 2013
44
45
46
#define _8SVX 0x58565338 /* "8SVX" */
#define VHDR 0x52444856 /* "VHDR" */
#define BODY 0x59444F42 /* "BODY" */
Feb 21, 2003
Feb 21, 2003
47
48
49
50
51
52
53
/* This function was taken from libsndfile. I don't pretend to fully
* understand it.
*/
static Uint32 SANE_to_Uint32 (Uint8 *sanebuf)
{
May 22, 2013
May 22, 2013
54
/* Is the frequency outside of what we can represent with Uint32? */
Oct 17, 2017
Oct 17, 2017
55
56
if ((sanebuf[0] & 0x80) || (sanebuf[0] <= 0x3F) || (sanebuf[0] > 0x40)
|| (sanebuf[0] == 0x40 && sanebuf[1] > 0x1C))
May 22, 2013
May 22, 2013
57
return 0;
May 22, 2013
May 22, 2013
59
60
return ((sanebuf[2] << 23) | (sanebuf[3] << 15) | (sanebuf[4] << 7)
| (sanebuf[5] >> 1)) >> (29 - sanebuf[1]);
61
62
63
64
65
}
/* This function is based on SDL_LoadWAV_RW(). */
SDL_AudioSpec *Mix_LoadAIFF_RW (SDL_RWops *src, int freesrc,
May 22, 2013
May 22, 2013
66
SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
May 22, 2013
May 22, 2013
68
69
70
71
72
int was_error;
int found_SSND;
int found_COMM;
int found_VHDR;
int found_BODY;
Jun 1, 2013
Jun 1, 2013
73
Sint64 start = 0;
May 22, 2013
May 22, 2013
74
75
76
Uint32 chunk_type;
Uint32 chunk_length;
Jun 1, 2013
Jun 1, 2013
77
Sint64 next_chunk;
May 22, 2013
May 22, 2013
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* AIFF magic header */
Uint32 FORMchunk;
Uint32 AIFFmagic;
/* SSND chunk */
Uint32 offset;
Uint32 blocksize;
/* COMM format chunk */
Uint16 channels = 0;
Uint32 numsamples = 0;
Uint16 samplesize = 0;
Uint8 sane_freq[10];
Uint32 frequency = 0;
/* Make sure we are passed a valid data source */
was_error = 0;
Oct 17, 2017
Oct 17, 2017
96
if (src == NULL) {
May 22, 2013
May 22, 2013
97
98
99
100
101
102
was_error = 1;
goto done;
}
FORMchunk = SDL_ReadLE32(src);
chunk_length = SDL_ReadBE32(src);
Oct 17, 2017
Oct 17, 2017
103
if (chunk_length == AIFF) { /* The FORMchunk has already been read */
May 22, 2013
May 22, 2013
104
105
106
107
108
109
AIFFmagic = chunk_length;
chunk_length = FORMchunk;
FORMchunk = FORM;
} else {
AIFFmagic = SDL_ReadLE32(src);
}
Oct 17, 2017
Oct 17, 2017
110
if ((FORMchunk != FORM) || ((AIFFmagic != AIFF) && (AIFFmagic != _8SVX))) {
May 22, 2013
May 22, 2013
111
112
113
114
115
116
117
118
119
120
121
122
123
124
SDL_SetError("Unrecognized file type (not AIFF nor 8SVX)");
was_error = 1;
goto done;
}
/* TODO: Better santity-checking. */
found_SSND = 0;
found_COMM = 0;
found_VHDR = 0;
found_BODY = 0;
do {
chunk_type = SDL_ReadLE32(src);
Jun 1, 2013
Jun 1, 2013
125
chunk_length = SDL_ReadBE32(src);
May 22, 2013
May 22, 2013
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
next_chunk = SDL_RWtell(src) + chunk_length;
/* Paranoia to avoid infinite loops */
if (chunk_length == 0)
break;
switch (chunk_type) {
case SSND:
found_SSND = 1;
offset = SDL_ReadBE32(src);
blocksize = SDL_ReadBE32(src);
start = SDL_RWtell(src) + offset;
break;
case COMM:
found_COMM = 1;
channels = SDL_ReadBE16(src);
numsamples = SDL_ReadBE32(src);
samplesize = SDL_ReadBE16(src);
SDL_RWread(src, sane_freq, sizeof(sane_freq), 1);
frequency = SANE_to_Uint32(sane_freq);
if (frequency == 0) {
SDL_SetError("Bad AIFF sample frequency");
was_error = 1;
goto done;
}
break;
case VHDR:
found_VHDR = 1;
SDL_ReadBE32(src);
SDL_ReadBE32(src);
SDL_ReadBE32(src);
frequency = SDL_ReadBE16(src);
channels = 1;
samplesize = 8;
break;
case BODY:
found_BODY = 1;
numsamples = chunk_length;
start = SDL_RWtell(src);
break;
default:
break;
}
/* a 0 pad byte can be stored for any odd-length chunk */
if (chunk_length&1)
next_chunk++;
Oct 17, 2017
Oct 17, 2017
175
176
177
} while ((((AIFFmagic == AIFF) && (!found_SSND || !found_COMM))
|| ((AIFFmagic == _8SVX) && (!found_VHDR || !found_BODY)))
&& SDL_RWseek(src, next_chunk, RW_SEEK_SET) != 1);
May 22, 2013
May 22, 2013
178
Oct 17, 2017
Oct 17, 2017
179
if ((AIFFmagic == AIFF) && !found_SSND) {
May 22, 2013
May 22, 2013
180
181
182
183
184
SDL_SetError("Bad AIFF (no SSND chunk)");
was_error = 1;
goto done;
}
Oct 17, 2017
Oct 17, 2017
185
if ((AIFFmagic == AIFF) && !found_COMM) {
May 22, 2013
May 22, 2013
186
187
188
189
190
SDL_SetError("Bad AIFF (no COMM chunk)");
was_error = 1;
goto done;
}
Oct 17, 2017
Oct 17, 2017
191
if ((AIFFmagic == _8SVX) && !found_VHDR) {
May 22, 2013
May 22, 2013
192
193
194
195
196
SDL_SetError("Bad 8SVX (no VHDR chunk)");
was_error = 1;
goto done;
}
Oct 17, 2017
Oct 17, 2017
197
if ((AIFFmagic == _8SVX) && !found_BODY) {
May 22, 2013
May 22, 2013
198
199
200
201
202
203
SDL_SetError("Bad 8SVX (no BODY chunk)");
was_error = 1;
goto done;
}
/* Decode the audio data format */
Jun 1, 2013
Jun 1, 2013
204
SDL_memset(spec, 0, sizeof(*spec));
May 22, 2013
May 22, 2013
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
spec->freq = frequency;
switch (samplesize) {
case 8:
spec->format = AUDIO_S8;
break;
case 16:
spec->format = AUDIO_S16MSB;
break;
default:
SDL_SetError("Unsupported AIFF samplesize");
was_error = 1;
goto done;
}
spec->channels = (Uint8) channels;
spec->samples = 4096; /* Good default buffer size */
*audio_len = channels * numsamples * (samplesize / 8);
*audio_buf = (Uint8 *)SDL_malloc(*audio_len);
Oct 17, 2017
Oct 17, 2017
223
if (*audio_buf == NULL) {
May 22, 2013
May 22, 2013
224
225
226
227
SDL_SetError("Out of memory");
return(NULL);
}
SDL_RWseek(src, start, RW_SEEK_SET);
Oct 17, 2017
Oct 17, 2017
228
if (SDL_RWread(src, *audio_buf, *audio_len, 1) != 1) {
May 22, 2013
May 22, 2013
229
230
231
232
233
234
SDL_SetError("Unable to read audio data");
return(NULL);
}
/* Don't return a buffer that isn't a multiple of samplesize */
*audio_len &= ~((samplesize / 8) - 1);
235
236
done:
Jun 2, 2013
Jun 2, 2013
237
if (freesrc && src) {
May 22, 2013
May 22, 2013
238
239
SDL_RWclose(src);
}
Jun 2, 2013
Jun 2, 2013
240
if (was_error) {
May 22, 2013
May 22, 2013
241
242
243
spec = NULL;
}
return(spec);
Feb 21, 2003
Feb 21, 2003
245
Oct 17, 2017
Oct 17, 2017
246
/* vi: set ts=4 sw=4 expandtab: */