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>
30 static const char alphabet[26][4] = {
31 { DI, DAH, END, END }, /* A */
32 { DA, DI, DI, DIT }, /* B */
33 { DA, DI, DA, DIT }, /* C */
34 { DA, DI, DIT, END }, /* D */
35 { DIT, END, END, END }, /* E */
36 { DI, DI, DA, DIT }, /* F */
37 { DA, DA, DIT, END }, /* G */
38 { DI, DI, DI, DIT }, /* H */
39 { DI, DIT, END, END }, /* I */
40 { DI, DA, DA, DAH }, /* J */
41 { DA, DI, DAH, END }, /* K */
42 { DI, DA, DI, DIT }, /* L */
43 { DA, DAH, END, END }, /* M */
44 { DA, DIT, END, END }, /* N */
45 { DA, DA, DAH, END }, /* O */
46 { DI, DA, DA, DIT }, /* P */
47 { DA, DA, DI, DAH }, /* Q */
48 { DI, DA, DIT, END }, /* R */
49 { DI, DI, DIT, END }, /* S */
50 { DAH, END, END, END }, /* T */
51 { DI, DI, DAH, END }, /* U */
52 { DI, DI, DI, DAH }, /* V */
53 { DI, DA, DAH, END }, /* W */
54 { DA, DI, DI, DAH }, /* X */
55 { DA, DI, DA, DAH }, /* Y */
56 { DA, DA, DI, DIT } /* Z */
59 static const char numerals[10][5] = {
60 { DA, DA, DA, DA, DAH }, // 0
61 { DI, DA, DA, DA, DAH }, // 1
62 { DI, DI, DA, DA, DAH }, // 2
63 { DI, DI, DI, DA, DAH }, // 3
64 { DI, DI, DI, DI, DAH }, // 4
65 { DI, DI, DI, DI, DIT }, // 5
66 { DA, DI, DI, DI, DIT }, // 6
67 { DA, DA, DI, DI, DIT }, // 7
68 { DA, DA, DA, DI, DIT }, // 8
69 { DA, DA, DA, DA, DIT } // 9
82 // Make a tone of specified freq and total_len with trans_len ramp in
83 // and out and only the first len bytes with sound, the rest with
85 void make_tone( unsigned char *buf, int freq,
86 int len, int total_len, int trans_len )
90 for ( i = 0; i < trans_len; ++i ) {
91 float level = ( sin( (double) i * SGD_2PI / (BYTES_PER_SECOND / freq) ) )
92 * ((double)i / trans_len) / 2.0 + 0.5;
94 /* Convert to unsigned byte */
95 buf[ i ] = (unsigned char) ( level * 255.0 ) ;
98 for ( i = trans_len; i < len - trans_len; ++i ) {
99 float level = ( sin( (double) i * SGD_2PI / (BYTES_PER_SECOND / freq) ) )
102 /* Convert to unsigned byte */
103 buf[ i ] = (unsigned char) ( level * 255.0 ) ;
106 for ( i = len - trans_len; i < len; ++i ) {
107 float level = ( sin( (double) i * SGD_2PI / (BYTES_PER_SECOND / freq) ) )
108 * ((double)j / trans_len) / 2.0 + 0.5;
111 /* Convert to unsigned byte */
112 buf[ i ] = (unsigned char) ( level * 255.0 ) ;
114 for ( i = len; i < total_len; ++i ) {
115 buf[ i ] = (unsigned char) ( 0.5 * 255.0 ) ;
120 // allocate and initialize sound samples
121 bool FGMorse::init() {
123 make_tone( lo_dit, LO_FREQUENCY, DIT_SIZE - COUNT_SIZE, DIT_SIZE,
127 make_tone( hi_dit, HI_FREQUENCY, DIT_SIZE - COUNT_SIZE, DIT_SIZE,
131 make_tone( lo_dah, LO_FREQUENCY, DAH_SIZE - COUNT_SIZE, DAH_SIZE,
135 make_tone( hi_dah, HI_FREQUENCY, DAH_SIZE - COUNT_SIZE, DAH_SIZE,
140 for ( i = 0; i < SPACE_SIZE; ++i ) {
141 space[ i ] = (unsigned char) ( 0.5 * 255 ) ;
148 // allocate and initialize sound samples
149 bool FGMorse::cust_init(const int freq ) {
153 make_tone( cust_dit, freq, DIT_SIZE - COUNT_SIZE, DIT_SIZE,
157 make_tone( cust_dah, freq, DAH_SIZE - COUNT_SIZE, DAH_SIZE,
161 for ( i = 0; i < SPACE_SIZE; ++i ) {
162 space[ i ] = (unsigned char) ( 0.5 * 255 ) ;
169 // make a SGSoundSample morse code transmission for the specified string
170 SGSoundSample *FGMorse::make_ident( const string& id, const int freq ) {
172 char *idptr = (char *)id.c_str();
177 // 0. Select the frequency. If custom frequency, generate the
178 // sound fragments we need on the fly.
179 unsigned char *dit_ptr, *dah_ptr;
181 if ( freq == LO_FREQUENCY ) {
184 } else if ( freq == HI_FREQUENCY ) {
193 // 1. Determine byte length of message
194 for ( i = 0; i < (int)id.length(); ++i ) {
195 if ( idptr[i] >= 'A' && idptr[i] <= 'Z' ) {
196 int c = (int)(idptr[i] - 'A');
197 for ( j = 0; j < 4 && alphabet[c][j] != END; ++j ) {
198 if ( alphabet[c][j] == DIT ) {
200 } else if ( alphabet[c][j] == DAH ) {
204 length += SPACE_SIZE;
205 } else if ( idptr[i] >= '0' && idptr[i] <= '9' ) {
206 int c = (int)(idptr[i] - '0');
207 for ( j = 0; j < 5; ++j) {
208 if ( numerals[c][j] == DIT ) {
210 } else if ( numerals[c][j] == DAH ) {
214 length += SPACE_SIZE;
216 // skip unknown character
219 // add 2x more space to the end of the string
220 length += 2 * SPACE_SIZE;
222 // 2. Allocate space for the message
223 const unsigned char* buffer = (const unsigned char *)malloc(length);
225 // 3. Assemble the message;
226 unsigned char *buf_ptr = (unsigned char*)buffer;
228 for ( i = 0; i < (int)id.length(); ++i ) {
229 if ( idptr[i] >= 'A' && idptr[i] <= 'Z' ) {
230 int c = (int)(idptr[i] - 'A');
231 for ( j = 0; j < 4 && alphabet[c][j] != END; ++j ) {
232 if ( alphabet[c][j] == DIT ) {
233 memcpy( buf_ptr, dit_ptr, DIT_SIZE );
235 } else if ( alphabet[c][j] == DAH ) {
236 memcpy( buf_ptr, dah_ptr, DAH_SIZE );
240 memcpy( buf_ptr, space, SPACE_SIZE );
241 buf_ptr += SPACE_SIZE;
242 } else if ( idptr[i] >= '0' && idptr[i] <= '9' ) {
243 int c = (int)(idptr[i] - '0');
244 for ( j = 0; j < 5; ++j ) {
245 if ( numerals[c][j] == DIT ) {
246 memcpy( buf_ptr, dit_ptr, DIT_SIZE );
248 } else if ( numerals[c][j] == DAH ) {
249 memcpy( buf_ptr, dah_ptr, DAH_SIZE );
253 memcpy( buf_ptr, space, SPACE_SIZE );
254 buf_ptr += SPACE_SIZE;
256 // skip unknown character
259 memcpy( buf_ptr, space, SPACE_SIZE );
260 buf_ptr += SPACE_SIZE;
261 memcpy( buf_ptr, space, SPACE_SIZE );
262 buf_ptr += SPACE_SIZE;
264 // 4. create the simple sound and return
265 SGSoundSample *sample = new SGSoundSample( &buffer, length,
268 sample->set_reference_dist( 10.0 );
269 sample->set_max_dist( 20.0 );