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