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