2 Converts WAV/WAVE files to SWF.
4 Part of the swftools package.
6 Copyright (c) 2001 Matthias Kramm <kramm@quiss.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
24 #include "../lib/rfxswf.h"
25 #include "../lib/log.h"
26 #include "../lib/args.h"
30 char * outputname = "output.swf";
34 #define DEFINESOUND_MP3 1 //define sound uses mp3?- undefine for raw sound.
36 struct options_t options[] =
52 static int definesound = 0;
53 static int framerate = 0;
54 static int samplerate = 11025;
55 static int bitrate = 32;
56 static int do_cgi = 0;
58 static int mp3_bitrates[] =
59 { 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0};
61 int args_callback_option(char*name,char*val)
63 if(!strcmp(name, "V")) {
64 printf("wav2swf - part of %s %s\n", PACKAGE, VERSION);
67 else if(!strcmp(name, "o")) {
71 else if(!strcmp(name, "d")) {
75 else if(!strcmp(name, "l")) {
80 else if(!strcmp(name, "v")) {
84 else if(!strcmp(name, "S")) {
88 else if(!strcmp(name, "C")) {
92 else if(!strcmp(name, "r")) {
94 sscanf(val, "%f", &f);
98 else if(!strcmp(name, "s")) {
99 samplerate = atoi(val);
100 if(samplerate > 5000 && samplerate < 6000)
102 else if(samplerate > 11000 && samplerate < 12000)
104 else if(samplerate > 22000 && samplerate < 23000)
106 else if(samplerate > 44000 && samplerate < 45000)
109 fprintf(stderr, "Invalid samplerate: %d\n", samplerate);
110 fprintf(stderr, "Allowed values: 11025, 22050, 44100\n", samplerate);
115 else if(!strcmp(name, "b")) {
119 fprintf(stderr, "Not a valid bitrate: %s\n", val);
123 fprintf(stderr, "Bitrate must be <144. (%s)\n", val);
126 for(t=0;mp3_bitrates[t];t++) {
127 if(b== mp3_bitrates[t]) {
132 fprintf(stderr, "Invalid bitrate. Allowed bitrates are:\n");
133 for(t=0;mp3_bitrates[t];t++) {
134 printf("%d ", mp3_bitrates[t]);
140 printf("Unknown option: -%s\n", name);
145 int args_callback_longoption(char*name,char*val)
147 return args_long2shortoption(options, name, val);
149 void args_callback_usage(char*name)
151 printf("Usage: %s [-o filename] file.wav\n", name);
152 printf("\t-v , --verbose\t\t\t Be more verbose\n");
153 printf("\t-d , --definesound\t\t Generate a DefineSound tag instead of streaming sound\n");
154 printf("\t-l , --loop n\t\t\t Loop sound n times (implies -d)\n");
155 printf("\t-r , --framerate fps\t\t Set framerate to fps frames per second\n");
156 printf("\t-s , --samplerate sps\t\t Set samplerate to sps frames per second (default: 11025)\n");
157 printf("\t-b , --bitrate bps\t\t Set mp3 bitrate (default: 32)\n");
158 printf("\t-o , --output filename\t\t set output filename (default: output.swf)\n");
159 printf("\t-C , --cgi\t\t\t For use as CGI- prepend http header, write to stdout\n");
160 printf("\t-S , --stop\t\t\t Stop the movie at frame 0\n");
161 printf("\t \t\t\t (For use with flashsound.js)\n");
162 printf("\t-V , --version\t\t\t Print program version and exit\n");
164 int args_callback_command(char*name,char*val)
167 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
174 extern int swf_mp3_bitrate;
175 extern int swf_mp3_out_samplerate;
176 extern int swf_mp3_in_samplerate;
178 int main (int argc,char ** argv)
183 S32 width=300,height = 300;
191 float blockspersecond;
192 float framespersecond;
193 float samplesperframe;
194 float framesperblock;
195 float samplesperblock;
199 processargs(argc, argv);
201 blocksize = (samplerate > 22050) ? 1152 : 576;
203 blockspersecond = (float)samplerate/blocksize;
205 framespersecond = blockspersecond;
207 framespersecond = framerate/256.0;
209 framesperblock = framespersecond / blockspersecond;
210 samplesperframe = (blocksize * blockspersecond) / framespersecond;
211 samplesperblock = samplesperframe * framesperblock;
213 initLog(0,-1,0,0,-1,verbose);
216 msg("<fatal> You must supply a filename");
220 if(!readWAV(filename, &wav))
222 msg("<fatal> Error reading %s", filename);
225 convertWAV2mono(&wav,&wav2, samplerate);
226 //printWAVInfo(&wav);
227 //printWAVInfo(&wav2);
228 samples = (U16*)wav2.data;
229 numsamples = wav2.size/2;
231 if(numsamples%blocksize != 0)
233 // apply padding, so that block is a multiple of blocksize
234 int numblocks = (numsamples+blocksize-1)/blocksize;
237 numsamples2 = numblocks * blocksize;
238 samples2 = malloc(sizeof(U16)*numsamples2);
239 memcpy(samples2, samples, numsamples*sizeof(U16));
240 memset(&samples2[numsamples], 0, sizeof(U16)*(numsamples2 - numsamples));
241 numsamples = numsamples2;
245 memset(&swf,0x00,sizeof(SWF));
248 swf.frameRate = (int)(framespersecond*256);
250 swf.movieSize.xmax = 20*width;
251 swf.movieSize.ymax = 20*height;
253 swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
258 swf_SetRGB(tag,&rgb);
261 ActionTAG*action = 0;
262 tag = swf_InsertTag(tag, ST_DOACTION);
263 action = action_Stop(action);
264 action = action_End(action);
265 swf_ActionSet(tag, action);
266 swf_ActionFree(action);
268 tag = swf_InsertTag(tag, ST_SHOWFRAME);
271 swf_mp3_bitrate = bitrate;
272 swf_mp3_out_samplerate = samplerate;
273 swf_mp3_in_samplerate = samplerate;
277 int oldframepos=-1, newframepos=0;
278 float framesamplepos = 0;
283 tag = swf_InsertTag(tag, ST_SOUNDSTREAMHEAD);
284 swf_SetSoundStreamHead(tag, samplesperframe);
285 msg("<notice> %d blocks", numsamples/blocksize);
286 for(t=0;t<numsamples/blocksize;t++) {
289 int seek = blocksize - ((int)samplepos - (int)framesamplepos);
291 if(newframepos!=oldframepos) {
292 tag = swf_InsertTag(tag, ST_SOUNDSTREAMBLOCK);
293 msg("<notice> Starting block %d %d+%d", t, (int)samplepos, (int)blocksize);
294 block1 = &samples[t*blocksize];
295 swf_SetSoundStreamBlock(tag, block1, seek, 1);
296 v1 = v2 = GET16(tag->data);
298 msg("<notice> Adding data...", t);
299 block1 = &samples[t*blocksize];
300 swf_SetSoundStreamBlock(tag, block1, seek, 0);
302 PUT16(tag->data, v1);
304 samplepos += blocksize;
306 oldframepos = (int)framepos;
307 framepos += framesperblock;
308 newframepos = (int)framepos;
310 for(s=oldframepos;s<newframepos;s++) {
311 tag = swf_InsertTag(tag, ST_SHOWFRAME);
312 framesamplepos += samplesperframe;
315 tag = swf_InsertTag(tag, ST_END);
318 tag = swf_InsertTag(tag, ST_DEFINESOUND);
319 swf_SetU16(tag, 24); //id
320 #ifdef DEFINESOUND_MP3
321 swf_SetSoundDefine(tag, samples, numsamples);
323 swf_SetU8(tag,(/*compression*/0<<4)|(/*rate*/3<<2)|(/*size*/1<<1)|/*mono*/0);
324 swf_SetU32(tag, numsamples); // 44100 -> 11025
325 swf_SetBlock(tag, samples, numsamples*2);
329 tag = swf_InsertTag(tag, ST_STARTSOUND);
330 swf_SetU16(tag, 24); //id
331 memset(&info, 0, sizeof(info));
333 swf_SetSoundInfo(tag, &info);
334 tag = swf_InsertTag(tag, ST_SHOWFRAME);
335 tag = swf_InsertTag(tag, ST_END);
339 if FAILED(swf_WriteCGI(&swf)) fprintf(stderr,"WriteCGI() failed.\n");
341 f = open(outputname,O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644);
342 if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");