X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FSound%2Fmorse.cxx;h=df6c969face529f7572d3e55f9f15c59af5b3f7a;hb=2d6bf222acdb6de32d6dff07993dc340834dbfff;hp=0e134fbf5f031db584b99059d95dbda30f0b3a7b;hpb=1bf3001749ee560d3da6dafe556418f3f53348a3;p=flightgear.git diff --git a/src/Sound/morse.cxx b/src/Sound/morse.cxx index 0e134fbf5..df6c969fa 100644 --- a/src/Sound/morse.cxx +++ b/src/Sound/morse.cxx @@ -2,7 +2,7 @@ // // Written by Curtis Olson, started March 2001. // -// Copyright (C) 2001 Curtis L. Olson - curt@flightgear.org +// Copyright (C) 2001 Curtis L. Olson - http://www.flightgear.org/~curt // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License as @@ -16,7 +16,7 @@ // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software -// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. // // $Id$ @@ -55,6 +55,19 @@ static const char alphabet[26][4] = { { DA, DA, DI, DIT } /* Z */ }; +static const char numerals[10][5] = { + { DA, DA, DA, DA, DAH }, // 0 + { DI, DA, DA, DA, DAH }, // 1 + { DI, DI, DA, DA, DAH }, // 2 + { DI, DI, DI, DA, DAH }, // 3 + { DI, DI, DI, DI, DAH }, // 4 + { DI, DI, DI, DI, DIT }, // 5 + { DA, DI, DI, DI, DIT }, // 6 + { DA, DA, DI, DI, DIT }, // 7 + { DA, DA, DA, DI, DIT }, // 8 + { DA, DA, DA, DA, DIT } // 9 +}; + // constructor FGMorse::FGMorse() { @@ -65,154 +78,64 @@ FGMorse::~FGMorse() { } -// allocate and initialize sound samples -bool FGMorse::init() { +// Make a tone of specified freq and total_len with trans_len ramp in +// and out and only the first len bytes with sound, the rest with +// silence +void make_tone( unsigned char *buf, int freq, + int len, int total_len, int trans_len ) +{ int i, j; - // Make Low DIT - for ( i = 0; i < TRANSITION_BYTES; ++i ) { - float level = ( sin( (double) i * 2.0 * SG_PI - / (8000.0 / LO_FREQUENCY) ) ) - * ((double)i / TRANSITION_BYTES) - / 2.0 + 0.5; + for ( i = 0; i < trans_len; ++i ) { + float level = ( sin( (double) i * SGD_2PI / (BYTES_PER_SECOND / freq) ) ) + * ((double)i / trans_len) / 2.0 + 0.5; /* Convert to unsigned byte */ - lo_dit[ i ] = (unsigned char) ( level * 255.0 ) ; + buf[ i ] = (unsigned char) ( level * 255.0 ) ; } - for ( i = TRANSITION_BYTES; - i < DIT_SIZE - TRANSITION_BYTES - COUNT_SIZE; ++i ) { - float level = ( sin( (double) i * 2.0 * SG_PI - / (8000.0 / LO_FREQUENCY) ) ) + for ( i = trans_len; i < len - trans_len; ++i ) { + float level = ( sin( (double) i * SGD_2PI / (BYTES_PER_SECOND / freq) ) ) / 2.0 + 0.5; /* Convert to unsigned byte */ - lo_dit[ i ] = (unsigned char) ( level * 255.0 ) ; + buf[ i ] = (unsigned char) ( level * 255.0 ) ; } - j = TRANSITION_BYTES; - for ( i = DIT_SIZE - TRANSITION_BYTES - COUNT_SIZE; - i < DIT_SIZE - COUNT_SIZE; - ++i ) { - float level = ( sin( (double) i * 2.0 * SG_PI - / (8000.0 / LO_FREQUENCY) ) ) - * ((double)j / TRANSITION_BYTES) / 2.0 + 0.5; + j = trans_len; + for ( i = len - trans_len; i < len; ++i ) { + float level = ( sin( (double) i * SGD_2PI / (BYTES_PER_SECOND / freq) ) ) + * ((double)j / trans_len) / 2.0 + 0.5; --j; /* Convert to unsigned byte */ - lo_dit[ i ] = (unsigned char) ( level * 255.0 ) ; - } - for ( i = DIT_SIZE - COUNT_SIZE; i < DIT_SIZE; ++i ) { - lo_dit[ i ] = (unsigned char) ( 0.5 * 255.0 ) ; + buf[ i ] = (unsigned char) ( level * 255.0 ) ; } - - // Make High DIT - for ( i = 0; i < TRANSITION_BYTES; ++i ) { - float level = ( sin( (double) i * 2.0 * SG_PI - / (8000.0 / HI_FREQUENCY)) ) - * ((double)i / TRANSITION_BYTES) / 2.0 + 0.5; - - /* Convert to unsigned byte */ - hi_dit[ i ] = (unsigned char) ( level * 255.0 ) ; + for ( i = len; i < total_len; ++i ) { + buf[ i ] = (unsigned char) ( 0.5 * 255.0 ) ; } +} - for ( i = TRANSITION_BYTES; - i < DIT_SIZE - TRANSITION_BYTES - COUNT_SIZE; ++i ) { - float level = ( sin( (double) i * 2.0 * SG_PI - / (8000.0 / HI_FREQUENCY) ) ) - / 2.0 + 0.5; - /* Convert to unsigned byte */ - hi_dit[ i ] = (unsigned char) ( level * 255.0 ) ; - } - j = TRANSITION_BYTES; - for ( i = DIT_SIZE - TRANSITION_BYTES - COUNT_SIZE; - i < DIT_SIZE - COUNT_SIZE; - ++i ) { - float level = ( sin( (double) i * 2.0 * SG_PI - / (8000.0 / HI_FREQUENCY) ) ) - * ((double)j / TRANSITION_BYTES) / 2.0 + 0.5; - --j; +// allocate and initialize sound samples +bool FGMorse::init() { + // Make Low DIT + make_tone( lo_dit, LO_FREQUENCY, DIT_SIZE - COUNT_SIZE, DIT_SIZE, + TRANSITION_BYTES ); - /* Convert to unsigned byte */ - hi_dit[ i ] = (unsigned char) ( level * 255.0 ) ; - } - for ( i = DIT_SIZE - COUNT_SIZE; i < DIT_SIZE; ++i ) { - hi_dit[ i ] = (unsigned char) ( 0.5 * 255.0 ) ; - } + // Make High DIT + make_tone( hi_dit, HI_FREQUENCY, DIT_SIZE - COUNT_SIZE, DIT_SIZE, + TRANSITION_BYTES ); // Make Low DAH - for ( i = 0; i < TRANSITION_BYTES; ++i ) { - float level = ( sin( (double) i * 2.0 * SG_PI - / (8000.0 / LO_FREQUENCY) ) ) - * ((double)i / TRANSITION_BYTES) / 2.0 + 0.5; - - /* Convert to unsigned byte */ - lo_dah[ i ] = (unsigned char) ( level * 255.0 ) ; - } - - for ( i = TRANSITION_BYTES; - i < DAH_SIZE - TRANSITION_BYTES - COUNT_SIZE; - ++i ) { - float level = ( sin( (double) i * 2.0 * SG_PI - / (8000.0 / LO_FREQUENCY) ) ) - / 2.0 + 0.5; - - /* Convert to unsigned byte */ - lo_dah[ i ] = (unsigned char) ( level * 255.0 ) ; - } - j = TRANSITION_BYTES; - for ( i = DAH_SIZE - TRANSITION_BYTES - COUNT_SIZE; - i < DAH_SIZE - COUNT_SIZE; - ++i ) { - float level = ( sin( (double) i * 2.0 * SG_PI - / (8000.0 / LO_FREQUENCY) ) ) - * ((double)j / TRANSITION_BYTES) / 2.0 + 0.5; - --j; - - /* Convert to unsigned byte */ - lo_dah[ i ] = (unsigned char) ( level * 255.0 ) ; - } - for ( i = DAH_SIZE - COUNT_SIZE; i < DAH_SIZE; ++i ) { - lo_dah[ i ] = (unsigned char) ( 0.5 * 255.0 ) ; - } + make_tone( lo_dah, LO_FREQUENCY, DAH_SIZE - COUNT_SIZE, DAH_SIZE, + TRANSITION_BYTES ); // Make High DAH - for ( i = 0; i < TRANSITION_BYTES; ++i ) { - float level = ( sin( (double) i * 2.0 * SG_PI - / (8000.0 / HI_FREQUENCY) ) ) - * ((double)i / TRANSITION_BYTES) / 2.0 + 0.5; - - /* Convert to unsigned byte */ - hi_dah[ i ] = (unsigned char) ( level * 255.0 ) ; - } - - for ( i = TRANSITION_BYTES; - i < DAH_SIZE - TRANSITION_BYTES - COUNT_SIZE; - ++i ) { - float level = ( sin( (double) i * 2.0 * SG_PI - / (8000.0 / HI_FREQUENCY) ) ) - / 2.0 + 0.5; - - /* Convert to unsigned byte */ - hi_dah[ i ] = (unsigned char) ( level * 255.0 ) ; - } - j = TRANSITION_BYTES; - for ( i = DAH_SIZE - TRANSITION_BYTES - COUNT_SIZE; - i < DAH_SIZE - COUNT_SIZE; - ++i ) { - float level = ( sin( (double) i * 2.0 * SG_PI - / (8000.0 / HI_FREQUENCY) ) ) - * ((double)j / TRANSITION_BYTES) / 2.0 + 0.5; - --j; - - /* Convert to unsigned byte */ - hi_dah[ i ] = (unsigned char) ( level * 255.0 ) ; - } - for ( i = DAH_SIZE - COUNT_SIZE; i < DAH_SIZE; ++i ) { - hi_dah[ i ] = (unsigned char) ( 0.5 * 255.0 ) ; - } + make_tone( hi_dah, HI_FREQUENCY, DAH_SIZE - COUNT_SIZE, DAH_SIZE, + TRANSITION_BYTES ); // Make SPACE + int i; for ( i = 0; i < SPACE_SIZE; ++i ) { space[ i ] = (unsigned char) ( 0.5 * 255 ) ; } @@ -223,72 +146,15 @@ bool FGMorse::init() { // allocate and initialize sound samples bool FGMorse::cust_init(const int freq ) { - int i, j; + int i; // Make DIT - for ( i = 0; i < TRANSITION_BYTES; ++i ) { - float level = ( sin( (double) i * 2.0 * SG_PI / (8000.0 / freq)) ) - * ((double)i / TRANSITION_BYTES) / 2.0 + 0.5; - - /* Convert to unsigned byte */ - cust_dit[ i ] = (unsigned char) ( level * 255.0 ) ; - } - - for ( i = TRANSITION_BYTES; - i < DIT_SIZE - TRANSITION_BYTES - COUNT_SIZE; ++i ) { - float level = ( sin( (double) i * 2.0 * SG_PI / (8000.0 / freq) ) ) - / 2.0 + 0.5; - - /* Convert to unsigned byte */ - cust_dit[ i ] = (unsigned char) ( level * 255.0 ) ; - } - j = TRANSITION_BYTES; - for ( i = DIT_SIZE - TRANSITION_BYTES - COUNT_SIZE; - i < DIT_SIZE - COUNT_SIZE; - ++i ) { - float level = ( sin( (double) i * 2.0 * SG_PI / (8000.0 / freq) ) ) - * ((double)j / TRANSITION_BYTES) / 2.0 + 0.5; - --j; - - /* Convert to unsigned byte */ - cust_dit[ i ] = (unsigned char) ( level * 255.0 ) ; - } - for ( i = DIT_SIZE - COUNT_SIZE; i < DIT_SIZE; ++i ) { - cust_dit[ i ] = (unsigned char) ( 0.5 * 255.0 ) ; - } + make_tone( cust_dit, freq, DIT_SIZE - COUNT_SIZE, DIT_SIZE, + TRANSITION_BYTES ); // Make DAH - for ( i = 0; i < TRANSITION_BYTES; ++i ) { - float level = ( sin( (double) i * 2.0 * SG_PI / (8000.0 / freq) ) ) - * ((double)i / TRANSITION_BYTES) / 2.0 + 0.5; - - /* Convert to unsigned byte */ - cust_dah[ i ] = (unsigned char) ( level * 255.0 ) ; - } - - for ( i = TRANSITION_BYTES; - i < DAH_SIZE - TRANSITION_BYTES - COUNT_SIZE; - ++i ) { - float level = ( sin( (double) i * 2.0 * SG_PI / (8000.0 / freq) ) ) - / 2.0 + 0.5; - - /* Convert to unsigned byte */ - cust_dah[ i ] = (unsigned char) ( level * 255.0 ) ; - } - j = TRANSITION_BYTES; - for ( i = DAH_SIZE - TRANSITION_BYTES - COUNT_SIZE; - i < DAH_SIZE - COUNT_SIZE; - ++i ) { - float level = ( sin( (double) i * 2.0 * SG_PI / (8000.0 / freq) ) ) - * ((double)j / TRANSITION_BYTES) / 2.0 + 0.5; - --j; - - /* Convert to unsigned byte */ - cust_dah[ i ] = (unsigned char) ( level * 255.0 ) ; - } - for ( i = DAH_SIZE - COUNT_SIZE; i < DAH_SIZE; ++i ) { - cust_dah[ i ] = (unsigned char) ( 0.5 * 255.0 ) ; - } + make_tone( cust_dah, freq, DAH_SIZE - COUNT_SIZE, DAH_SIZE, + TRANSITION_BYTES ); // Make SPACE for ( i = 0; i < SPACE_SIZE; ++i ) { @@ -299,8 +165,9 @@ bool FGMorse::cust_init(const int freq ) { } -// make a FGSimpleSound morse code transmission for the specified string -FGSimpleSound *FGMorse::make_ident( const string& id, const int freq ) { +// make a SGSoundSample morse code transmission for the specified string +SGSoundSample *FGMorse::make_ident( const string& id, const int freq ) { + char *idptr = (char *)id.c_str(); int length = 0; @@ -325,8 +192,8 @@ FGSimpleSound *FGMorse::make_ident( const string& id, const int freq ) { // 1. Determine byte length of message for ( i = 0; i < (int)id.length(); ++i ) { if ( idptr[i] >= 'A' && idptr[i] <= 'Z' ) { - char c = idptr[i] - 'A'; - for ( j = 0; j < 4 || alphabet[c][j] == end; ++j ) { + int c = (int)(idptr[i] - 'A'); + for ( j = 0; j < 4 && alphabet[c][j] != end; ++j ) { if ( alphabet[c][j] == DIT ) { length += DIT_SIZE; } else if ( alphabet[c][j] == DAH ) { @@ -334,6 +201,16 @@ FGSimpleSound *FGMorse::make_ident( const string& id, const int freq ) { } } length += SPACE_SIZE; + } else if ( idptr[i] >= '0' && idptr[i] <= '9' ) { + int c = (int)(idptr[i] - '0'); + for ( j = 0; j < 5; ++j) { + if ( numerals[c][j] == DIT ) { + length += DIT_SIZE; + } else if ( numerals[c][j] == DAH ) { + length += DAH_SIZE; + } + } + length += SPACE_SIZE; } else { // skip unknown character } @@ -342,15 +219,15 @@ FGSimpleSound *FGMorse::make_ident( const string& id, const int freq ) { length += 2 * SPACE_SIZE; // 2. Allocate space for the message - unsigned char *buffer = new unsigned char[length]; + const unsigned char* buffer = (const unsigned char *)malloc(length); // 3. Assemble the message; - unsigned char *buf_ptr = buffer; + unsigned char *buf_ptr = (unsigned char*)buffer; for ( i = 0; i < (int)id.length(); ++i ) { if ( idptr[i] >= 'A' && idptr[i] <= 'Z' ) { - char c = idptr[i] - 'A'; - for ( j = 0; j < 4 || alphabet[c][j] == end; ++j ) { + int c = (int)(idptr[i] - 'A'); + for ( j = 0; j < 4 && alphabet[c][j] != end; ++j ) { if ( alphabet[c][j] == DIT ) { memcpy( buf_ptr, dit_ptr, DIT_SIZE ); buf_ptr += DIT_SIZE; @@ -361,6 +238,19 @@ FGSimpleSound *FGMorse::make_ident( const string& id, const int freq ) { } memcpy( buf_ptr, space, SPACE_SIZE ); buf_ptr += SPACE_SIZE; + } else if ( idptr[i] >= '0' && idptr[i] <= '9' ) { + int c = (int)(idptr[i] - '0'); + for ( j = 0; j < 5; ++j ) { + if ( numerals[c][j] == DIT ) { + memcpy( buf_ptr, dit_ptr, DIT_SIZE ); + buf_ptr += DIT_SIZE; + } else if ( numerals[c][j] == DAH ) { + memcpy( buf_ptr, dah_ptr, DAH_SIZE ); + buf_ptr += DAH_SIZE; + } + } + memcpy( buf_ptr, space, SPACE_SIZE ); + buf_ptr += SPACE_SIZE; } else { // skip unknown character } @@ -371,7 +261,11 @@ FGSimpleSound *FGMorse::make_ident( const string& id, const int freq ) { buf_ptr += SPACE_SIZE; // 4. create the simple sound and return - FGSimpleSound *sample = new FGSimpleSound( buffer, length ); + SGSoundSample *sample = new SGSoundSample( &buffer, length, + BYTES_PER_SECOND ); + + sample->set_reference_dist( 10.0 ); + sample->set_max_dist( 20.0 ); return sample; }