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);
194 blockspersecond = (float)samplerate/blocksize;
196 framespersecond = blockspersecond;
198 framespersecond = framerate/256.0;
200 framesperblock = framespersecond / blockspersecond;
201 samplesperframe = (blocksize * blockspersecond) / framespersecond;
202 samplesperblock = samplesperframe * framesperblock;
204 initLog(0,-1,0,0,-1,verbose);
207 msg("<fatal> You must supply a filename");
211 if(!readWAV(filename, &wav))
213 msg("<fatal> Error reading %s", filename);
216 convertWAV2mono(&wav,&wav2, samplerate);
217 //printWAVInfo(&wav);
218 //printWAVInfo(&wav2);
219 samples = (U16*)wav2.data;
220 numsamples = wav2.size/2;
222 if(numsamples%blocksize != 0)
224 // apply padding, so that block is a multiple of blocksize
225 int numblocks = (numsamples+blocksize-1)/blocksize;
228 numsamples2 = numblocks * blocksize;
229 samples2 = malloc(sizeof(U16)*numsamples2);
230 memcpy(samples2, samples, numsamples*sizeof(U16));
231 memset(&samples2[numsamples], 0, sizeof(U16)*(numsamples2 - numsamples));
232 numsamples = numsamples2;
236 memset(&swf,0x00,sizeof(SWF));
239 swf.frameRate = (int)(framespersecond*256);
241 swf.movieSize.xmax = 20*width;
242 swf.movieSize.ymax = 20*height;
244 swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
249 swf_SetRGB(tag,&rgb);
251 swf_mp3_bitrate = bitrate;
252 swf_mp3_out_samplerate = samplerate;
253 swf_mp3_in_samplerate = samplerate;
257 int oldframepos=-1, newframepos=0;
258 float framesamplepos = 0;
263 tag = swf_InsertTag(tag, ST_SOUNDSTREAMHEAD);
264 swf_SetSoundStreamHead(tag, samplesperframe);
265 msg("<notice> %d blocks", numsamples/blocksize);
266 for(t=0;t<numsamples/blocksize;t++) {
269 int seek = blocksize - ((int)samplepos - (int)framesamplepos);
271 if(newframepos!=oldframepos) {
272 tag = swf_InsertTag(tag, ST_SOUNDSTREAMBLOCK);
273 msg("<notice> Starting block %d %d+%d", t, (int)samplepos, (int)blocksize);
274 block1 = &samples[t*blocksize];
275 swf_SetSoundStreamBlock(tag, block1, seek, 1);
276 v1 = v2 = GET16(tag->data);
278 msg("<notice> Adding data...", t);
279 block1 = &samples[t*blocksize];
280 swf_SetSoundStreamBlock(tag, block1, seek, 0);
282 PUT16(tag->data, v1);
284 samplepos += blocksize;
286 oldframepos = (int)framepos;
287 framepos += framesperblock;
288 newframepos = (int)framepos;
290 for(s=oldframepos;s<newframepos;s++) {
291 tag = swf_InsertTag(tag, ST_SHOWFRAME);
292 framesamplepos += samplesperframe;
295 tag = swf_InsertTag(tag, ST_END);
298 tag = swf_InsertTag(tag, ST_DEFINESOUND);
299 swf_SetU16(tag, 24); //id
300 #ifdef DEFINESOUND_MP3
301 swf_SetSoundDefine(tag, samples, numsamples);
303 swf_SetU8(tag,(/*compression*/0<<4)|(/*rate*/3<<2)|(/*size*/1<<1)|/*mono*/0);
304 swf_SetU32(tag, numsamples); // 44100 -> 11025
305 swf_SetBlock(tag, samples, numsamples*2);
309 tag = swf_InsertTag(tag, ST_STARTSOUND);
310 swf_SetU16(tag, 24); //id
311 memset(&info, 0, sizeof(info));
313 swf_SetSoundInfo(tag, &info);
314 tag = swf_InsertTag(tag, ST_SHOWFRAME);
315 tag = swf_InsertTag(tag, ST_END);
319 if FAILED(swf_WriteCGI(&swf)) fprintf(stderr,"WriteCGI() failed.\n");
321 f = open(outputname,O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644);
322 if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");