removed duplicate bitrate option.
[swftools.git] / src / wav2swf.c
1 /* wav2swf.c
2    Converts WAV/WAVE files to SWF.
3
4    Part of the swftools package.
5
6    Copyright (c) 2001 Matthias Kramm <kramm@quiss.org>
7  
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.
12
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.
17
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 */
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include "../lib/rfxswf.h"
25 #include "../lib/log.h"
26 #include "../lib/args.h"
27 #include "wav.h"
28
29 char * filename = 0;
30 char * outputname = "output.swf";
31 int verbose = 2;
32 int stopframe0 = 0;
33 int stopframe1 = 0;
34
35 #define DEFINESOUND_MP3 1 //define sound uses mp3?- undefine for raw sound.
36
37 static struct options_t options[] = {
38 {"h", "help"},
39 {"V", "version"},
40 {"o", "output"},
41 {"r", "framerate"},
42 {"s", "samplerate"},
43 {"d", "definesound"},
44 {"l", "loop"},
45 {"C", "cgi"},
46 {"S", "stop"},
47 {"E", "end"},
48 {"b", "bitrate"},
49 {"v", "verbose"},
50 {0,0}
51 };
52
53 static int loop = 0;
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;
59
60 static int mp3_bitrates[] =
61 { 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0};
62
63 int args_callback_option(char*name,char*val)
64 {
65     if(!strcmp(name, "V")) {
66         printf("wav2swf - part of %s %s\n", PACKAGE, VERSION);
67         exit(0);
68     }
69     else if(!strcmp(name, "o")) {
70         outputname = val;
71         return 1;
72     }
73     else if(!strcmp(name, "d")) {
74         definesound = 1;
75         return 0;
76     }
77     else if(!strcmp(name, "l")) {
78         loop = atoi(val);
79         definesound = 1;
80         return 1;
81     }
82     else if(!strcmp(name, "v")) {
83         verbose ++;
84         return 0;
85     }
86     else if(!strcmp(name, "S")) {
87         stopframe0 = 1;
88         return 0;
89     }
90     else if(!strcmp(name, "E")) {
91         stopframe1 = 1;
92         return 0;
93     }
94     else if(!strcmp(name, "C")) {
95         do_cgi = 1;
96         return 0;
97     }
98     else if(!strcmp(name, "r")) {
99         float f;
100         sscanf(val, "%f", &f);
101         framerate = f*256;
102         return 1;
103     }
104     else if(!strcmp(name, "s")) {
105         samplerate = atoi(val);
106         if(samplerate > 5000 && samplerate < 6000)
107             samplerate = 5512;
108         else if(samplerate > 11000 && samplerate < 12000)
109             samplerate = 11025;
110         else if(samplerate > 22000 && samplerate < 23000)
111             samplerate = 22050;
112         else if(samplerate > 44000 && samplerate < 45000)
113             samplerate = 44100;
114         else {
115             fprintf(stderr, "Invalid samplerate: %d\n", samplerate);
116             fprintf(stderr, "Allowed values: 11025, 22050, 44100\n", samplerate);
117             exit(1);
118         }
119         return 1;
120     }
121     else if(!strcmp(name, "b")) {
122         int t;
123         int b = atoi(val);
124         if(b<=0) {
125             fprintf(stderr, "Not a valid bitrate: %s\n", val);
126             exit(1);
127         }
128         if(b>160) {
129             fprintf(stderr, "Bitrate must be <144. (%s)\n", val);
130             exit(1);
131         }
132         for(t=0;mp3_bitrates[t];t++) {
133             if(b== mp3_bitrates[t]) {
134                 bitrate = b;
135                 return 1;
136             }
137         }
138         fprintf(stderr, "Invalid bitrate. Allowed bitrates are:\n");
139         for(t=0;mp3_bitrates[t];t++) {
140             printf("%d ", mp3_bitrates[t]);
141         }
142         printf("\n");
143         exit(1);
144     }
145     else {
146         printf("Unknown option: -%s\n", name);
147         exit(1);
148     }
149     return 0;
150 }
151 int args_callback_longoption(char*name,char*val)
152 {
153     return args_long2shortoption(options, name, val);
154 }
155 void args_callback_usage(char *name)
156 {
157     printf("\n");
158     printf("Usage: %s [-o filename] file.wav\n", name);
159     printf("\n");
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");
172     printf("\n");
173 }
174 int args_callback_command(char*name,char*val)
175 {
176     if(filename) {
177         fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
178                  filename, name);
179     }
180     filename = name;
181     return 0;
182 }
183
184 extern int swf_mp3_bitrate;
185 extern int swf_mp3_out_samplerate;
186 extern int swf_mp3_in_samplerate;
187
188 int main (int argc,char ** argv)
189
190     SWF swf;
191     RGBA rgb;
192     SRECT r;
193     S32 width=300,height = 300;
194     TAG * tag;
195
196     int f,i,ls1,fs1;
197     int count;
198     int t;
199     struct WAV wav,wav2;
200     int blocksize;
201     float blockspersecond;
202     float framespersecond;
203     float samplesperframe;
204     float framesperblock;
205     float samplesperblock;
206     U16* samples;
207     int numsamples;
208
209     processargs(argc, argv);
210
211     blocksize = (samplerate > 22050) ? 1152 : 576;
212
213     blockspersecond = (float)samplerate/blocksize;
214
215     framespersecond = blockspersecond;
216     if(framerate)
217         framespersecond = framerate/256.0;
218
219     framesperblock = framespersecond / blockspersecond;
220     samplesperframe = (blocksize * blockspersecond) / framespersecond;
221     samplesperblock = samplesperframe * framesperblock;
222     
223     initLog(0,-1,0,0,-1,verbose);
224
225     if(!filename) {
226         msg("<fatal> You must supply a filename");
227         exit(1);
228     }
229
230     if(!readWAV(filename, &wav))
231     {
232         msg("<fatal> Error reading %s", filename);
233         exit(1);
234     }
235     convertWAV2mono(&wav,&wav2, samplerate);
236     //printWAVInfo(&wav);
237     //printWAVInfo(&wav2);
238     samples = (U16*)wav2.data;
239     numsamples = wav2.size/2;
240
241     if(numsamples%blocksize != 0)
242     {
243         // apply padding, so that block is a multiple of blocksize
244         int numblocks = (numsamples+blocksize-1)/blocksize;
245         int numsamples2;
246         U16* samples2;
247         numsamples2 = numblocks * blocksize;
248         samples2 = malloc(sizeof(U16)*numsamples2);
249         memcpy(samples2, samples, numsamples*sizeof(U16));
250         memset(&samples2[numsamples], 0, sizeof(U16)*(numsamples2 - numsamples));
251         numsamples = numsamples2;
252         samples = samples2;
253     }
254
255     memset(&swf,0x00,sizeof(SWF));
256
257     swf.fileVersion    = 5;
258     swf.frameRate      = (int)(framespersecond*256);
259
260     swf.movieSize.xmax = 20*width;
261     swf.movieSize.ymax = 20*height;
262
263     swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
264     tag = swf.firstTag;
265     rgb.r = 0xff;
266     rgb.g = 0xff;
267     rgb.b = 0xff;
268     swf_SetRGB(tag,&rgb);
269
270     if(stopframe0) {
271         ActionTAG*action = 0;
272         tag = swf_InsertTag(tag, ST_DOACTION);
273         action = action_Stop(action);
274         action = action_End(action);
275         swf_ActionSet(tag, action);
276         swf_ActionFree(action);
277
278         tag = swf_InsertTag(tag, ST_SHOWFRAME);
279     }
280         
281     swf_mp3_bitrate = bitrate;
282     swf_mp3_out_samplerate = samplerate;
283     swf_mp3_in_samplerate = samplerate;
284
285     if(!definesound)
286     {
287         int oldframepos=-1, newframepos=0;
288         float framesamplepos = 0;
289         float framepos = 0;
290         float samplepos = 0;
291         ActionTAG* a = 0;
292         U16 v1=0,v2=0;
293         tag = swf_InsertTag(tag, ST_SOUNDSTREAMHEAD);
294         swf_SetSoundStreamHead(tag, samplesperframe);
295         msg("<notice> %d blocks", numsamples/blocksize);
296         for(t=0;t<numsamples/blocksize;t++) {
297             int s;
298             U16*block1;
299             int seek = blocksize - ((int)samplepos - (int)framesamplepos);
300
301             if(newframepos!=oldframepos) {
302                 tag = swf_InsertTag(tag, ST_SOUNDSTREAMBLOCK);
303                 msg("<notice> Starting block %d %d+%d", t, (int)samplepos, (int)blocksize);
304                 block1 = &samples[t*blocksize];
305                 swf_SetSoundStreamBlock(tag, block1, seek, 1);
306                 v1 = v2 = GET16(tag->data);
307             } else {
308                 msg("<notice> Adding data...", t);
309                 block1 = &samples[t*blocksize];
310                 swf_SetSoundStreamBlock(tag, block1, seek, 0);
311                 v1+=v2;
312                 PUT16(tag->data, v1);
313             }
314             samplepos += blocksize;
315
316             oldframepos = (int)framepos;
317             framepos += framesperblock;
318             newframepos = (int)framepos;
319
320             for(s=oldframepos;s<newframepos;s++) {
321                 tag = swf_InsertTag(tag, ST_SHOWFRAME);
322                 framesamplepos += samplesperframe;
323             }
324         }
325         tag = swf_InsertTag(tag, ST_END);
326     } else {
327         SOUNDINFO info;
328         tag = swf_InsertTag(tag, ST_DEFINESOUND);
329         swf_SetU16(tag, 24); //id
330 #ifdef DEFINESOUND_MP3
331         swf_SetSoundDefine(tag, samples, numsamples);
332 #else
333         swf_SetU8(tag,(/*compression*/0<<4)|(/*rate*/3<<2)|(/*size*/1<<1)|/*mono*/0);
334         swf_SetU32(tag, numsamples); // 44100 -> 11025
335         swf_SetBlock(tag, samples, numsamples*2);
336 #endif
337
338
339         tag = swf_InsertTag(tag, ST_STARTSOUND);
340         swf_SetU16(tag, 24); //id
341         memset(&info, 0, sizeof(info));
342         info.loops = loop;
343         swf_SetSoundInfo(tag, &info);
344         tag = swf_InsertTag(tag, ST_SHOWFRAME);
345         if(stopframe1) {
346             ActionTAG*action = 0;
347             tag = swf_InsertTag(tag, ST_DOACTION);
348             action = action_Stop(action);
349             action = action_End(action);
350             swf_ActionSet(tag, action);
351             swf_ActionFree(action);
352             tag = swf_InsertTag(tag, ST_SHOWFRAME);
353         }
354         tag = swf_InsertTag(tag, ST_END);
355     }
356
357     if(do_cgi) {
358         if FAILED(swf_WriteCGI(&swf)) fprintf(stderr,"WriteCGI() failed.\n");
359     } else {
360         f = open(outputname,O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644);
361         if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");
362         close(f);
363     }
364
365     swf_FreeTags(&swf);
366     return 0;
367 }
368
369