]> git.mxchange.org Git - flightgear.git/blobdiff - src/Sound/morse.cxx
Refactor morse and beacon as singleton
[flightgear.git] / src / Sound / morse.cxx
index d2f9fc639017125413642faf26147a3d0f2d87e4..9ff603322b8452ddb4451f3fb25e900265ffe47b 100644 (file)
@@ -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
 //
 // 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$
 
 
 #include <simgear/constants.h>
 
 #include "morse.hxx"
 
+#include <cstring>
 
 static const char alphabet[26][4] = {
-    { DI, DAH, end, end },     /* A */ 
+    { DI, DAH, END, END },     /* A */ 
     { DA, DI, DI, DIT },       /* B */ 
     { DA, DI, DA, DIT },       /* C */ 
-    { DA, DI, DIT, end },      /* D */ 
-    { DIT, end, end, end },    /* E */ 
+    { DA, DI, DIT, END },      /* D */ 
+    { DIT, END, END, END },    /* E */ 
     { DI, DI, DA, DIT },       /* F */ 
-    { DA, DA, DIT, end },      /* G */ 
+    { DA, DA, DIT, END },      /* G */ 
     { DI, DI, DI, DIT },       /* H */ 
-    { DI, DIT, end, end },     /* I */ 
+    { DI, DIT, END, END },     /* I */ 
     { DI, DA, DA, DAH },       /* J */ 
-    { DA, DI, DAH, end },      /* K */ 
+    { DA, DI, DAH, END },      /* K */ 
     { DI, DA, DI, DIT },       /* L */ 
-    { DA, DAH, end, end },     /* M */ 
-    { DA, DIT, end, end },     /* N */ 
-    { DA, DA, DAH, end },      /* O */ 
+    { DA, DAH, END, END },     /* M */ 
+    { DA, DIT, END, END },     /* N */ 
+    { DA, DA, DAH, END },      /* O */ 
     { DI, DA, DA, DIT },       /* P */ 
     { DA, DA, DI, DAH },       /* Q */ 
-    { DI, DA, DIT, end },      /* R */ 
-    { DI, DI, DIT, end },      /* S */ 
-    { DAH, end, end, end },    /* T */ 
-    { DI, DI, DAH, end },      /* U */ 
+    { DI, DA, DIT, END },      /* R */ 
+    { DI, DI, DIT, END },      /* S */ 
+    { DAH, END, END, END },    /* T */ 
+    { DI, DI, DAH, END },      /* U */ 
     { DI, DI, DI, DAH },       /* V */ 
-    { DI, DA, DAH, end },      /* W */ 
+    { DI, DA, DAH, END },      /* W */ 
     { DA, DI, DI, DAH },       /* X */ 
     { DA, DI, DA, DAH },       /* Y */ 
     { DA, DA, DI, DIT }                /* Z */ 
@@ -78,44 +78,6 @@ FGMorse::~FGMorse() {
 }
 
 
-// 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;
-
-    for ( i = 0; i < trans_len; ++i ) {
-       float level = ( sin( (double) i * 2.0 * SGD_PI / (BYTES_PER_SECOND / freq) ) )
-           * ((double)i / trans_len) / 2.0 + 0.5;
-
-       /* Convert to unsigned byte */
-       buf[ i ] = (unsigned char) ( level * 255.0 ) ;
-    }
-
-    for ( i = trans_len; i < len - trans_len; ++i ) {
-       float level = ( sin( (double) i * 2.0 * SGD_PI / (BYTES_PER_SECOND / freq) ) )
-           / 2.0 + 0.5;
-
-       /* Convert to unsigned byte */
-       buf[ i ] = (unsigned char) ( level * 255.0 ) ;
-    }
-    j = trans_len;
-    for ( i = len - trans_len; i < len; ++i ) {
-       float level = ( sin( (double) i * 2.0 * SGD_PI / (BYTES_PER_SECOND / freq) ) )
-           * ((double)j / trans_len) / 2.0 + 0.5;
-       --j;
-
-       /* Convert to unsigned byte */
-       buf[ i ] = (unsigned char) ( level * 255.0 ) ;
-    }
-    for ( i = len; i < total_len; ++i ) {
-       buf[ i ] = (unsigned char) ( 0.5 * 255.0 ) ;
-    }
-}
-
-
 // allocate and initialize sound samples
 bool FGMorse::init() {
     // Make Low DIT
@@ -167,6 +129,7 @@ bool FGMorse::cust_init(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;
@@ -192,7 +155,7 @@ SGSoundSample *FGMorse::make_ident( const string& id, const int freq ) {
     for ( i = 0; i < (int)id.length(); ++i ) {
        if ( idptr[i] >= 'A' && idptr[i] <= 'Z' ) {
            int c = (int)(idptr[i] - 'A');
-           for ( j = 0; j < 4 || alphabet[c][j] == end; ++j ) {
+           for ( j = 0; j < 4 && alphabet[c][j] != END; ++j ) {
                if ( alphabet[c][j] == DIT ) {
                    length += DIT_SIZE;
                } else if ( alphabet[c][j] == DAH ) {
@@ -218,15 +181,15 @@ SGSoundSample *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' ) {
            int c = (int)(idptr[i] - 'A');
-           for ( j = 0; j < 4 || alphabet[c][j] == end; ++j ) {
+           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;
@@ -260,10 +223,22 @@ SGSoundSample *FGMorse::make_ident( const string& id, const int freq ) {
     buf_ptr += SPACE_SIZE;
 
     // 4. create the simple sound and return
-    SGSoundSample *sample = new SGSoundSample( 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;
 }
+
+FGMorse * FGMorse::_instance = NULL;
+
+FGMorse * FGMorse::instance()
+{
+    if( _instance == NULL ) {
+        _instance = new FGMorse();
+        _instance->init();
+    }
+    return _instance;
+}