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