-{ SWF swf;
- RGBA rgb;
- SRECT r;
- S32 width=300,height = 300;
- TAG * tag;
-
- int f,i,ls1,fs1;
- int count;
- int t;
- struct WAV wav,wav2;
- int blocksize = 1152;
- U16* samples;
- int numsamples;
-
- processargs(argc, argv);
- initLog(0,-1,0,0,-1,verbose);
-
- if(!readWAV(filename, &wav))
- {
- logf("<fatal> Error reading %s", filename);
- exit(1);
- }
- convertWAV2mono(&wav,&wav2, 44100);
- //printWAVInfo(&wav);
- //printWAVInfo(&wav2);
- samples = (U16*)wav2.data;
- numsamples = wav2.size/2;
-
- memset(&swf,0x00,sizeof(SWF));
-
- swf.fileVersion = 4;
- swf.frameRate = 11025*256/(blocksize);
-
- swf.movieSize.xmax = 20*width;
- swf.movieSize.ymax = 20*height;
-
- swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
- tag = swf.firstTag;
- rgb.r = 0xff;
- rgb.g = 0xff;
- rgb.b = 0xff;
- swf_SetRGB(tag,&rgb);
-
- tag = swf_InsertTag(tag, ST_SOUNDSTREAMHEAD);
- swf_SetSoundStreamHead(tag, blocksize);
-
- logf("<notice> %d blocks", numsamples/(blocksize*2));
- for(t=0;t<numsamples/(blocksize*2);t++) {
- int s;
- U16*block1;
- tag = swf_InsertTag(tag, ST_SOUNDSTREAMBLOCK);
- logf("<notice> Writing block %d", t);
- block1 = &samples[t*2*blocksize];
- swf_SetSoundStreamBlock(tag, block1, blocksize*2,1);
- tag = swf_InsertTag(tag, ST_SHOWFRAME);
- }
-
- f = open(outputname,O_WRONLY|O_CREAT|O_TRUNC, 0644);
- if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");
- close(f);
-
- swf_FreeTags(&swf);
- return 0;
+{
+ SWF swf;
+ RGBA rgb;
+ SRECT r;
+ S32 width=300,height = 300;
+ TAG * tag;
+
+ int f,i,ls1,fs1;
+ int count;
+ int t;
+ struct WAV wav,wav2;
+ int blocksize;
+ float blockspersecond;
+ float framespersecond;
+ float samplesperframe;
+ float framesperblock;
+ float samplesperblock;
+ U16* samples;
+ int numsamples;
+
+ processargs(argc, argv);
+
+ blocksize = (samplerate > 22050) ? 1152 : 576;
+
+ blockspersecond = (float)samplerate/blocksize;
+
+ framespersecond = blockspersecond;
+ if(framerate)
+ framespersecond = framerate/256.0;
+
+ framesperblock = framespersecond / blockspersecond;
+ samplesperframe = (blocksize * blockspersecond) / framespersecond;
+ samplesperblock = samplesperframe * framesperblock;
+
+ initLog(0,-1,0,0,-1,verbose);
+
+ if(!filename) {
+ msg("<fatal> You must supply a filename");
+ exit(1);
+ }
+
+ if(!readWAV(filename, &wav))
+ {
+ msg("<fatal> Error reading %s", filename);
+ exit(1);
+ }
+ convertWAV2mono(&wav,&wav2, samplerate);
+ //printWAVInfo(&wav);
+ //printWAVInfo(&wav2);
+ samples = (U16*)wav2.data;
+ numsamples = wav2.size/2;
+
+ if(numsamples%blocksize != 0)
+ {
+ // apply padding, so that block is a multiple of blocksize
+ int numblocks = (numsamples+blocksize-1)/blocksize;
+ int numsamples2;
+ U16* samples2;
+ numsamples2 = numblocks * blocksize;
+ samples2 = malloc(sizeof(U16)*numsamples2);
+ memcpy(samples2, samples, numsamples*sizeof(U16));
+ memset(&samples2[numsamples], 0, sizeof(U16)*(numsamples2 - numsamples));
+ numsamples = numsamples2;
+ samples = samples2;
+ }
+
+ memset(&swf,0x00,sizeof(SWF));
+
+ swf.fileVersion = 5;
+ swf.frameRate = (int)(framespersecond*256);
+
+ swf.movieSize.xmax = 20*width;
+ swf.movieSize.ymax = 20*height;
+
+ swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
+ tag = swf.firstTag;
+ rgb.r = 0xff;
+ rgb.g = 0xff;
+ rgb.b = 0xff;
+ swf_SetRGB(tag,&rgb);
+
+ if(stopframe0) {
+ ActionTAG*action = 0;
+ tag = swf_InsertTag(tag, ST_DOACTION);
+ action = action_Stop(action);
+ action = action_End(action);
+ swf_ActionSet(tag, action);
+ swf_ActionFree(action);
+
+ tag = swf_InsertTag(tag, ST_SHOWFRAME);
+ }
+
+ swf_mp3_bitrate = bitrate;
+ swf_mp3_out_samplerate = samplerate;
+ swf_mp3_in_samplerate = samplerate;
+
+ if(!definesound)
+ {
+ int oldframepos=-1, newframepos=0;
+ float framesamplepos = 0;
+ float framepos = 0;
+ float samplepos = 0;
+ ActionTAG* a = 0;
+ U16 v1=0,v2=0;
+ tag = swf_InsertTag(tag, ST_SOUNDSTREAMHEAD);
+ swf_SetSoundStreamHead(tag, samplesperframe);
+ msg("<notice> %d blocks", numsamples/blocksize);
+ for(t=0;t<numsamples/blocksize;t++) {
+ int s;
+ U16*block1;
+ int seek = blocksize - ((int)samplepos - (int)framesamplepos);
+
+ if(newframepos!=oldframepos) {
+ tag = swf_InsertTag(tag, ST_SOUNDSTREAMBLOCK);
+ msg("<notice> Starting block %d %d+%d", t, (int)samplepos, (int)blocksize);
+ block1 = &samples[t*blocksize];
+ swf_SetSoundStreamBlock(tag, block1, seek, 1);
+ v1 = v2 = GET16(tag->data);
+ } else {
+ msg("<notice> Adding data...", t);
+ block1 = &samples[t*blocksize];
+ swf_SetSoundStreamBlock(tag, block1, seek, 0);
+ v1+=v2;
+ PUT16(tag->data, v1);
+ }
+ samplepos += blocksize;
+
+ oldframepos = (int)framepos;
+ framepos += framesperblock;
+ newframepos = (int)framepos;
+
+ for(s=oldframepos;s<newframepos;s++) {
+ tag = swf_InsertTag(tag, ST_SHOWFRAME);
+ framesamplepos += samplesperframe;
+ }
+ }
+ tag = swf_InsertTag(tag, ST_END);
+ } else {
+ SOUNDINFO info;
+ tag = swf_InsertTag(tag, ST_DEFINESOUND);
+ swf_SetU16(tag, 24); //id
+#ifdef DEFINESOUND_MP3
+ swf_SetSoundDefine(tag, samples, numsamples);
+#else
+ swf_SetU8(tag,(/*compression*/0<<4)|(/*rate*/3<<2)|(/*size*/1<<1)|/*mono*/0);
+ swf_SetU32(tag, numsamples); // 44100 -> 11025
+ swf_SetBlock(tag, samples, numsamples*2);
+#endif
+
+
+ tag = swf_InsertTag(tag, ST_STARTSOUND);
+ swf_SetU16(tag, 24); //id
+ memset(&info, 0, sizeof(info));
+ info.loops = loop;
+ swf_SetSoundInfo(tag, &info);
+ tag = swf_InsertTag(tag, ST_SHOWFRAME);
+ tag = swf_InsertTag(tag, ST_END);
+ }
+
+ if(do_cgi) {
+ if FAILED(swf_WriteCGI(&swf)) fprintf(stderr,"WriteCGI() failed.\n");
+ } else {
+ 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);
+ }
+
+ swf_FreeTags(&swf);
+ return 0;