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