openalbridge/loaders.c
author nemo
Wed, 24 Jun 2009 23:53:02 +0000
changeset 2193 4d5d78aa7ca4
parent 2191 20c62f787a4d
child 2200 8192be6e3aef
permissions -rw-r--r--
rename jetpack to flying saucer
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
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
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
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    12
 * GNU General Public License for more details.
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    13
 *
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
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 <stdio.h>
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    20
#include <stdlib.h>
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    21
#include <stdint.h>
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    22
#include "al.h"
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    23
#include "alc.h"
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    24
#include "loaders.h"
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    25
#include "endianness.h"
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    26
#include "wrappers.h"
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    27
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    28
#ifdef __CPLUSPLUS
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    29
extern "C" {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    30
#endif 
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    31
	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    32
	extern int ov_open(FILE *f,OggVorbis_File *vf,char *initial,long ibytes);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    33
	extern long ov_read(OggVorbis_File *vf,char *buffer,int length,int bigendianp,int word,int sgned,int *bitstream);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    34
	extern ogg_int64_t ov_pcm_total(OggVorbis_File *vf,int i);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    35
	extern long ov_read(OggVorbis_File *vf,char *buffer,int length,int bigendianp,int word,int sgned,int *bitstream);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    36
	extern vorbis_info *ov_info(OggVorbis_File *vf,int link);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    37
	extern vorbis_comment *ov_comment(OggVorbis_File *f, int num);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    38
	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    39
	int load_WavPcm (const char *filename, ALenum *format, uint8_t** data, ALsizei *bitsize, ALsizei *freq) {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    40
		WAV_header_t WAVHeader;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    41
		FILE *wavfile;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    42
		int t, n = 0;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    43
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    44
		wavfile = Fopen(filename, "rb");
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    45
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    46
		fread(&WAVHeader.ChunkID, sizeof(uint32_t), 1, wavfile);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    47
		fread(&WAVHeader.ChunkSize, sizeof(uint32_t), 1, wavfile);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    48
		fread(&WAVHeader.Format, sizeof(uint32_t), 1, wavfile);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    49
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    50
#ifdef DEBUG
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    51
		fprintf(stderr, "ChunkID: %X\n", invert_endianness(WAVHeader.ChunkID));
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    52
		fprintf(stderr, "ChunkSize: %d\n", WAVHeader.ChunkSize);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    53
		fprintf(stderr, "Format: %X\n", invert_endianness(WAVHeader.Format));
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    54
#endif
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    55
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    56
		fread(&WAVHeader.Subchunk1ID, sizeof(uint32_t), 1, wavfile);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    57
		fread(&WAVHeader.Subchunk1Size, sizeof(uint32_t), 1, wavfile);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    58
		fread(&WAVHeader.AudioFormat, sizeof(uint16_t), 1, wavfile);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    59
		fread(&WAVHeader.NumChannels, sizeof(uint16_t), 1, wavfile);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    60
		fread(&WAVHeader.SampleRate, sizeof(uint32_t), 1, wavfile);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    61
		fread(&WAVHeader.ByteRate, sizeof(uint32_t), 1, wavfile);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    62
		fread(&WAVHeader.BlockAlign, sizeof(uint16_t), 1, wavfile);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    63
		fread(&WAVHeader.BitsPerSample, sizeof(uint16_t), 1, wavfile);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    64
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    65
#ifdef DEBUG
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    66
		fprintf(stderr, "Subchunk1ID: %X\n", invert_endianness(WAVHeader.Subchunk1ID));
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    67
		fprintf(stderr, "Subchunk1Size: %d\n", WAVHeader.Subchunk1Size);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    68
		fprintf(stderr, "AudioFormat: %d\n", WAVHeader.AudioFormat);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    69
		fprintf(stderr, "NumChannels: %d\n", WAVHeader.NumChannels);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    70
		fprintf(stderr, "SampleRate: %d\n", WAVHeader.SampleRate);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    71
		fprintf(stderr, "ByteRate: %d\n", WAVHeader.ByteRate);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    72
		fprintf(stderr, "BlockAlign: %d\n", WAVHeader.BlockAlign);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    73
		fprintf(stderr, "BitsPerSample: %d\n", WAVHeader.BitsPerSample);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    74
#endif
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    75
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    76
		do { //remove useless header chunks (plenty room for improvements)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    77
			t = fread(&WAVHeader.Subchunk2ID, sizeof(uint32_t), 1, wavfile);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    78
			if (invert_endianness(WAVHeader.Subchunk2ID) == 0x64617461)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    79
				break;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    80
			if (t <= 0) { //eof found
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    81
				fprintf(stderr, "ERROR: wrong WAV header\n");
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    82
				return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    83
			}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    84
		} while (1);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    85
		fread(&WAVHeader.Subchunk2Size, sizeof(uint32_t), 1, wavfile);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    86
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    87
#ifdef DEBUG
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    88
		fprintf(stderr, "Subchunk2ID: %X\n", invert_endianness(WAVHeader.Subchunk2ID));
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    89
		fprintf(stderr, "Subchunk2Size: %d\n", WAVHeader.Subchunk2Size);
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
		*data = (uint8_t*) malloc (sizeof(uint8_t) * WAVHeader.Subchunk2Size);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    93
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    94
		//this could be improved
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    95
		do {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    96
			n += fread(&((*data)[n]), sizeof(uint8_t), 1, wavfile);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    97
		} while (n < WAVHeader.Subchunk2Size);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    98
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    99
		fclose(wavfile);	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   100
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   101
#ifdef DEBUG
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   102
		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
   103
#endif
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   104
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   105
		/*remaining parameters*/
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   106
		//Valid formats are AL_FORMAT_MONO8, AL_FORMAT_MONO16, AL_FORMAT_STEREO8, and AL_FORMAT_STEREO16. 
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   107
		if (WAVHeader.NumChannels == 1) {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   108
			if (WAVHeader.BitsPerSample == 8)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   109
				*format = AL_FORMAT_MONO8;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   110
			else {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   111
				if (WAVHeader.BitsPerSample == 16)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   112
					*format = AL_FORMAT_MONO16;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   113
				else {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   114
					fprintf(stderr, "ERROR: wrong WAV header - bitsample value\n");
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   115
					return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   116
				}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   117
			} 
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   118
		} else {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   119
			if (WAVHeader.NumChannels == 2) {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   120
				if (WAVHeader.BitsPerSample == 8)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   121
					*format = AL_FORMAT_STEREO8;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   122
				else {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   123
					if (WAVHeader.BitsPerSample == 16)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   124
						*format = AL_FORMAT_STEREO16;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   125
					else {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   126
						fprintf(stderr, "ERROR: wrong WAV header - bitsample value\n");
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   127
						return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   128
					}				
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   129
				}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   130
			} else {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   131
				fprintf(stderr, "ERROR: wrong WAV header - format value\n");
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   132
				return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   133
			}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   134
		}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   135
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   136
		*bitsize = WAVHeader.Subchunk2Size;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   137
		*freq = WAVHeader.SampleRate;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   138
		return AL_TRUE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   139
	}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   140
	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   141
	int load_OggVorbis (const char *filename, ALenum *format, uint8_t**data, ALsizei *bitsize, ALsizei *freq) {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   142
		//implementation inspired from http://www.devmaster.net/forums/showthread.php?t=1153
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   143
		FILE			*oggFile;		// ogg handle
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   144
		OggVorbis_File  oggStream;		// stream handle
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   145
		vorbis_info		*vorbisInfo;	// some formatting data
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   146
		vorbis_comment	*vorbisComment;	// other less useful data
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   147
		int64_t			pcm_length;		// length of the decoded data
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   148
		int size = 0;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   149
		int section, result, i;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   150
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   151
		oggFile = Fopen(filename, "rb");
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   152
		result = ov_open(oggFile, &oggStream, NULL, 0);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   153
		//TODO: check returning value of result
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   154
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   155
		vorbisInfo = ov_info(&oggStream, -1);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   156
		pcm_length = ov_pcm_total(&oggStream,-1) << vorbisInfo->channels;	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   157
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   158
#ifdef DEBUG
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   159
		vorbisComment = ov_comment(&oggStream, -1);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   160
		fprintf(stderr, "Version: %d\n", vorbisInfo->version);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   161
		fprintf(stderr, "Channels: %d\n", vorbisInfo->channels);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   162
		fprintf(stderr, "Rate (Hz): %d\n", vorbisInfo->rate);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   163
		fprintf(stderr, "Bitrate Upper: %d\n", vorbisInfo->bitrate_upper);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   164
		fprintf(stderr, "Bitrate Nominal: %d\n", vorbisInfo->bitrate_nominal);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   165
		fprintf(stderr, "Bitrate Lower: %d\n", vorbisInfo->bitrate_lower);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   166
		fprintf(stderr, "Bitrate Windows: %d\n", vorbisInfo->bitrate_window);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   167
		fprintf(stderr, "Vendor: %s\n", vorbisComment->vendor);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   168
		fprintf(stderr, "PCM data size: %d\n", pcm_length);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   169
		fprintf(stderr, "# comment: %d\n", vorbisComment->comments);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   170
		for (i = 0; i < vorbisComment->comments; i++)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   171
			fprintf(stderr, "\tComment %d: %s\n", i, vorbisComment->user_comments[i]);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   172
#endif
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   173
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   174
		//allocates enough room for the decoded data
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   175
		*data = (uint8_t*) malloc (sizeof(uint8_t) * pcm_length);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   176
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   177
		//there *should* not be ogg at 8 bits
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   178
		if (vorbisInfo->channels == 1)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   179
			*format = AL_FORMAT_MONO16;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   180
		else {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   181
			if (vorbisInfo->channels == 2)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   182
				*format = AL_FORMAT_STEREO16;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   183
			else {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   184
				fprintf(stderr, "ERROR: wrong OGG header - channel value (%d)\n", vorbisInfo->channels);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   185
				return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   186
			}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   187
		}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   188
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   189
		while(size < pcm_length)	{
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   190
			//ov_read decodes the ogg stream and storse the pcm in data 
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   191
			result = ov_read (&oggStream, *data + size, pcm_length - size, 0, 2, 1, &section);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   192
			if(result > 0) {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   193
				size += result;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   194
			} else {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   195
				if (result == 0)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   196
					break;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   197
				else { 
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   198
					fprintf(stderr, "ERROR: end of file from OGG stream\n");
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   199
					return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   200
				}
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
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   204
		//records the last fields
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   205
		*bitsize = size;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   206
		*freq = vorbisInfo->rate;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   207
		return AL_TRUE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   208
	}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   209
	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   210
#ifdef __CPLUSPLUS
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   211
}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   212
#endif