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