misc/libopenalbridge/loaders.c
author koda
Wed, 27 Apr 2011 02:10:22 +0200
changeset 5180 a881cd3d3163
parent 3514 59dbd31e9953
permissions -rw-r--r--
small cosmetics
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
3364
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3362
diff changeset
    24
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
    25
int load_wavpcm (const char *filename, ALenum *format, char **data, ALsizei *bitsize, ALsizei *freq) {
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    26
    WAV_header_t WAVHeader;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    27
    FILE *wavfile;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    28
    int32_t t;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    29
    uint32_t n = 0;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    30
    uint8_t sub0, sub1, sub2, sub3;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    31
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    32
    wavfile = Fopen(filename, "rb");
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    33
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    34
    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
    35
    fread(&WAVHeader.ChunkSize, sizeof(uint32_t), 1, wavfile);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    36
    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
    37
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    38
#ifdef DEBUG
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    39
    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
    40
    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
    41
    fprintf(stderr, "Format: %X\n", ENDIAN_BIG_32(WAVHeader.Format));
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    42
#endif
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    43
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    44
    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
    45
    fread(&WAVHeader.Subchunk1Size, sizeof(uint32_t), 1, wavfile);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    46
    fread(&WAVHeader.AudioFormat, sizeof(uint16_t), 1, wavfile);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    47
    fread(&WAVHeader.NumChannels, sizeof(uint16_t), 1, wavfile);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    48
    fread(&WAVHeader.SampleRate, sizeof(uint32_t), 1, wavfile);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    49
    fread(&WAVHeader.ByteRate, sizeof(uint32_t), 1, wavfile);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    50
    fread(&WAVHeader.BlockAlign, sizeof(uint16_t), 1, wavfile);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    51
    fread(&WAVHeader.BitsPerSample, sizeof(uint16_t), 1, wavfile);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    52
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    53
#ifdef DEBUG
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    54
    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
    55
    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
    56
    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
    57
    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
    58
    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
    59
    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
    60
    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
    61
    fprintf(stderr, "BitsPerSample: %d\n", ENDIAN_LITTLE_16(WAVHeader.BitsPerSample));
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    62
#endif
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    63
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    64
    /*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
    65
    do {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    66
        t = fread(&sub0, sizeof(uint8_t), 1, wavfile);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    67
        if(sub0 == 0x64) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    68
            t = fread(&sub1, sizeof(uint8_t), 1, wavfile);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    69
            if(sub1 == 0x61) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    70
                t = fread(&sub2, sizeof(uint8_t), 1, wavfile);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    71
                if(sub2 == 0x74) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    72
                    t = fread(&sub3, sizeof(uint8_t), 1, wavfile);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    73
                    if(sub3 == 0x61) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    74
                        WAVHeader.Subchunk2ID = WAV_HEADER_SUBCHUNK2ID;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    75
                        break;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    76
                    }
3356
3ae3fccb439e add missing controller stubs
koda
parents: 3353
diff changeset
    77
                }
3ae3fccb439e add missing controller stubs
koda
parents: 3353
diff changeset
    78
            }
3362
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
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
    81
        if (t <= 0) {
3364
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3362
diff changeset
    82
            // eof
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
    83
            fprintf(stderr,"(Bridge Error) - wrong WAV header\n");
3364
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3362
diff changeset
    84
            return -1;
3362
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
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   105
    fprintf(stderr,"(Bridge Info) - WAV data loaded\n");
3362
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 {
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   117
                fprintf(stderr,"(Bridge Error) - wrong WAV header [bitsample value]\n");
3364
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3362
diff changeset
   118
                return -2;
3356
3ae3fccb439e add missing controller stubs
koda
parents: 3353
diff changeset
   119
            }
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   120
        }
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   121
    } else {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   122
        if (ENDIAN_LITTLE_16(WAVHeader.NumChannels) == 2) {
3356
3ae3fccb439e add missing controller stubs
koda
parents: 3353
diff changeset
   123
            if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 8)
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   124
                *format = AL_FORMAT_STEREO8;
3356
3ae3fccb439e add missing controller stubs
koda
parents: 3353
diff changeset
   125
            else {
3ae3fccb439e add missing controller stubs
koda
parents: 3353
diff changeset
   126
                if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 16)
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   127
                    *format = AL_FORMAT_STEREO16;
3356
3ae3fccb439e add missing controller stubs
koda
parents: 3353
diff changeset
   128
                else {
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   129
                    fprintf(stderr,"(Bridge Error) - wrong WAV header [bitsample value]\n");
3364
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3362
diff changeset
   130
                    return -2;
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
   131
                }
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   132
            }
3356
3ae3fccb439e add missing controller stubs
koda
parents: 3353
diff changeset
   133
        } else {
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   134
            fprintf(stderr,"(Bridge Error) - wrong WAV header [format value]\n");
3364
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3362
diff changeset
   135
            return -2;
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
   136
        }
3356
3ae3fccb439e add missing controller stubs
koda
parents: 3353
diff changeset
   137
    }
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   138
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   139
    *bitsize = ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   140
    *freq    = ENDIAN_LITTLE_32(WAVHeader.SampleRate);
3364
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3362
diff changeset
   141
    return 0;
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   142
}
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   143
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   144
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   145
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
   146
    /*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
   147
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   148
    /*ogg handle*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   149
    FILE *oggFile;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   150
    /*stream handle*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   151
    OggVorbis_File oggStream;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   152
    /*some formatting data*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   153
    vorbis_info *vorbisInfo;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   154
    /*length of the decoded data*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   155
    int64_t pcm_length;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   156
    /*other vars*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   157
    int section, result, size, endianness;
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
   158
#ifdef DEBUG
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   159
    int i;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   160
    /*other less useful data*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   161
    vorbis_comment *vorbisComment;
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
   162
#endif
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   163
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   164
    oggFile = Fopen(filename, "rb");
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   165
    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
   166
    if (result < 0) {
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   167
        fprintf(stderr,"(Bridge Error) - ov_open_callbacks() failed with %X\n", result);
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   168
        ov_clear(&oggStream);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   169
        return -1;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   170
    }
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   171
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   172
    /*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
   173
    vorbisInfo = ov_info(&oggStream, -1);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   174
    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
   175
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   176
#ifdef DEBUG
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   177
    vorbisComment = ov_comment(&oggStream, -1);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   178
    fprintf(stderr, "Version: %d\n", vorbisInfo->version);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   179
    fprintf(stderr, "Channels: %d\n", vorbisInfo->channels);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   180
    fprintf(stderr, "Rate (Hz): %ld\n", vorbisInfo->rate);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   181
    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
   182
    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
   183
    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
   184
    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
   185
    fprintf(stderr, "Vendor: %s\n", vorbisComment->vendor);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   186
    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
   187
    fprintf(stderr, "# comment: %d\n", vorbisComment->comments);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   188
    for (i = 0; i < vorbisComment->comments; i++)
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   189
        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
   190
#endif
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   191
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   192
    /*allocates enough room for the decoded data*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   193
    *data = (char*) Malloc (sizeof(char) * pcm_length);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   194
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   195
    /*there *should* not be ogg at 8 bits*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   196
    if (vorbisInfo->channels == 1)
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   197
        *format = AL_FORMAT_MONO16;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   198
    else {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   199
        if (vorbisInfo->channels == 2)
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   200
            *format = AL_FORMAT_STEREO16;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   201
        else {
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   202
            fprintf(stderr,"(Bridge Error) - wrong OGG header [channel %d]\n", vorbisInfo->channels);
3356
3ae3fccb439e add missing controller stubs
koda
parents: 3353
diff changeset
   203
            ov_clear(&oggStream);
3364
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3362
diff changeset
   204
            return -2;
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
   205
        }
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   206
    }
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   207
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   208
    size = 0;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   209
#ifdef __LITTLE_ENDIAN__
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   210
    endianness = 0;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   211
#elif __BIG_ENDIAN__
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   212
    endianness = 1;
3356
3ae3fccb439e add missing controller stubs
koda
parents: 3353
diff changeset
   213
#endif
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   214
    while (size < pcm_length) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   215
        /*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
   216
        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
   217
        if (result > 0) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   218
            size += result;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   219
        } else {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   220
            if (result == 0)
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   221
                break;
3356
3ae3fccb439e add missing controller stubs
koda
parents: 3353
diff changeset
   222
            else {
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   223
                fprintf(stderr,"(Bridge Error) - End of file from OGG stream\n");
3356
3ae3fccb439e add missing controller stubs
koda
parents: 3353
diff changeset
   224
                ov_clear(&oggStream);
3364
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3362
diff changeset
   225
                return -3;
3356
3ae3fccb439e add missing controller stubs
koda
parents: 3353
diff changeset
   226
            }
3ae3fccb439e add missing controller stubs
koda
parents: 3353
diff changeset
   227
        }
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   228
    }
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   229
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   230
    /*set the last fields*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   231
    *bitsize = size;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   232
    *freq = vorbisInfo->rate;
3360
717b4e46e855 updates to openalbridge (modified api and simplified some sections)
koda
parents: 3356
diff changeset
   233
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   234
    /*cleaning time (ov_clear also closes file handler)*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   235
    ov_clear(&oggStream);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   236
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3360
diff changeset
   237
    return 0;
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
   238
}