]> git.mxchange.org Git - flightgear.git/blob - src/Sound/audioident.cxx
Make the view-manager and sound-manager independent.
[flightgear.git] / src / Sound / audioident.cxx
1 // audioident.cxx -- audible station identifiers
2 //
3 // Written by Torsten Dreyer, September 2011
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 "audioident.hxx"
23 #include <simgear/sg_inlines.h>
24
25 #include <Main/globals.hxx>
26 #include <Sound/morse.hxx>
27
28 AudioIdent::AudioIdent( const std::string & fx_name, const double interval_secs, const int frequency_hz ) :
29   _fx_name(fx_name),
30   _frequency(frequency_hz),
31   _timer(0.0),
32   _interval(interval_secs),
33   _running(false)
34 {
35 }
36
37 void AudioIdent::init()
38 {
39     _timer = 0.0;
40     _ident = "";
41     _running = false;
42     _sgr = globals->get_soundmgr()->find("avionics", true);
43     _sgr->tie_to_listener();
44 }
45
46 void AudioIdent::stop()
47 {
48     if( _sgr->exists( _fx_name ) )
49         _sgr->stop( _fx_name );
50     _running = false;
51 }
52
53 void AudioIdent::start()
54 {
55     _timer = _interval;
56     _sgr->play_once(_fx_name);
57     _running = true;
58 }
59
60 void AudioIdent::setVolumeNorm( double volumeNorm )
61 {
62     SG_CLAMP_RANGE(volumeNorm, 0.0, 1.0);
63
64     SGSoundSample *sound = _sgr->find( _fx_name );
65
66     if ( sound != NULL ) {
67         sound->set_volume( volumeNorm );
68     }
69 }
70
71 void AudioIdent::setIdent( const std::string & ident, double volumeNorm )
72 {
73     // Signal may flicker very frequently (due to our realistic newnavradio...).
74     // Avoid recreating identical sound samples all the time, instead turn off
75     // volume when signal is lost, and save the most recent sample.
76     if (ident.empty())
77         volumeNorm = 0;
78
79     if(( _ident == ident )||
80        (volumeNorm == 0))  // don't bother with sounds when volume is OFF anyway...
81     {
82         if( false == _ident.empty() )
83             setVolumeNorm( volumeNorm );
84         return;
85     }
86
87     try {
88         stop();
89
90         if ( _sgr->exists( _fx_name ) )
91             _sgr->remove( _fx_name );
92
93         if( false == ident.empty() ) {
94
95             SGSoundSample* sound = FGMorse::instance()->make_ident(ident, _frequency );
96             sound->set_volume( volumeNorm );
97             if (!_sgr->add( sound, _fx_name )) {
98                 SG_LOG(SG_SOUND, SG_WARN, "Failed to add sound '" << _fx_name << "' for ident '" << ident << "'" );
99                 delete sound;
100                 return;
101             }
102
103             start();
104         }
105         _ident = ident;
106
107     } catch (sg_io_exception& e) {
108         SG_LOG(SG_SOUND, SG_ALERT, e.getFormattedMessage());
109     }
110
111 }
112
113 void AudioIdent::update( double dt )
114 {
115     // single-shot
116     if( false == _running || _interval < SGLimitsd::min() ) 
117         return;
118
119     _timer -= dt;
120
121     if( _timer < SGLimitsd::min() ) {
122         _timer = _interval;
123         stop();
124         start();
125     }
126 }
127
128 // FIXME: shall transmit at least 6 wpm (ICAO Annex 10 - 3.5.3.6.3)
129 DMEAudioIdent::DMEAudioIdent( const std::string & fx_name )
130 : AudioIdent( fx_name, 40, FGMorse::HI_FREQUENCY )
131 {
132 }
133
134
135 //FIXME: for co-located VOR/DME or ILS/DME, assign four time-slots
136 // 3xVOR/ILS ident, 1xDME ident
137
138 // FIXME: shall transmit at approx. 7 wpm (ICAO Annex 10 - 3.3.6.5.1)
139 VORAudioIdent::VORAudioIdent( const std::string & fx_name )
140 : AudioIdent( fx_name, 10, FGMorse::LO_FREQUENCY )
141 {
142 }
143
144 //FIXME: LOCAudioIdent at approx 7wpm (ICAO Annex 10 - 3.1.3.9.4)
145 // not less than six times per minute at approx equal intervals
146 // frequency 1020+/-50Hz (3.1.3.9.2)
147 LOCAudioIdent::LOCAudioIdent( const std::string & fx_name )
148 : AudioIdent( fx_name, 10, FGMorse::LO_FREQUENCY )
149 {
150 }
151
152
153 // FIXME: NDBAudioIdent at approx 7 wpm (ICAO ANNEX 10 - 3.4.5.1)
154 // at least once every 10s (3.4.5.2.1)
155 // frequency 1020+/-50Hz or 400+/-25Hz (3.4.5.4)