~ubuntu-branches/ubuntu/utopic/sdl-sound1.2/utopic-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/*
 * SDL_sound -- An abstract sound format decoding API.
 * Copyright (C) 2001  Ryan C. Gordon.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * AIFF decoder for SDL_sound
 *
 * [Insert something profound about the AIFF file format here.]
 *
 * This code was ripped from a decoder I had written for SDL_mixer, which was
 * based on SDL_mixer's old AIFF music loader. (This loader was unfortunately
 * completely broken, but it was still useful because all the pieces were
 * still there, so to speak.)
 *
 * When rewriting it for SDL_sound, I changed its structure to be more like
 * the WAV loader Ryan wrote. Had they not both been part of the same project
 * it would have been embarrassing how similar they are.
 *
 * It is not the most feature-complete AIFF loader the world has ever seen.
 * For instance, it only makes a token attempt at implementing the AIFF-C
 * standard; basically the parts of it that I can easily understand and test.
 * It's a start, though.
 *
 * Please see the file COPYING in the source's root directory.
 *
 *  This file was written by Torbjörn Andersson. (d91tan@Update.UU.SE)
 */

#if HAVE_CONFIG_H
#  include <config.h>
#endif

#ifdef SOUND_SUPPORTS_AIFF

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "SDL_sound.h"

#define __SDL_SOUND_INTERNAL__
#include "SDL_sound_internal.h"

static Uint32 SANE_to_Uint32 (Uint8 *sanebuf);


static int AIFF_init(void);
static void AIFF_quit(void);
static int AIFF_open(Sound_Sample *sample, const char *ext);
static void AIFF_close(Sound_Sample *sample);
static Uint32 AIFF_read(Sound_Sample *sample);
static int AIFF_rewind(Sound_Sample *sample);
static int AIFF_seek(Sound_Sample *sample, Uint32 ms);

static const char *extensions_aiff[] = { "AIFF", NULL };
const Sound_DecoderFunctions __Sound_DecoderFunctions_AIFF =
{
    {
        extensions_aiff,
        "Audio Interchange File Format",
        "Torbjörn Andersson <d91tan@Update.UU.SE>",
        "http://www.icculus.org/SDL_sound/"
    },

    AIFF_init,      /*   init() method */
    AIFF_quit,      /*   quit() method */
    AIFF_open,      /*   open() method */
    AIFF_close,     /*  close() method */
    AIFF_read,      /*   read() method */
    AIFF_rewind,    /* rewind() method */
    AIFF_seek       /*   seek() method */
};


/*****************************************************************************
 * aiff_t is what we store in our internal->decoder_private field...         *
 *****************************************************************************/
typedef struct S_AIFF_FMT_T
{
    Uint32 type;

    Uint32 total_bytes;
    Uint32 data_starting_offset;

    void (*free)(struct S_AIFF_FMT_T *fmt);
    Uint32 (*read_sample)(Sound_Sample *sample);
    int (*rewind_sample)(Sound_Sample *sample);
    int (*seek_sample)(Sound_Sample *sample, Uint32 ms);


#if 0
/*
   this is ripped from wav.c as ann example of format-specific data.
   please replace with something more appropriate when the need arises.
*/
    union
    {
        struct
        {
            Uint16 cbSize;
            Uint16 wSamplesPerBlock;
            Uint16 wNumCoef;
            ADPCMCOEFSET *aCoeff;
        } adpcm;

        /* put other format-specific data here... */
    } fmt;
#endif
} fmt_t;


typedef struct
{
    fmt_t fmt;
    Sint32 bytesLeft;
} aiff_t;



    /* Chunk management code... */

#define formID 0x4D524F46  /* "FORM", in ascii. */
#define aiffID 0x46464941  /* "AIFF", in ascii. */
#define aifcID 0x43464941  /* "AIFC", in ascii. */
#define ssndID 0x444E5353  /* "SSND", in ascii. */


/*****************************************************************************
 * The COMM chunk...                                                         *
 *****************************************************************************/

#define commID 0x4D4D4F43  /* "COMM", in ascii. */

/* format/compression types... */
#define noneID 0x454E4F4E  /* "NONE", in ascii. */

typedef struct
{
    Uint32 ckID;
    Uint32 ckDataSize;
    Uint16 numChannels;
    Uint32 numSampleFrames;
    Uint16 sampleSize;
    Uint32 sampleRate;
        /*
         * We don't handle AIFF-C compressed audio yet, but for those
         * interested the allowed compression types are supposed to be
         *
         *   compressionType   compressionName   meaning
         *   ---------------------------------------------------------------
         *   'NONE'            "not compressed"  uncompressed, that is,
         *                                        straight digitized samples
         *   'ACE2'            "ACE 2-to-1"      2-to-1 IIGS ACE (Audio
         *                                        Compression / Expansion)
         *   'ACE8'            "ACE 8-to-3"      8-to-3 IIGS ACE (Audio
         *                                        Compression / Expansion)
         *   'MAC3'            "MACE 3-to-1"     3-to-1 Macintosh Audio
         *                                        Compression / Expansion
         *   'MAC6'            "MACE 6-to-1"     6-to-1 Macintosh Audio
         *                                        Compression / Expansion
         *
         * A pstring is a "Pascal-style string", that is, "one byte followed
         * by test bytes followed when needed by one pad byte. The total
         * number of bytes in a pstring must be even. The pad byte is
         * included when the number of text bytes is even, so the total of
         * text bytes + one count byte + one pad byte will be even. This pad
         * byte is not reflected in the count."
         *
         * As for how these compression algorithms work, your guess is as
         * good as mine.
         */
    Uint32 compressionType;
#if 0
    pstring compressionName;
#endif
} comm_t;


/*
 * Read in a comm_t from disk. This makes this process safe regardless of
 *  the processor's byte order or how the comm_t structure is packed.
 */

static int read_comm_chunk(SDL_RWops *rw, comm_t *comm)
{
    Uint8 sampleRate[10];

    /* skip reading the chunk ID, since it was already read at this point... */
    comm->ckID = commID;

    if (SDL_RWread(rw, &comm->ckDataSize, sizeof (comm->ckDataSize), 1) != 1)
        return(0);
    comm->ckDataSize = SDL_SwapBE32(comm->ckDataSize);

    if (SDL_RWread(rw, &comm->numChannels, sizeof (comm->numChannels), 1) != 1)
        return(0);
    comm->numChannels = SDL_SwapBE16(comm->numChannels);

    if (SDL_RWread(rw, &comm->numSampleFrames,
                   sizeof (comm->numSampleFrames), 1) != 1)
        return(0);
    comm->numSampleFrames = SDL_SwapBE32(comm->numSampleFrames);

    if (SDL_RWread(rw, &comm->sampleSize, sizeof (comm->sampleSize), 1) != 1)
        return(0);
    comm->sampleSize = SDL_SwapBE16(comm->sampleSize);

    if (SDL_RWread(rw, sampleRate, sizeof (sampleRate), 1) != 1)
        return(0);
    comm->sampleRate = SANE_to_Uint32(sampleRate);

    if (comm->ckDataSize > sizeof(comm->numChannels)
                         + sizeof(comm->numSampleFrames)
                         + sizeof(comm->sampleSize)
                         + sizeof(sampleRate))
    {
        if (SDL_RWread(rw, &comm->compressionType,
                       sizeof (comm->compressionType), 1) != 1)
            return(0);
        comm->compressionType = SDL_SwapBE32(comm->compressionType);
    } /* if */
    else
    {
        comm->compressionType = noneID;
    } /* else */

    return(1);
} /* read_comm_chunk */



/*****************************************************************************
 * The SSND chunk...                                                         *
 *****************************************************************************/

typedef struct
{
    Uint32 ckID;
    Uint32 ckDataSize;
    Uint32 offset;
    Uint32 blockSize;
    /*
     * Then, comm->numSampleFrames sample frames. (It's better to get the
     * length from numSampleFrames than from ckDataSize.)
     */
} ssnd_t;


static int read_ssnd_chunk(SDL_RWops *rw, ssnd_t *ssnd)
{
    /* skip reading the chunk ID, since it was already read at this point... */
    ssnd->ckID = ssndID;

    if (SDL_RWread(rw, &ssnd->ckDataSize, sizeof (ssnd->ckDataSize), 1) != 1)
        return(0);
    ssnd->ckDataSize = SDL_SwapBE32(ssnd->ckDataSize);

    if (SDL_RWread(rw, &ssnd->offset, sizeof (ssnd->offset), 1) != 1)
        return(0);
    ssnd->offset = SDL_SwapBE32(ssnd->offset);

    if (SDL_RWread(rw, &ssnd->blockSize, sizeof (ssnd->blockSize), 1) != 1)
        return(0);
    ssnd->blockSize = SDL_SwapBE32(ssnd->blockSize);

    /* Leave the SDL_RWops position indicator at the start of the samples */
    if (SDL_RWseek(rw, (int) ssnd->offset, SEEK_CUR) == -1)
        return(0);

    return(1);
} /* read_ssnd_chunk */



/*****************************************************************************
 * Normal, uncompressed aiff handler...                                      *
 *****************************************************************************/

static Uint32 read_sample_fmt_normal(Sound_Sample *sample)
{
    Uint32 retval;
    Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
    aiff_t *a = (aiff_t *) internal->decoder_private;
    Uint32 max = (internal->buffer_size < (Uint32) a->bytesLeft) ?
                    internal->buffer_size : (Uint32) a->bytesLeft;

    assert(max > 0);

        /*
         * We don't actually do any decoding, so we read the AIFF data
         *  directly into the internal buffer...
         */
    retval = SDL_RWread(internal->rw, internal->buffer, 1, max);

    a->bytesLeft -= retval;

        /* Make sure the read went smoothly... */
    if ((retval == 0) || (a->bytesLeft == 0))
        sample->flags |= SOUND_SAMPLEFLAG_EOF;

    else if (retval == -1)
        sample->flags |= SOUND_SAMPLEFLAG_ERROR;

        /* (next call this EAGAIN may turn into an EOF or error.) */
    else if (retval < internal->buffer_size)
        sample->flags |= SOUND_SAMPLEFLAG_EAGAIN;

    return(retval);
} /* read_sample_fmt_normal */


static int rewind_sample_fmt_normal(Sound_Sample *sample)
{
    /* no-op. */
    return(1);
} /* rewind_sample_fmt_normal */


static int seek_sample_fmt_normal(Sound_Sample *sample, Uint32 ms)
{
    Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
    aiff_t *a = (aiff_t *) internal->decoder_private;
    fmt_t *fmt = &a->fmt;
    int offset = __Sound_convertMsToBytePos(&sample->actual, ms);
    int pos = (int) (fmt->data_starting_offset + offset);
    int rc = SDL_RWseek(internal->rw, pos, SEEK_SET);
    BAIL_IF_MACRO(rc != pos, ERR_IO_ERROR, 0);
    a->bytesLeft = fmt->total_bytes - offset;
    return(1);  /* success. */
} /* seek_sample_fmt_normal */


static void free_fmt_normal(fmt_t *fmt)
{
    /* it's a no-op. */
} /* free_fmt_normal */


static int read_fmt_normal(SDL_RWops *rw, fmt_t *fmt)
{
    /* (don't need to read more from the RWops...) */
    fmt->free = free_fmt_normal;
    fmt->read_sample = read_sample_fmt_normal;
    fmt->rewind_sample = rewind_sample_fmt_normal;
    fmt->seek_sample = seek_sample_fmt_normal;
    return(1);
} /* read_fmt_normal */




/*****************************************************************************
 * Everything else...                                                        *
 *****************************************************************************/

static int AIFF_init(void)
{
    return(1);  /* always succeeds. */
} /* AIFF_init */


static void AIFF_quit(void)
{
    /* it's a no-op. */
} /* AIFF_quit */


/* 
 * Sample rate is encoded as an "80 bit IEEE Standard 754 floating point
 * number (Standard Apple Numeric Environment [SANE] data type Extended)".
 * Whose bright idea was that?
 *
 * This function was adapted from libsndfile, and while I do know a little
 * bit about the IEEE floating point standard I don't pretend to fully
 * understand this.
 */
static Uint32 SANE_to_Uint32 (Uint8 *sanebuf)
{
    /* Is the frequency outside of what we can represent with Uint32? */
    if ( (sanebuf[0] & 0x80)
      || (sanebuf[0] <= 0x3F)
      || (sanebuf[0] > 0x40)
      || (sanebuf[0] == 0x40 && sanebuf[1] > 0x1C) )
        return 0;

    return ((sanebuf[2] << 23) | (sanebuf[3] << 15) | (sanebuf[4] << 7)
        | (sanebuf[5] >> 1)) >> (29 - sanebuf[1]);
} /* SANE_to_Uint32 */


static int find_chunk(SDL_RWops *rw, Uint32 id)
{
    Sint32 siz = 0;
    Uint32 _id = 0;

    while (1)
    {
        BAIL_IF_MACRO(SDL_RWread(rw, &_id, sizeof (_id), 1) != 1, NULL, 0);
        if (SDL_SwapLE32(_id) == id)
            return(1);

        BAIL_IF_MACRO(SDL_RWread(rw, &siz, sizeof (siz), 1) != 1, NULL, 0);
        siz = SDL_SwapBE32(siz);
        assert(siz > 0);
        BAIL_IF_MACRO(SDL_RWseek(rw, siz, SEEK_CUR) == -1, NULL, 0);
    } /* while */

    return(0);  /* shouldn't hit this, but just in case... */
} /* find_chunk */


static int read_fmt(SDL_RWops *rw, comm_t *c, fmt_t *fmt)
{
    fmt->type = c->compressionType;

    /* if it's in this switch statement, we support the format. */
    switch (fmt->type)
    {
        case noneID:
            SNDDBG(("AIFF: Appears to be uncompressed audio.\n"));
            return(read_fmt_normal(rw, fmt));

        /* add other types here. */

        default:
            SNDDBG(("AIFF: Format %lu is unknown.\n",
                    (unsigned int) fmt->type));
            BAIL_MACRO("AIFF: Unsupported format", 0);
    } /* switch */

    assert(0);  /* shouldn't hit this point. */
    return(0);
} /* read_fmt */


static int AIFF_open(Sound_Sample *sample, const char *ext)
{
    Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
    SDL_RWops *rw = internal->rw;
    Uint32 chunk_id;
    int bytes_per_sample;
    long pos;
    comm_t c;
    ssnd_t s;
    aiff_t *a;

    BAIL_IF_MACRO(SDL_ReadLE32(rw) != formID, "AIFF: Not a FORM file.", 0);
        SDL_ReadBE32(rw);  /* throw the length away; we don't need it. */

    chunk_id = SDL_ReadLE32(rw);
    BAIL_IF_MACRO(chunk_id != aiffID && chunk_id != aifcID,
        "AIFF: Not an AIFF or AIFC file.", 0);

    /* Chunks may appear in any order, so we establish base camp here. */
    pos = SDL_RWtell(rw);

    BAIL_IF_MACRO(!find_chunk(rw, commID), "AIFF: No common chunk.", 0);
    BAIL_IF_MACRO(!read_comm_chunk(rw, &c),
                  "AIFF: Can't read common chunk.", 0);

    sample->actual.channels = (Uint8) c.numChannels;
    sample->actual.rate = c.sampleRate;

    if (c.sampleSize <= 8)
    {
        sample->actual.format = AUDIO_S8;
        bytes_per_sample = c.numChannels;
    } /* if */
    else if (c.sampleSize <= 16)
    {
        sample->actual.format = AUDIO_S16MSB;
        bytes_per_sample = 2 * c.numChannels;
    } /* if */
    else
    {
        BAIL_MACRO("AIFF: Unsupported sample size.", 0);
    } /* else */

    BAIL_IF_MACRO(c.sampleRate == 0, "AIFF: Unsupported sample rate.", 0);

    a = (aiff_t *) malloc(sizeof(aiff_t));
    BAIL_IF_MACRO(a == NULL, ERR_OUT_OF_MEMORY, 0);

    if (!read_fmt(rw, &c, &(a->fmt)))
    {
        free(a);
        return(0);
    } /* if */

    SDL_RWseek(rw, pos, SEEK_SET);    /* if the seek fails, let it go... */

    if (!find_chunk(rw, ssndID))
    {
        free(a);
        BAIL_MACRO("AIFF: No sound data chunk.", 0);
    } /* if */

    if (!read_ssnd_chunk(rw, &s))
    {
        free(a);
        BAIL_MACRO("AIFF: Can't read sound data chunk.", 0);
    } /* if */

    a->fmt.total_bytes = a->bytesLeft = bytes_per_sample * c.numSampleFrames;
    a->fmt.data_starting_offset = SDL_RWtell(rw);
    internal->decoder_private = (void *) a;

    sample->flags = SOUND_SAMPLEFLAG_CANSEEK;

    SNDDBG(("AIFF: Accepting data stream.\n"));
    return(1); /* we'll handle this data. */
} /* AIFF_open */


static void AIFF_close(Sound_Sample *sample)
{
    Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
    aiff_t *a = (aiff_t *) internal->decoder_private;
    a->fmt.free(&(a->fmt));
    free(a);
} /* AIFF_close */


static Uint32 AIFF_read(Sound_Sample *sample)
{
    Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
    aiff_t *a = (aiff_t *) internal->decoder_private;
    return(a->fmt.read_sample(sample));
} /* AIFF_read */


static int AIFF_rewind(Sound_Sample *sample)
{
    Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
    aiff_t *a = (aiff_t *) internal->decoder_private;
    fmt_t *fmt = &a->fmt;
    int rc = SDL_RWseek(internal->rw, fmt->data_starting_offset, SEEK_SET);
    BAIL_IF_MACRO(rc != fmt->data_starting_offset, ERR_IO_ERROR, 0);
    a->bytesLeft = fmt->total_bytes;
    return(fmt->rewind_sample(sample));
} /* AIFF_rewind */


static int AIFF_seek(Sound_Sample *sample, Uint32 ms)
{
    Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
    aiff_t *a = (aiff_t *) internal->decoder_private;
    return(a->fmt.seek_sample(sample, ms));
} /* AIFF_seek */

#endif /* SOUND_SUPPORTS_AIFF */

/* end of aiff.c ... */