X-Git-Url: http://git.asbjorn.biz/?p=swftools.git;a=blobdiff_plain;f=src%2Fwav2swf.c;h=d27f172ea28f5b5cc797661e8f128b24868f856b;hp=26b103e4be12a8f79146a51a5423f86962f3f7e8;hb=3717652f61a47a4d6f47481f2b1977d9df2d55b5;hpb=8688ff52583c35c739485a81ef1215ea48214ce3 diff --git a/src/wav2swf.c b/src/wav2swf.c index 26b103e..d27f172 100644 --- a/src/wav2swf.c +++ b/src/wav2swf.c @@ -1,5 +1,5 @@ -/* swfextract.c - Allows to extract parts of the swf into a new file. +/* wav2swf.c + Converts WAV/WAVE files to SWF. Part of the swftools package. @@ -18,6 +18,8 @@ char * filename = 0; char * outputname = "output.swf"; int verbose = 2; +#define DEFINESOUND_MP3 1 //define sound uses mp3?- undefine for raw sound. + struct options_t options[] = { {"o","output"}, @@ -25,6 +27,7 @@ struct options_t options[] = {"d","definesound"}, {"l","loop"}, {"r","framerate"}, + {"b","bitrate"}, {"V","version"}, {0,0} }; @@ -32,6 +35,7 @@ struct options_t options[] = static int loop = 0; static int definesound = 0; static int framerate = 0; +static int bitrate = 32; int args_callback_option(char*name,char*val) { @@ -62,6 +66,18 @@ int args_callback_option(char*name,char*val) framerate = f*256; return 1; } + else if(!strcmp(name, "b")) { + bitrate = atoi(val); + if(bitrate<=0) { + fprintf(stderr, "Not a valid bitrate: %s\n", val); + exit(1); + } + if(bitrate>144) { + fprintf(stderr, "Bitrate must be <144. (%s)\n", val); + exit(1); + } + return 1; + } else { printf("Unknown option: -%s\n", name); exit(1); @@ -76,9 +92,10 @@ void args_callback_usage(char*name) { printf("Usage: %s [-o filename] file.wav\n", name); printf("\t-v , --verbose\t\t\t Be more verbose\n"); - printf("\t-d , --definesound\t\t\t Generate a DefineSound tag instead of streaming sound\n"); + printf("\t-d , --definesound\t\t Generate a DefineSound tag instead of streaming sound\n"); printf("\t-l , --loop n\t\t\t Loop sound n times (implies -d)\n"); - printf("\t-r , --framerate fps\t\t\t Set framerate to fps frames per seond\n"); + printf("\t-r , --framerate fps\t\t Set framerate to fps frames per seond\n"); + printf("\t-b , --bitrate bps\t\t Set mp3 bitrate (default: 32)\n"); printf("\t-o , --output filename\t\t set output filename (default: output.swf)\n"); printf("\t-V , --version\t\t\t Print program version and exit\n"); } @@ -92,6 +109,8 @@ int args_callback_command(char*name,char*val) return 0; } +extern int swf_mp3_bitrate; + int main (int argc,char ** argv) { SWF swf; @@ -107,24 +126,25 @@ int main (int argc,char ** argv) int blocksize; float blockspersecond; float framespersecond; + float samplesperframe; float framesperblock; - float framepos = 0; + float samplesperblock; U16* samples; int numsamples; processargs(argc, argv); - if(!definesound && framerate) { - printf("Warning! The -r option is experimental and won't work without -d\n"); - } - - blocksize = 1152; + blocksize = 576; blockspersecond = 11025.0/blocksize; + framespersecond = blockspersecond; if(framerate) framespersecond = framerate/256.0; - framesperblock = framespersecond/blockspersecond; + framesperblock = framespersecond / blockspersecond; + samplesperframe = (blocksize * blockspersecond) / framespersecond; + samplesperblock = samplesperframe * framesperblock; + initLog(0,-1,0,0,-1,verbose); if(!readWAV(filename, &wav)) @@ -152,34 +172,62 @@ int main (int argc,char ** argv) rgb.g = 0xff; rgb.b = 0xff; swf_SetRGB(tag,&rgb); + + swf_mp3_bitrate = bitrate; if(!definesound) { + int oldframepos=-1, newframepos=0; + float framesamplepos = 0; + float framepos = 0; + float samplepos = 0; + U16 v1=0,v2=0; tag = swf_InsertTag(tag, ST_SOUNDSTREAMHEAD); - swf_SetSoundStreamHead(tag, blocksize); + swf_SetSoundStreamHead(tag, samplesperframe); - logf(" %d blocks", numsamples/(blocksize*2)); - for(t=0;t %d blocks", numsamples/(blocksize*4)); + for(t=0;t Writing block %d", t); - block1 = &samples[t*2*blocksize]; - swf_SetSoundStreamBlock(tag, block1, 1); - - oldframe = (int)framepos; + int seek = blocksize - ((int)samplepos - (int)framesamplepos); + + if(newframepos!=oldframepos) { + tag = swf_InsertTag(tag, ST_SOUNDSTREAMBLOCK); + logf(" Starting block %d %d+%d", t, (int)samplepos, (int)blocksize); + block1 = &samples[t*blocksize*4]; + swf_SetSoundStreamBlock(tag, block1, seek, 1); + v1 = v2 = GET16(tag->data); + } else { + logf(" Adding data...", t); + block1 = &samples[t*blocksize*4]; + swf_SetSoundStreamBlock(tag, block1, seek, 0); + v1+=v2; + PUT16(tag->data, v1); + } + samplepos += blocksize; + + oldframepos = (int)framepos; framepos += framesperblock; - newframe = (int)framepos; - for(s=oldframe;s 11025 + swf_SetBlock(tag, wav2.data, numsamples*2); +#endif + tag = swf_InsertTag(tag, ST_STARTSOUND); swf_SetU16(tag, 24); //id memset(&info, 0, sizeof(info)); @@ -189,7 +237,7 @@ int main (int argc,char ** argv) tag = swf_InsertTag(tag, ST_END); } - f = open(outputname,O_WRONLY|O_CREAT|O_TRUNC, 0644); + f = open(outputname,O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644); if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n"); close(f);