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