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