bugfixes.
[swftools.git] / src / wav2swf.c
1 /* swfextract.c
2    Allows to extract parts of the swf into a new file.
3
4    Part of the swftools package.
5
6    Copyright (c) 2001 Matthias Kramm <kramm@quiss.org>
7
8    This file is distributed under the GPL, see file COPYING for details */
9
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include "../lib/rfxswf.h"
13 #include "../lib/log.h"
14 #include "../lib/args.h"
15 #include "wav.h"
16
17 char * filename = 0;
18 char * outputname = "output.swf";
19 int verbose = 2;
20
21 struct options_t options[] =
22 {
23  {"o","output"},
24  {"v","verbose"},
25  {"d","definesound"},
26  {"l","loop"},
27  {"r","framerate"},
28  {"V","version"},
29  {0,0}
30 };
31
32 static int loop = 0;
33 static int definesound = 0;
34 static int framerate = 0;
35
36 int args_callback_option(char*name,char*val)
37 {
38     if(!strcmp(name, "V")) {
39         printf("wav2swf - part of %s %s\n", PACKAGE, VERSION);
40         exit(0);
41     }
42     else if(!strcmp(name, "o")) {
43         outputname = val;
44         return 1;
45     }
46     else if(!strcmp(name, "d")) {
47         definesound = 1;
48         return 0;
49     }
50     else if(!strcmp(name, "l")) {
51         loop = atoi(val);
52         return 1;
53     }
54     else if(!strcmp(name, "v")) {
55         verbose ++;
56         return 0;
57     }
58     else if(!strcmp(name, "r")) {
59         float f;
60         sscanf(val, "%f", &f);
61         framerate = f*256;
62         return 1;
63     }
64     else {
65         printf("Unknown option: -%s\n", name);
66         exit(1);
67     }
68     return 0;
69 }
70 int args_callback_longoption(char*name,char*val)
71 {
72     return args_long2shortoption(options, name, val);
73 }
74 void args_callback_usage(char*name)
75 {
76     printf("Usage: %s [-o filename] file.wav\n", name);
77     printf("\t-v , --verbose\t\t\t Be more verbose\n");
78     printf("\t-d , --definesound\t\t\t Generate a DefineSound tag instead of streaming sound\n");
79     printf("\t-l , --loop n\t\t\t Loop sound n times (implies -d)\n");
80     printf("\t-r , --framerate fps\t\t\t Set framerate to fps frames per seond\n");
81     printf("\t-o , --output filename\t\t set output filename (default: output.swf)\n");
82     printf("\t-V , --version\t\t\t Print program version and exit\n");
83 }
84 int args_callback_command(char*name,char*val)
85 {
86     if(filename) {
87         fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
88                  filename, name);
89     }
90     filename = name;
91     return 0;
92 }
93
94 int main (int argc,char ** argv)
95
96     SWF swf;
97     RGBA rgb;
98     SRECT r;
99     S32 width=300,height = 300;
100     TAG * tag;
101
102     int f,i,ls1,fs1;
103     int count;
104     int t;
105     struct WAV wav,wav2;
106     int blocksize;
107     float blockspersecond;
108     float framespersecond;
109     float framesperblock;
110     float framepos = 0;
111     U16* samples;
112     int numsamples;
113
114     processargs(argc, argv);
115
116     if(!definesound && framerate) {
117         printf("Warning! The -r option is experimental and won't work without -d\n");
118     }
119
120     blocksize = 1152;
121     blockspersecond = 11025.0/blocksize;
122     framespersecond = blockspersecond;
123     if(framerate)
124         framespersecond = framerate/256.0;
125     framesperblock = framespersecond/blockspersecond;
126
127     initLog(0,-1,0,0,-1,verbose);
128
129     if(!readWAV(filename, &wav))
130     {
131         logf("<fatal> Error reading %s", filename);
132         exit(1);
133     }
134     convertWAV2mono(&wav,&wav2, 44100);
135     //printWAVInfo(&wav);
136     //printWAVInfo(&wav2);
137     samples = (U16*)wav2.data;
138     numsamples = wav2.size/2;
139
140     memset(&swf,0x00,sizeof(SWF));
141
142     swf.fileVersion    = 5;
143     swf.frameRate      = (int)(framespersecond*256);
144
145     swf.movieSize.xmax = 20*width;
146     swf.movieSize.ymax = 20*height;
147
148     swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
149     tag = swf.firstTag;
150     rgb.r = 0xff;
151     rgb.g = 0xff;
152     rgb.b = 0xff;
153     swf_SetRGB(tag,&rgb);
154
155     if(!definesound)
156     {
157         tag = swf_InsertTag(tag, ST_SOUNDSTREAMHEAD);
158         swf_SetSoundStreamHead(tag, blocksize);
159
160         logf("<notice> %d blocks", numsamples/(blocksize*2));
161         for(t=0;t<numsamples/(blocksize*2);t++) {
162             int s;
163             int oldframe, newframe;
164             U16*block1;
165             tag = swf_InsertTag(tag, ST_SOUNDSTREAMBLOCK);
166             logf("<notice> Writing block %d", t);
167             block1 = &samples[t*2*blocksize];
168             swf_SetSoundStreamBlock(tag, block1, 1);
169
170             oldframe = (int)framepos;
171             framepos += framesperblock;
172             newframe = (int)framepos;
173             for(s=oldframe;s<newframe;s++)
174                 tag = swf_InsertTag(tag, ST_SHOWFRAME);
175         }
176         tag = swf_InsertTag(tag, ST_END);
177     } else {
178         SOUNDINFO info;
179         tag = swf_InsertTag(tag, ST_DEFINESOUND);
180         swf_SetU16(tag, 24); //id
181         swf_SetSoundDefine(tag, samples, numsamples);
182         tag = swf_InsertTag(tag, ST_STARTSOUND);
183         swf_SetU16(tag, 24); //id
184         memset(&info, 0, sizeof(info));
185         info.loops = loop;
186         swf_SetSoundInfo(tag, &info);
187         tag = swf_InsertTag(tag, ST_SHOWFRAME);
188         tag = swf_InsertTag(tag, ST_END);
189     }
190
191     f = open(outputname,O_WRONLY|O_CREAT|O_TRUNC, 0644);
192     if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");
193     close(f);
194
195     swf_FreeTags(&swf);
196     return 0;
197 }
198
199