cleaned up.
[swftools.git] / lib / example / sound.c
1 /* sound.c
2
3    Generates a sine wave and tries to create an swf which
4    plays it as mp3 sound.
5    
6    Part of the swftools package.
7
8    Copyright (c) 2002 Matthias Kramm <kramm@quiss.org>
9  
10    This file is distributed under the GPL, see file COPYING for details 
11 */
12
13 #include <stdio.h>
14 #include <fcntl.h>
15 #include <math.h>
16 #include "../rfxswf.h"
17
18 int main (int argc,char ** argv)
19 { SWF swf;
20   RGBA rgb;
21   SRECT r;
22   S32 width=300,height = 300;
23   U16 block[1152*2];
24   TAG * tag;
25   
26   int f,i,ls1,fs1;
27   int count;
28   int t;
29
30   memset(&swf,0x00,sizeof(SWF));        // set global movie parameters
31
32   swf.fileVersion    = 4;               // make flash 4 compatible swf
33   swf.frameRate      = 19*256;          // about 19 frames per second
34   
35   swf.movieSize.xmax = 20*width;        // flash units: 1 pixel = 20 units ("twips")
36   swf.movieSize.ymax = 20*height;
37
38   swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
39   tag = swf.firstTag;
40   rgb.r = 0xff;
41   rgb.g = 0xff;
42   rgb.b = 0xff;
43   swf_SetRGB(tag,&rgb);
44   
45   tag = swf_InsertTag(tag, ST_SOUNDSTREAMHEAD2);
46   swf_SetSoundStreamHead(tag, 1152);
47
48   for(t=0;t<64;t++) {
49       int s;
50       tag = swf_InsertTag(tag, ST_SOUNDSTREAMBLOCK);
51       for(s=0;s<1152*2;s++) {
52           block[s] = (int)(32767*sin((s*16*3.14159)/1152));
53       }
54       swf_SetSoundStreamBlock(tag, block, 1152,1);
55       swf_SetSoundStreamBlock(tag, &block[1152], 1152,0);
56       tag = swf_InsertTag(tag, ST_SHOWFRAME);
57   }
58
59   f = open("sound.swf",O_WRONLY|O_CREAT|O_TRUNC, 0644);
60   if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");
61   close(f);
62
63   swf_FreeTags(&swf);                       // cleanup
64   return 0;
65 }
66
67