bladeenc -> lame
[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 = 576*4;
23   S32 width=300,height = 300;
24   U16 block[blocksize*2];
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      = 40*256;          // about 19 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, blocksize);
48
49   for(t=0;t<64;t++) {
50       int s;
51       tag = swf_InsertTag(tag, ST_SOUNDSTREAMBLOCK);
52       for(s=0;s<blocksize*2;s++) {
53           block[s] = 32767 + (int)(32767*sin((s*t*16*3.14159)/blocksize));
54       }
55       swf_SetSoundStreamBlock(tag, block, blocksize,1);
56       swf_SetSoundStreamBlock(tag, &block[blocksize], blocksize,0);
57       swf_SetSoundStreamBlock(tag, &block[blocksize], blocksize,0);
58       tag = swf_InsertTag(tag, ST_SHOWFRAME);
59   }
60   
61   tag = swf_InsertTag(tag, ST_END);
62
63   f = open("sound.swf",O_WRONLY|O_CREAT|O_TRUNC, 0644);
64   if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");
65   close(f);
66
67   swf_FreeTags(&swf);                       // cleanup
68   return 0;
69 }
70
71