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