]> git.mxchange.org Git - flightgear.git/blob - src/Main/morse.hxx
Continued work on morse code generator.
[flightgear.git] / src / Main / morse.hxx
1 // morse.hxx -- 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 #ifndef _MORSE_HXX
25 #define _MORSE_HXX
26
27 #ifdef HAVE_CONFIG_H
28 #  include <config.h>
29 #endif
30
31 #include <simgear/compiler.h>
32
33 #include <plib/sl.h>
34 #include <plib/sm.h>
35
36 #include "soundmgr.hxx"
37
38
39 // Quoting from http://www.kluft.com/~ikluft/ham/morse-intro.html by
40 // Ian Kluft KO6YQ <ikluft@kluft.com>
41 //
42 // [begin quote]
43 //
44 // What is the Standard for Measuring Morse Code Speed?
45 // 
46 // [This was adapted from the Ham Radio FAQ which used to be posted on UseNet.] 
47 // 
48 // The word PARIS was chosen as the standard length for CW code
49 // speed. Each dit counts for one count, each dah counts for three
50 // counts, intra-character spacing is one count, inter-character
51 // spacing is three counts and inter-word spacing is seven counts, so
52 // the word PARIS is exactly 50 counts:
53 // 
54 // PPPPPPPPPPPPPP    AAAAAA    RRRRRRRRRR    IIIIII    SSSSSSSSSS
55 // di  da  da  di    di  da    di  da  di    di  di    di  di  di
56 // 1 1 3 1 3 1 1  3  1 1 3  3  1 1 3 1 1  3  1 1 1  3  1 1 1 1 1  7 = 50
57 //   ^                      ^                                     ^
58 //   ^Intra-character       ^Inter-character            Inter-word^
59 // 
60 // So 5 words-per-minute = 250 counts-per-minute / 50 counts-per-word
61 // or one count every 240 milliseconds. 13 words-per-minute is one
62 // count every ~92.3 milliseconds. This method of sending code is
63 // sometimes called "Slow Code", because at 5 wpm it sounds VERY SLOW.
64 // 
65 // The "Farnsworth" method is accomplished by sending the dits and
66 // dahs and intra-character spacing at a higher speed, then increasing
67 // the inter-character and inter-word spacing to slow the sending
68 // speed down to the desired speed. For example, to send at 5 wpm with
69 // 13 wpm characters in Farnsworth method, the dits and
70 // intra-character spacing would be 92.3 milliseconds, the dah would
71 // be 276.9 milliseconds, the inter-character spacing would be 1.443
72 // seconds and inter-word spacing would be 3.367 seconds.
73 //
74 // [end quote]
75
76 // Ok, back to Curt 
77
78 // My formulation is based dit = 1 count, dah = 3 counts, 1 count for
79 // intRA-character space, 3 counts for intER-character space.  Target
80 // is 5 wpm which by the above means 1 count = 240 milliseconds.
81 // 
82 // AIM 1-1-7 (f) states that the frequency of the tone should be 1020
83 // Hz for the VOR ident.
84
85
86 static const char DI = '1';
87 static const char DIT = '1';
88 static const char DA = '2';
89 static const char DAH = '2';
90 static const char end = '0';
91
92 static const int BYTES_PER_SECOND = 8000;
93 static const int BEAT_LENGTH = 240; // milleseconds (5 wpm)
94 static const int TRANSITION_BYTES = 40;
95 static const int COUNT_SIZE = BYTES_PER_SECOND * BEAT_LENGTH / 1000;
96 static const int DIT_SIZE = 2 * COUNT_SIZE;   // 2 counts
97 static const int DAH_SIZE = 4 * COUNT_SIZE;   // 4 counts
98 static const int SPACE_SIZE = 3 * COUNT_SIZE; // 3 counts
99 static const int FREQUENCY = 1020;       // AIM 1-1-7 (f) specified in Hz
100
101 static char alphabet[26][4] = {
102     { DI, DAH, end, end },      /* A */ 
103     { DA, DI, DI, DIT },        /* B */ 
104     { DA, DI, DA, DIT },        /* C */ 
105     { DA, DI, DIT, end },       /* D */ 
106     { DIT, end, end, end },     /* E */ 
107     { DI, DI, DA, DIT },        /* F */ 
108     { DA, DA, DIT, end },       /* G */ 
109     { DI, DI, DI, DIT },        /* H */ 
110     { DI, DIT, end, end },      /* I */ 
111     { DI, DA, DA, DAH },        /* J */ 
112     { DA, DI, DAH, end },       /* K */ 
113     { DI, DA, DI, DIT },        /* L */ 
114     { DA, DAH, end, end },      /* M */ 
115     { DA, DIT, end, end },      /* N */ 
116     { DA, DA, DAH, end },       /* O */ 
117     { DI, DA, DA, DIT },        /* P */ 
118     { DA, DA, DI, DAH },        /* Q */ 
119     { DI, DA, DIT, end },       /* R */ 
120     { DI, DI, DIT, end },       /* S */ 
121     { DAH, end, end, end },     /* T */ 
122     { DI, DI, DAH, end },       /* U */ 
123     { DI, DI, DI, DAH },        /* V */ 
124     { DI, DA, DAH, end },       /* W */ 
125     { DA, DI, DI, DAH },        /* X */ 
126     { DA, DI, DA, DAH },        /* Y */ 
127     { DA, DA, DI, DIT }         /* Z */ 
128 };
129
130 // manages everything we need to know for an individual sound sample
131 class FGMorse {
132
133     unsigned char dit[ DIT_SIZE ] ;
134     unsigned char dah[ DAH_SIZE ] ;
135     unsigned char space[ SPACE_SIZE ] ;
136
137 public:
138
139     FGMorse();
140     ~FGMorse();
141
142     // allocate and initialize sound samples
143     bool init();
144
145     // make a FGSimpleSound morse code transmission for the specified string
146     FGSimpleSound make_ident( const string& id );
147 };
148
149
150 #endif // _MORSE_HXX
151
152