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