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 static struct options_t options
[] = {
53 static int definesound
= 0;
54 static int framerate
= 0;
55 static int samplerate
= 11025;
56 static int bitrate
= 32;
57 static int do_cgi
= 0;
59 static int mp3_bitrates
[] =
60 { 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0};
62 int args_callback_option(char*name
,char*val
)
64 if(!strcmp(name
, "V")) {
65 printf("wav2swf - part of %s %s\n", PACKAGE
, VERSION
);
68 else if(!strcmp(name
, "o")) {
72 else if(!strcmp(name
, "d")) {
76 else if(!strcmp(name
, "l")) {
81 else if(!strcmp(name
, "v")) {
85 else if(!strcmp(name
, "S")) {
89 else if(!strcmp(name
, "C")) {
93 else if(!strcmp(name
, "r")) {
95 sscanf(val
, "%f", &f
);
99 else if(!strcmp(name
, "s")) {
100 samplerate
= atoi(val
);
101 if(samplerate
> 5000 && samplerate
< 6000)
103 else if(samplerate
> 11000 && samplerate
< 12000)
105 else if(samplerate
> 22000 && samplerate
< 23000)
107 else if(samplerate
> 44000 && samplerate
< 45000)
110 fprintf(stderr
, "Invalid samplerate: %d\n", samplerate
);
111 fprintf(stderr
, "Allowed values: 11025, 22050, 44100\n", samplerate
);
116 else if(!strcmp(name
, "b")) {
120 fprintf(stderr
, "Not a valid bitrate: %s\n", val
);
124 fprintf(stderr
, "Bitrate must be <144. (%s)\n", val
);
127 for(t
=0;mp3_bitrates
[t
];t
++) {
128 if(b
== mp3_bitrates
[t
]) {
133 fprintf(stderr
, "Invalid bitrate. Allowed bitrates are:\n");
134 for(t
=0;mp3_bitrates
[t
];t
++) {
135 printf("%d ", mp3_bitrates
[t
]);
141 printf("Unknown option: -%s\n", name
);
146 int args_callback_longoption(char*name
,char*val
)
148 return args_long2shortoption(options
, name
, val
);
150 void args_callback_usage(char *name
)
153 printf("Usage: %s [-o filename] file.wav\n", name
);
155 printf("-h , --help Print short help message and exit\n");
156 printf("-V , --version Print version info and exit\n");
157 printf("-o , --output <filename> Explicitly specify output file. (Otherwise, output will go to output.swf)\n");
158 printf("-r , --framerate <fps> Set file framerate to <fps> frames per second.\n");
159 printf("-s , --samplerate <sps> Set samplerate to <sps> frames per second (default: 11025).\n");
160 printf("-b , --bitrate bps Set mp3 bitrate to <bps>.\n");
161 printf("-d , --definesound Generate a DefineSound tag instead of streaming sound.\n");
162 printf("-l , --loop n (Only used with -d)\n");
163 printf("-C , --cgi For use as CGI- prepend http header, write to stdout.\n");
164 printf("-S , --stop Stop the movie at frame 0\n");
165 printf("-b , --bitrate <bps> Set mp3 bitrate to <bps> (default: 32)\n");
166 printf("-v , --verbose Be more verbose\n");
169 int args_callback_command(char*name
,char*val
)
172 fprintf(stderr
, "Only one file allowed. You supplied at least two. (%s and %s)\n",
179 extern int swf_mp3_bitrate
;
180 extern int swf_mp3_out_samplerate
;
181 extern int swf_mp3_in_samplerate
;
183 int main (int argc
,char ** argv
)
188 S32 width
=300,height
= 300;
196 float blockspersecond
;
197 float framespersecond
;
198 float samplesperframe
;
199 float framesperblock
;
200 float samplesperblock
;
204 processargs(argc
, argv
);
206 blocksize
= (samplerate
> 22050) ? 1152 : 576;
208 blockspersecond
= (float)samplerate
/blocksize
;
210 framespersecond
= blockspersecond
;
212 framespersecond
= framerate
/256.0;
214 framesperblock
= framespersecond
/ blockspersecond
;
215 samplesperframe
= (blocksize
* blockspersecond
) / framespersecond
;
216 samplesperblock
= samplesperframe
* framesperblock
;
218 initLog(0,-1,0,0,-1,verbose
);
221 msg("<fatal> You must supply a filename");
225 if(!readWAV(filename
, &wav
))
227 msg("<fatal> Error reading %s", filename
);
230 convertWAV2mono(&wav
,&wav2
, samplerate
);
231 //printWAVInfo(&wav);
232 //printWAVInfo(&wav2);
233 samples
= (U16
*)wav2
.data
;
234 numsamples
= wav2
.size
/2;
236 if(numsamples
%blocksize
!= 0)
238 // apply padding, so that block is a multiple of blocksize
239 int numblocks
= (numsamples
+blocksize
-1)/blocksize
;
242 numsamples2
= numblocks
* blocksize
;
243 samples2
= malloc(sizeof(U16
)*numsamples2
);
244 memcpy(samples2
, samples
, numsamples
*sizeof(U16
));
245 memset(&samples2
[numsamples
], 0, sizeof(U16
)*(numsamples2
- numsamples
));
246 numsamples
= numsamples2
;
250 memset(&swf
,0x00,sizeof(SWF
));
253 swf
.frameRate
= (int)(framespersecond
*256);
255 swf
.movieSize
.xmax
= 20*width
;
256 swf
.movieSize
.ymax
= 20*height
;
258 swf
.firstTag
= swf_InsertTag(NULL
,ST_SETBACKGROUNDCOLOR
);
263 swf_SetRGB(tag
,&rgb
);
266 ActionTAG
*action
= 0;
267 tag
= swf_InsertTag(tag
, ST_DOACTION
);
268 action
= action_Stop(action
);
269 action
= action_End(action
);
270 swf_ActionSet(tag
, action
);
271 swf_ActionFree(action
);
273 tag
= swf_InsertTag(tag
, ST_SHOWFRAME
);
276 swf_mp3_bitrate
= bitrate
;
277 swf_mp3_out_samplerate
= samplerate
;
278 swf_mp3_in_samplerate
= samplerate
;
282 int oldframepos
=-1, newframepos
=0;
283 float framesamplepos
= 0;
288 tag
= swf_InsertTag(tag
, ST_SOUNDSTREAMHEAD
);
289 swf_SetSoundStreamHead(tag
, samplesperframe
);
290 msg("<notice> %d blocks", numsamples
/blocksize
);
291 for(t
=0;t
<numsamples
/blocksize
;t
++) {
294 int seek
= blocksize
- ((int)samplepos
- (int)framesamplepos
);
296 if(newframepos
!=oldframepos
) {
297 tag
= swf_InsertTag(tag
, ST_SOUNDSTREAMBLOCK
);
298 msg("<notice> Starting block %d %d+%d", t
, (int)samplepos
, (int)blocksize
);
299 block1
= &samples
[t
*blocksize
];
300 swf_SetSoundStreamBlock(tag
, block1
, seek
, 1);
301 v1
= v2
= GET16(tag
->data
);
303 msg("<notice> Adding data...", t
);
304 block1
= &samples
[t
*blocksize
];
305 swf_SetSoundStreamBlock(tag
, block1
, seek
, 0);
307 PUT16(tag
->data
, v1
);
309 samplepos
+= blocksize
;
311 oldframepos
= (int)framepos
;
312 framepos
+= framesperblock
;
313 newframepos
= (int)framepos
;
315 for(s
=oldframepos
;s
<newframepos
;s
++) {
316 tag
= swf_InsertTag(tag
, ST_SHOWFRAME
);
317 framesamplepos
+= samplesperframe
;
320 tag
= swf_InsertTag(tag
, ST_END
);
323 tag
= swf_InsertTag(tag
, ST_DEFINESOUND
);
324 swf_SetU16(tag
, 24); //id
325 #ifdef DEFINESOUND_MP3
326 swf_SetSoundDefine(tag
, samples
, numsamples
);
328 swf_SetU8(tag
,(/*compression*/0<<4)|(/*rate*/3<<2)|(/*size*/1<<1)|/*mono*/0);
329 swf_SetU32(tag
, numsamples
); // 44100 -> 11025
330 swf_SetBlock(tag
, samples
, numsamples
*2);
334 tag
= swf_InsertTag(tag
, ST_STARTSOUND
);
335 swf_SetU16(tag
, 24); //id
336 memset(&info
, 0, sizeof(info
));
338 swf_SetSoundInfo(tag
, &info
);
339 tag
= swf_InsertTag(tag
, ST_SHOWFRAME
);
340 tag
= swf_InsertTag(tag
, ST_END
);
344 if FAILED(swf_WriteCGI(&swf
)) fprintf(stderr
,"WriteCGI() failed.\n");
346 f
= open(outputname
,O_WRONLY
|O_CREAT
|O_TRUNC
|O_BINARY
, 0644);
347 if FAILED(swf_WriteSWF(f
,&swf
)) fprintf(stderr
,"WriteSWF() failed.\n");