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