openalbridge/loaders.c
author koda
Sun, 12 Jul 2009 19:12:08 +0000
changeset 2260 31756e21c436
parent 2259 ca42efdce3ce
child 2265 eae64600fb69
permissions -rw-r--r--
other indentation, binding and miscellaneous fixes to openalbridge
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;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    30
        
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    31
        wavfile = Fopen(filename, "rb");
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    32
        
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    33
        fread(&WAVHeader.ChunkID, sizeof(uint32_t), 1, wavfile);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    34
        fread(&WAVHeader.ChunkSize, sizeof(uint32_t), 1, wavfile);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    35
        fread(&WAVHeader.Format, sizeof(uint32_t), 1, wavfile);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    36
        
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    37
#ifdef DEBUG
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    38
        fprintf(stderr, "ChunkID: %X\n", invert_endianness(WAVHeader.ChunkID));
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    39
        fprintf(stderr, "ChunkSize: %d\n", WAVHeader.ChunkSize);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    40
        fprintf(stderr, "Format: %X\n", invert_endianness(WAVHeader.Format));
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    41
#endif
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    42
        
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    43
        fread(&WAVHeader.Subchunk1ID, sizeof(uint32_t), 1, wavfile);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    44
        fread(&WAVHeader.Subchunk1Size, sizeof(uint32_t), 1, wavfile);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    45
        fread(&WAVHeader.AudioFormat, sizeof(uint16_t), 1, wavfile);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    46
        fread(&WAVHeader.NumChannels, sizeof(uint16_t), 1, wavfile);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    47
        fread(&WAVHeader.SampleRate, sizeof(uint32_t), 1, wavfile);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    48
        fread(&WAVHeader.ByteRate, sizeof(uint32_t), 1, wavfile);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    49
        fread(&WAVHeader.BlockAlign, sizeof(uint16_t), 1, wavfile);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    50
        fread(&WAVHeader.BitsPerSample, sizeof(uint16_t), 1, wavfile);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    51
        
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    52
#ifdef DEBUG
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    53
        fprintf(stderr, "Subchunk1ID: %X\n", invert_endianness(WAVHeader.Subchunk1ID));
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    54
        fprintf(stderr, "Subchunk1Size: %d\n", WAVHeader.Subchunk1Size);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    55
        fprintf(stderr, "AudioFormat: %d\n", WAVHeader.AudioFormat);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    56
        fprintf(stderr, "NumChannels: %d\n", WAVHeader.NumChannels);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    57
        fprintf(stderr, "SampleRate: %d\n", WAVHeader.SampleRate);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    58
        fprintf(stderr, "ByteRate: %d\n", WAVHeader.ByteRate);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    59
        fprintf(stderr, "BlockAlign: %d\n", WAVHeader.BlockAlign);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    60
        fprintf(stderr, "BitsPerSample: %d\n", WAVHeader.BitsPerSample);
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    61
#endif
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    62
        
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    63
        do { /*remove useless header chunks (plenty room for improvements)*/
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    64
            t = fread(&WAVHeader.Subchunk2ID, sizeof(uint32_t), 1, wavfile);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    65
            if (invert_endianness(WAVHeader.Subchunk2ID) == 0x64617461)
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    66
                break;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    67
            if (t <= 0) { /*eof*/
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    68
                fprintf(stderr, "ERROR: wrong WAV header\n");
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    69
                return AL_FALSE;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    70
            }
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    71
        } while (1);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    72
        fread(&WAVHeader.Subchunk2Size, sizeof(uint32_t), 1, wavfile);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    73
        
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    74
#ifdef DEBUG
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    75
        fprintf(stderr, "Subchunk2ID: %X\n", invert_endianness(WAVHeader.Subchunk2ID));
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    76
        fprintf(stderr, "Subchunk2Size: %d\n", WAVHeader.Subchunk2Size);
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    77
#endif
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    78
        
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    79
        *data = (char*) Malloc (sizeof(char) * WAVHeader.Subchunk2Size);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    80
        
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    81
        /*this could be improved*/
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    82
        do {
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    83
            n += fread(&((*data)[n]), sizeof(uint8_t), 1, wavfile);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    84
        } while (n < WAVHeader.Subchunk2Size);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    85
        
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    86
        fclose(wavfile);	
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    87
        
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    88
#ifdef DEBUG
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    89
        fprintf(stderr, "Last two bytes of data: %X%X\n", (*data)[n-2], (*data)[n-1]);
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    90
#endif
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    91
        
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    92
        /*remaining parameters*/
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    93
        /*Valid formats are AL_FORMAT_MONO8, AL_FORMAT_MONO16, AL_FORMAT_STEREO8, and AL_FORMAT_STEREO16*/
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    94
        if (WAVHeader.NumChannels == 1) {
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    95
            if (WAVHeader.BitsPerSample == 8)
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    96
                *format = AL_FORMAT_MONO8;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    97
            else {
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    98
                if (WAVHeader.BitsPerSample == 16)
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    99
                    *format = AL_FORMAT_MONO16;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   100
                else {
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   101
                    fprintf(stderr, "ERROR: wrong WAV header - bitsample value\n");
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   102
                    return AL_FALSE;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   103
                }
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   104
            } 
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   105
        } else {
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   106
            if (WAVHeader.NumChannels == 2) {
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   107
                if (WAVHeader.BitsPerSample == 8)
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   108
                    *format = AL_FORMAT_STEREO8;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   109
                else {
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   110
                    if (WAVHeader.BitsPerSample == 16)
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   111
                        *format = AL_FORMAT_STEREO16;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   112
                    else {
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   113
                        fprintf(stderr, "ERROR: wrong WAV header - bitsample value\n");
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   114
                        return AL_FALSE;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   115
                    }				
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   116
                }
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   117
            } else {
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   118
                fprintf(stderr, "ERROR: wrong WAV header - format value\n");
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   119
                return AL_FALSE;
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
        }
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   122
        
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   123
        *bitsize = WAVHeader.Subchunk2Size;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   124
        *freq = WAVHeader.SampleRate;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   125
        return AL_TRUE;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   126
    }
2257
7eb31efcfb9b updates licence and fix a memory leak (which was consuming iphone memory)
koda
parents: 2220
diff changeset
   127
    
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   128
    
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   129
    int load_oggvorbis (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
   130
        /*implementation inspired from http://www.devmaster.net/forums/showthread.php?t=1153 */
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   131
        FILE		*oggFile;		/*ogg handle*/
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   132
        OggVorbis_File  oggStream;		/*stream handle*/
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   133
        vorbis_info	*vorbisInfo;	/*some formatting data*/
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   134
        int64_t		pcm_length;		/*length of the decoded data*/
2260
31756e21c436 other indentation, binding and miscellaneous fixes to openalbridge
koda
parents: 2259
diff changeset
   135
        int             section, result, size = 0;
2210
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   136
#ifdef DEBUG
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   137
        int i;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   138
        vorbis_comment	*vorbisComment;	/*other less useful data*/
2210
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   139
#endif
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   140
        
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   141
        oggFile = Fopen(filename, "rb");
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   142
        result = ov_open(oggFile, &oggStream, NULL, 0);	/*TODO: check returning value of result*/
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   143
        
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   144
        vorbisInfo = ov_info(&oggStream, -1);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   145
        pcm_length = ov_pcm_total(&oggStream, -1) << vorbisInfo->channels;	
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   146
        
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   147
#ifdef DEBUG
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   148
        vorbisComment = ov_comment(&oggStream, -1);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   149
        fprintf(stderr, "Version: %d\n", vorbisInfo->version);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   150
        fprintf(stderr, "Channels: %d\n", vorbisInfo->channels);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   151
        fprintf(stderr, "Rate (Hz): %ld\n", vorbisInfo->rate);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   152
        fprintf(stderr, "Bitrate Upper: %ld\n", vorbisInfo->bitrate_upper);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   153
        fprintf(stderr, "Bitrate Nominal: %ld\n", vorbisInfo->bitrate_nominal);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   154
        fprintf(stderr, "Bitrate Lower: %ld\n", vorbisInfo->bitrate_lower);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   155
        fprintf(stderr, "Bitrate Windows: %ld\n", vorbisInfo->bitrate_window);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   156
        fprintf(stderr, "Vendor: %s\n", vorbisComment->vendor);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   157
        fprintf(stderr, "PCM data size: %lld\n", pcm_length);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   158
        fprintf(stderr, "# comment: %d\n", vorbisComment->comments);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   159
        for (i = 0; i < vorbisComment->comments; i++)
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   160
            fprintf(stderr, "\tComment %d: %s\n", i, vorbisComment->user_comments[i]);
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   161
#endif
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   162
        
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   163
        /*allocates enough room for the decoded data*/
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   164
        *data = (char*) Malloc (sizeof(char) * pcm_length);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   165
        
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   166
        /*there *should* not be ogg at 8 bits*/
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   167
        if (vorbisInfo->channels == 1)
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   168
            *format = AL_FORMAT_MONO16;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   169
        else {
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   170
            if (vorbisInfo->channels == 2)
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   171
                *format = AL_FORMAT_STEREO16;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   172
            else {
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   173
                fprintf(stderr, "ERROR: wrong OGG header - channel value (%d)\n", vorbisInfo->channels);
2260
31756e21c436 other indentation, binding and miscellaneous fixes to openalbridge
koda
parents: 2259
diff changeset
   174
                ov_clear (&oggStream);
31756e21c436 other indentation, binding and miscellaneous fixes to openalbridge
koda
parents: 2259
diff changeset
   175
                fclose(oggFile);
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   176
                return AL_FALSE;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   177
            }
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   178
        }
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   179
        
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   180
        while(size < pcm_length) {
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   181
            /*ov_read decodes the ogg stream and storse the pcm in data*/
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   182
            result = ov_read (&oggStream, *data + size, pcm_length - size, 0, 2, 1, &section);
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   183
            if(result > 0) {
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   184
                size += result;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   185
            } else {
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   186
                if (result == 0)
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   187
                    break;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   188
                else { 
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   189
                    fprintf(stderr, "ERROR: end of file from OGG stream\n");
2260
31756e21c436 other indentation, binding and miscellaneous fixes to openalbridge
koda
parents: 2259
diff changeset
   190
                    ov_clear (&oggStream);
31756e21c436 other indentation, binding and miscellaneous fixes to openalbridge
koda
parents: 2259
diff changeset
   191
                    fclose(oggFile);
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   192
                    return AL_FALSE;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   193
                }
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   194
            }
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   195
        }
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   196
        
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   197
        /*records the last fields*/
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   198
        *bitsize = size;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   199
        *freq    = vorbisInfo->rate;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   200
        
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   201
        ov_clear (&oggStream);
2260
31756e21c436 other indentation, binding and miscellaneous fixes to openalbridge
koda
parents: 2259
diff changeset
   202
        fclose (oggFile);
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   203
        return AL_TRUE;
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   204
    }
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   205
    
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   206
#ifdef __CPLUSPLUS
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   207
}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   208
#endif