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"
27 #include "../lib/wav.h"
30 char * outputname = "output.swf";
35 #define DEFINESOUND_MP3 1 //define sound uses mp3?- undefine for raw sound.
37 static struct options_t options[] = {
54 static int definesound = 0;
55 static int framerate = 0;
56 static int samplerate = 11025;
57 static int bitrate = 32;
58 static int do_cgi = 0;
60 static int mp3_bitrates[] =
61 { 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0};
63 int args_callback_option(char*name,char*val)
65 if(!strcmp(name, "V")) {
66 printf("wav2swf - part of %s %s\n", PACKAGE, VERSION);
69 else if(!strcmp(name, "o")) {
73 else if(!strcmp(name, "d")) {
77 else if(!strcmp(name, "l")) {
82 else if(!strcmp(name, "v")) {
86 else if(!strcmp(name, "S")) {
90 else if(!strcmp(name, "E")) {
94 else if(!strcmp(name, "C")) {
98 else if(!strcmp(name, "r")) {
100 sscanf(val, "%f", &f);
104 else if(!strcmp(name, "s")) {
105 samplerate = atoi(val);
106 if(samplerate > 5000 && samplerate < 6000)
108 else if(samplerate > 11000 && samplerate < 12000)
110 else if(samplerate > 22000 && samplerate < 23000)
112 else if(samplerate > 44000 && samplerate < 45000)
115 fprintf(stderr, "Invalid samplerate: %d\n", samplerate);
116 fprintf(stderr, "Allowed values: 11025, 22050, 44100\n", samplerate);
121 else if(!strcmp(name, "b")) {
125 fprintf(stderr, "Not a valid bitrate: %s\n", val);
129 fprintf(stderr, "Bitrate must be <144. (%s)\n", val);
132 for(t=0;mp3_bitrates[t];t++) {
133 if(b== mp3_bitrates[t]) {
138 fprintf(stderr, "Invalid bitrate. Allowed bitrates are:\n");
139 for(t=0;mp3_bitrates[t];t++) {
140 printf("%d ", mp3_bitrates[t]);
146 printf("Unknown option: -%s\n", name);
151 int args_callback_longoption(char*name,char*val)
153 return args_long2shortoption(options, name, val);
155 void args_callback_usage(char *name)
158 printf("Usage: %s [-o filename] file.wav\n", name);
160 printf("-h , --help Print short help message and exit\n");
161 printf("-V , --version Print version info and exit\n");
162 printf("-o , --output <filename> Explicitly specify output file. (Otherwise, output will go to output.swf)\n");
163 printf("-r , --framerate <fps> Set file framerate to <fps> frames per second.\n");
164 printf("-s , --samplerate <sps> Set samplerate to <sps> frames per second (default: 11025).\n");
165 printf("-d , --definesound Generate a DefineSound tag instead of streaming sound.\n");
166 printf("-l , --loop n (Only used with -d)\n");
167 printf("-C , --cgi For use as CGI- prepend http header, write to stdout.\n");
168 printf("-S , --stop Stop the movie at frame 0\n");
169 printf("-E , --end Stop the movie at the end frame\n");
170 printf("-b , --bitrate <bps> Set mp3 bitrate to <bps> (default: 32)\n");
171 printf("-v , --verbose Be more verbose\n");
174 int args_callback_command(char*name,char*val)
177 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
184 extern int swf_mp3_bitrate;
185 extern int swf_mp3_out_samplerate;
186 extern int swf_mp3_in_samplerate;
188 int main (int argc,char ** argv)
193 S32 width=300,height = 300;
201 float blockspersecond;
202 float framespersecond;
203 float samplesperframe;
204 float framesperblock;
205 float samplesperblock;
209 processargs(argc, argv);
211 blocksize = (samplerate > 22050) ? 1152 : 576;
213 blockspersecond = (float)samplerate/blocksize;
215 framespersecond = blockspersecond;
217 framespersecond = framerate/256.0;
219 framesperblock = framespersecond / blockspersecond;
220 samplesperframe = (blocksize * blockspersecond) / framespersecond;
221 samplesperblock = samplesperframe * framesperblock;
223 initLog(0,-1,0,0,-1,verbose);
226 msg("<fatal> You must supply a filename");
230 if(!wav_read(filename, &wav))
232 msg("<fatal> Error reading %s", filename);
235 wav_convert2mono(&wav,&wav2, samplerate);
238 samples = (U16*)wav2.data;
239 numsamples = wav2.size/2;
241 #ifdef WORDS_BIGENDIAN
243 for(t=0;t<numsamples;t++) {
244 samples[t] = (samples[t]>>8)&0xff | (samples[t]<<8)&0xff00;
248 if(numsamples%blocksize != 0)
250 // apply padding, so that block is a multiple of blocksize
251 int numblocks = (numsamples+blocksize-1)/blocksize;
254 numsamples2 = numblocks * blocksize;
255 samples2 = malloc(sizeof(U16)*numsamples2);
256 memcpy(samples2, samples, numsamples*sizeof(U16));
257 memset(&samples2[numsamples], 0, sizeof(U16)*(numsamples2 - numsamples));
258 numsamples = numsamples2;
262 memset(&swf,0x00,sizeof(SWF));
265 swf.frameRate = (int)(framespersecond*256);
267 swf.movieSize.xmax = 20*width;
268 swf.movieSize.ymax = 20*height;
270 swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
275 swf_SetRGB(tag,&rgb);
278 ActionTAG*action = 0;
279 tag = swf_InsertTag(tag, ST_DOACTION);
280 action = action_Stop(action);
281 action = action_End(action);
282 swf_ActionSet(tag, action);
283 swf_ActionFree(action);
285 tag = swf_InsertTag(tag, ST_SHOWFRAME);
288 swf_mp3_bitrate = bitrate;
289 swf_mp3_out_samplerate = samplerate;
290 swf_mp3_in_samplerate = samplerate;
294 int oldframepos=-1, newframepos=0;
295 float framesamplepos = 0;
300 tag = swf_InsertTag(tag, ST_SOUNDSTREAMHEAD);
301 swf_SetSoundStreamHead(tag, samplesperframe);
302 msg("<notice> %d blocks", numsamples/blocksize);
303 for(t=0;t<numsamples/blocksize;t++) {
306 int seek = blocksize - ((int)samplepos - (int)framesamplepos);
308 if(newframepos!=oldframepos) {
309 tag = swf_InsertTag(tag, ST_SOUNDSTREAMBLOCK);
310 msg("<notice> Starting block %d %d+%d", t, (int)samplepos, (int)blocksize);
311 block1 = &samples[t*blocksize];
312 swf_SetSoundStreamBlock(tag, block1, seek, 1);
313 v1 = v2 = GET16(tag->data);
315 msg("<notice> Adding data...", t);
316 block1 = &samples[t*blocksize];
317 swf_SetSoundStreamBlock(tag, block1, seek, 0);
319 PUT16(tag->data, v1);
321 samplepos += blocksize;
323 oldframepos = (int)framepos;
324 framepos += framesperblock;
325 newframepos = (int)framepos;
327 for(s=oldframepos;s<newframepos;s++) {
328 tag = swf_InsertTag(tag, ST_SHOWFRAME);
329 framesamplepos += samplesperframe;
332 tag = swf_InsertTag(tag, ST_END);
335 tag = swf_InsertTag(tag, ST_DEFINESOUND);
336 swf_SetU16(tag, 24); //id
337 #ifdef DEFINESOUND_MP3
338 swf_SetSoundDefine(tag, samples, numsamples);
340 swf_SetU8(tag,(/*compression*/0<<4)|(/*rate*/3<<2)|(/*size*/1<<1)|/*mono*/0);
341 swf_SetU32(tag, numsamples); // 44100 -> 11025
342 swf_SetBlock(tag, samples, numsamples*2);
346 tag = swf_InsertTag(tag, ST_STARTSOUND);
347 swf_SetU16(tag, 24); //id
348 memset(&info, 0, sizeof(info));
350 swf_SetSoundInfo(tag, &info);
351 tag = swf_InsertTag(tag, ST_SHOWFRAME);
353 ActionTAG*action = 0;
354 tag = swf_InsertTag(tag, ST_DOACTION);
355 action = action_Stop(action);
356 action = action_End(action);
357 swf_ActionSet(tag, action);
358 swf_ActionFree(action);
359 tag = swf_InsertTag(tag, ST_SHOWFRAME);
361 tag = swf_InsertTag(tag, ST_END);
365 if FAILED(swf_WriteCGI(&swf)) fprintf(stderr,"WriteCGI() failed.\n");
367 f = open(outputname,O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644);
368 if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");