]> git.mxchange.org Git - flightgear.git/blob - src/Sound/soundgenerator.cxx
Flight-history men usage cap.
[flightgear.git] / src / Sound / soundgenerator.cxx
1 // soundgenerator.cxx -- simple sound generation \r
2 //\r
3 // Written by Curtis Olson, started March 2001.\r
4 //\r
5 // Copyright (C) 2001  Curtis L. Olson - http://www.flightgear.org/~curt\r
6 //\r
7 // This program is free software; you can redistribute it and/or\r
8 // modify it under the terms of the GNU General Public License as\r
9 // published by the Free Software Foundation; either version 2 of the\r
10 // License, or (at your option) any later version.\r
11 //\r
12 // This program is distributed in the hope that it will be useful, but\r
13 // WITHOUT ANY WARRANTY; without even the implied warranty of\r
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
15 // General Public License for more details.\r
16 //\r
17 // You should have received a copy of the GNU General Public License\r
18 // along with this program; if not, write to the Free Software\r
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.\r
20 //\r
21 \r
22 #include "soundgenerator.hxx"\r
23 #include <simgear/constants.h>\r
24 \r
25 FGSoundGenerator::~FGSoundGenerator()\r
26 {\r
27 }\r
28 \r
29 // Make a tone of specified freq and total_len with trans_len ramp in\r
30 // and out and only the first len bytes with sound, the rest with\r
31 // silence\r
32 void FGSoundGenerator::make_tone( unsigned char *buf, int freq, \r
33                 int len, int total_len, int trans_len )\r
34 {\r
35     int i, j;\r
36 \r
37     for ( i = 0; i < trans_len; ++i ) {\r
38         float level = ( sin( (double) i * SGD_2PI / (BYTES_PER_SECOND / freq) ) )\r
39             * ((double)i / trans_len) / 2.0 + 0.5;\r
40 \r
41         /* Convert to unsigned byte */\r
42         buf[ i ] = (unsigned char) ( level * 255.0 ) ;\r
43     }\r
44 \r
45     for ( i = trans_len; i < len - trans_len; ++i ) {\r
46         float level = ( sin( (double) i * SGD_2PI / (BYTES_PER_SECOND / freq) ) )\r
47             / 2.0 + 0.5;\r
48 \r
49         /* Convert to unsigned byte */\r
50         buf[ i ] = (unsigned char) ( level * 255.0 ) ;\r
51     }\r
52     j = trans_len;\r
53     for ( i = len - trans_len; i < len; ++i ) {\r
54         float level = ( sin( (double) i * SGD_2PI / (BYTES_PER_SECOND / freq) ) )\r
55             * ((double)j / trans_len) / 2.0 + 0.5;\r
56         --j;\r
57 \r
58         /* Convert to unsigned byte */\r
59         buf[ i ] = (unsigned char) ( level * 255.0 ) ;\r
60     }\r
61     for ( i = len; i < total_len; ++i ) {\r
62         buf[ i ] = (unsigned char) ( 0.5 * 255.0 ) ;\r
63     }\r
64 }\r