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