]> git.mxchange.org Git - flightgear.git/blob - src/Sound/morse.cxx
Merge branch 'next' of D:\Git_New\flightgear into next
[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 #include <cstring>
29
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 */ 
57 };
58
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
70 };
71
72
73 // constructor
74 FGMorse::FGMorse() {
75 }
76
77 // destructor
78 FGMorse::~FGMorse() {
79 }
80
81
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
84 // silence
85 void make_tone( unsigned char *buf, int freq, 
86                 int len, int total_len, int trans_len )
87 {
88     int i, j;
89
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;
93
94         /* Convert to unsigned byte */
95         buf[ i ] = (unsigned char) ( level * 255.0 ) ;
96     }
97
98     for ( i = trans_len; i < len - trans_len; ++i ) {
99         float level = ( sin( (double) i * SGD_2PI / (BYTES_PER_SECOND / freq) ) )
100             / 2.0 + 0.5;
101
102         /* Convert to unsigned byte */
103         buf[ i ] = (unsigned char) ( level * 255.0 ) ;
104     }
105     j = trans_len;
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;
109         --j;
110
111         /* Convert to unsigned byte */
112         buf[ i ] = (unsigned char) ( level * 255.0 ) ;
113     }
114     for ( i = len; i < total_len; ++i ) {
115         buf[ i ] = (unsigned char) ( 0.5 * 255.0 ) ;
116     }
117 }
118
119
120 // allocate and initialize sound samples
121 bool FGMorse::init() {
122     // Make Low DIT
123     make_tone( lo_dit, LO_FREQUENCY, DIT_SIZE - COUNT_SIZE, DIT_SIZE,
124                TRANSITION_BYTES );
125
126     // Make High DIT
127     make_tone( hi_dit, HI_FREQUENCY, DIT_SIZE - COUNT_SIZE, DIT_SIZE,
128                TRANSITION_BYTES );
129
130     // Make Low DAH
131     make_tone( lo_dah, LO_FREQUENCY, DAH_SIZE - COUNT_SIZE, DAH_SIZE,
132                TRANSITION_BYTES );
133
134     // Make High DAH
135     make_tone( hi_dah, HI_FREQUENCY, DAH_SIZE - COUNT_SIZE, DAH_SIZE,
136                TRANSITION_BYTES );
137
138     // Make SPACE
139     int i;
140     for ( i = 0; i < SPACE_SIZE; ++i ) {
141         space[ i ] = (unsigned char) ( 0.5 * 255 ) ;
142     }
143
144     return true;
145 }
146
147
148 // allocate and initialize sound samples
149 bool FGMorse::cust_init(const int freq ) {
150     int i;
151
152     // Make DIT
153     make_tone( cust_dit, freq, DIT_SIZE - COUNT_SIZE, DIT_SIZE,
154                TRANSITION_BYTES );
155
156     // Make DAH
157     make_tone( cust_dah, freq, DAH_SIZE - COUNT_SIZE, DAH_SIZE,
158                TRANSITION_BYTES );
159
160     // Make SPACE
161     for ( i = 0; i < SPACE_SIZE; ++i ) {
162         space[ i ] = (unsigned char) ( 0.5 * 255 ) ;
163     }
164
165     return true;
166 }
167
168
169 // make a SGSoundSample morse code transmission for the specified string
170 SGSoundSample *FGMorse::make_ident( const string& id, const int freq ) {
171
172     char *idptr = (char *)id.c_str();
173
174     int length = 0;
175     int i, j;
176
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;
180
181     if ( freq == LO_FREQUENCY ) {
182         dit_ptr = lo_dit;
183         dah_ptr = lo_dah;
184     } else if ( freq == HI_FREQUENCY ) {
185         dit_ptr = hi_dit;
186         dah_ptr = hi_dah;
187     } else {
188         cust_init( freq );
189         dit_ptr = cust_dit;
190         dah_ptr = cust_dah;
191     }
192
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 ) {
199                     length += DIT_SIZE;
200                 } else if ( alphabet[c][j] == DAH ) {
201                     length += DAH_SIZE;
202                 }
203             }
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 ) {
209                     length += DIT_SIZE;
210                 } else if ( numerals[c][j] == DAH ) {
211                     length += DAH_SIZE;
212                 }
213             }
214             length += SPACE_SIZE;
215         } else {
216             // skip unknown character
217         }
218     }
219     // add 2x more space to the end of the string
220     length += 2 * SPACE_SIZE;
221
222     // 2. Allocate space for the message
223     const unsigned char* buffer = (const unsigned char *)malloc(length);
224
225     // 3. Assemble the message;
226     unsigned char *buf_ptr = (unsigned char*)buffer;
227
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 );
234                     buf_ptr += DIT_SIZE;
235                 } else if ( alphabet[c][j] == DAH ) {
236                     memcpy( buf_ptr, dah_ptr, DAH_SIZE );
237                     buf_ptr += DAH_SIZE;
238                 }
239             }
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 );
247                     buf_ptr += DIT_SIZE;
248                 } else if ( numerals[c][j] == DAH ) {
249                     memcpy( buf_ptr, dah_ptr, DAH_SIZE );
250                     buf_ptr += DAH_SIZE;
251                 }
252             }
253             memcpy( buf_ptr, space, SPACE_SIZE );
254             buf_ptr += SPACE_SIZE;
255         } else {
256             // skip unknown character
257         }
258     }
259     memcpy( buf_ptr, space, SPACE_SIZE );
260     buf_ptr += SPACE_SIZE;
261     memcpy( buf_ptr, space, SPACE_SIZE );
262     buf_ptr += SPACE_SIZE;
263
264     // 4. create the simple sound and return
265     SGSoundSample *sample = new SGSoundSample( &buffer, length,
266                                                BYTES_PER_SECOND );
267
268     sample->set_reference_dist( 10.0 );
269     sample->set_max_dist( 20.0 );
270
271     return sample;
272 }