]> git.mxchange.org Git - flightgear.git/blobdiff - src/ATCDCL/ATC.cxx
Merge branch 'next' into durk-atc
[flightgear.git] / src / ATCDCL / ATC.cxx
index 193b824572c102dd0e9cb6ac111ebe3cb9c9f7bc..30d3d23e8dc3ff82f9b4a0a94cfb5d93379fe8ef 100644 (file)
@@ -35,7 +35,9 @@
 
 
 FGATC::FGATC() :
-  _voiceOK(false),
+       _playing(false),
+       _voiceOK(false),
+       _sgr(NULL),
        freqClear(true),
        receiving(false),
        respond(false),
@@ -49,12 +51,19 @@ FGATC::FGATC() :
        pending_transmission(""),
        _timeout(0),
        _pending(false),
-       _callback_code(0),
        _transmit(false),
        _transmitting(false),
        _counter(0.0),
        _max_count(5.0)
 {
+       SGSoundMgr *smgr = globals->get_soundmgr();
+       _sgr = smgr->find("atc", true);
+        _sgr->tie_to_listener();
+
+       _volume = fgGetNode("/sim/sound/atc/volume", true);
+       _enabled = fgGetNode("/sim/sound/atc/enabled", true);
+       _atc_external = fgGetNode("/sim/sound/atc/external-view", true);
+       _internal = fgGetNode("/sim/current-view/internal", true);
 }
 
 FGATC::~FGATC() {
@@ -103,6 +112,18 @@ void FGATC::Update(double dt) {
                }
        }
 
+#ifdef ENABLE_AUDIO_SUPPORT
+       bool active = _atc_external->getBoolValue() ||
+                     _internal->getBoolValue();
+
+       if ( active && _enabled->getBoolValue() ) {
+               _sgr->set_volume( _volume->getFloatValue() );
+               _sgr->resume(); // no-op if already in resumed state
+       } else {
+               _sgr->suspend();
+       }
+#endif
+
        if(_transmit) {
                _counter = 0.0;
                _max_count = 5.0;               // FIXME - hardwired length of message - need to calculate it!
@@ -112,11 +133,6 @@ void FGATC::Update(double dt) {
                        //Render(pending_transmission, ident, false);
                        Render(pending_transmission);
                }
-               // Run the callback regardless of whether on same freq as user or not.
-               //cout << "_callback_code = " << _callback_code << '\n';
-               if(_callback_code) {
-                       ProcessCallback(_callback_code);
-               }
                _transmit = false;
                _transmitting = true;
        } else if(_transmitting) {
@@ -165,45 +181,8 @@ void FGATC::NotifyTransmissionFinished(const string& rid) {
        }
 }
 
-void FGATC::Transmit(int callback_code) {
-       SG_LOG(SG_ATC, SG_INFO, "Transmit called by " << ident << " " << _type << ", msg = " << pending_transmission);
-       _pending = true;
-       _callback_code = callback_code;
-       _timeout = 0.0;
-}
-
-void FGATC::ConditionalTransmit(double timeout, int callback_code) {
-       SG_LOG(SG_ATC, SG_INFO, "Timed transmit called by " << ident << " " << _type << ", msg = " << pending_transmission);
-       _pending = true;
-       _callback_code = callback_code;
-       _timeout = timeout;
-}
-
-void FGATC::ImmediateTransmit(int callback_code) {
-       SG_LOG(SG_ATC, SG_INFO, "Immediate transmit called by " << ident << " " << _type << ", msg = " << pending_transmission);
-       if(_display) {
-               //Render(pending_transmission, ident, false);
-               Render(pending_transmission);
-               // At the moment Render doesn't work except for ATIS
-       }
-       if(callback_code) {
-               ProcessCallback(callback_code);
-       }
-}
-
-// Derived classes should override this.
-void FGATC::ProcessCallback(int code) {
-}
-
-void FGATC::AddPlane(const string& pid) {
-}
-
-int FGATC::RemovePlane() {
-       return 0;
-}
-
 void FGATC::SetData(ATCData* d) {
-  _type = d->type;
+       _type = d->type;
        _geod = d->geod;
        _cart = d->cart;
        range = d->range;
@@ -216,41 +195,36 @@ void FGATC::SetData(ATCData* d) {
 // Outputs the transmission either on screen or as audio depending on user preference
 // The refname is a string to identify this sample to the sound manager
 // The repeating flag indicates whether the message should be repeated continuously or played once.
-void FGATC::Render(string& msg, const double volume, 
-               const string& refname, const bool repeating) {
+void FGATC::Render(string& msg, const float volume, 
+                                  const string& refname, const bool repeating) {
+       if (volume < 0.05) return;
+
        if (repeating)
                fgSetString("/sim/messages/atis", msg.c_str());
        else
                fgSetString("/sim/messages/atc", msg.c_str());
 
-       #ifdef ENABLE_AUDIO_SUPPORT
+#ifdef ENABLE_AUDIO_SUPPORT
        _voice = (_voiceOK && fgGetBool("/sim/sound/voice"));
        if(_voice) {
-           string buf = _vPtr->WriteMessage((char*)msg.c_str(), _voice);
-           if(_voice) {
-               NoRender(refname);
-               try {
+               size_t len;
+               void* buf = _vPtr->WriteMessage((char*)msg.c_str(), &len);
+               if(buf) {
+                       NoRender(refname);
+                       try {
 // >>> Beware: must pass a (new) object to the (add) method,
 // >>> because the (remove) method is going to do a (delete)
 // >>> whether that's what you want or not.
-                   SGSoundSample *simple = 
-                       new SGSoundSample((unsigned char*) buf.c_str(), 
-                          buf.length(), 8000, AL_FORMAT_MONO8);
-                   // TODO - at the moment the volume can't be changed 
-                   // after the transmission has started.
-                   simple->set_volume(volume);
-                   globals->get_soundmgr()->add(simple, refname);
-                   if(repeating) {
-                           globals->get_soundmgr()->play_looped(refname);
-                   } else {
-                           globals->get_soundmgr()->play_once(refname);
-                   }
-               } catch ( sg_io_exception &e ) {
-                   SG_LOG(SG_GENERAL, SG_ALERT, e.getFormattedMessage());
+                               SGSoundSample *simple = new SGSoundSample(&buf, len, 8000);
+                               simple->set_volume(volume);
+                               _sgr->add(simple, refname);
+                               _sgr->play(refname, repeating);
+                       } catch ( sg_io_exception &e ) {
+                               SG_LOG(SG_GENERAL, SG_ALERT, e.getFormattedMessage());
+                       }
                }
-           }
        }
-       #endif  // ENABLE_AUDIO_SUPPORT
+#endif // ENABLE_AUDIO_SUPPORT
        if(!_voice) {
                // first rip the underscores and the pause hints out of the string - these are for the convienience of the voice parser
                for(unsigned int i = 0; i < msg.length(); ++i) {
@@ -268,8 +242,8 @@ void FGATC::NoRender(const string& refname) {
        if(_playing) {
                if(_voice) {
 #ifdef ENABLE_AUDIO_SUPPORT            
-                       globals->get_soundmgr()->stop(refname);
-                       globals->get_soundmgr()->remove(refname);
+                       _sgr->stop(refname);
+                       _sgr->remove(refname);
 #endif
                }
                _playing = false;
@@ -283,14 +257,14 @@ string FGATC::GenText(const string& m, int c) {
 
 ostream& operator << (ostream& os, atc_type atc) {
        switch(atc) {
-               case(AWOS):       return(os << "AWOS");
-               case(ATIS):       return(os << "ATIS");
-               case(GROUND):     return(os << "GROUND");
-               case(TOWER):      return(os << "TOWER");
+               case(AWOS):        return(os << "AWOS");
+               case(ATIS):        return(os << "ATIS");
+               case(GROUND):    return(os << "GROUND");
+               case(TOWER):      return(os << "TOWER");
                case(APPROACH):   return(os << "APPROACH");
                case(DEPARTURE):  return(os << "DEPARTURE");
-               case(ENROUTE):    return(os << "ENROUTE");
-               case(INVALID):    return(os << "INVALID");
+               case(ENROUTE):  return(os << "ENROUTE");
+               case(INVALID):  return(os << "INVALID");
        }
        return(os << "ERROR - Unknown switch in atc_type operator << ");
 }
@@ -325,8 +299,8 @@ std::istream& operator >> ( std::istream& fin, ATCData& a )
                return fin >> skipeol;
        }
        
-  double lat, lon, elev;
-  
+       double lat, lon, elev;
+
        fin >> lat >> lon >> elev >> f >> a.range >> a.ident;
        a.geod = SGGeod::fromDegM(lon, lat, elev);
        a.name = "";
@@ -349,7 +323,7 @@ std::istream& operator >> ( std::istream& fin, ATCData& a )
        // cout << a.ident << endl;
        
        // generate cartesian coordinates
-  a.cart = SGVec3d::fromGeod(a.geod);  
+       a.cart = SGVec3d::fromGeod(a.geod);     
        return fin >> skipeol;
 }