From: Laszlo T. <ave...@us...> - 2004-10-04 22:03:28
|
Update of /cvsroot/gemrb/gemrb/gemrb/plugins/ACMImporter In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14860/plugins/ACMImporter Modified Files: ACMImp.cpp Makefile.am readers.cpp readers.h Log Message: added Ogg Vorbis support in ACMImporter Index: readers.cpp =================================================================== RCS file: /cvsroot/gemrb/gemrb/gemrb/plugins/ACMImporter/readers.cpp,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** readers.cpp 10 Aug 2004 19:11:29 -0000 1.8 --- readers.cpp 4 Oct 2004 22:02:59 -0000 1.9 *************** *** 23,27 **** // Classes for sound files. // Supported formats: PCM-RAW, PCM-WAV (both 8 and 16 bits), ! // and Interplay's ACM. #include <stdio.h> --- 23,27 ---- // Classes for sound files. // Supported formats: PCM-RAW, PCM-WAV (both 8 and 16 bits), ! // Ogg Vorbis and Interplay's ACM. #include <stdio.h> *************** *** 29,32 **** --- 29,130 ---- #include "general.h" + #ifdef HAS_VORBIS_SUPPORT + static size_t ovfd_read(void *ptr, size_t size, size_t nmemb, void *datasource) + { + DataStream *vb = (DataStream *) datasource; + int bytesToRead = size * nmemb; + + int remains = vb->Remains(); + if(remains<=0) { + /* no more reading, we're at the end */ + return 0; + } + if(bytesToRead > remains ) { + bytesToRead = remains; + } + vb->Read(ptr, bytesToRead); + return bytesToRead; + } + + static int ovfd_seek(void *datasource, int64_t offset, int whence) { + DataStream *vb = (DataStream *) datasource; + switch(whence) { + case SEEK_SET: + if(vb->Seek(offset, GEM_STREAM_START)<0) + return -1; + break; + case SEEK_CUR: + if(vb->Seek(offset, GEM_CURRENT_POS)<0) + return -1; + break; + case SEEK_END: + if(vb->Seek(vb->Size()+offset, GEM_STREAM_START)<0) + return -1; + break; + default: + return -1; + } + return vb->Pos; + } + + static int ovfd_close(void */*datasource*/) { + return 0; + } + + static long ovfd_tell(void *datasource) { + DataStream *vb = (DataStream *) datasource; + return (long) vb->Pos; + } + + int COGGReader::init_reader() + { + vorbis_info *info; + int res; + ov_callbacks cbstruct = { + ovfd_read, ovfd_seek, ovfd_close, ovfd_tell + }; + + res=ov_open_callbacks(stream, &OggStream, NULL, 0, cbstruct); + if(res<0) { + printf("Couldn't initialize vorbis!\n"); + return 0; + } + info = ov_info(&OggStream, -1); + channels = info->channels; + printf("Channels: %d\n",channels); + samplerate = info->rate; + printf("Samplerate: %d\n",samplerate); + samples_left = ( samples = ov_pcm_total(&OggStream, -1) ); + return 1; + } + + int COGGReader::read_samples(short* buffer, int count) + { + int whatisthis; + + if(samples_left<count) { + count=samples_left; + } + int samples_got=0; + int samples_need=count; + while(samples_need) { + int rd=ov_read(&OggStream, (char *)buffer, samples_need<<1, 0, 2, 1, &whatisthis); + if(rd==OV_HOLE) { + continue; + } + if(rd<=0) { + break; + } + rd>>=1; + buffer+=rd; + samples_got+=rd; + samples_need-=rd; + } + samples_left-=samples_got; + return samples_got; + } + + #endif + int CACMReader::init_reader() { *************** *** 105,108 **** --- 203,209 ---- switch (type) { + case SND_READER_OGG: + res = new COGGReader( stream, autoFree ); + break; case SND_READER_ACM: res = new CACMReader( stream, autoFree ); *************** *** 121,135 **** } - /* - short CSoundReader::read_one_sample() - { - short res; - if (!read_samples( &res, 1 )) { - res = 0; - } // no more samples left => return 0; - return res; - } - */ - int CRawPCMReader::init_reader() { --- 222,225 ---- Index: readers.h =================================================================== RCS file: /cvsroot/gemrb/gemrb/gemrb/plugins/ACMImporter/readers.h,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** readers.h 10 Aug 2004 19:11:29 -0000 1.8 --- readers.h 4 Oct 2004 22:02:59 -0000 1.9 *************** *** 31,34 **** --- 31,38 ---- #include "../Core/DataStream.h" + #ifdef HAS_VORBIS_SUPPORT + #include <vorbis/vorbisfile.h> + #endif + #define INIT_NO_ERROR_MSG 0 #define INIT_NEED_ERROR_MSG 1 *************** *** 42,55 **** int samples_left; // count of unread samples int is16bit; // 1 - if 16 bit file, 0 - otherwise - //FILE* file; // file handle DataStream* stream; bool autoFree; public: ! CSoundReader(DataStream* stream, bool autoFree = true)//int fhandle) : samples( 0 ), channels( 0 ), samples_left( 0 ), is16bit( 1 ) { - //file=fdopen(fhandle,"rb"); this->stream = stream; this->autoFree = autoFree; --- 46,57 ---- int samples_left; // count of unread samples int is16bit; // 1 - if 16 bit file, 0 - otherwise DataStream* stream; bool autoFree; public: ! CSoundReader(DataStream* stream, bool autoFree = true) : samples( 0 ), channels( 0 ), samples_left( 0 ), is16bit( 1 ) { this->stream = stream; this->autoFree = autoFree; *************** *** 58,62 **** virtual ~CSoundReader() { - //if (file) fclose (file); if (stream && autoFree) { delete( stream ); --- 60,63 ---- *************** *** 126,129 **** --- 127,153 ---- }; + #ifdef HAS_VORBIS_SUPPORT + class COGGReader : public CSoundReader { + private: + OggVorbis_File OggStream; + public: + COGGReader(DataStream* stream, bool autoFree = true) + : CSoundReader( stream, autoFree) + { + memset(&OggStream, 0, sizeof(OggStream) ); + }; + virtual ~COGGReader() + { + ov_clear(&OggStream); + }; + virtual int init_reader(); + virtual const char* get_file_type() + { + return "OGG"; + }; + virtual int read_samples(short* buffer, int count); + }; + #endif //HAS_VORBIS_SUPPORT + // IP's ACM files class CACMReader : public CSoundReader { *************** *** 138,142 **** int make_new_samples(); public: ! CACMReader(DataStream* stream, bool autoFree = true)//int fhandle) : CSoundReader( stream, autoFree ), block( NULL ), values( NULL ), --- 162,166 ---- int make_new_samples(); public: ! CACMReader(DataStream* stream, bool autoFree = true) : CSoundReader( stream, autoFree ), block( NULL ), values( NULL ), *************** *** 219,222 **** --- 243,247 ---- #define SND_READER_WAV 3 #define SND_READER_ACM 4 + #define SND_READER_OGG 5 #endif Index: Makefile.am =================================================================== RCS file: /cvsroot/gemrb/gemrb/gemrb/plugins/ACMImporter/Makefile.am,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** Makefile.am 29 Aug 2004 01:19:01 -0000 1.12 --- Makefile.am 4 Oct 2004 22:02:59 -0000 1.13 *************** *** 1,5 **** plugin_LTLIBRARIES = libACMImporter.la INCLUDES = $(all_includes) ! libACMImporter_la_LDFLAGS = -module -version-info 0:0:0 -lopenal -lSDL @LIBPTHREAD@ libACMImporter_la_LIBADD = ../Core/libgemrb_core.la libACMImporter_la_METASOURCES = AUTO --- 1,5 ---- plugin_LTLIBRARIES = libACMImporter.la INCLUDES = $(all_includes) ! libACMImporter_la_LDFLAGS = -module -version-info 0:0:0 -lopenal -lvorbis -lvorbisfile -lSDL @LIBPTHREAD@ libACMImporter_la_LIBADD = ../Core/libgemrb_core.la libACMImporter_la_METASOURCES = AUTO Index: ACMImp.cpp =================================================================== RCS file: /cvsroot/gemrb/gemrb/gemrb/plugins/ACMImporter/ACMImp.cpp,v retrieving revision 1.58 retrieving revision 1.59 diff -C2 -d -r1.58 -r1.59 *** ACMImp.cpp 29 Aug 2004 01:19:01 -0000 1.58 --- ACMImp.cpp 4 Oct 2004 22:02:59 -0000 1.59 *************** *** 36,44 **** #define DisplayALError(string, error) printf("%s0x%04X", string, error); #define ACM_BUFFERSIZE 8192 ! #define MUSICBUFERS 10 static AudioStream streams[MAX_STREAMS], speech; static CSoundReader *MusicReader; ! static ALuint MusicSource, MusicBuffers[MUSICBUFERS]; static SDL_mutex* musicMutex; static bool musicPlaying; --- 36,44 ---- #define DisplayALError(string, error) printf("%s0x%04X", string, error); #define ACM_BUFFERSIZE 8192 ! #define MUSICBUFFERS 10 static AudioStream streams[MAX_STREAMS], speech; static CSoundReader *MusicReader; ! static ALuint MusicSource, MusicBuffers[MUSICBUFFERS]; static SDL_mutex* musicMutex; static bool musicPlaying; *************** *** 46,76 **** static unsigned char* static_memory; ! bool isWAVC(DataStream* stream) { if (!stream) { ! return false; } char Signature[4]; stream->Read( Signature, 4 ); stream->Seek( 0, GEM_STREAM_START ); /* ! if(strnicmp(Signature, "RIFF", 4) == 0) ! return false; //wav ! if(strnicmp(Signature, "oggs", 4) == 0) ! return false; //ogg ! if( * (unsigned short *) Signature == 0xfffb) ! return false; //mp3 ! return true; */ if (*( unsigned int * ) Signature == IP_ACM_SIG) { ! return true; } //acm if (memcmp( Signature, "WAVC", 4 ) == 0) { ! return true; } //wavc ! return false; } ! ALenum GetFormatEnum(int channels, int bits) { switch (channels) { --- 46,75 ---- static unsigned char* static_memory; ! static int isWAVC(DataStream* stream) { if (!stream) { ! return -1; } char Signature[4]; stream->Read( Signature, 4 ); stream->Seek( 0, GEM_STREAM_START ); + if(strnicmp(Signature, "oggs", 4) == 0) + return SND_READER_OGG; + if(strnicmp(Signature, "RIFF", 4) == 0) + return SND_READER_WAV; //wav /* ! if( * (unsigned short *) Signature == 0xfffb) ! return SND_READER_MP3; //mp3 */ if (*( unsigned int * ) Signature == IP_ACM_SIG) { ! return SND_READER_ACM; } //acm if (memcmp( Signature, "WAVC", 4 ) == 0) { ! return SND_READER_ACM; } //wavc ! return -1; } ! static ALenum GetFormatEnum(int channels, int bits) { switch (channels) { *************** *** 95,99 **** { if (musicPlaying && free) { ! for (int i = 0; i < MUSICBUFERS; i++) { if (alIsBuffer( MusicBuffers[i] )) alDeleteBuffers( 1, &MusicBuffers[i] ); --- 94,98 ---- { if (musicPlaying && free) { ! for (int i = 0; i < MUSICBUFFERS; i++) { if (alIsBuffer( MusicBuffers[i] )) alDeleteBuffers( 1, &MusicBuffers[i] ); *************** *** 135,139 **** { printf( "Music in INITIAL State. AutoStarting\n" ); ! for (int i = 0; i < MUSICBUFERS; i++) { MusicReader->read_samples( ( short* ) static_memory, ACM_BUFFERSIZE >> 1 ); alBufferData( MusicBuffers[i], AL_FORMAT_STEREO16, --- 134,138 ---- { printf( "Music in INITIAL State. AutoStarting\n" ); ! for (int i = 0; i < MUSICBUFFERS; i++) { MusicReader->read_samples( ( short* ) static_memory, ACM_BUFFERSIZE >> 1 ); alBufferData( MusicBuffers[i], AL_FORMAT_STEREO16, *************** *** 141,145 **** MusicReader->get_samplerate() ); } ! alSourceQueueBuffers( MusicSource, MUSICBUFERS, MusicBuffers ); if (alIsSource( MusicSource )) { alSourcePlay( MusicSource ); --- 140,144 ---- MusicReader->get_samplerate() ); } ! alSourceQueueBuffers( MusicSource, MUSICBUFFERS, MusicBuffers ); if (alIsSource( MusicSource )) { alSourcePlay( MusicSource ); *************** *** 201,205 **** unsigned int i; ! for (i = 0; i < MUSICBUFERS; i++) MusicBuffers[i] = 0; MusicSource = 0; --- 200,204 ---- unsigned int i; ! for (i = 0; i < MUSICBUFFERS; i++) MusicBuffers[i] = 0; MusicSource = 0; *************** *** 309,323 **** if (error != AL_NO_ERROR) { DisplayALError( "Cannot Create a Buffer for this sound. Skipping", error ); return 0; } ! CSoundReader* acm; ! if (isWAVC( stream )) { ! acm = CreateSoundReader( stream, SND_READER_ACM, stream->Size(), true ); ! } else { ! acm = CreateSoundReader( stream, SND_READER_WAV, stream->Size(), true ); } long cnt = acm->get_length(); long riff_chans = acm->get_channels(); - //long bits = acm->get_bits(); long samplerate = acm->get_samplerate(); //multiply always by 2 because it is in 16 bits --- 308,324 ---- if (error != AL_NO_ERROR) { DisplayALError( "Cannot Create a Buffer for this sound. Skipping", error ); + delete( stream ); return 0; } ! int type = isWAVC( stream ); ! if (type<0 ) { ! delete( stream ); ! return 0; } + + CSoundReader* acm; + acm = CreateSoundReader( stream, type, stream->Size(), true ); long cnt = acm->get_length(); long riff_chans = acm->get_channels(); long samplerate = acm->get_samplerate(); //multiply always by 2 because it is in 16 bits *************** *** 332,335 **** --- 333,337 ---- delete( acm ); free(memory); + if (( error = alGetError() ) != AL_NO_ERROR) { DisplayALError( "[ACMImp::Play] alBufferData : ", error ); *************** *** 458,470 **** } if (MusicBuffers[0] == 0) { ! alGenBuffers( MUSICBUFERS, MusicBuffers ); } ! if (isWAVC( str )) { ! MusicReader = CreateSoundReader( str, SND_READER_ACM, str->Size(), ! true ); ! } else { ! MusicReader = CreateSoundReader( str, SND_READER_WAV, str->Size(), ! true ); } if (MusicSource == 0) { --- 460,471 ---- } if (MusicBuffers[0] == 0) { ! alGenBuffers( MUSICBUFFERS, MusicBuffers ); } ! int type = isWAVC( str ); ! if (type<0 ) { ! delete( str ); ! return 0; } + MusicReader = CreateSoundReader( str, type, str->Size(), true ); if (MusicSource == 0) { |