553aaeaf801ca0b8e804e06d17930abbb1c7f996
[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   int blocksize = 2304;
23   S32 width=300,height = 300;
24   U16 block[blocksize];
25   TAG * tag;
26   
27   int f,i,ls1,fs1;
28   int count;
29   int t;
30
31   memset(&swf,0x00,sizeof(SWF));        // set global movie parameters
32
33   swf.fileVersion    = 4;               // make flash 4 compatible swf
34   swf.frameRate      = 18*256;          // about 18 frames per second
35   
36   swf.movieSize.xmax = 20*width;        // flash units: 1 pixel = 20 units ("twips")
37   swf.movieSize.ymax = 20*height;
38
39   swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
40   tag = swf.firstTag;
41   rgb.r = 0xff;
42   rgb.g = 0xff;
43   rgb.b = 0xff;
44   swf_SetRGB(tag,&rgb);
45   
46   tag = swf_InsertTag(tag, ST_SOUNDSTREAMHEAD2);
47   swf_SetSoundStreamHead(tag, 11025/18);
48
49   for(t=256;t>=0;t--) {
50       int s;
51       tag = swf_InsertTag(tag, ST_SOUNDSTREAMBLOCK);
52       for(s=0;s<blocksize;s++) {
53           block[s] = (int)(32767*sin(s*8*3.14159/blocksize));
54           if(t==0) block[s] = 0;
55       }
56       swf_SetSoundStreamBlock(tag, block, 1);
57       tag = swf_InsertTag(tag, ST_SHOWFRAME);
58   }
59   
60   tag = swf_InsertTag(tag, ST_END);
61
62   f = open("sound.swf",O_WRONLY|O_CREAT|O_TRUNC, 0644);
63   if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");
64   close(f);
65
66   swf_FreeTags(&swf);                       // cleanup
67   return 0;
68 }
69
70