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