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