]> git.mxchange.org Git - flightgear.git/blob - Lib/example/example.cxx
Merge FG_Lib as subdirectory
[flightgear.git] / Lib / example / example.cxx
1
2
3 #include "sl.h"
4 #include "sm.h"
5 #include <math.h>
6
7 /*
8   Construct a sound scheduler and a mixer.
9 */
10
11 slScheduler sched ( 8000 ) ;
12 smMixer mixer ;
13
14 int main ()
15 {
16   mixer . setMasterVolume ( 30 ) ;
17   sched . setSafetyMargin ( 0.128 ) ;
18
19   /* Just for fun, let's make a one second synthetic engine sample... */
20
21   Uchar buffer [ 8000 ] ;
22
23   for ( int i = 0 ; i < 8000 ; i++ )
24   {
25     /* Sum some sin waves and convert to range 0..1 */
26
27     float level = ( sin ( (double) i * 2.0 * M_PI / (8000.0/ 50.0) ) +
28                     sin ( (double) i * 2.0 * M_PI / (8000.0/149.0) ) +
29                     sin ( (double) i * 2.0 * M_PI / (8000.0/152.0) ) +
30                     sin ( (double) i * 2.0 * M_PI / (8000.0/192.0) )
31                   ) / 8.0f + 0.5f ;
32
33     /* Convert to unsigned byte */
34
35     buffer [ i ] = (Uchar) ( level * 255.0 ) ;
36   }
37
38   /* Set up four samples and a loop */
39
40   slSample  *s = new slSample ( buffer, 8000 ) ;
41   slSample *s1 = new slSample ( "scream.ub", & sched ) ;
42   slSample *s2 = new slSample ( "zzap.wav" , & sched ) ;
43   slSample *s3 = new slSample ( "cuckoo.au", & sched ) ;
44   slSample *s4 = new slSample ( "wheeee.ub", & sched ) ;
45
46   /* Mess about with some of the samples... */
47
48   s1 -> adjustVolume ( 2.2  ) ;
49   s2 -> adjustVolume ( 0.5  ) ;
50   s3 -> adjustVolume ( 0.2  ) ;
51
52   /* Play the engine sample continuously. */
53
54   sched . loopSample ( s ) ;
55
56   int tim = 0 ;  /* My periodic event timer. */
57
58   while ( SL_TRUE )
59   {
60     tim++ ;  /* Time passes */
61
62     if ( tim % 200 == 0 ) sched.playSample ( s1 ) ;
63     if ( tim % 180 == 0 ) sched.playSample ( s2 ) ;
64     if ( tim % 150 == 0 ) sched.playSample ( s3 ) ;
65     if ( tim % 120 == 0 ) sched.playSample ( s4 ) ;
66
67     /*
68       For the sake of realism, I'll delay for 1/30th second to
69       simulate a graphics update process.
70     */
71
72 #ifdef WIN32
73     Sleep ( 1000 / 30 ) ;      /* 30Hz */
74 #elif defined(sgi)
75     sginap( 3 );               /* ARG */
76 #else
77     usleep ( 1000000 / 30 ) ;  /* 30Hz */
78 #endif
79
80     /*
81       This would normally be called just before the graphics buffer swap
82       - but it could be anywhere where it's guaranteed to get called
83       fairly often.
84     */
85
86     sched . update () ;
87   }
88 }
89