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