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.
23 #include <simgear/constants.h>
27 #include <simgear/sound/sample_openal.hxx>
30 static const char DI = '1';
31 static const char DIT = '1';
32 static const char DA = '2';
33 static const char DAH = '2';
34 static const char END = '0';
36 static const char alphabet[26][4] = {
37 { DI, DAH, END, END }, /* A */
38 { DA, DI, DI, DIT }, /* B */
39 { DA, DI, DA, DIT }, /* C */
40 { DA, DI, DIT, END }, /* D */
41 { DIT, END, END, END }, /* E */
42 { DI, DI, DA, DIT }, /* F */
43 { DA, DA, DIT, END }, /* G */
44 { DI, DI, DI, DIT }, /* H */
45 { DI, DIT, END, END }, /* I */
46 { DI, DA, DA, DAH }, /* J */
47 { DA, DI, DAH, END }, /* K */
48 { DI, DA, DI, DIT }, /* L */
49 { DA, DAH, END, END }, /* M */
50 { DA, DIT, END, END }, /* N */
51 { DA, DA, DAH, END }, /* O */
52 { DI, DA, DA, DIT }, /* P */
53 { DA, DA, DI, DAH }, /* Q */
54 { DI, DA, DIT, END }, /* R */
55 { DI, DI, DIT, END }, /* S */
56 { DAH, END, END, END }, /* T */
57 { DI, DI, DAH, END }, /* U */
58 { DI, DI, DI, DAH }, /* V */
59 { DI, DA, DAH, END }, /* W */
60 { DA, DI, DI, DAH }, /* X */
61 { DA, DI, DA, DAH }, /* Y */
62 { DA, DA, DI, DIT } /* Z */
65 static const char numerals[10][5] = {
66 { DA, DA, DA, DA, DAH }, // 0
67 { DI, DA, DA, DA, DAH }, // 1
68 { DI, DI, DA, DA, DAH }, // 2
69 { DI, DI, DI, DA, DAH }, // 3
70 { DI, DI, DI, DI, DAH }, // 4
71 { DI, DI, DI, DI, DIT }, // 5
72 { DA, DI, DI, DI, DIT }, // 6
73 { DA, DA, DI, DI, DIT }, // 7
74 { DA, DA, DA, DI, DIT }, // 8
75 { DA, DA, DA, DA, DIT } // 9
88 // allocate and initialize sound samples
89 bool FGMorse::init() {
91 make_tone( lo_dit, LO_FREQUENCY, DIT_SIZE - COUNT_SIZE, DIT_SIZE,
95 make_tone( hi_dit, HI_FREQUENCY, DIT_SIZE - COUNT_SIZE, DIT_SIZE,
99 make_tone( lo_dah, LO_FREQUENCY, DAH_SIZE - COUNT_SIZE, DAH_SIZE,
103 make_tone( hi_dah, HI_FREQUENCY, DAH_SIZE - COUNT_SIZE, DAH_SIZE,
108 for ( i = 0; i < SPACE_SIZE; ++i ) {
109 space[ i ] = (unsigned char) ( 0.5 * 255 ) ;
116 // allocate and initialize sound samples
117 bool FGMorse::cust_init(const int freq ) {
121 make_tone( cust_dit, freq, DIT_SIZE - COUNT_SIZE, DIT_SIZE,
125 make_tone( cust_dah, freq, DAH_SIZE - COUNT_SIZE, DAH_SIZE,
129 for ( i = 0; i < SPACE_SIZE; ++i ) {
130 space[ i ] = (unsigned char) ( 0.5 * 255 ) ;
137 // make a SGSoundSample morse code transmission for the specified string
138 SGSoundSample *FGMorse::make_ident( const std::string& id, const int freq ) {
140 char *idptr = (char *)id.c_str();
145 // 0. Select the frequency. If custom frequency, generate the
146 // sound fragments we need on the fly.
147 unsigned char *dit_ptr, *dah_ptr;
149 if ( freq == LO_FREQUENCY ) {
152 } else if ( freq == HI_FREQUENCY ) {
161 // 1. Determine byte length of message
162 for ( i = 0; i < (int)id.length(); ++i ) {
163 if ( idptr[i] >= 'A' && idptr[i] <= 'Z' ) {
164 int c = (int)(idptr[i] - 'A');
165 for ( j = 0; j < 4 && alphabet[c][j] != END; ++j ) {
166 if ( alphabet[c][j] == DIT ) {
168 } else if ( alphabet[c][j] == DAH ) {
172 length += SPACE_SIZE;
173 } else if ( idptr[i] >= '0' && idptr[i] <= '9' ) {
174 int c = (int)(idptr[i] - '0');
175 for ( j = 0; j < 5; ++j) {
176 if ( numerals[c][j] == DIT ) {
178 } else if ( numerals[c][j] == DAH ) {
182 length += SPACE_SIZE;
184 // skip unknown character
187 // add 2x more space to the end of the string
188 length += 2 * SPACE_SIZE;
190 // 2. Allocate space for the message
191 const unsigned char* buffer = (const unsigned char *)malloc(length);
193 // 3. Assemble the message;
194 unsigned char *buf_ptr = (unsigned char*)buffer;
196 for ( i = 0; i < (int)id.length(); ++i ) {
197 if ( idptr[i] >= 'A' && idptr[i] <= 'Z' ) {
198 int c = (int)(idptr[i] - 'A');
199 for ( j = 0; j < 4 && alphabet[c][j] != END; ++j ) {
200 if ( alphabet[c][j] == DIT ) {
201 memcpy( buf_ptr, dit_ptr, DIT_SIZE );
203 } else if ( alphabet[c][j] == DAH ) {
204 memcpy( buf_ptr, dah_ptr, DAH_SIZE );
208 memcpy( buf_ptr, space, SPACE_SIZE );
209 buf_ptr += SPACE_SIZE;
210 } else if ( idptr[i] >= '0' && idptr[i] <= '9' ) {
211 int c = (int)(idptr[i] - '0');
212 for ( j = 0; j < 5; ++j ) {
213 if ( numerals[c][j] == DIT ) {
214 memcpy( buf_ptr, dit_ptr, DIT_SIZE );
216 } else if ( numerals[c][j] == DAH ) {
217 memcpy( buf_ptr, dah_ptr, DAH_SIZE );
221 memcpy( buf_ptr, space, SPACE_SIZE );
222 buf_ptr += SPACE_SIZE;
224 // skip unknown character
227 memcpy( buf_ptr, space, SPACE_SIZE );
228 buf_ptr += SPACE_SIZE;
229 memcpy( buf_ptr, space, SPACE_SIZE );
230 buf_ptr += SPACE_SIZE;
232 // 4. create the simple sound and return
233 SGSoundSample *sample = new SGSoundSample( &buffer, length,
236 sample->set_reference_dist( 10.0 );
237 sample->set_max_dist( 20.0 );
242 FGMorse * FGMorse::_instance = NULL;
244 FGMorse * FGMorse::instance()
246 if( _instance == NULL ) {
247 _instance = new FGMorse();