misc/openalbridge/loaders.c
author nemo
Wed, 21 Apr 2010 21:38:07 +0000
changeset 3363 bcd6d76db4f7
parent 3362 8d3b4d19ce27
child 3364 e5403e2bf02c
permissions -rw-r--r--
More work on slovak locale from jose1711
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
     1
/*
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
     2
* OpenAL Bridge - a simple portable library for OpenAL interface
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
     3
* Copyright (c) 2009 Vittorio Giovara <vittorio.giovara@gmail.com>
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
     4
*
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
     5
* This program is free software; you can redistribute it and/or modify
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
     6
* it under the terms of the GNU Lesser General Public License as published by
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
     7
* the Free Software Foundation; version 2 of the License
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
     8
*
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
     9
* This program is distributed in the hope that it will be useful,
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    12
* GNU Lesser General Public License for more details.
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    13
*
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    14
* You should have received a copy of the GNU Lesser General Public License
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    15
* along with this program; if not, write to the Free Software
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    17
*/
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    18
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    19
#include "loaders.h"
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    20
#include "wrappers.h"
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    21
#include "vorbis/vorbisfile.h"
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    22
#include "openalbridge_t.h"
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    23
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    24
int load_wavpcm (const char *filename, ALenum *format, char ** data, ALsizei *bitsize, ALsizei *freq) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    25
    WAV_header_t WAVHeader;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    26
    FILE *wavfile;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    27
    int32_t t;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    28
    uint32_t n = 0;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    29
    uint8_t sub0, sub1, sub2, sub3;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    30
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    31
    wavfile = Fopen(filename, "rb");
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    32
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    33
    fread(&WAVHeader.ChunkID, sizeof(uint32_t), 1, wavfile);                /*RIFF*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    34
    fread(&WAVHeader.ChunkSize, sizeof(uint32_t), 1, wavfile);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    35
    fread(&WAVHeader.Format, sizeof(uint32_t), 1, wavfile);                 /*WAVE*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    36
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    37
#ifdef DEBUG
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    38
    fprintf(stderr, "ChunkID: %X\n", ENDIAN_BIG_32(WAVHeader.ChunkID));
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    39
    fprintf(stderr, "ChunkSize: %d\n", ENDIAN_LITTLE_32(WAVHeader.ChunkSize));
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    40
    fprintf(stderr, "Format: %X\n", ENDIAN_BIG_32(WAVHeader.Format));
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    41
#endif
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    42
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    43
    fread(&WAVHeader.Subchunk1ID, sizeof(uint32_t), 1, wavfile);            /*fmt */
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    44
    fread(&WAVHeader.Subchunk1Size, sizeof(uint32_t), 1, wavfile);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    45
    fread(&WAVHeader.AudioFormat, sizeof(uint16_t), 1, wavfile);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    46
    fread(&WAVHeader.NumChannels, sizeof(uint16_t), 1, wavfile);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    47
    fread(&WAVHeader.SampleRate, sizeof(uint32_t), 1, wavfile);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    48
    fread(&WAVHeader.ByteRate, sizeof(uint32_t), 1, wavfile);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    49
    fread(&WAVHeader.BlockAlign, sizeof(uint16_t), 1, wavfile);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    50
    fread(&WAVHeader.BitsPerSample, sizeof(uint16_t), 1, wavfile);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    51
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    52
#ifdef DEBUG
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    53
    fprintf(stderr, "Subchunk1ID: %X\n", ENDIAN_BIG_32(WAVHeader.Subchunk1ID));
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    54
    fprintf(stderr, "Subchunk1Size: %d\n", ENDIAN_LITTLE_32(WAVHeader.Subchunk1Size));
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    55
    fprintf(stderr, "AudioFormat: %d\n", ENDIAN_LITTLE_16(WAVHeader.AudioFormat));
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    56
    fprintf(stderr, "NumChannels: %d\n", ENDIAN_LITTLE_16(WAVHeader.NumChannels));
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    57
    fprintf(stderr, "SampleRate: %d\n", ENDIAN_LITTLE_32(WAVHeader.SampleRate));
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    58
    fprintf(stderr, "ByteRate: %d\n", ENDIAN_LITTLE_32(WAVHeader.ByteRate));
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    59
    fprintf(stderr, "BlockAlign: %d\n", ENDIAN_LITTLE_16(WAVHeader.BlockAlign));
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    60
    fprintf(stderr, "BitsPerSample: %d\n", ENDIAN_LITTLE_16(WAVHeader.BitsPerSample));
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    61
#endif
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    62
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    63
    /*remove useless header chunks by looking for the WAV_HEADER_SUBCHUNK2ID integer */
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    64
    do {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    65
        t = fread(&sub0, sizeof(uint8_t), 1, wavfile);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    66
        if(sub0 == 0x64) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    67
            t = fread(&sub1, sizeof(uint8_t), 1, wavfile);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    68
            if(sub1 == 0x61) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    69
                t = fread(&sub2, sizeof(uint8_t), 1, wavfile);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    70
                if(sub2 == 0x74) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    71
                    t = fread(&sub3, sizeof(uint8_t), 1, wavfile);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    72
                    if(sub3 == 0x61) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    73
                        WAVHeader.Subchunk2ID = WAV_HEADER_SUBCHUNK2ID;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    74
                        break;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    75
                    }
3356
3ae3fccb439e add missing controller stubs
koda
parents: 3353
diff changeset
    76
                }
3ae3fccb439e add missing controller stubs
koda
parents: 3353
diff changeset
    77
            }
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    78
        }
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    79
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    80
        if (t <= 0) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    81
            /*eof*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    82
            errno = EILSEQ;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    83
            err_ret("(%s) ERROR - wrong WAV header", prog);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    84
            return AL_FALSE;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    85
        }
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    86
    } while (1);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    87
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    88
    fread(&WAVHeader.Subchunk2Size, sizeof(uint32_t), 1, wavfile);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    89
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    90
#ifdef DEBUG
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    91
    fprintf(stderr, "Subchunk2ID: %X\n", ENDIAN_LITTLE_32(WAVHeader.Subchunk2ID));
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    92
    fprintf(stderr, "Subchunk2Size: %d\n", ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size));
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    93
#endif
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    94
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    95
    *data = (char*) Malloc (sizeof(char) * ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size));
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    96
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    97
    /*read the actual sound data*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    98
    do {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    99
        n += fread(&((*data)[n]), sizeof(uint8_t), 4, wavfile);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   100
    } while (n < ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size));
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   101
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   102
    fclose(wavfile);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   103
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   104
#ifdef DEBUG
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   105
    err_msg("(%s) INFO - WAV data loaded", prog);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   106
#endif
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   107
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   108
    /*set parameters for OpenAL*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   109
    /*Valid formats are AL_FORMAT_MONO8, AL_FORMAT_MONO16, AL_FORMAT_STEREO8, and AL_FORMAT_STEREO16*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   110
    if (ENDIAN_LITTLE_16(WAVHeader.NumChannels) == 1) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   111
        if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 8)
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   112
            *format = AL_FORMAT_MONO8;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   113
        else {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   114
            if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 16)
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   115
                *format = AL_FORMAT_MONO16;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   116
            else {
3356
3ae3fccb439e add missing controller stubs
koda
parents: 3353
diff changeset
   117
                errno = EILSEQ;
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   118
                err_ret("(%s) ERROR - wrong WAV header [bitsample value]", prog);
3356
3ae3fccb439e add missing controller stubs
koda
parents: 3353
diff changeset
   119
                return AL_FALSE;
3ae3fccb439e add missing controller stubs
koda
parents: 3353
diff changeset
   120
            }
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   121
        }
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   122
    } else {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   123
        if (ENDIAN_LITTLE_16(WAVHeader.NumChannels) == 2) {
3356
3ae3fccb439e add missing controller stubs
koda
parents: 3353
diff changeset
   124
            if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 8)
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   125
                *format = AL_FORMAT_STEREO8;
3356
3ae3fccb439e add missing controller stubs
koda
parents: 3353
diff changeset
   126
            else {
3ae3fccb439e add missing controller stubs
koda
parents: 3353
diff changeset
   127
                if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 16)
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   128
                    *format = AL_FORMAT_STEREO16;
3356
3ae3fccb439e add missing controller stubs
koda
parents: 3353
diff changeset
   129
                else {
3ae3fccb439e add missing controller stubs
koda
parents: 3353
diff changeset
   130
                    errno = EILSEQ;
3ae3fccb439e add missing controller stubs
koda
parents: 3353
diff changeset
   131
                    err_ret("(%s) ERROR - wrong WAV header [bitsample value]", prog);
3ae3fccb439e add missing controller stubs
koda
parents: 3353
diff changeset
   132
                    return AL_FALSE;
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
   133
                }
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   134
            }
3356
3ae3fccb439e add missing controller stubs
koda
parents: 3353
diff changeset
   135
        } else {
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   136
            errno = EILSEQ;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   137
            err_ret("(%s) ERROR - wrong WAV header [format value]", prog);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   138
            return AL_FALSE;
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
   139
        }
3356
3ae3fccb439e add missing controller stubs
koda
parents: 3353
diff changeset
   140
    }
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   141
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   142
    *bitsize = ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   143
    *freq    = ENDIAN_LITTLE_32(WAVHeader.SampleRate);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   144
    return AL_TRUE;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   145
}
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   146
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   147
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   148
int load_oggvorbis (const char *filename, ALenum *format, char **data, ALsizei *bitsize, ALsizei *freq) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   149
    /*implementation inspired from http://www.devmaster.net/forums/showthread.php?t=1153 */
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   150
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   151
    /*ogg handle*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   152
    FILE *oggFile;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   153
    /*stream handle*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   154
    OggVorbis_File oggStream;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   155
    /*some formatting data*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   156
    vorbis_info *vorbisInfo;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   157
    /*length of the decoded data*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   158
    int64_t pcm_length;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   159
    /*other vars*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   160
    int section, result, size, endianness;
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
   161
#ifdef DEBUG
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   162
    int i;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   163
    /*other less useful data*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   164
    vorbis_comment *vorbisComment;
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
   165
#endif
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   166
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   167
    oggFile = Fopen(filename, "rb");
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   168
    result = ov_open_callbacks(oggFile, &oggStream, NULL, 0, OV_CALLBACKS_DEFAULT);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   169
    if (result < 0) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   170
        errno = EINVAL;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   171
        err_ret("(%s) ERROR - ov_fopen() failed with %X", prog, result);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   172
        ov_clear(&oggStream);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   173
        return -1;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   174
    }
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   175
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   176
    /*load OGG header and determine the decoded data size*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   177
    vorbisInfo = ov_info(&oggStream, -1);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   178
    pcm_length = ov_pcm_total(&oggStream, -1) << vorbisInfo->channels;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   179
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   180
#ifdef DEBUG
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   181
    vorbisComment = ov_comment(&oggStream, -1);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   182
    fprintf(stderr, "Version: %d\n", vorbisInfo->version);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   183
    fprintf(stderr, "Channels: %d\n", vorbisInfo->channels);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   184
    fprintf(stderr, "Rate (Hz): %ld\n", vorbisInfo->rate);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   185
    fprintf(stderr, "Bitrate Upper: %ld\n", vorbisInfo->bitrate_upper);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   186
    fprintf(stderr, "Bitrate Nominal: %ld\n", vorbisInfo->bitrate_nominal);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   187
    fprintf(stderr, "Bitrate Lower: %ld\n", vorbisInfo->bitrate_lower);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   188
    fprintf(stderr, "Bitrate Windows: %ld\n", vorbisInfo->bitrate_window);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   189
    fprintf(stderr, "Vendor: %s\n", vorbisComment->vendor);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   190
    fprintf(stderr, "PCM data size: %lld\n", pcm_length);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   191
    fprintf(stderr, "# comment: %d\n", vorbisComment->comments);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   192
    for (i = 0; i < vorbisComment->comments; i++)
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   193
        fprintf(stderr, "\tComment %d: %s\n", i, vorbisComment->user_comments[i]);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   194
#endif
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   195
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   196
    /*allocates enough room for the decoded data*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   197
    *data = (char*) Malloc (sizeof(char) * pcm_length);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   198
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   199
    /*there *should* not be ogg at 8 bits*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   200
    if (vorbisInfo->channels == 1)
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   201
        *format = AL_FORMAT_MONO16;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   202
    else {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   203
        if (vorbisInfo->channels == 2)
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   204
            *format = AL_FORMAT_STEREO16;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   205
        else {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   206
            errno = EILSEQ;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   207
            err_ret("(%s) ERROR - wrong OGG header [channel %d]", prog, vorbisInfo->channels);
3356
3ae3fccb439e add missing controller stubs
koda
parents: 3353
diff changeset
   208
            ov_clear(&oggStream);
3360
717b4e46e855 updates to openalbridge (modified api and simplified some sections)
koda
parents: 3356
diff changeset
   209
            return -1;
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
   210
        }
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   211
    }
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   212
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   213
    size = 0;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   214
#ifdef __LITTLE_ENDIAN__
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   215
    endianness = 0;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   216
#elif __BIG_ENDIAN__
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   217
    endianness = 1;
3356
3ae3fccb439e add missing controller stubs
koda
parents: 3353
diff changeset
   218
#endif
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   219
    while (size < pcm_length) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   220
        /*ov_read decodes the ogg stream and storse the pcm in data*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   221
        result = ov_read (&oggStream, *data + size, pcm_length - size, endianness, 2, 1, &section);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   222
        if (result > 0) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   223
            size += result;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   224
        } else {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   225
            if (result == 0)
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   226
                break;
3356
3ae3fccb439e add missing controller stubs
koda
parents: 3353
diff changeset
   227
            else {
3ae3fccb439e add missing controller stubs
koda
parents: 3353
diff changeset
   228
                errno = EILSEQ;
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   229
                err_ret("(%s) ERROR - End of file from OGG stream", prog);
3356
3ae3fccb439e add missing controller stubs
koda
parents: 3353
diff changeset
   230
                ov_clear(&oggStream);
3360
717b4e46e855 updates to openalbridge (modified api and simplified some sections)
koda
parents: 3356
diff changeset
   231
                return -1;
3356
3ae3fccb439e add missing controller stubs
koda
parents: 3353
diff changeset
   232
            }
3ae3fccb439e add missing controller stubs
koda
parents: 3353
diff changeset
   233
        }
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   234
    }
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   235
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   236
    /*set the last fields*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   237
    *bitsize = size;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   238
    *freq = vorbisInfo->rate;
3360
717b4e46e855 updates to openalbridge (modified api and simplified some sections)
koda
parents: 3356
diff changeset
   239
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   240
    /*cleaning time (ov_clear also closes file handler)*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   241
    ov_clear(&oggStream);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   242
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   243
    return 0;
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
   244
}