1 // soundgenerator.cxx -- simple sound generation
\r
3 // Written by Curtis Olson, started March 2001.
\r
5 // Copyright (C) 2001 Curtis L. Olson - http://www.flightgear.org/~curt
\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
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
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
22 #include "soundgenerator.hxx"
\r
23 #include <simgear/constants.h>
\r
25 FGSoundGenerator::~FGSoundGenerator()
\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
32 void FGSoundGenerator::make_tone( unsigned char *buf, int freq,
\r
33 int len, int total_len, int trans_len )
\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
41 /* Convert to unsigned byte */
\r
42 buf[ i ] = (unsigned char) ( level * 255.0 ) ;
\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
49 /* Convert to unsigned byte */
\r
50 buf[ i ] = (unsigned char) ( level * 255.0 ) ;
\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
58 /* Convert to unsigned byte */
\r
59 buf[ i ] = (unsigned char) ( level * 255.0 ) ;
\r
61 for ( i = len; i < total_len; ++i ) {
\r
62 buf[ i ] = (unsigned char) ( 0.5 * 255.0 ) ;
\r