misc/openalbridge/loaders.c
changeset 3513 f589230fa21b
parent 3364 e5403e2bf02c
equal deleted inserted replaced
3512:6a8b5f313190 3513:f589230fa21b
    20 #include "wrappers.h"
    20 #include "wrappers.h"
    21 #include "vorbis/vorbisfile.h"
    21 #include "vorbis/vorbisfile.h"
    22 #include "openalbridge_t.h"
    22 #include "openalbridge_t.h"
    23 
    23 
    24 
    24 
    25 int load_wavpcm (const char *filename, ALenum *format, char ** data, ALsizei *bitsize, ALsizei *freq) {
    25 int load_wavpcm (const char *filename, ALenum *format, char **data, ALsizei *bitsize, ALsizei *freq) {
    26     WAV_header_t WAVHeader;
    26     WAV_header_t WAVHeader;
    27     FILE *wavfile;
    27     FILE *wavfile;
    28     int32_t t;
    28     int32_t t;
    29     uint32_t n = 0;
    29     uint32_t n = 0;
    30     uint8_t sub0, sub1, sub2, sub3;
    30     uint8_t sub0, sub1, sub2, sub3;
    78             }
    78             }
    79         }
    79         }
    80 
    80 
    81         if (t <= 0) {
    81         if (t <= 0) {
    82             // eof
    82             // eof
    83             fprintf(stderr,"(Bridge Error) - wrong WAV header");
    83             fprintf(stderr,"(Bridge Error) - wrong WAV header\n");
    84             return -1;
    84             return -1;
    85         }
    85         }
    86     } while (1);
    86     } while (1);
    87 
    87 
    88     fread(&WAVHeader.Subchunk2Size, sizeof(uint32_t), 1, wavfile);
    88     fread(&WAVHeader.Subchunk2Size, sizeof(uint32_t), 1, wavfile);
   100     } while (n < ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size));
   100     } while (n < ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size));
   101 
   101 
   102     fclose(wavfile);
   102     fclose(wavfile);
   103 
   103 
   104 #ifdef DEBUG
   104 #ifdef DEBUG
   105     fprintf(stderr,"(Bridge Info) - WAV data loaded");
   105     fprintf(stderr,"(Bridge Info) - WAV data loaded\n");
   106 #endif
   106 #endif
   107 
   107 
   108     /*set parameters for OpenAL*/
   108     /*set parameters for OpenAL*/
   109     /*Valid formats are AL_FORMAT_MONO8, AL_FORMAT_MONO16, AL_FORMAT_STEREO8, and AL_FORMAT_STEREO16*/
   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) {
   110     if (ENDIAN_LITTLE_16(WAVHeader.NumChannels) == 1) {
   112             *format = AL_FORMAT_MONO8;
   112             *format = AL_FORMAT_MONO8;
   113         else {
   113         else {
   114             if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 16)
   114             if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 16)
   115                 *format = AL_FORMAT_MONO16;
   115                 *format = AL_FORMAT_MONO16;
   116             else {
   116             else {
   117                 fprintf(stderr,"(Bridge Error) - wrong WAV header [bitsample value]");
   117                 fprintf(stderr,"(Bridge Error) - wrong WAV header [bitsample value]\n");
   118                 return -2;
   118                 return -2;
   119             }
   119             }
   120         }
   120         }
   121     } else {
   121     } else {
   122         if (ENDIAN_LITTLE_16(WAVHeader.NumChannels) == 2) {
   122         if (ENDIAN_LITTLE_16(WAVHeader.NumChannels) == 2) {
   124                 *format = AL_FORMAT_STEREO8;
   124                 *format = AL_FORMAT_STEREO8;
   125             else {
   125             else {
   126                 if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 16)
   126                 if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 16)
   127                     *format = AL_FORMAT_STEREO16;
   127                     *format = AL_FORMAT_STEREO16;
   128                 else {
   128                 else {
   129                     fprintf(stderr,"(Bridge Error) - wrong WAV header [bitsample value]");
   129                     fprintf(stderr,"(Bridge Error) - wrong WAV header [bitsample value]\n");
   130                     return -2;
   130                     return -2;
   131                 }
   131                 }
   132             }
   132             }
   133         } else {
   133         } else {
   134             fprintf(stderr,"(Bridge Error) - wrong WAV header [format value]");
   134             fprintf(stderr,"(Bridge Error) - wrong WAV header [format value]\n");
   135             return -2;
   135             return -2;
   136         }
   136         }
   137     }
   137     }
   138 
   138 
   139     *bitsize = ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size);
   139     *bitsize = ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size);
   162 #endif
   162 #endif
   163 
   163 
   164     oggFile = Fopen(filename, "rb");
   164     oggFile = Fopen(filename, "rb");
   165     result = ov_open_callbacks(oggFile, &oggStream, NULL, 0, OV_CALLBACKS_DEFAULT);
   165     result = ov_open_callbacks(oggFile, &oggStream, NULL, 0, OV_CALLBACKS_DEFAULT);
   166     if (result < 0) {
   166     if (result < 0) {
   167         fprintf(stderr,"(Bridge Error) - ov_open_callbacks() failed with %X", result);
   167         fprintf(stderr,"(Bridge Error) - ov_open_callbacks() failed with %X\n", result);
   168         ov_clear(&oggStream);
   168         ov_clear(&oggStream);
   169         return -1;
   169         return -1;
   170     }
   170     }
   171 
   171 
   172     /*load OGG header and determine the decoded data size*/
   172     /*load OGG header and determine the decoded data size*/
   197         *format = AL_FORMAT_MONO16;
   197         *format = AL_FORMAT_MONO16;
   198     else {
   198     else {
   199         if (vorbisInfo->channels == 2)
   199         if (vorbisInfo->channels == 2)
   200             *format = AL_FORMAT_STEREO16;
   200             *format = AL_FORMAT_STEREO16;
   201         else {
   201         else {
   202             fprintf(stderr,"(Bridge Error) - wrong OGG header [channel %d]", vorbisInfo->channels);
   202             fprintf(stderr,"(Bridge Error) - wrong OGG header [channel %d]\n", vorbisInfo->channels);
   203             ov_clear(&oggStream);
   203             ov_clear(&oggStream);
   204             return -2;
   204             return -2;
   205         }
   205         }
   206     }
   206     }
   207 
   207 
   218             size += result;
   218             size += result;
   219         } else {
   219         } else {
   220             if (result == 0)
   220             if (result == 0)
   221                 break;
   221                 break;
   222             else {
   222             else {
   223                 fprintf(stderr,"(Bridge Error) - End of file from OGG stream");
   223                 fprintf(stderr,"(Bridge Error) - End of file from OGG stream\n");
   224                 ov_clear(&oggStream);
   224                 ov_clear(&oggStream);
   225                 return -3;
   225                 return -3;
   226             }
   226             }
   227         }
   227         }
   228     }
   228     }