set seek value.
[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         definesound = 1;
53         return 1;
54     }
55     else if(!strcmp(name, "v")) {
56         verbose ++;
57         return 0;
58     }
59     else if(!strcmp(name, "r")) {
60         float f;
61         sscanf(val, "%f", &f);
62         framerate = f*256;
63         return 1;
64     }
65     else {
66         printf("Unknown option: -%s\n", name);
67         exit(1);
68     }
69     return 0;
70 }
71 int args_callback_longoption(char*name,char*val)
72 {
73     return args_long2shortoption(options, name, val);
74 }
75 void args_callback_usage(char*name)
76 {
77     printf("Usage: %s [-o filename] file.wav\n", name);
78     printf("\t-v , --verbose\t\t\t Be more verbose\n");
79     printf("\t-d , --definesound\t\t\t Generate a DefineSound tag instead of streaming sound\n");
80     printf("\t-l , --loop n\t\t\t Loop sound n times (implies -d)\n");
81     printf("\t-r , --framerate fps\t\t\t Set framerate to fps frames per seond\n");
82     printf("\t-o , --output filename\t\t set output filename (default: output.swf)\n");
83     printf("\t-V , --version\t\t\t Print program version and exit\n");
84 }
85 int args_callback_command(char*name,char*val)
86 {
87     if(filename) {
88         fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
89                  filename, name);
90     }
91     filename = name;
92     return 0;
93 }
94
95 int main (int argc,char ** argv)
96
97     SWF swf;
98     RGBA rgb;
99     SRECT r;
100     S32 width=300,height = 300;
101     TAG * tag;
102
103     int f,i,ls1,fs1;
104     int count;
105     int t;
106     struct WAV wav,wav2;
107     int blocksize;
108     float blockspersecond;
109     float framespersecond;
110     float framesperblock;
111     float framepos = 0;
112     U16* samples;
113     int numsamples;
114
115     processargs(argc, argv);
116
117     if(!definesound && framerate) {
118         printf("Warning! The -r option is experimental and won't work without -d\n");
119     }
120
121     blocksize = 1152;
122     blockspersecond = 11025.0/blocksize;
123     framespersecond = blockspersecond;
124     if(framerate)
125         framespersecond = framerate/256.0;
126     framesperblock = framespersecond/blockspersecond;
127
128     initLog(0,-1,0,0,-1,verbose);
129
130     if(!readWAV(filename, &wav))
131     {
132         logf("<fatal> Error reading %s", filename);
133         exit(1);
134     }
135     convertWAV2mono(&wav,&wav2, 44100);
136     //printWAVInfo(&wav);
137     //printWAVInfo(&wav2);
138     samples = (U16*)wav2.data;
139     numsamples = wav2.size/2;
140
141     memset(&swf,0x00,sizeof(SWF));
142
143     swf.fileVersion    = 5;
144     swf.frameRate      = (int)(framespersecond*256);
145
146     swf.movieSize.xmax = 20*width;
147     swf.movieSize.ymax = 20*height;
148
149     swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
150     tag = swf.firstTag;
151     rgb.r = 0xff;
152     rgb.g = 0xff;
153     rgb.b = 0xff;
154     swf_SetRGB(tag,&rgb);
155
156     if(!definesound)
157     {
158         tag = swf_InsertTag(tag, ST_SOUNDSTREAMHEAD);
159         swf_SetSoundStreamHead(tag, blocksize);
160
161         logf("<notice> %d blocks", numsamples/(blocksize*2));
162         for(t=0;t<numsamples/(blocksize*2);t++) {
163             int s;
164             int oldframe, newframe;
165             U16*block1;
166             tag = swf_InsertTag(tag, ST_SOUNDSTREAMBLOCK);
167             logf("<notice> Writing block %d", t);
168             block1 = &samples[t*2*blocksize];
169             swf_SetSoundStreamBlock(tag, block1, 0, 1);
170
171             oldframe = (int)framepos;
172             framepos += framesperblock;
173             newframe = (int)framepos;
174             for(s=oldframe;s<newframe;s++)
175                 tag = swf_InsertTag(tag, ST_SHOWFRAME);
176         }
177         tag = swf_InsertTag(tag, ST_END);
178     } else {
179         SOUNDINFO info;
180         tag = swf_InsertTag(tag, ST_DEFINESOUND);
181         swf_SetU16(tag, 24); //id
182         swf_SetSoundDefine(tag, samples, numsamples);
183         tag = swf_InsertTag(tag, ST_STARTSOUND);
184         swf_SetU16(tag, 24); //id
185         memset(&info, 0, sizeof(info));
186         info.loops = loop;
187         swf_SetSoundInfo(tag, &info);
188         tag = swf_InsertTag(tag, ST_SHOWFRAME);
189         tag = swf_InsertTag(tag, ST_END);
190     }
191
192     f = open(outputname,O_WRONLY|O_CREAT|O_TRUNC, 0644);
193     if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");
194     close(f);
195
196     swf_FreeTags(&swf);
197     return 0;
198 }
199
200