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