]> git.mxchange.org Git - flightgear.git/blob - src/Sound/morse.cxx
Cosmetic changes to #include order.
[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 - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, 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
59 // constructor
60 FGMorse::FGMorse() {
61 }
62
63 // destructor
64 FGMorse::~FGMorse() {
65 }
66
67
68 // Make a tone of specified freq and total_len with trans_len ramp in
69 // and out and only the first len bytes with sound, the rest with
70 // silence
71 void make_tone( unsigned char *buf, int freq, 
72                 int len, int total_len, int trans_len )
73 {
74     int i, j;
75
76     for ( i = 0; i < trans_len; ++i ) {
77         float level = ( sin( (double) i * 2.0 * SGD_PI / (8000.0 / freq) ) )
78             * ((double)i / trans_len) / 2.0 + 0.5;
79
80         /* Convert to unsigned byte */
81         buf[ i ] = (unsigned char) ( level * 255.0 ) ;
82     }
83
84     for ( i = trans_len; i < len - trans_len; ++i ) {
85         float level = ( sin( (double) i * 2.0 * SGD_PI / (8000.0 / freq) ) )
86             / 2.0 + 0.5;
87
88         /* Convert to unsigned byte */
89         buf[ i ] = (unsigned char) ( level * 255.0 ) ;
90     }
91     j = trans_len;
92     for ( i = len - trans_len; i < len; ++i ) {
93         float level = ( sin( (double) i * 2.0 * SGD_PI / (8000.0 / freq) ) )
94             * ((double)j / trans_len) / 2.0 + 0.5;
95         --j;
96
97         /* Convert to unsigned byte */
98         buf[ i ] = (unsigned char) ( level * 255.0 ) ;
99     }
100     for ( i = len; i < total_len; ++i ) {
101         buf[ i ] = (unsigned char) ( 0.5 * 255.0 ) ;
102     }
103 }
104
105
106 // allocate and initialize sound samples
107 bool FGMorse::init() {
108     // Make Low DIT
109     make_tone( lo_dit, LO_FREQUENCY, DIT_SIZE - COUNT_SIZE, DIT_SIZE,
110                TRANSITION_BYTES );
111
112     // Make High DIT
113     make_tone( hi_dit, HI_FREQUENCY, DIT_SIZE - COUNT_SIZE, DIT_SIZE,
114                TRANSITION_BYTES );
115
116     // Make Low DAH
117     make_tone( lo_dah, LO_FREQUENCY, DAH_SIZE - COUNT_SIZE, DAH_SIZE,
118                TRANSITION_BYTES );
119
120     // Make High DAH
121     make_tone( hi_dah, HI_FREQUENCY, DAH_SIZE - COUNT_SIZE, DAH_SIZE,
122                TRANSITION_BYTES );
123
124     // Make SPACE
125     int i;
126     for ( i = 0; i < SPACE_SIZE; ++i ) {
127         space[ i ] = (unsigned char) ( 0.5 * 255 ) ;
128     }
129
130     return true;
131 }
132
133
134 // allocate and initialize sound samples
135 bool FGMorse::cust_init(const int freq ) {
136     int i;
137
138     // Make DIT
139     make_tone( cust_dit, freq, DIT_SIZE - COUNT_SIZE, DIT_SIZE,
140                TRANSITION_BYTES );
141
142     // Make DAH
143     make_tone( cust_dah, freq, DAH_SIZE - COUNT_SIZE, DAH_SIZE,
144                TRANSITION_BYTES );
145
146     // Make SPACE
147     for ( i = 0; i < SPACE_SIZE; ++i ) {
148         space[ i ] = (unsigned char) ( 0.5 * 255 ) ;
149     }
150
151     return true;
152 }
153
154
155 // make a FGSimpleSound morse code transmission for the specified string
156 FGSimpleSound *FGMorse::make_ident( const string& id, const int freq ) {
157     char *idptr = (char *)id.c_str();
158
159     int length = 0;
160     int i, j;
161
162     // 0. Select the frequency.  If custom frequency, generate the
163     // sound fragments we need on the fly.
164     unsigned char *dit_ptr, *dah_ptr;
165
166     if ( freq == LO_FREQUENCY ) {
167         dit_ptr = lo_dit;
168         dah_ptr = lo_dah;
169     } else if ( freq == HI_FREQUENCY ) {
170         dit_ptr = hi_dit;
171         dah_ptr = hi_dah;
172     } else {
173         cust_init( freq );
174         dit_ptr = cust_dit;
175         dah_ptr = cust_dah;
176     }
177
178     // 1. Determine byte length of message
179     for ( i = 0; i < (int)id.length(); ++i ) {
180         if ( idptr[i] >= 'A' && idptr[i] <= 'Z' ) {
181             char c = idptr[i] - 'A';
182             for ( j = 0; j < 4 || alphabet[c][j] == end; ++j ) {
183                 if ( alphabet[c][j] == DIT ) {
184                     length += DIT_SIZE;
185                 } else if ( alphabet[c][j] == DAH ) {
186                     length += DAH_SIZE;
187                 }
188             }
189             length += SPACE_SIZE;
190         } else {
191             // skip unknown character
192         }
193     }
194     // add 2x more space to the end of the string
195     length += 2 * SPACE_SIZE;
196
197     // 2. Allocate space for the message
198     unsigned char *buffer = new unsigned char[length];
199
200     // 3. Assemble the message;
201     unsigned char *buf_ptr = buffer;
202
203     for ( i = 0; i < (int)id.length(); ++i ) {
204         if ( idptr[i] >= 'A' && idptr[i] <= 'Z' ) {
205             char c = idptr[i] - 'A';
206             for ( j = 0; j < 4 || alphabet[c][j] == end; ++j ) {
207                 if ( alphabet[c][j] == DIT ) {
208                     memcpy( buf_ptr, dit_ptr, DIT_SIZE );
209                     buf_ptr += DIT_SIZE;
210                 } else if ( alphabet[c][j] == DAH ) {
211                     memcpy( buf_ptr, dah_ptr, DAH_SIZE );
212                     buf_ptr += DAH_SIZE;
213                 }
214             }
215             memcpy( buf_ptr, space, SPACE_SIZE );
216             buf_ptr += SPACE_SIZE;
217         } else {
218             // skip unknown character
219         }
220     }
221     memcpy( buf_ptr, space, SPACE_SIZE );
222     buf_ptr += SPACE_SIZE;
223     memcpy( buf_ptr, space, SPACE_SIZE );
224     buf_ptr += SPACE_SIZE;
225
226     // 4. create the simple sound and return
227     FGSimpleSound *sample = new FGSimpleSound( buffer, length );
228
229     return sample;
230 }