openalbridge/loaders.c
author koda
Sun, 11 Oct 2009 16:19:32 +0000
changeset 2416 3278cf087339
parent 2415 35d09cbf819a
child 2418 538a777f90c4
permissions -rw-r--r--
frotten piece from last patch
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
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
     3
 * Copyright (c) 2009 Vittorio Giovara <vittorio.giovara@gmail.com>
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
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    21
#ifdef __CPLUSPLUS
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    22
extern "C" {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    23
#endif 
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    24
    
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    25
    int load_wavpcm (const char *filename, ALenum *format, char ** data, ALsizei *bitsize, ALsizei *freq) {
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    26
        WAV_header_t WAVHeader;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    27
        FILE *wavfile;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    28
        int32_t t;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    29
        uint32_t n = 0;
2415
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    30
        uint8_t sub0, sub1, sub2, sub3;
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    31
        
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    32
        wavfile = Fopen(filename, "rb");
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    33
        
2415
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    34
        fread(&WAVHeader.ChunkID, sizeof(uint32_t), 1, wavfile);                /*RIFF*/
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    35
        fread(&WAVHeader.ChunkSize, sizeof(uint32_t), 1, wavfile);
2415
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    36
        fread(&WAVHeader.Format, sizeof(uint32_t), 1, wavfile);                 /*WAVE*/
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    37
        
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    38
#ifdef DEBUG
2415
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    39
        fprintf(stderr, "ChunkID: %X\n", ENDIAN_BIG_32(WAVHeader.ChunkID));
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    40
        fprintf(stderr, "ChunkSize: %d\n", ENDIAN_LITTLE_32(WAVHeader.ChunkSize));
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    41
        fprintf(stderr, "Format: %X\n", ENDIAN_BIG_32(WAVHeader.Format));
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    42
#endif
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    43
        
2415
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    44
        fread(&WAVHeader.Subchunk1ID, sizeof(uint32_t), 1, wavfile);            /*fmt */
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    45
        fread(&WAVHeader.Subchunk1Size, sizeof(uint32_t), 1, wavfile);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    46
        fread(&WAVHeader.AudioFormat, sizeof(uint16_t), 1, wavfile);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    47
        fread(&WAVHeader.NumChannels, sizeof(uint16_t), 1, wavfile);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    48
        fread(&WAVHeader.SampleRate, sizeof(uint32_t), 1, wavfile);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    49
        fread(&WAVHeader.ByteRate, sizeof(uint32_t), 1, wavfile);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    50
        fread(&WAVHeader.BlockAlign, sizeof(uint16_t), 1, wavfile);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    51
        fread(&WAVHeader.BitsPerSample, sizeof(uint16_t), 1, wavfile);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    52
        
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    53
#ifdef DEBUG
2415
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    54
        fprintf(stderr, "Subchunk1ID: %X\n", ENDIAN_BIG_32(WAVHeader.Subchunk1ID));
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    55
        fprintf(stderr, "Subchunk1Size: %d\n", ENDIAN_LITTLE_32(WAVHeader.Subchunk1Size));
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    56
        fprintf(stderr, "AudioFormat: %d\n", ENDIAN_LITTLE_16(WAVHeader.AudioFormat));
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    57
        fprintf(stderr, "NumChannels: %d\n", ENDIAN_LITTLE_16(WAVHeader.NumChannels));
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    58
        fprintf(stderr, "SampleRate: %d\n", ENDIAN_LITTLE_32(WAVHeader.SampleRate));
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    59
        fprintf(stderr, "ByteRate: %d\n", ENDIAN_LITTLE_32(WAVHeader.ByteRate));
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    60
        fprintf(stderr, "BlockAlign: %d\n", ENDIAN_LITTLE_16(WAVHeader.BlockAlign));
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    61
        fprintf(stderr, "BitsPerSample: %d\n", ENDIAN_LITTLE_16(WAVHeader.BitsPerSample));
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    62
#endif
2415
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    63
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    64
        /*remove useless header chunks by looking for the WAV_HEADER_SUBCHUNK2ID integer */
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    65
        do {
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    66
                t = fread(&sub0, sizeof(uint8_t), 1, wavfile);
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    67
                if(sub0 == 0x64) {
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    68
                        t = fread(&sub1, sizeof(uint8_t), 1, wavfile);
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    69
                        if(sub1 == 0x61) {
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    70
                                t = fread(&sub2, sizeof(uint8_t), 1, wavfile);
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    71
                                if(sub2 == 0x74) {
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    72
                                        t = fread(&sub3, sizeof(uint8_t), 1, wavfile);
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    73
                                        if(sub3 == 0x61) {
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    74
                                                WAVHeader.Subchunk2ID = WAV_HEADER_SUBCHUNK2ID;
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    75
                                                break;                                                
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    76
                                        } 
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    77
                                }       
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    78
                        }
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    79
                }
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    80
                
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    81
                if (t <= 0) { 
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    82
                        /*eof*/
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    83
                        fprintf(stderr, "ERROR 'load_wavpcm()': wrong WAV header\n");
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    84
                        return AL_FALSE;
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    85
                }
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    86
        } while (1);
2415
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    87
            
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    88
        fread(&WAVHeader.Subchunk2Size, sizeof(uint32_t), 1, wavfile);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    89
        
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    90
#ifdef DEBUG
2415
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    91
        fprintf(stderr, "Subchunk2ID: %X\n", ENDIAN_LITTLE_32(WAVHeader.Subchunk2ID));
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    92
        fprintf(stderr, "Subchunk2Size: %d\n", ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size));
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    93
#endif
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    94
        
2415
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    95
        *data = (char*) Malloc (sizeof(char) * ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size));
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    96
        
2415
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    97
        /*read the actual sound data*/
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    98
        do {
2415
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
    99
                n += fread(&((*data)[n]), sizeof(uint8_t), 4, wavfile);
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
   100
        } while (n < ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size));
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   101
        
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   102
        fclose(wavfile);	
2415
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
   103
            
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   104
#ifdef DEBUG
2415
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
   105
        fprintf(stderr, "WAV data loaded\n");
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   106
#endif
2415
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
   107
            
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
   108
        /*set parameters for OpenAL*/
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   109
        /*Valid formats are AL_FORMAT_MONO8, AL_FORMAT_MONO16, AL_FORMAT_STEREO8, and AL_FORMAT_STEREO16*/
2415
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
   110
        if (ENDIAN_LITTLE_16(WAVHeader.NumChannels) == 1) {
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
   111
            if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 8)
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   112
                *format = AL_FORMAT_MONO8;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   113
            else {
2415
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
   114
                if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 16)
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   115
                    *format = AL_FORMAT_MONO16;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   116
                else {
2415
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
   117
                    fprintf(stderr, "ERROR 'load_wavpcm()': wrong WAV header - bitsample value\n");
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   118
                    return AL_FALSE;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   119
                }
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   120
            } 
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   121
        } else {
2415
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
   122
            if (ENDIAN_LITTLE_16(WAVHeader.NumChannels) == 2) {
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
   123
                if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 8)
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   124
                    *format = AL_FORMAT_STEREO8;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   125
                else {
2415
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
   126
                    if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 16)
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   127
                        *format = AL_FORMAT_STEREO16;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   128
                    else {
2415
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
   129
                        fprintf(stderr, "ERROR 'load_wavpcm()': wrong WAV header - bitsample value\n");
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   130
                        return AL_FALSE;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   131
                    }				
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   132
                }
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   133
            } else {
2415
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
   134
                fprintf(stderr, "ERROR 'load_wavpcm()': wrong WAV header - format value\n");
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   135
                return AL_FALSE;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   136
            }
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   137
        }
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   138
        
2415
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
   139
        *bitsize = ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size);
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
   140
        *freq    = ENDIAN_LITTLE_32(WAVHeader.SampleRate);
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   141
        return AL_TRUE;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   142
    }
2257
7eb31efcfb9b updates licence and fix a memory leak (which was consuming iphone memory)
koda
parents: 2220
diff changeset
   143
    
2265
eae64600fb69 fix a bug where a fclose() was called after an ov_clear()
koda
parents: 2260
diff changeset
   144
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   145
    int load_oggvorbis (const char *filename, ALenum *format, char **data, ALsizei *bitsize, ALsizei *freq) {
2416
3278cf087339 frotten piece from last patch
koda
parents: 2415
diff changeset
   146
            /*implementation inspired from http://www.devmaster.net/forums/showthread.php?t=1153 */
3278cf087339 frotten piece from last patch
koda
parents: 2415
diff changeset
   147
            OggVorbis_File  oggStream; /*stream handle*/
3278cf087339 frotten piece from last patch
koda
parents: 2415
diff changeset
   148
            vorbis_info	*vorbisInfo;   /*some formatting data*/
3278cf087339 frotten piece from last patch
koda
parents: 2415
diff changeset
   149
            int64_t pcm_length;	       /*length of the decoded data*/
3278cf087339 frotten piece from last patch
koda
parents: 2415
diff changeset
   150
            int section, result, size, endianness;
2210
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   151
#ifdef DEBUG
2416
3278cf087339 frotten piece from last patch
koda
parents: 2415
diff changeset
   152
            int i;
3278cf087339 frotten piece from last patch
koda
parents: 2415
diff changeset
   153
            vorbis_comment *vorbisComment; /*other less useful data*/
2210
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   154
#endif
2265
eae64600fb69 fix a bug where a fclose() was called after an ov_clear()
koda
parents: 2260
diff changeset
   155
2266
289dc8e51210 switch to ov_fopen for simplicity
koda
parents: 2265
diff changeset
   156
	result = ov_fopen((char*) filename, &oggStream);
2265
eae64600fb69 fix a bug where a fclose() was called after an ov_clear()
koda
parents: 2260
diff changeset
   157
	if (result < 0) {
2415
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
   158
		fprintf (stderr, "ERROR 'load_oggvorbis()': ov_fopen failed with %X", result);
2266
289dc8e51210 switch to ov_fopen for simplicity
koda
parents: 2265
diff changeset
   159
                ov_clear(&oggStream);
2265
eae64600fb69 fix a bug where a fclose() was called after an ov_clear()
koda
parents: 2260
diff changeset
   160
		return -1;
eae64600fb69 fix a bug where a fclose() was called after an ov_clear()
koda
parents: 2260
diff changeset
   161
	}
eae64600fb69 fix a bug where a fclose() was called after an ov_clear()
koda
parents: 2260
diff changeset
   162
2416
3278cf087339 frotten piece from last patch
koda
parents: 2415
diff changeset
   163
        /*load OGG header and determine the decoded data size*/
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   164
        vorbisInfo = ov_info(&oggStream, -1);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   165
        pcm_length = ov_pcm_total(&oggStream, -1) << vorbisInfo->channels;	
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   166
        
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   167
#ifdef DEBUG
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   168
        vorbisComment = ov_comment(&oggStream, -1);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   169
        fprintf(stderr, "Version: %d\n", vorbisInfo->version);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   170
        fprintf(stderr, "Channels: %d\n", vorbisInfo->channels);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   171
        fprintf(stderr, "Rate (Hz): %ld\n", vorbisInfo->rate);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   172
        fprintf(stderr, "Bitrate Upper: %ld\n", vorbisInfo->bitrate_upper);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   173
        fprintf(stderr, "Bitrate Nominal: %ld\n", vorbisInfo->bitrate_nominal);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   174
        fprintf(stderr, "Bitrate Lower: %ld\n", vorbisInfo->bitrate_lower);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   175
        fprintf(stderr, "Bitrate Windows: %ld\n", vorbisInfo->bitrate_window);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   176
        fprintf(stderr, "Vendor: %s\n", vorbisComment->vendor);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   177
        fprintf(stderr, "PCM data size: %lld\n", pcm_length);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   178
        fprintf(stderr, "# comment: %d\n", vorbisComment->comments);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   179
        for (i = 0; i < vorbisComment->comments; i++)
2415
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
   180
                fprintf(stderr, "\tComment %d: %s\n", i, vorbisComment->user_comments[i]);
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   181
#endif
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   182
        
2416
3278cf087339 frotten piece from last patch
koda
parents: 2415
diff changeset
   183
            /*allocates enough room for the decoded data*/
3278cf087339 frotten piece from last patch
koda
parents: 2415
diff changeset
   184
            *data = (char*) Malloc (sizeof(char) * pcm_length);
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   185
        
2416
3278cf087339 frotten piece from last patch
koda
parents: 2415
diff changeset
   186
            /*there *should* not be ogg at 8 bits*/
3278cf087339 frotten piece from last patch
koda
parents: 2415
diff changeset
   187
            if (vorbisInfo->channels == 1)
3278cf087339 frotten piece from last patch
koda
parents: 2415
diff changeset
   188
                    *format = AL_FORMAT_MONO16;
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   189
            else {
2416
3278cf087339 frotten piece from last patch
koda
parents: 2415
diff changeset
   190
                    if (vorbisInfo->channels == 2)
3278cf087339 frotten piece from last patch
koda
parents: 2415
diff changeset
   191
                            *format = AL_FORMAT_STEREO16;
3278cf087339 frotten piece from last patch
koda
parents: 2415
diff changeset
   192
                    else {
3278cf087339 frotten piece from last patch
koda
parents: 2415
diff changeset
   193
                            fprintf(stderr, "ERROR 'load_oggvorbis()': wrong OGG header - channel value (%d)\n", vorbisInfo->channels);
3278cf087339 frotten piece from last patch
koda
parents: 2415
diff changeset
   194
                            ov_clear(&oggStream);
3278cf087339 frotten piece from last patch
koda
parents: 2415
diff changeset
   195
                            return AL_FALSE;
3278cf087339 frotten piece from last patch
koda
parents: 2415
diff changeset
   196
                    }
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   197
            }
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   198
        
2416
3278cf087339 frotten piece from last patch
koda
parents: 2415
diff changeset
   199
            size = 0;
3278cf087339 frotten piece from last patch
koda
parents: 2415
diff changeset
   200
#ifdef __LITTLE_ENDIAN__
3278cf087339 frotten piece from last patch
koda
parents: 2415
diff changeset
   201
            endianness = 0;
3278cf087339 frotten piece from last patch
koda
parents: 2415
diff changeset
   202
#elif __BIG_ENDIAN__
3278cf087339 frotten piece from last patch
koda
parents: 2415
diff changeset
   203
            endianness = 1;
3278cf087339 frotten piece from last patch
koda
parents: 2415
diff changeset
   204
#endif
2265
eae64600fb69 fix a bug where a fclose() was called after an ov_clear()
koda
parents: 2260
diff changeset
   205
        while (size < pcm_length) {
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   206
            /*ov_read decodes the ogg stream and storse the pcm in data*/
2416
3278cf087339 frotten piece from last patch
koda
parents: 2415
diff changeset
   207
            result = ov_read (&oggStream, *data + size, pcm_length - size, endianness, 2, 1, &section);
2265
eae64600fb69 fix a bug where a fclose() was called after an ov_clear()
koda
parents: 2260
diff changeset
   208
            if (result > 0) {
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   209
                size += result;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   210
            } else {
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   211
                if (result == 0)
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   212
                    break;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   213
                else { 
2415
35d09cbf819a OpenALBridge updates
koda
parents: 2266
diff changeset
   214
                    fprintf(stderr, "ERROR 'load_oggvorbis()': end of file from OGG stream\n");
2265
eae64600fb69 fix a bug where a fclose() was called after an ov_clear()
koda
parents: 2260
diff changeset
   215
                    ov_clear(&oggStream);
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   216
                    return AL_FALSE;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   217
                }
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   218
            }
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   219
        }
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   220
        
2265
eae64600fb69 fix a bug where a fclose() was called after an ov_clear()
koda
parents: 2260
diff changeset
   221
        /*set the last fields*/
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   222
        *bitsize = size;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   223
        *freq    = vorbisInfo->rate;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   224
        
2265
eae64600fb69 fix a bug where a fclose() was called after an ov_clear()
koda
parents: 2260
diff changeset
   225
	/*cleaning time*/
eae64600fb69 fix a bug where a fclose() was called after an ov_clear()
koda
parents: 2260
diff changeset
   226
        ov_clear(&oggStream);
eae64600fb69 fix a bug where a fclose() was called after an ov_clear()
koda
parents: 2260
diff changeset
   227
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   228
        return AL_TRUE;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   229
    }
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   230
    
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   231
#ifdef __CPLUSPLUS
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   232
}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   233
#endif