]> git.mxchange.org Git - flightgear.git/blob - src/Sound/morse.cxx
Refactor morse and beacon as singleton
[flightgear.git] / src / Sound / morse.cxx
1 // morse.cxx -- Morse code generation class
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
23 #include <simgear/constants.h>
24
25 #include "morse.hxx"
26
27 #include <cstring>
28
29 static const char alphabet[26][4] = {
30     { DI, DAH, END, END },      /* A */ 
31     { DA, DI, DI, DIT },        /* B */ 
32     { DA, DI, DA, DIT },        /* C */ 
33     { DA, DI, DIT, END },       /* D */ 
34     { DIT, END, END, END },     /* E */ 
35     { DI, DI, DA, DIT },        /* F */ 
36     { DA, DA, DIT, END },       /* G */ 
37     { DI, DI, DI, DIT },        /* H */ 
38     { DI, DIT, END, END },      /* I */ 
39     { DI, DA, DA, DAH },        /* J */ 
40     { DA, DI, DAH, END },       /* K */ 
41     { DI, DA, DI, DIT },        /* L */ 
42     { DA, DAH, END, END },      /* M */ 
43     { DA, DIT, END, END },      /* N */ 
44     { DA, DA, DAH, END },       /* O */ 
45     { DI, DA, DA, DIT },        /* P */ 
46     { DA, DA, DI, DAH },        /* Q */ 
47     { DI, DA, DIT, END },       /* R */ 
48     { DI, DI, DIT, END },       /* S */ 
49     { DAH, END, END, END },     /* T */ 
50     { DI, DI, DAH, END },       /* U */ 
51     { DI, DI, DI, DAH },        /* V */ 
52     { DI, DA, DAH, END },       /* W */ 
53     { DA, DI, DI, DAH },        /* X */ 
54     { DA, DI, DA, DAH },        /* Y */ 
55     { DA, DA, DI, DIT }         /* Z */ 
56 };
57
58 static const char numerals[10][5] = {
59     { DA, DA, DA, DA, DAH },    // 0
60     { DI, DA, DA, DA, DAH },    // 1
61     { DI, DI, DA, DA, DAH },    // 2
62     { DI, DI, DI, DA, DAH },    // 3
63     { DI, DI, DI, DI, DAH },    // 4
64     { DI, DI, DI, DI, DIT },    // 5
65     { DA, DI, DI, DI, DIT },    // 6
66     { DA, DA, DI, DI, DIT },    // 7
67     { DA, DA, DA, DI, DIT },    // 8
68     { DA, DA, DA, DA, DIT }     // 9
69 };
70
71
72 // constructor
73 FGMorse::FGMorse() {
74 }
75
76 // destructor
77 FGMorse::~FGMorse() {
78 }
79
80
81 // allocate and initialize sound samples
82 bool FGMorse::init() {
83     // Make Low DIT
84     make_tone( lo_dit, LO_FREQUENCY, DIT_SIZE - COUNT_SIZE, DIT_SIZE,
85                TRANSITION_BYTES );
86
87     // Make High DIT
88     make_tone( hi_dit, HI_FREQUENCY, DIT_SIZE - COUNT_SIZE, DIT_SIZE,
89                TRANSITION_BYTES );
90
91     // Make Low DAH
92     make_tone( lo_dah, LO_FREQUENCY, DAH_SIZE - COUNT_SIZE, DAH_SIZE,
93                TRANSITION_BYTES );
94
95     // Make High DAH
96     make_tone( hi_dah, HI_FREQUENCY, DAH_SIZE - COUNT_SIZE, DAH_SIZE,
97                TRANSITION_BYTES );
98
99     // Make SPACE
100     int i;
101     for ( i = 0; i < SPACE_SIZE; ++i ) {
102         space[ i ] = (unsigned char) ( 0.5 * 255 ) ;
103     }
104
105     return true;
106 }
107
108
109 // allocate and initialize sound samples
110 bool FGMorse::cust_init(const int freq ) {
111     int i;
112
113     // Make DIT
114     make_tone( cust_dit, freq, DIT_SIZE - COUNT_SIZE, DIT_SIZE,
115                TRANSITION_BYTES );
116
117     // Make DAH
118     make_tone( cust_dah, freq, DAH_SIZE - COUNT_SIZE, DAH_SIZE,
119                TRANSITION_BYTES );
120
121     // Make SPACE
122     for ( i = 0; i < SPACE_SIZE; ++i ) {
123         space[ i ] = (unsigned char) ( 0.5 * 255 ) ;
124     }
125
126     return true;
127 }
128
129
130 // make a SGSoundSample morse code transmission for the specified string
131 SGSoundSample *FGMorse::make_ident( const string& id, const int freq ) {
132
133     char *idptr = (char *)id.c_str();
134
135     int length = 0;
136     int i, j;
137
138     // 0. Select the frequency.  If custom frequency, generate the
139     // sound fragments we need on the fly.
140     unsigned char *dit_ptr, *dah_ptr;
141
142     if ( freq == LO_FREQUENCY ) {
143         dit_ptr = lo_dit;
144         dah_ptr = lo_dah;
145     } else if ( freq == HI_FREQUENCY ) {
146         dit_ptr = hi_dit;
147         dah_ptr = hi_dah;
148     } else {
149         cust_init( freq );
150         dit_ptr = cust_dit;
151         dah_ptr = cust_dah;
152     }
153
154     // 1. Determine byte length of message
155     for ( i = 0; i < (int)id.length(); ++i ) {
156         if ( idptr[i] >= 'A' && idptr[i] <= 'Z' ) {
157             int c = (int)(idptr[i] - 'A');
158             for ( j = 0; j < 4 && alphabet[c][j] != END; ++j ) {
159                 if ( alphabet[c][j] == DIT ) {
160                     length += DIT_SIZE;
161                 } else if ( alphabet[c][j] == DAH ) {
162                     length += DAH_SIZE;
163                 }
164             }
165             length += SPACE_SIZE;
166         } else if ( idptr[i] >= '0' && idptr[i] <= '9' ) {
167             int c = (int)(idptr[i] - '0');
168             for ( j = 0; j < 5; ++j) {
169                 if ( numerals[c][j] == DIT ) {
170                     length += DIT_SIZE;
171                 } else if ( numerals[c][j] == DAH ) {
172                     length += DAH_SIZE;
173                 }
174             }
175             length += SPACE_SIZE;
176         } else {
177             // skip unknown character
178         }
179     }
180     // add 2x more space to the end of the string
181     length += 2 * SPACE_SIZE;
182
183     // 2. Allocate space for the message
184     const unsigned char* buffer = (const unsigned char *)malloc(length);
185
186     // 3. Assemble the message;
187     unsigned char *buf_ptr = (unsigned char*)buffer;
188
189     for ( i = 0; i < (int)id.length(); ++i ) {
190         if ( idptr[i] >= 'A' && idptr[i] <= 'Z' ) {
191             int c = (int)(idptr[i] - 'A');
192             for ( j = 0; j < 4 && alphabet[c][j] != END; ++j ) {
193                 if ( alphabet[c][j] == DIT ) {
194                     memcpy( buf_ptr, dit_ptr, DIT_SIZE );
195                     buf_ptr += DIT_SIZE;
196                 } else if ( alphabet[c][j] == DAH ) {
197                     memcpy( buf_ptr, dah_ptr, DAH_SIZE );
198                     buf_ptr += DAH_SIZE;
199                 }
200             }
201             memcpy( buf_ptr, space, SPACE_SIZE );
202             buf_ptr += SPACE_SIZE;
203         } else if ( idptr[i] >= '0' && idptr[i] <= '9' ) {
204             int c = (int)(idptr[i] - '0');
205             for ( j = 0; j < 5; ++j ) {
206                 if ( numerals[c][j] == DIT ) {
207                     memcpy( buf_ptr, dit_ptr, DIT_SIZE );
208                     buf_ptr += DIT_SIZE;
209                 } else if ( numerals[c][j] == DAH ) {
210                     memcpy( buf_ptr, dah_ptr, DAH_SIZE );
211                     buf_ptr += DAH_SIZE;
212                 }
213             }
214             memcpy( buf_ptr, space, SPACE_SIZE );
215             buf_ptr += SPACE_SIZE;
216         } else {
217             // skip unknown character
218         }
219     }
220     memcpy( buf_ptr, space, SPACE_SIZE );
221     buf_ptr += SPACE_SIZE;
222     memcpy( buf_ptr, space, SPACE_SIZE );
223     buf_ptr += SPACE_SIZE;
224
225     // 4. create the simple sound and return
226     SGSoundSample *sample = new SGSoundSample( &buffer, length,
227                                                BYTES_PER_SECOND );
228
229     sample->set_reference_dist( 10.0 );
230     sample->set_max_dist( 20.0 );
231
232     return sample;
233 }
234
235 FGMorse * FGMorse::_instance = NULL;
236
237 FGMorse * FGMorse::instance()
238 {
239     if( _instance == NULL ) {
240         _instance = new FGMorse();
241         _instance->init();
242     }
243     return _instance;
244 }