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