]> git.mxchange.org Git - flightgear.git/blob - src/Sound/beacon.cxx
Some more refactoring of the radios
[flightgear.git] / src / Sound / beacon.cxx
1 // beacon.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 #include <stdlib.h>
23 #include <cstring>
24
25 #include "beacon.hxx"
26
27 #include <simgear/structure/exception.hxx>
28
29 // constructor
30 FGBeacon::FGBeacon()
31 {
32 }
33
34 // destructor
35 FGBeacon::~FGBeacon() {
36 }
37
38
39 // allocate and initialize sound samples
40 bool FGBeacon::init() {
41     unsigned char *ptr;
42     size_t i, len;
43
44     const unsigned char* inner_buf = (const unsigned char*)malloc( INNER_SIZE );
45     const unsigned char* middle_buf = (const unsigned char*)malloc(MIDDLE_SIZE);
46     const unsigned char* outer_buf = (const unsigned char*)malloc( OUTER_SIZE );
47
48     // Make inner marker beacon sound
49     len= (int)(INNER_DIT_LEN / 2.0 );
50     unsigned char inner_dit[INNER_DIT_LEN];
51     make_tone( inner_dit, INNER_FREQ, len, INNER_DIT_LEN,
52                TRANSITION_BYTES );
53
54     ptr = (unsigned char*)inner_buf;
55     for ( i = 0; i < 6; ++i ) {
56         memcpy( ptr, inner_dit, INNER_DIT_LEN );
57         ptr += INNER_DIT_LEN;
58     }
59
60     try {
61         inner = new SGSoundSample( &inner_buf, INNER_SIZE, BYTES_PER_SECOND );
62         inner->set_reference_dist( 10.0 );
63         inner->set_max_dist( 20.0 );
64
65         // Make middle marker beacon sound
66         len= (int)(MIDDLE_DIT_LEN / 2.0 );
67         unsigned char middle_dit[MIDDLE_DIT_LEN];
68         make_tone( middle_dit, MIDDLE_FREQ, len, MIDDLE_DIT_LEN,
69                     TRANSITION_BYTES );
70
71         len= (int)(MIDDLE_DAH_LEN * 3 / 4.0 );
72         unsigned char middle_dah[MIDDLE_DAH_LEN];
73         make_tone( middle_dah, MIDDLE_FREQ, len, MIDDLE_DAH_LEN,
74                     TRANSITION_BYTES );
75
76         ptr = (unsigned char*)middle_buf;
77         memcpy( ptr, middle_dit, MIDDLE_DIT_LEN );
78         ptr += MIDDLE_DIT_LEN;
79         memcpy( ptr, middle_dah, MIDDLE_DAH_LEN );
80
81         middle = new SGSoundSample( &middle_buf, MIDDLE_SIZE, BYTES_PER_SECOND);
82         middle->set_reference_dist( 10.0 );
83         middle->set_max_dist( 20.0 );
84
85         // Make outer marker beacon sound
86         len= (int)(OUTER_DAH_LEN * 3.0 / 4.0 );
87         unsigned char outer_dah[OUTER_DAH_LEN];
88         make_tone( outer_dah, OUTER_FREQ, len, OUTER_DAH_LEN,
89                     TRANSITION_BYTES );
90            
91         ptr = (unsigned char*)outer_buf;
92         memcpy( ptr, outer_dah, OUTER_DAH_LEN );
93         ptr += OUTER_DAH_LEN;
94         memcpy( ptr, outer_dah, OUTER_DAH_LEN );
95
96         outer = new SGSoundSample( &outer_buf, OUTER_SIZE, BYTES_PER_SECOND );
97         outer->set_reference_dist( 10.0 );
98         outer->set_max_dist( 20.0 );
99     } catch ( sg_io_exception &e ) {
100         SG_LOG(SG_SOUND, SG_ALERT, e.getFormattedMessage());
101     }
102
103     return true;
104 }
105
106 FGBeacon * FGBeacon::_instance = NULL;\r
107 \r
108 FGBeacon * FGBeacon::instance()\r
109 {\r
110     if( _instance == NULL ) {\r
111         _instance = new FGBeacon();\r
112         _instance->init();\r
113     }\r
114     return _instance;\r
115 }\r