initial revision
[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/args.h"
14 #include "wav.h"
15
16 char * filename = 0;
17 char * outputname = "output.swf";
18 int verbose = 2;
19
20 struct options_t options[] =
21 {
22  {"o","output"},
23  {"v","verbose"},
24  {"V","version"},
25  {0,0}
26 };
27
28 int args_callback_option(char*name,char*val)
29 {
30     if(!strcmp(name, "V")) {
31         printf("wav2swf - part of %s %s\n", PACKAGE, VERSION);
32         exit(0);
33     }
34     else if(!strcmp(name, "o")) {
35         outputname = val;
36         return 1;
37     }
38     else if(!strcmp(name, "v")) {
39         verbose ++;
40         return 0;
41     }
42     else {
43         printf("Unknown option: -%s\n", name);
44         exit(1);
45     }
46     return 0;
47 }
48 int args_callback_longoption(char*name,char*val)
49 {
50     return args_long2shortoption(options, name, val);
51 }
52 void args_callback_usage(char*name)
53 {
54     printf("Usage: %s [-o filename] file.wav\n", name);
55     printf("\t-v , --verbose\t\t\t Be more verbose\n");
56     printf("\t-o , --output filename\t\t set output filename (default: output.swf)\n");
57     printf("\t-V , --version\t\t Print program version and exit\n");
58 }
59 int args_callback_command(char*name,char*val)
60 {
61     if(filename) {
62         fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
63                  filename, name);
64     }
65     filename = name;
66     return 0;
67 }
68
69 int main (int argc,char ** argv)
70 { SWF swf;
71   RGBA rgb;
72   SRECT r;
73   S32 width=300,height = 300;
74   TAG * tag;
75
76   int f,i,ls1,fs1;
77   int count;
78   int t;
79   struct WAV wav,wav2;
80   int blocksize = 1152;
81   U16* samples;
82   int numsamples;
83
84   processargs(argc, argv);
85   initLog(0,-1,0,0,-1,verbose);
86
87   if(!readWAV(filename, &wav))
88   {
89       logf("<fatal> Error reading %s", filename);
90       exit(1);
91   }
92   convertWAV2mono(&wav,&wav2, 44100);
93   printWAVInfo(&wav);
94   printWAVInfo(&wav2);
95   samples = (U16*)wav2.data;
96   numsamples = wav2.size/2;
97
98   /* the following is a big hack to prevent against
99      periods of total silence- these get written as
100      blocks of length 0 by bladeenc, which causes
101      the flashplayer to crash */
102
103   for(t=0;t<wav2.size/2;t+=2)
104       wav2.data[t]^=(t&2)<<1;
105
106   memset(&swf,0x00,sizeof(SWF));
107
108   swf.fileVersion    = 4;
109   swf.frameRate      = 44100*256/(blocksize*2);
110
111   swf.movieSize.xmax = 20*width;
112   swf.movieSize.ymax = 20*height;
113
114   swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
115   tag = swf.firstTag;
116   rgb.r = 0xff;
117   rgb.g = 0xff;
118   rgb.b = 0xff;
119   swf_SetRGB(tag,&rgb);
120
121   tag = swf_InsertTag(tag, ST_SOUNDSTREAMHEAD);
122   swf_SetSoundStreamHead(tag, blocksize);
123
124   logf("<notice> %d blocks", numsamples/(blocksize*2));
125   for(t=0;t<numsamples/(blocksize*2);t++) {
126       int s;
127       U16*block1;
128       U16*block2;
129       tag = swf_InsertTag(tag, ST_SOUNDSTREAMBLOCK);
130       logf("<notice> Writing block %d", t);
131       block1 = &samples[(t*2+0)*blocksize];
132       block2 = &samples[(t*2+1)*blocksize];
133       swf_SetSoundStreamBlock(tag, block1, blocksize,1);
134       swf_SetSoundStreamBlock(tag, block2, blocksize,0);
135       tag = swf_InsertTag(tag, ST_SHOWFRAME);
136   }
137
138   f = open(outputname,O_WRONLY|O_CREAT|O_TRUNC, 0644);
139   if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");
140   close(f);
141
142   swf_FreeTags(&swf);
143   return 0;
144 }
145
146