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