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";
33 #define DEFINESOUND_MP3 1 //define sound uses mp3?- undefine for raw sound.
35 struct options_t options[] =
50 static int definesound = 0;
51 static int framerate = 0;
52 static int samplerate = 11025;
53 static int bitrate = 32;
54 static int do_cgi = 0;
56 static int mp3_bitrates[] =
57 { 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0};
59 int args_callback_option(char*name,char*val)
61 if(!strcmp(name, "V")) {
62 printf("wav2swf - part of %s %s\n", PACKAGE, VERSION);
65 else if(!strcmp(name, "o")) {
69 else if(!strcmp(name, "d")) {
73 else if(!strcmp(name, "l")) {
78 else if(!strcmp(name, "v")) {
82 else if(!strcmp(name, "C")) {
86 else if(!strcmp(name, "r")) {
88 sscanf(val, "%f", &f);
92 else if(!strcmp(name, "s")) {
93 samplerate = atoi(val);
94 if(samplerate > 5000 && samplerate < 6000)
96 else if(samplerate > 11000 && samplerate < 12000)
98 else if(samplerate > 22000 && samplerate < 23000)
100 else if(samplerate > 44000 && samplerate < 45000)
103 fprintf(stderr, "Invalid samplerate: %d\n", samplerate);
104 fprintf(stderr, "Allowed values: 11025, 22050\n", samplerate);
109 else if(!strcmp(name, "b")) {
113 fprintf(stderr, "Not a valid bitrate: %s\n", val);
117 fprintf(stderr, "Bitrate must be <144. (%s)\n", val);
120 for(t=0;mp3_bitrates[t];t++) {
121 if(b== mp3_bitrates[t]) {
126 fprintf(stderr, "Invalid bitrate. Allowed bitrates are:\n");
127 for(t=0;mp3_bitrates[t];t++) {
128 printf("%d ", mp3_bitrates[t]);
134 printf("Unknown option: -%s\n", name);
139 int args_callback_longoption(char*name,char*val)
141 return args_long2shortoption(options, name, val);
143 void args_callback_usage(char*name)
145 printf("Usage: %s [-o filename] file.wav\n", name);
146 printf("\t-v , --verbose\t\t\t Be more verbose\n");
147 printf("\t-d , --definesound\t\t Generate a DefineSound tag instead of streaming sound\n");
148 printf("\t-l , --loop n\t\t\t Loop sound n times (implies -d)\n");
149 printf("\t-r , --framerate fps\t\t Set framerate to fps frames per second\n");
150 printf("\t-s , --samplerate sps\t\t Set samplerate to sps frames per second (default: 11025)\n");
151 printf("\t-b , --bitrate bps\t\t Set mp3 bitrate (default: 32)\n");
152 printf("\t-o , --output filename\t\t set output filename (default: output.swf)\n");
153 printf("\t-C , --cgi\t\t\t For use as CGI- prepend http header, write to stdout\n");
154 printf("\t-V , --version\t\t\t Print program version and exit\n");
156 int args_callback_command(char*name,char*val)
159 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
166 extern int swf_mp3_bitrate;
167 extern int swf_mp3_out_samplerate;
168 extern int swf_mp3_in_samplerate;
170 int main (int argc,char ** argv)
175 S32 width=300,height = 300;
183 float blockspersecond;
184 float framespersecond;
185 float samplesperframe;
186 float framesperblock;
187 float samplesperblock;
191 processargs(argc, argv);
193 blocksize = (samplerate > 22050) ? 1152 : 576;
195 blockspersecond = (float)samplerate/blocksize;
197 framespersecond = blockspersecond;
199 framespersecond = framerate/256.0;
201 framesperblock = framespersecond / blockspersecond;
202 samplesperframe = (blocksize * blockspersecond) / framespersecond;
203 samplesperblock = samplesperframe * framesperblock;
205 initLog(0,-1,0,0,-1,verbose);
208 msg("<fatal> You must supply a filename");
212 if(!readWAV(filename, &wav))
214 msg("<fatal> Error reading %s", filename);
217 convertWAV2mono(&wav,&wav2, samplerate);
218 //printWAVInfo(&wav);
219 //printWAVInfo(&wav2);
220 samples = (U16*)wav2.data;
221 numsamples = wav2.size/2;
223 if(numsamples%blocksize != 0)
225 // apply padding, so that block is a multiple of blocksize
226 int numblocks = (numsamples+blocksize-1)/blocksize;
229 numsamples2 = numblocks * blocksize;
230 samples2 = malloc(sizeof(U16)*numsamples2);
231 memcpy(samples2, samples, numsamples*sizeof(U16));
232 memset(&samples2[numsamples], 0, sizeof(U16)*(numsamples2 - numsamples));
233 numsamples = numsamples2;
237 memset(&swf,0x00,sizeof(SWF));
240 swf.frameRate = (int)(framespersecond*256);
242 swf.movieSize.xmax = 20*width;
243 swf.movieSize.ymax = 20*height;
245 swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
250 swf_SetRGB(tag,&rgb);
252 swf_mp3_bitrate = bitrate;
253 swf_mp3_out_samplerate = samplerate;
254 swf_mp3_in_samplerate = samplerate;
258 int oldframepos=-1, newframepos=0;
259 float framesamplepos = 0;
264 tag = swf_InsertTag(tag, ST_SOUNDSTREAMHEAD);
265 swf_SetSoundStreamHead(tag, samplesperframe);
266 msg("<notice> %d blocks", numsamples/blocksize);
267 for(t=0;t<numsamples/blocksize;t++) {
270 int seek = blocksize - ((int)samplepos - (int)framesamplepos);
272 if(newframepos!=oldframepos) {
273 tag = swf_InsertTag(tag, ST_SOUNDSTREAMBLOCK);
274 msg("<notice> Starting block %d %d+%d", t, (int)samplepos, (int)blocksize);
275 block1 = &samples[t*blocksize];
276 swf_SetSoundStreamBlock(tag, block1, seek, 1);
277 v1 = v2 = GET16(tag->data);
279 msg("<notice> Adding data...", t);
280 block1 = &samples[t*blocksize];
281 swf_SetSoundStreamBlock(tag, block1, seek, 0);
283 PUT16(tag->data, v1);
285 samplepos += blocksize;
287 oldframepos = (int)framepos;
288 framepos += framesperblock;
289 newframepos = (int)framepos;
291 for(s=oldframepos;s<newframepos;s++) {
292 tag = swf_InsertTag(tag, ST_SHOWFRAME);
293 framesamplepos += samplesperframe;
296 tag = swf_InsertTag(tag, ST_END);
299 tag = swf_InsertTag(tag, ST_DEFINESOUND);
300 swf_SetU16(tag, 24); //id
301 #ifdef DEFINESOUND_MP3
302 swf_SetSoundDefine(tag, samples, numsamples);
304 swf_SetU8(tag,(/*compression*/0<<4)|(/*rate*/3<<2)|(/*size*/1<<1)|/*mono*/0);
305 swf_SetU32(tag, numsamples); // 44100 -> 11025
306 swf_SetBlock(tag, samples, numsamples*2);
310 tag = swf_InsertTag(tag, ST_STARTSOUND);
311 swf_SetU16(tag, 24); //id
312 memset(&info, 0, sizeof(info));
314 swf_SetSoundInfo(tag, &info);
315 tag = swf_InsertTag(tag, ST_SHOWFRAME);
316 tag = swf_InsertTag(tag, ST_END);
320 if FAILED(swf_WriteCGI(&swf)) fprintf(stderr,"WriteCGI() failed.\n");
322 f = open(outputname,O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644);
323 if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");