1 // morse.cxx -- Morse code generation class
3 // Written by Curtis Olson, started March 2001.
5 // Copyright (C) 2001 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
24 #include <simgear/constants.h>
26 #include <Main/fg_props.hxx>
27 #include <Main/globals.hxx>
32 static const char alphabet[26][4] = {
33 { DI, DAH, end, end }, /* A */
34 { DA, DI, DI, DIT }, /* B */
35 { DA, DI, DA, DIT }, /* C */
36 { DA, DI, DIT, end }, /* D */
37 { DIT, end, end, end }, /* E */
38 { DI, DI, DA, DIT }, /* F */
39 { DA, DA, DIT, end }, /* G */
40 { DI, DI, DI, DIT }, /* H */
41 { DI, DIT, end, end }, /* I */
42 { DI, DA, DA, DAH }, /* J */
43 { DA, DI, DAH, end }, /* K */
44 { DI, DA, DI, DIT }, /* L */
45 { DA, DAH, end, end }, /* M */
46 { DA, DIT, end, end }, /* N */
47 { DA, DA, DAH, end }, /* O */
48 { DI, DA, DA, DIT }, /* P */
49 { DA, DA, DI, DAH }, /* Q */
50 { DI, DA, DIT, end }, /* R */
51 { DI, DI, DIT, end }, /* S */
52 { DAH, end, end, end }, /* T */
53 { DI, DI, DAH, end }, /* U */
54 { DI, DI, DI, DAH }, /* V */
55 { DI, DA, DAH, end }, /* W */
56 { DA, DI, DI, DAH }, /* X */
57 { DA, DI, DA, DAH }, /* Y */
58 { DA, DA, DI, DIT } /* Z */
61 static const char numerals[10][5] = {
62 { DA, DA, DA, DA, DAH }, // 0
63 { DI, DA, DA, DA, DAH }, // 1
64 { DI, DI, DA, DA, DAH }, // 2
65 { DI, DI, DI, DA, DAH }, // 3
66 { DI, DI, DI, DI, DAH }, // 4
67 { DI, DI, DI, DI, DIT }, // 5
68 { DA, DI, DI, DI, DIT }, // 6
69 { DA, DA, DI, DI, DIT }, // 7
70 { DA, DA, DA, DI, DIT }, // 8
71 { DA, DA, DA, DA, DIT } // 9
84 // Make a tone of specified freq and total_len with trans_len ramp in
85 // and out and only the first len bytes with sound, the rest with
87 void make_tone( unsigned char *buf, int freq,
88 int len, int total_len, int trans_len )
92 for ( i = 0; i < trans_len; ++i ) {
93 float level = ( sin( (double) i * SGD_2PI / (BYTES_PER_SECOND / freq) ) )
94 * ((double)i / trans_len) / 2.0 + 0.5;
96 /* Convert to unsigned byte */
97 buf[ i ] = (unsigned char) ( level * 255.0 ) ;
100 for ( i = trans_len; i < len - trans_len; ++i ) {
101 float level = ( sin( (double) i * SGD_2PI / (BYTES_PER_SECOND / freq) ) )
104 /* Convert to unsigned byte */
105 buf[ i ] = (unsigned char) ( level * 255.0 ) ;
108 for ( i = len - trans_len; i < len; ++i ) {
109 float level = ( sin( (double) i * SGD_2PI / (BYTES_PER_SECOND / freq) ) )
110 * ((double)j / trans_len) / 2.0 + 0.5;
113 /* Convert to unsigned byte */
114 buf[ i ] = (unsigned char) ( level * 255.0 ) ;
116 for ( i = len; i < total_len; ++i ) {
117 buf[ i ] = (unsigned char) ( 0.5 * 255.0 ) ;
122 // allocate and initialize sound samples
123 bool FGMorse::init() {
125 make_tone( lo_dit, LO_FREQUENCY, DIT_SIZE - COUNT_SIZE, DIT_SIZE,
129 make_tone( hi_dit, HI_FREQUENCY, DIT_SIZE - COUNT_SIZE, DIT_SIZE,
133 make_tone( lo_dah, LO_FREQUENCY, DAH_SIZE - COUNT_SIZE, DAH_SIZE,
137 make_tone( hi_dah, HI_FREQUENCY, DAH_SIZE - COUNT_SIZE, DAH_SIZE,
142 for ( i = 0; i < SPACE_SIZE; ++i ) {
143 space[ i ] = (unsigned char) ( 0.5 * 255 ) ;
150 // allocate and initialize sound samples
151 bool FGMorse::cust_init(const int freq ) {
155 make_tone( cust_dit, freq, DIT_SIZE - COUNT_SIZE, DIT_SIZE,
159 make_tone( cust_dah, freq, DAH_SIZE - COUNT_SIZE, DAH_SIZE,
163 for ( i = 0; i < SPACE_SIZE; ++i ) {
164 space[ i ] = (unsigned char) ( 0.5 * 255 ) ;
171 // make a SGSoundSample morse code transmission for the specified string
172 SGSoundSample *FGMorse::make_ident( const string& id, const int freq ) {
174 if (globals->get_soundmgr()->is_working() == false) {
178 char *idptr = (char *)id.c_str();
183 // 0. Select the frequency. If custom frequency, generate the
184 // sound fragments we need on the fly.
185 unsigned char *dit_ptr, *dah_ptr;
187 if ( freq == LO_FREQUENCY ) {
190 } else if ( freq == HI_FREQUENCY ) {
199 // 1. Determine byte length of message
200 for ( i = 0; i < (int)id.length(); ++i ) {
201 if ( idptr[i] >= 'A' && idptr[i] <= 'Z' ) {
202 int c = (int)(idptr[i] - 'A');
203 for ( j = 0; j < 4 || alphabet[c][j] == end; ++j ) {
204 if ( alphabet[c][j] == DIT ) {
206 } else if ( alphabet[c][j] == DAH ) {
210 length += SPACE_SIZE;
211 } else if ( idptr[i] >= '0' && idptr[i] <= '9' ) {
212 int c = (int)(idptr[i] - '0');
213 for ( j = 0; j < 5; ++j) {
214 if ( numerals[c][j] == DIT ) {
216 } else if ( numerals[c][j] == DAH ) {
220 length += SPACE_SIZE;
222 // skip unknown character
225 // add 2x more space to the end of the string
226 length += 2 * SPACE_SIZE;
228 // 2. Allocate space for the message
229 unsigned char *buffer = new unsigned char[length];
231 // 3. Assemble the message;
232 unsigned char *buf_ptr = buffer;
234 for ( i = 0; i < (int)id.length(); ++i ) {
235 if ( idptr[i] >= 'A' && idptr[i] <= 'Z' ) {
236 int c = (int)(idptr[i] - 'A');
237 for ( j = 0; j < 4 || alphabet[c][j] == end; ++j ) {
238 if ( alphabet[c][j] == DIT ) {
239 memcpy( buf_ptr, dit_ptr, DIT_SIZE );
241 } else if ( alphabet[c][j] == DAH ) {
242 memcpy( buf_ptr, dah_ptr, DAH_SIZE );
246 memcpy( buf_ptr, space, SPACE_SIZE );
247 buf_ptr += SPACE_SIZE;
248 } else if ( idptr[i] >= '0' && idptr[i] <= '9' ) {
249 int c = (int)(idptr[i] - '0');
250 for ( j = 0; j < 5; ++j ) {
251 if ( numerals[c][j] == DIT ) {
252 memcpy( buf_ptr, dit_ptr, DIT_SIZE );
254 } else if ( numerals[c][j] == DAH ) {
255 memcpy( buf_ptr, dah_ptr, DAH_SIZE );
259 memcpy( buf_ptr, space, SPACE_SIZE );
260 buf_ptr += SPACE_SIZE;
262 // skip unknown character
265 memcpy( buf_ptr, space, SPACE_SIZE );
266 buf_ptr += SPACE_SIZE;
267 memcpy( buf_ptr, space, SPACE_SIZE );
268 buf_ptr += SPACE_SIZE;
270 // 4. create the simple sound and return
271 SGSoundSample *sample = new SGSoundSample( buffer, length,
274 // clean up the buffer
277 sample->set_reference_dist( 10.0 );
278 sample->set_max_dist( 20.0 );