]> git.mxchange.org Git - flightgear.git/blob - src/Sound/morse.cxx
Fix MSVC compilation
[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 // $Id$
22
23
24 #include <simgear/constants.h>
25
26 #include <Main/fg_props.hxx>
27 #include <Main/globals.hxx>
28
29 #include "morse.hxx"
30
31
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 */ 
59 };
60
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
72 };
73
74
75 // constructor
76 FGMorse::FGMorse() {
77 }
78
79 // destructor
80 FGMorse::~FGMorse() {
81 }
82
83
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
86 // silence
87 void make_tone( unsigned char *buf, int freq, 
88                 int len, int total_len, int trans_len )
89 {
90     int i, j;
91
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;
95
96         /* Convert to unsigned byte */
97         buf[ i ] = (unsigned char) ( level * 255.0 ) ;
98     }
99
100     for ( i = trans_len; i < len - trans_len; ++i ) {
101         float level = ( sin( (double) i * SGD_2PI / (BYTES_PER_SECOND / freq) ) )
102             / 2.0 + 0.5;
103
104         /* Convert to unsigned byte */
105         buf[ i ] = (unsigned char) ( level * 255.0 ) ;
106     }
107     j = trans_len;
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;
111         --j;
112
113         /* Convert to unsigned byte */
114         buf[ i ] = (unsigned char) ( level * 255.0 ) ;
115     }
116     for ( i = len; i < total_len; ++i ) {
117         buf[ i ] = (unsigned char) ( 0.5 * 255.0 ) ;
118     }
119 }
120
121
122 // allocate and initialize sound samples
123 bool FGMorse::init() {
124     // Make Low DIT
125     make_tone( lo_dit, LO_FREQUENCY, DIT_SIZE - COUNT_SIZE, DIT_SIZE,
126                TRANSITION_BYTES );
127
128     // Make High DIT
129     make_tone( hi_dit, HI_FREQUENCY, DIT_SIZE - COUNT_SIZE, DIT_SIZE,
130                TRANSITION_BYTES );
131
132     // Make Low DAH
133     make_tone( lo_dah, LO_FREQUENCY, DAH_SIZE - COUNT_SIZE, DAH_SIZE,
134                TRANSITION_BYTES );
135
136     // Make High DAH
137     make_tone( hi_dah, HI_FREQUENCY, DAH_SIZE - COUNT_SIZE, DAH_SIZE,
138                TRANSITION_BYTES );
139
140     // Make SPACE
141     int i;
142     for ( i = 0; i < SPACE_SIZE; ++i ) {
143         space[ i ] = (unsigned char) ( 0.5 * 255 ) ;
144     }
145
146     return true;
147 }
148
149
150 // allocate and initialize sound samples
151 bool FGMorse::cust_init(const int freq ) {
152     int i;
153
154     // Make DIT
155     make_tone( cust_dit, freq, DIT_SIZE - COUNT_SIZE, DIT_SIZE,
156                TRANSITION_BYTES );
157
158     // Make DAH
159     make_tone( cust_dah, freq, DAH_SIZE - COUNT_SIZE, DAH_SIZE,
160                TRANSITION_BYTES );
161
162     // Make SPACE
163     for ( i = 0; i < SPACE_SIZE; ++i ) {
164         space[ i ] = (unsigned char) ( 0.5 * 255 ) ;
165     }
166
167     return true;
168 }
169
170
171 // make a SGSoundSample morse code transmission for the specified string
172 SGSoundSample *FGMorse::make_ident( const string& id, const int freq ) {
173
174     if (globals->get_soundmgr()->is_working() == false) {
175        return 0;
176     }
177
178     char *idptr = (char *)id.c_str();
179
180     int length = 0;
181     int i, j;
182
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;
186
187     if ( freq == LO_FREQUENCY ) {
188         dit_ptr = lo_dit;
189         dah_ptr = lo_dah;
190     } else if ( freq == HI_FREQUENCY ) {
191         dit_ptr = hi_dit;
192         dah_ptr = hi_dah;
193     } else {
194         cust_init( freq );
195         dit_ptr = cust_dit;
196         dah_ptr = cust_dah;
197     }
198
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 ) {
205                     length += DIT_SIZE;
206                 } else if ( alphabet[c][j] == DAH ) {
207                     length += DAH_SIZE;
208                 }
209             }
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 ) {
215                     length += DIT_SIZE;
216                 } else if ( numerals[c][j] == DAH ) {
217                     length += DAH_SIZE;
218                 }
219             }
220             length += SPACE_SIZE;
221         } else {
222             // skip unknown character
223         }
224     }
225     // add 2x more space to the end of the string
226     length += 2 * SPACE_SIZE;
227
228     // 2. Allocate space for the message
229     unsigned char *buffer = new unsigned char[length];
230
231     // 3. Assemble the message;
232     unsigned char *buf_ptr = buffer;
233
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 );
240                     buf_ptr += DIT_SIZE;
241                 } else if ( alphabet[c][j] == DAH ) {
242                     memcpy( buf_ptr, dah_ptr, DAH_SIZE );
243                     buf_ptr += DAH_SIZE;
244                 }
245             }
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 );
253                     buf_ptr += DIT_SIZE;
254                 } else if ( numerals[c][j] == DAH ) {
255                     memcpy( buf_ptr, dah_ptr, DAH_SIZE );
256                     buf_ptr += DAH_SIZE;
257                 }
258             }
259             memcpy( buf_ptr, space, SPACE_SIZE );
260             buf_ptr += SPACE_SIZE;
261         } else {
262             // skip unknown character
263         }
264     }
265     memcpy( buf_ptr, space, SPACE_SIZE );
266     buf_ptr += SPACE_SIZE;
267     memcpy( buf_ptr, space, SPACE_SIZE );
268     buf_ptr += SPACE_SIZE;
269
270     // 4. create the simple sound and return
271     SGSoundSample *sample = new SGSoundSample( buffer, length,
272                                                BYTES_PER_SECOND );
273
274     // clean up the buffer
275     delete [] buffer;
276
277     sample->set_reference_dist( 10.0 );
278     sample->set_max_dist( 20.0 );
279
280     return sample;
281 }