X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=lib%2Fmodules%2Fswfsound.c;h=d76baec513275bd69f53906d29e331ed0f85f3f2;hb=879d0eec420fe0fd5ddcd56c8fe62b82a6744edd;hp=fe3461d35881cf229c3097f3e1d6d8646f9ec5a3;hpb=a970a6438fa892d043faac61ea6e27c8c88f79fa;p=swftools.git diff --git a/lib/modules/swfsound.c b/lib/modules/swfsound.c index fe3461d..d76baec 100644 --- a/lib/modules/swfsound.c +++ b/lib/modules/swfsound.c @@ -7,16 +7,27 @@ Copyright (c) 2001, 2002 Matthias Kramm - This file is distributed under the GPL, see file COPYING for details + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. -*/ + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. -#ifndef RFXSWF_DISABLESOUND + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +#ifndef NO_MP3 #include "../rfxswf.h" #ifdef BLADEENC -fjokjklj +#define HAVE_SOUND + CodecInitOut * init = 0; void swf_SetSoundStreamHead(TAG*tag, U16 avgnumsamples) { @@ -49,7 +60,7 @@ void swf_SetSoundStreamBlock(TAG*tag, S16*samples, int numsamples, char first) char*buf; int len = 0; - buf = malloc(init->bufferSize); + buf = rfx_alloc(init->bufferSize); if(!buf) return; @@ -62,53 +73,74 @@ void swf_SetSoundStreamBlock(TAG*tag, S16*samples, int numsamples, char first) swf_SetU16(tag, 0); // seek } swf_SetBlock(tag, buf, len); - free(buf); + rfx_free(buf); } #endif +void swf_SetSoundDefineRaw(TAG*tag, S16*samples, int numsamples) +{ + swf_SetU8(tag,(/*compression*/0<<4)|(/*rate*/3<<2)|(/*size*/1<<1)|/*mono*/0); + swf_SetU32(tag, numsamples); // 44100 -> 11025 + swf_SetBlock(tag, (U8*)samples, numsamples*2); +} + +/* TODO: find a better way to set these from the outside */ -#ifdef LAME +int swf_mp3_in_samplerate = 44100; +int swf_mp3_out_samplerate = 11025; +int swf_mp3_channels = 1; +int swf_mp3_bitrate = 32; + +#ifdef HAVE_LAME +#define HAVE_SOUND + +#include +#include -#include "../lame/lame.h" - static lame_global_flags*lame_flags; -static void initlame(unsigned char*buf, int bufsize) +void null_errorf(const char *format, va_list ap) { +} + +static void initlame() +{ + unsigned char buf[4096]; + int bufsize = 1152*2; + lame_flags = lame_init(); - lame_set_in_samplerate(lame_flags, 44100); - lame_set_num_channels(lame_flags, 1); + lame_set_in_samplerate(lame_flags, swf_mp3_in_samplerate); + lame_set_num_channels(lame_flags, swf_mp3_channels); lame_set_scale(lame_flags, 0); // MPEG1 32, 44.1, 48khz // MPEG2 16, 22.05, 24 // MPEG2.5 8, 11.025, 12 - // (not used by decoding routines) - lame_set_out_samplerate(lame_flags, 11025); + lame_set_out_samplerate(lame_flags, swf_mp3_out_samplerate); lame_set_quality(lame_flags, 0); lame_set_mode(lame_flags, MONO/*3*/); + lame_set_brate(lame_flags, swf_mp3_bitrate); //lame_set_compression_ratio(lame_flags, 11.025); lame_set_bWriteVbrTag(lame_flags, 0); lame_init_params(lame_flags); lame_init_bitstream(lame_flags); + lame_set_errorf(lame_flags, null_errorf); /* The first two flush calls to lame always fail, for some reason. Do them here where they cause no damage. */ lame_encode_flush_nogap(lame_flags, buf, bufsize); //printf("init:flush_nogap():%d\n", len); lame_encode_flush(lame_flags, buf, bufsize); //printf("init:flush():%d\n", len); + lame_set_errorf(lame_flags, 0); } void swf_SetSoundStreamHead(TAG*tag, int avgnumsamples) { - unsigned char buf[4096]; - int bufsize = 1152*2; int len; - short int samples[1152*2]; U8 playbackrate = 1; // 0 = 5.5 Khz, 1 = 11 Khz, 2 = 22 Khz, 3 = 44 Khz U8 playbacksize = 1; // 0 = 8 bit, 1 = 16 bit @@ -117,48 +149,49 @@ void swf_SetSoundStreamHead(TAG*tag, int avgnumsamples) U8 rate = 1; // 0 = 5.5 Khz, 1 = 11 Khz, 2 = 22 Khz, 3 = 44 Khz U8 size = 1; // 0 = 8 bit, 1 = 16 bit U8 type = 0; // 0 = mono, 1 = stereo - - memset(samples,0,sizeof(samples)); - initlame(buf, bufsize); + if(swf_mp3_out_samplerate == 5512) playbackrate = rate = 0; // lame doesn't support this + else if(swf_mp3_out_samplerate == 11025) playbackrate = rate = 1; + else if(swf_mp3_out_samplerate == 22050) playbackrate = rate = 2; + else if(swf_mp3_out_samplerate == 44100) playbackrate = rate = 3; + else fprintf(stderr, "Invalid samplerate: %d\n", swf_mp3_out_samplerate); + + initlame(); swf_SetU8(tag,(playbackrate<<2)|(playbacksize<<1)|playbacktype); swf_SetU8(tag,(compression<<4)|(rate<<2)|(size<<1)|type); swf_SetU16(tag,avgnumsamples); } -void swf_SetSoundStreamBlock(TAG*tag, S16*samples, char first) +void swf_SetSoundStreamBlock(TAG*tag, S16*samples, int seek, char first) { char*buf; - int oldlen=0,len = 0; + int len = 0; int bufsize = 16384; - int numsamples = 1152*2; + int numsamples = (int)(((swf_mp3_out_samplerate > 22050) ? 1152 : 576) * ((double)swf_mp3_in_samplerate/swf_mp3_out_samplerate)); + int fs = 0; - buf = malloc(bufsize); + buf = rfx_alloc(bufsize); if(!buf) return; if(first) { - int fs = lame_get_framesize(lame_flags); - //printf("framesize:%d\n", fs); - swf_SetU16(tag, fs); // samples per mp3 frame - swf_SetU16(tag, 0); // seek + fs = lame_get_framesize(lame_flags); + swf_SetU16(tag, fs * first); // samples per mp3 frame + swf_SetU16(tag, seek); // seek } len += lame_encode_buffer(lame_flags, samples, samples, numsamples, &buf[len], bufsize-len); - //printf("block: %d (+%d)\n", len, len-oldlen); - oldlen = len; - len += lame_encode_flush_nogap(lame_flags, &buf[len], bufsize-len); - //printf("flush: %d (+%d)\n", len, len-oldlen); - oldlen = len; - swf_SetBlock(tag, buf, len); - - /* len += lame_encode_flush(lame_flags, &buf[len], bufsize-len); - printf("flush! %d (+%d)\n", len, len-oldlen);*/ - - free(buf); + if(len == 0) { + fprintf(stderr, "error: mp3 empty block, %d samples, first:%d, framesize:%d\n", + numsamples, first, fs); + }/* else { + fprintf(stderr, "ok: mp3 nonempty block, %d samples, first:%d, framesize:%d\n", + numsamples, first, fs); + }*/ + rfx_free(buf); } void swf_SetSoundStreamEnd(TAG*tag) @@ -171,7 +204,7 @@ void swf_SetSoundDefine(TAG*tag, S16*samples, int num) char*buf; int oldlen=0,len = 0; int bufsize = 16384; - int blocksize = 1152*2; + int blocksize = (int)(((swf_mp3_out_samplerate > 22050) ? 1152 : 576) * ((double)swf_mp3_in_samplerate/swf_mp3_out_samplerate)); int t; int blocks; @@ -179,17 +212,26 @@ void swf_SetSoundDefine(TAG*tag, S16*samples, int num) U8 rate = 1; // 0 = 5.5 Khz, 1 = 11 Khz, 2 = 22 Khz, 3 = 44 Khz U8 size = 1; // 0 = 8 bit, 1 = 16 bit U8 type = 0; // 0 = mono, 1 = stereo + + if(swf_mp3_out_samplerate == 5512) rate = 0; + else if(swf_mp3_out_samplerate == 11025) rate = 1; + else if(swf_mp3_out_samplerate == 22050) rate = 2; + else if(swf_mp3_out_samplerate == 44100) rate = 3; + else fprintf(stderr, "Invalid samplerate: %d\n", swf_mp3_out_samplerate); blocks = num / (blocksize); swf_SetU8(tag,(compression<<4)|(rate<<2)|(size<<1)|type); - swf_SetU32(tag,blocks*blocksize); - buf = malloc(bufsize); + swf_SetU32(tag, (int)(tag,blocks*blocksize / + ((double)swf_mp3_in_samplerate/swf_mp3_out_samplerate)) // account for resampling + ); + + buf = rfx_alloc(bufsize); if(!buf) return; - initlame(buf, bufsize); + initlame(); swf_SetU16(tag, 0); //delayseek for(t=0;tstop?SOUNDINFO_STOP:0) - |(info->multiple?SOUNDINFO_NOMULTIPLE:0) + |(info->nomultiple?SOUNDINFO_NOMULTIPLE:0) |(info->envelopes?SOUNDINFO_HASENVELOPE:0) |(info->loops?SOUNDINFO_HASLOOPS:0) |(info->outpoint?SOUNDINFO_HASOUTPOINT:0) @@ -238,6 +308,26 @@ void swf_SetSoundInfo(TAG*tag, SOUNDINFO*info) } } -#endif -#endif // RFXSWF_DISABLESOUND +void swf_SetSoundDefineMP3(TAG*tag, U8* data, unsigned length, + unsigned SampRate, + unsigned Channels, + unsigned NumFrames) +{ + U8 compression = 2; // 0 = raw, 1 = ADPCM, 2 = mp3, 3 = raw le, 6 = nellymoser + U8 rate; // 0 = 5.5 Khz, 1 = 11 Khz, 2 = 22 Khz, 3 = 44 Khz + U8 size = 1; // 0 = 8 bit, 1 = 16 bit + U8 type = Channels==2; // 0=mono, 1=stereo + + rate = (SampRate >= 40000) ? 3 + : (SampRate >= 19000) ? 2 + : (SampRate >= 8000) ? 1 + : 0; + + swf_SetU8(tag,(compression<<4)|(rate<<2)|(size<<1)|type); + + swf_SetU32(tag, NumFrames * 576); + + swf_SetU16(tag, 0); //delayseek + swf_SetBlock(tag, data, length); +}