Example file for sound generation.
[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   FILE* fi = fopen("test.mp3","wb");
26   FILE* fi2 = fopen("test2.wav","wb");
27   
28   int f,i,ls1,fs1;
29   int count;
30   int t;
31
32   memset(&swf,0x00,sizeof(SWF));        // set global movie parameters
33
34   swf.fileVersion    = 4;               // make flash 4 compatible swf
35   swf.frameRate      = 19*256;          // about 19 frames per second
36   
37   swf.movieSize.xmax = 20*width;        // flash units: 1 pixel = 20 units ("twips")
38   swf.movieSize.ymax = 20*height;
39
40   swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
41   tag = swf.firstTag;
42   rgb.r = 0xff;
43   rgb.g = 0xff;
44   rgb.b = 0xff;
45   swf_SetRGB(tag,&rgb);
46   
47   tag = swf_InsertTag(tag, ST_SOUNDSTREAMHEAD2);
48   swf_SetSoundStreamHead(tag, 1152);
49
50   for(t=0;t<64;t++) {
51       int s;
52       tag = swf_InsertTag(tag, ST_SOUNDSTREAMBLOCK);
53       for(s=0;s<1152*2;s++) {
54           block[s] = (int)(32767*sin((s*16*3.14159)/1152));
55       }
56       swf_SetSoundStreamBlock(tag, block, 1152,1);
57       swf_SetSoundStreamBlock(tag, &block[1152], 1152,0);
58       fwrite(&tag->data[4], tag->len-4, 1, fi);
59       fwrite(block, 1152*2*2, 1, fi2);
60       tag = swf_InsertTag(tag, ST_SHOWFRAME);
61   }
62   fclose(fi);
63   fclose(fi2);
64
65   f = open("sound.swf",O_WRONLY|O_CREAT|O_TRUNC, 0644);
66   if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");
67   close(f);
68
69   swf_FreeTags(&swf);                       // cleanup
70   return 0;
71 }
72
73