#include #include #if 1 #else #include "OpenAL/al.h" #include "OpenAL/alc.h" #include "libmad-0.15.1b/mad.h" #include "common.h" #include "osystemAL.h" #include "osystemAL_mp3.h" struct mad_stream mad_Stream; struct mad_frame mad_Frame; struct mad_synth mad_Synth; mad_timer_t mad_Timer; unsigned char* pMp3Data = NULL; unsigned long int mp3DataSize = 0; #define NUM_MUSIC_BUFFER 3 #define NUM_SAMPLES_PER_BUFFER (1152 * 40 * 2) // 40 frames per buffer (a bit more than a second of playback), 2 channels #define BUFFER_SIZE (NUM_SAMPLES_PER_BUFFER * 2) // 2 bytes per sample ALuint mp3_source = 0; ALuint mp3_buffers[NUM_MUSIC_BUFFER]; unsigned char decompressionBuffer[BUFFER_SIZE]; int decompressedSize = 0; bool decompressionFinished = false; bool bIsMp3Playing = false; static inline int scale_sample(mad_fixed_t sample) { // round sample += (1L << (MAD_F_FRACBITS - 16)); // clip if (sample > MAD_F_ONE - 1) sample = MAD_F_ONE - 1; else if (sample < -MAD_F_ONE) sample = -MAD_F_ONE; // quantize and scale to not saturate when mixing a lot of channels return sample >> (MAD_F_FRACBITS + 1 - 16); } void decodeMP3() { decompressedSize = 0; int16* pCurrentDecompressionBuffer = (int16*)decompressionBuffer; while((((unsigned char*)pCurrentDecompressionBuffer) < decompressionBuffer + BUFFER_SIZE) || decompressionFinished) { if(mad_frame_decode(&mad_Frame,&mad_Stream)) { if(MAD_RECOVERABLE(mad_Stream.error)) { /* Do not print a message if the error is a loss of * synchronization and this loss is due to the end of * stream guard bytes. (See the comments marked {3} * supra for more informations about guard bytes.) */ if(mad_Stream.error!=MAD_ERROR_LOSTSYNC) { fflush(stderr); } continue; } else { if(mad_Stream.error==MAD_ERROR_BUFLEN) { decompressionFinished = true; break; } else { assert(0); } } } mad_synth_frame(&mad_Synth, &mad_Frame); assert(mad_Synth.pcm.channels == 2); assert(mad_Synth.pcm.length == 1152); for(int sampleIdx=0; sampleIdx