1 // morse.hxx -- Morse code generation class
3 // Written by Curtis Olson, started March 2001.
5 // Copyright (C) 2001 Curtis L. Olson - curt@flightgear.org
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.
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.
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.
31 #include <simgear/compiler.h>
32 #include <simgear/sound/soundmgr.hxx>
38 // Quoting from http://www.kluft.com/~ikluft/ham/morse-intro.html by
39 // Ian Kluft KO6YQ <ikluft@kluft.com>
43 // What is the Standard for Measuring Morse Code Speed?
45 // [This was adapted from the Ham Radio FAQ which used to be posted on UseNet.]
47 // The word PARIS was chosen as the standard length for CW code
48 // speed. Each dit counts for one count, each dah counts for three
49 // counts, intra-character spacing is one count, inter-character
50 // spacing is three counts and inter-word spacing is seven counts, so
51 // the word PARIS is exactly 50 counts:
53 // PPPPPPPPPPPPPP AAAAAA RRRRRRRRRR IIIIII SSSSSSSSSS
54 // di da da di di da di da di di di di di di
55 // 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 // ^Intra-character ^Inter-character Inter-word^
59 // So 5 words-per-minute = 250 counts-per-minute / 50 counts-per-word
60 // or one count every 240 milliseconds. 13 words-per-minute is one
61 // count every ~92.3 milliseconds. This method of sending code is
62 // sometimes called "Slow Code", because at 5 wpm it sounds VERY SLOW.
64 // The "Farnsworth" method is accomplished by sending the dits and
65 // dahs and intra-character spacing at a higher speed, then increasing
66 // the inter-character and inter-word spacing to slow the sending
67 // speed down to the desired speed. For example, to send at 5 wpm with
68 // 13 wpm characters in Farnsworth method, the dits and
69 // intra-character spacing would be 92.3 milliseconds, the dah would
70 // be 276.9 milliseconds, the inter-character spacing would be 1.443
71 // seconds and inter-word spacing would be 3.367 seconds.
77 // My formulation is based dit = 1 count, dah = 3 counts, 1 count for
78 // intRA-character space, 3 counts for intER-character space. Target
79 // is 5 wpm which by the above means 1 count = 240 milliseconds.
81 // AIM 1-1-7 (f) states that the frequency of the tone should be 1020
82 // Hz for the VOR ident.
85 static const char DI = '1';
86 static const char DIT = '1';
87 static const char DA = '2';
88 static const char DAH = '2';
89 static const char end = '0';
91 static const int BYTES_PER_SECOND = 8000;
92 // static const int BEAT_LENGTH = 240; // milleseconds (5 wpm)
93 static const int BEAT_LENGTH = 92; // milleseconds (13 wpm)
94 static const int TRANSITION_BYTES = (int)(0.005 * BYTES_PER_SECOND);
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 LO_FREQUENCY = 1020; // AIM 1-1-7 (f) specified in Hz
100 static const int HI_FREQUENCY = 1350; // AIM 1-1-7 (f) specified in Hz
102 // manages everything we need to know for an individual sound sample
107 unsigned char hi_dit[ DIT_SIZE ] ;
108 unsigned char lo_dit[ DIT_SIZE ] ;
109 unsigned char hi_dah[ DAH_SIZE ] ;
110 unsigned char lo_dah[ DAH_SIZE ] ;
111 unsigned char space[ SPACE_SIZE ] ;
113 unsigned char cust_dit[ DIT_SIZE ] ;
114 unsigned char cust_dah[ DAH_SIZE ] ;
116 bool cust_init( const int freq );
123 // allocate and initialize sound samples
126 // make a SimpleSound morse code transmission for the specified string
127 SGSimpleSound *make_ident( const string& id,
128 const int freq = LO_FREQUENCY );
134 * Make a tone of specified freq and total_len with trans_len ramp in
135 * and out and only the first len bytes with sound, the rest with
137 * @param buf unsigned char pointer to sound buffer
138 * @param freq desired frequency of tone
139 * @param len length of tone within sound
140 * @param total_len total length of sound (anything more than len is padded
142 * @param trans_len length of ramp up and ramp down to avoid audio "pop"
144 void make_tone( unsigned char *buf, int freq,
145 int len, int total_len, int trans_len );