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";
35 #define DEFINESOUND_MP3 1 //define sound uses mp3?- undefine for raw sound.
37 static struct options_t options[] = {
55 static int definesound = 0;
56 static int framerate = 0;
57 static int samplerate = 11025;
58 static int bitrate = 32;
59 static int do_cgi = 0;
61 static int mp3_bitrates[] =
62 { 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0};
64 int args_callback_option(char*name,char*val)
66 if(!strcmp(name, "V")) {
67 printf("wav2swf - part of %s %s\n", PACKAGE, VERSION);
70 else if(!strcmp(name, "o")) {
74 else if(!strcmp(name, "d")) {
78 else if(!strcmp(name, "l")) {
83 else if(!strcmp(name, "v")) {
87 else if(!strcmp(name, "S")) {
91 else if(!strcmp(name, "E")) {
95 else if(!strcmp(name, "C")) {
99 else if(!strcmp(name, "r")) {
101 sscanf(val, "%f", &f);
105 else if(!strcmp(name, "s")) {
106 samplerate = atoi(val);
107 if(samplerate > 5000 && samplerate < 6000)
109 else if(samplerate > 11000 && samplerate < 12000)
111 else if(samplerate > 22000 && samplerate < 23000)
113 else if(samplerate > 44000 && samplerate < 45000)
116 fprintf(stderr, "Invalid samplerate: %d\n", samplerate);
117 fprintf(stderr, "Allowed values: 11025, 22050, 44100\n", samplerate);
122 else if(!strcmp(name, "b")) {
126 fprintf(stderr, "Not a valid bitrate: %s\n", val);
130 fprintf(stderr, "Bitrate must be <144. (%s)\n", val);
133 for(t=0;mp3_bitrates[t];t++) {
134 if(b== mp3_bitrates[t]) {
139 fprintf(stderr, "Invalid bitrate. Allowed bitrates are:\n");
140 for(t=0;mp3_bitrates[t];t++) {
141 printf("%d ", mp3_bitrates[t]);
147 printf("Unknown option: -%s\n", name);
152 int args_callback_longoption(char*name,char*val)
154 return args_long2shortoption(options, name, val);
156 void args_callback_usage(char *name)
159 printf("Usage: %s [-o filename] file.wav\n", name);
161 printf("-h , --help Print short help message and exit\n");
162 printf("-V , --version Print version info and exit\n");
163 printf("-o , --output <filename> Explicitly specify output file. (Otherwise, output will go to output.swf)\n");
164 printf("-r , --framerate <fps> Set file framerate to <fps> frames per second.\n");
165 printf("-s , --samplerate <sps> Set samplerate to <sps> frames per second (default: 11025).\n");
166 printf("-b , --bitrate bps Set mp3 bitrate to <bps>.\n");
167 printf("-d , --definesound Generate a DefineSound tag instead of streaming sound.\n");
168 printf("-l , --loop n (Only used with -d)\n");
169 printf("-C , --cgi For use as CGI- prepend http header, write to stdout.\n");
170 printf("-S , --stop Stop the movie at frame 0\n");
171 printf("-E , --end Stop the movie at the end frame\n");
172 printf("-b , --bitrate <bps> Set mp3 bitrate to <bps> (default: 32)\n");
173 printf("-v , --verbose Be more verbose\n");
176 int args_callback_command(char*name,char*val)
179 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
186 extern int swf_mp3_bitrate;
187 extern int swf_mp3_out_samplerate;
188 extern int swf_mp3_in_samplerate;
190 int main (int argc,char ** argv)
195 S32 width=300,height = 300;
203 float blockspersecond;
204 float framespersecond;
205 float samplesperframe;
206 float framesperblock;
207 float samplesperblock;
211 processargs(argc, argv);
213 blocksize = (samplerate > 22050) ? 1152 : 576;
215 blockspersecond = (float)samplerate/blocksize;
217 framespersecond = blockspersecond;
219 framespersecond = framerate/256.0;
221 framesperblock = framespersecond / blockspersecond;
222 samplesperframe = (blocksize * blockspersecond) / framespersecond;
223 samplesperblock = samplesperframe * framesperblock;
225 initLog(0,-1,0,0,-1,verbose);
228 msg("<fatal> You must supply a filename");
232 if(!readWAV(filename, &wav))
234 msg("<fatal> Error reading %s", filename);
237 convertWAV2mono(&wav,&wav2, samplerate);
238 //printWAVInfo(&wav);
239 //printWAVInfo(&wav2);
240 samples = (U16*)wav2.data;
241 numsamples = wav2.size/2;
243 if(numsamples%blocksize != 0)
245 // apply padding, so that block is a multiple of blocksize
246 int numblocks = (numsamples+blocksize-1)/blocksize;
249 numsamples2 = numblocks * blocksize;
250 samples2 = malloc(sizeof(U16)*numsamples2);
251 memcpy(samples2, samples, numsamples*sizeof(U16));
252 memset(&samples2[numsamples], 0, sizeof(U16)*(numsamples2 - numsamples));
253 numsamples = numsamples2;
257 memset(&swf,0x00,sizeof(SWF));
260 swf.frameRate = (int)(framespersecond*256);
262 swf.movieSize.xmax = 20*width;
263 swf.movieSize.ymax = 20*height;
265 swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
270 swf_SetRGB(tag,&rgb);
273 ActionTAG*action = 0;
274 tag = swf_InsertTag(tag, ST_DOACTION);
275 action = action_Stop(action);
276 action = action_End(action);
277 swf_ActionSet(tag, action);
278 swf_ActionFree(action);
280 tag = swf_InsertTag(tag, ST_SHOWFRAME);
283 swf_mp3_bitrate = bitrate;
284 swf_mp3_out_samplerate = samplerate;
285 swf_mp3_in_samplerate = samplerate;
289 int oldframepos=-1, newframepos=0;
290 float framesamplepos = 0;
295 tag = swf_InsertTag(tag, ST_SOUNDSTREAMHEAD);
296 swf_SetSoundStreamHead(tag, samplesperframe);
297 msg("<notice> %d blocks", numsamples/blocksize);
298 for(t=0;t<numsamples/blocksize;t++) {
301 int seek = blocksize - ((int)samplepos - (int)framesamplepos);
303 if(newframepos!=oldframepos) {
304 tag = swf_InsertTag(tag, ST_SOUNDSTREAMBLOCK);
305 msg("<notice> Starting block %d %d+%d", t, (int)samplepos, (int)blocksize);
306 block1 = &samples[t*blocksize];
307 swf_SetSoundStreamBlock(tag, block1, seek, 1);
308 v1 = v2 = GET16(tag->data);
310 msg("<notice> Adding data...", t);
311 block1 = &samples[t*blocksize];
312 swf_SetSoundStreamBlock(tag, block1, seek, 0);
314 PUT16(tag->data, v1);
316 samplepos += blocksize;
318 oldframepos = (int)framepos;
319 framepos += framesperblock;
320 newframepos = (int)framepos;
322 for(s=oldframepos;s<newframepos;s++) {
323 tag = swf_InsertTag(tag, ST_SHOWFRAME);
324 framesamplepos += samplesperframe;
327 tag = swf_InsertTag(tag, ST_END);
330 tag = swf_InsertTag(tag, ST_DEFINESOUND);
331 swf_SetU16(tag, 24); //id
332 #ifdef DEFINESOUND_MP3
333 swf_SetSoundDefine(tag, samples, numsamples);
335 swf_SetU8(tag,(/*compression*/0<<4)|(/*rate*/3<<2)|(/*size*/1<<1)|/*mono*/0);
336 swf_SetU32(tag, numsamples); // 44100 -> 11025
337 swf_SetBlock(tag, samples, numsamples*2);
341 tag = swf_InsertTag(tag, ST_STARTSOUND);
342 swf_SetU16(tag, 24); //id
343 memset(&info, 0, sizeof(info));
345 swf_SetSoundInfo(tag, &info);
346 tag = swf_InsertTag(tag, ST_SHOWFRAME);
348 ActionTAG*action = 0;
349 tag = swf_InsertTag(tag, ST_DOACTION);
350 action = action_Stop(action);
351 action = action_End(action);
352 swf_ActionSet(tag, action);
353 swf_ActionFree(action);
354 tag = swf_InsertTag(tag, ST_SHOWFRAME);
356 tag = swf_InsertTag(tag, ST_END);
360 if FAILED(swf_WriteCGI(&swf)) fprintf(stderr,"WriteCGI() failed.\n");
362 f = open(outputname,O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644);
363 if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");