misc/libopenalbridge/loaders.c
changeset 9373 b769a8e38cbd
parent 9372 915436ff64ab
child 9374 60063430445b
equal deleted inserted replaced
9372:915436ff64ab 9373:b769a8e38cbd
     1 /*
       
     2 * OpenAL Bridge - a simple portable library for OpenAL interface
       
     3 * Copyright (c) 2009 Vittorio Giovara <vittorio.giovara@gmail.com>
       
     4 *
       
     5 * This program is free software; you can redistribute it and/or modify
       
     6 * it under the terms of the GNU Lesser General Public License as published by
       
     7 * the Free Software Foundation; version 2 of the License
       
     8 *
       
     9 * This program is distributed in the hope that it will be useful,
       
    10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12 * GNU Lesser General Public License for more details.
       
    13 *
       
    14 * You should have received a copy of the GNU Lesser General Public License
       
    15 * along with this program; if not, write to the Free Software
       
    16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
       
    17 */
       
    18 
       
    19 #include "loaders.h"
       
    20 #include "wrappers.h"
       
    21 #include "vorbis/vorbisfile.h"
       
    22 #include "openalbridge_t.h"
       
    23 
       
    24 
       
    25 int load_wavpcm (const char *filename, ALenum *format, char **data, ALsizei *bitsize, ALsizei *freq) {
       
    26     WAV_header_t WAVHeader;
       
    27     FILE *wavfile;
       
    28     int32_t t;
       
    29     uint32_t n = 0;
       
    30     uint8_t sub0, sub1, sub2, sub3;
       
    31 
       
    32     wavfile = Fopen(filename, "rb");
       
    33 
       
    34     fread(&WAVHeader.ChunkID, sizeof(uint32_t), 1, wavfile);                /*RIFF*/
       
    35     fread(&WAVHeader.ChunkSize, sizeof(uint32_t), 1, wavfile);
       
    36     fread(&WAVHeader.Format, sizeof(uint32_t), 1, wavfile);                 /*WAVE*/
       
    37 
       
    38 #ifdef DEBUG
       
    39     fprintf(stderr, "ChunkID: %X\n", ENDIAN_BIG_32(WAVHeader.ChunkID));
       
    40     fprintf(stderr, "ChunkSize: %d\n", ENDIAN_LITTLE_32(WAVHeader.ChunkSize));
       
    41     fprintf(stderr, "Format: %X\n", ENDIAN_BIG_32(WAVHeader.Format));
       
    42 #endif
       
    43 
       
    44     fread(&WAVHeader.Subchunk1ID, sizeof(uint32_t), 1, wavfile);            /*fmt */
       
    45     fread(&WAVHeader.Subchunk1Size, sizeof(uint32_t), 1, wavfile);
       
    46     fread(&WAVHeader.AudioFormat, sizeof(uint16_t), 1, wavfile);
       
    47     fread(&WAVHeader.NumChannels, sizeof(uint16_t), 1, wavfile);
       
    48     fread(&WAVHeader.SampleRate, sizeof(uint32_t), 1, wavfile);
       
    49     fread(&WAVHeader.ByteRate, sizeof(uint32_t), 1, wavfile);
       
    50     fread(&WAVHeader.BlockAlign, sizeof(uint16_t), 1, wavfile);
       
    51     fread(&WAVHeader.BitsPerSample, sizeof(uint16_t), 1, wavfile);
       
    52 
       
    53 #ifdef DEBUG
       
    54     fprintf(stderr, "Subchunk1ID: %X\n", ENDIAN_BIG_32(WAVHeader.Subchunk1ID));
       
    55     fprintf(stderr, "Subchunk1Size: %d\n", ENDIAN_LITTLE_32(WAVHeader.Subchunk1Size));
       
    56     fprintf(stderr, "AudioFormat: %d\n", ENDIAN_LITTLE_16(WAVHeader.AudioFormat));
       
    57     fprintf(stderr, "NumChannels: %d\n", ENDIAN_LITTLE_16(WAVHeader.NumChannels));
       
    58     fprintf(stderr, "SampleRate: %d\n", ENDIAN_LITTLE_32(WAVHeader.SampleRate));
       
    59     fprintf(stderr, "ByteRate: %d\n", ENDIAN_LITTLE_32(WAVHeader.ByteRate));
       
    60     fprintf(stderr, "BlockAlign: %d\n", ENDIAN_LITTLE_16(WAVHeader.BlockAlign));
       
    61     fprintf(stderr, "BitsPerSample: %d\n", ENDIAN_LITTLE_16(WAVHeader.BitsPerSample));
       
    62 #endif
       
    63 
       
    64     /*remove useless header chunks by looking for the WAV_HEADER_SUBCHUNK2ID integer */
       
    65     do {
       
    66         t = fread(&sub0, sizeof(uint8_t), 1, wavfile);
       
    67         if(sub0 == 0x64) {
       
    68             t = fread(&sub1, sizeof(uint8_t), 1, wavfile);
       
    69             if(sub1 == 0x61) {
       
    70                 t = fread(&sub2, sizeof(uint8_t), 1, wavfile);
       
    71                 if(sub2 == 0x74) {
       
    72                     t = fread(&sub3, sizeof(uint8_t), 1, wavfile);
       
    73                     if(sub3 == 0x61) {
       
    74                         WAVHeader.Subchunk2ID = WAV_HEADER_SUBCHUNK2ID;
       
    75                         break;
       
    76                     }
       
    77                 }
       
    78             }
       
    79         }
       
    80 
       
    81         if (t <= 0) {
       
    82             // eof
       
    83             fprintf(stderr,"(Bridge Error) - wrong WAV header\n");
       
    84             return -1;
       
    85         }
       
    86     } while (1);
       
    87 
       
    88     fread(&WAVHeader.Subchunk2Size, sizeof(uint32_t), 1, wavfile);
       
    89 
       
    90 #ifdef DEBUG
       
    91     fprintf(stderr, "Subchunk2ID: %X\n", ENDIAN_LITTLE_32(WAVHeader.Subchunk2ID));
       
    92     fprintf(stderr, "Subchunk2Size: %d\n", ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size));
       
    93 #endif
       
    94 
       
    95     *data = (char*) Malloc (sizeof(char) * ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size));
       
    96 
       
    97     /*read the actual sound data*/
       
    98     do {
       
    99         n += fread(&((*data)[n]), sizeof(uint8_t), 4, wavfile);
       
   100     } while (n < ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size));
       
   101 
       
   102     fclose(wavfile);
       
   103 
       
   104 #ifdef DEBUG
       
   105     fprintf(stderr,"(Bridge Info) - WAV data loaded\n");
       
   106 #endif
       
   107 
       
   108     /*set parameters for OpenAL*/
       
   109     /*Valid formats are AL_FORMAT_MONO8, AL_FORMAT_MONO16, AL_FORMAT_STEREO8, and AL_FORMAT_STEREO16*/
       
   110     if (ENDIAN_LITTLE_16(WAVHeader.NumChannels) == 1) {
       
   111         if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 8)
       
   112             *format = AL_FORMAT_MONO8;
       
   113         else {
       
   114             if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 16)
       
   115                 *format = AL_FORMAT_MONO16;
       
   116             else {
       
   117                 fprintf(stderr,"(Bridge Error) - wrong WAV header [bitsample value]\n");
       
   118                 return -2;
       
   119             }
       
   120         }
       
   121     } else {
       
   122         if (ENDIAN_LITTLE_16(WAVHeader.NumChannels) == 2) {
       
   123             if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 8)
       
   124                 *format = AL_FORMAT_STEREO8;
       
   125             else {
       
   126                 if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 16)
       
   127                     *format = AL_FORMAT_STEREO16;
       
   128                 else {
       
   129                     fprintf(stderr,"(Bridge Error) - wrong WAV header [bitsample value]\n");
       
   130                     return -2;
       
   131                 }
       
   132             }
       
   133         } else {
       
   134             fprintf(stderr,"(Bridge Error) - wrong WAV header [format value]\n");
       
   135             return -2;
       
   136         }
       
   137     }
       
   138 
       
   139     *bitsize = ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size);
       
   140     *freq    = ENDIAN_LITTLE_32(WAVHeader.SampleRate);
       
   141     return 0;
       
   142 }
       
   143 
       
   144 
       
   145 int load_oggvorbis (const char *filename, ALenum *format, char **data, ALsizei *bitsize, ALsizei *freq) {
       
   146     /*implementation inspired from http://www.devmaster.net/forums/showthread.php?t=1153 */
       
   147 
       
   148     /*ogg handle*/
       
   149     FILE *oggFile;
       
   150     /*stream handle*/
       
   151     OggVorbis_File oggStream;
       
   152     /*some formatting data*/
       
   153     vorbis_info *vorbisInfo;
       
   154     /*length of the decoded data*/
       
   155     int64_t pcm_length;
       
   156     /*other vars*/
       
   157     int section, result, size, endianness;
       
   158 #ifdef DEBUG
       
   159     int i;
       
   160     /*other less useful data*/
       
   161     vorbis_comment *vorbisComment;
       
   162 #endif
       
   163 
       
   164     oggFile = Fopen(filename, "rb");
       
   165     result = ov_open_callbacks(oggFile, &oggStream, NULL, 0, OV_CALLBACKS_DEFAULT);
       
   166     if (result < 0) {
       
   167         fprintf(stderr,"(Bridge Error) - ov_open_callbacks() failed with %X\n", result);
       
   168         ov_clear(&oggStream);
       
   169         return -1;
       
   170     }
       
   171 
       
   172     /*load OGG header and determine the decoded data size*/
       
   173     vorbisInfo = ov_info(&oggStream, -1);
       
   174     pcm_length = ov_pcm_total(&oggStream, -1) << vorbisInfo->channels;
       
   175 
       
   176 #ifdef DEBUG
       
   177     vorbisComment = ov_comment(&oggStream, -1);
       
   178     fprintf(stderr, "Version: %d\n", vorbisInfo->version);
       
   179     fprintf(stderr, "Channels: %d\n", vorbisInfo->channels);
       
   180     fprintf(stderr, "Rate (Hz): %ld\n", vorbisInfo->rate);
       
   181     fprintf(stderr, "Bitrate Upper: %ld\n", vorbisInfo->bitrate_upper);
       
   182     fprintf(stderr, "Bitrate Nominal: %ld\n", vorbisInfo->bitrate_nominal);
       
   183     fprintf(stderr, "Bitrate Lower: %ld\n", vorbisInfo->bitrate_lower);
       
   184     fprintf(stderr, "Bitrate Windows: %ld\n", vorbisInfo->bitrate_window);
       
   185     fprintf(stderr, "Vendor: %s\n", vorbisComment->vendor);
       
   186     fprintf(stderr, "PCM data size: %lld\n", pcm_length);
       
   187     fprintf(stderr, "# comment: %d\n", vorbisComment->comments);
       
   188     for (i = 0; i < vorbisComment->comments; i++)
       
   189         fprintf(stderr, "\tComment %d: %s\n", i, vorbisComment->user_comments[i]);
       
   190 #endif
       
   191 
       
   192     /*allocates enough room for the decoded data*/
       
   193     *data = (char*) Malloc (sizeof(char) * pcm_length);
       
   194 
       
   195     /*there *should* not be ogg at 8 bits*/
       
   196     if (vorbisInfo->channels == 1)
       
   197         *format = AL_FORMAT_MONO16;
       
   198     else {
       
   199         if (vorbisInfo->channels == 2)
       
   200             *format = AL_FORMAT_STEREO16;
       
   201         else {
       
   202             fprintf(stderr,"(Bridge Error) - wrong OGG header [channel %d]\n", vorbisInfo->channels);
       
   203             ov_clear(&oggStream);
       
   204             return -2;
       
   205         }
       
   206     }
       
   207 
       
   208     size = 0;
       
   209 #ifdef __LITTLE_ENDIAN__
       
   210     endianness = 0;
       
   211 #elif __BIG_ENDIAN__
       
   212     endianness = 1;
       
   213 #endif
       
   214     while (size < pcm_length) {
       
   215         /*ov_read decodes the ogg stream and storse the pcm in data*/
       
   216         result = ov_read (&oggStream, *data + size, pcm_length - size, endianness, 2, 1, &section);
       
   217         if (result > 0) {
       
   218             size += result;
       
   219         } else {
       
   220             if (result == 0)
       
   221                 break;
       
   222             else {
       
   223                 fprintf(stderr,"(Bridge Error) - End of file from OGG stream\n");
       
   224                 ov_clear(&oggStream);
       
   225                 return -3;
       
   226             }
       
   227         }
       
   228     }
       
   229 
       
   230     /*set the last fields*/
       
   231     *bitsize = size;
       
   232     *freq = vorbisInfo->rate;
       
   233 
       
   234     /*cleaning time (ov_clear also closes file handler)*/
       
   235     ov_clear(&oggStream);
       
   236 
       
   237     return 0;
       
   238 }