]> git.mxchange.org Git - flightgear.git/blob - src/Sound/soundmanager.cxx
6d3908b3cd43e6572f6ad58a111fa33b3138addc
[flightgear.git] / src / Sound / soundmanager.cxx
1 // soundmanager.cxx -- Wraps the SimGear OpenAl sound manager class
2 //
3 // Copyright (C) 2001  Curtis L. Olson - http://www.flightgear.org/~curt
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 //
19
20 #include <simgear/sound/soundmgr_openal.hxx>
21
22 #include "soundmanager.hxx"
23 #include "Main/globals.hxx"
24 #include "Main/fg_props.hxx"
25
26 #include <vector>
27 #include <string>
28
29 #ifdef ENABLE_AUDIO_SUPPORT
30 /**
31  * Listener class that monitors the sim state.
32  */
33 class Listener : public SGPropertyChangeListener
34 {
35 public:
36     Listener(FGSoundManager *wrapper) : _wrapper(wrapper) {}
37     virtual void valueChanged (SGPropertyNode * node);
38
39 private:
40     FGSoundManager * _wrapper;
41 };
42
43 void Listener::valueChanged(SGPropertyNode * node)
44 {
45     _wrapper->activate(node->getBoolValue());
46 }
47
48 FGSoundManager::FGSoundManager()
49   : _is_initialized(false),
50     _listener(new Listener(this))
51 {
52     SGPropertyNode_ptr scenery_loaded = fgGetNode("sim/sceneryloaded", true);
53     scenery_loaded->addChangeListener(_listener);
54 }
55
56 FGSoundManager::~FGSoundManager()
57 {
58 }
59
60
61 void FGSoundManager::setNewSoundDevice(const char *device)
62 {
63     SGSoundMgr *smgr = globals->get_soundmgr();
64     smgr->suspend();
65     smgr->stop();
66     smgr->init(device);
67     smgr->resume();
68 }
69
70 void FGSoundManager::init()
71 {
72     globals->get_props()->tie("/sim/sound/devices/name",
73           SGRawValueFunctions<const char *>(0, FGSoundManager::setNewSoundDevice), false);
74 }
75
76 void FGSoundManager::bind()
77 {
78     _sound_working = fgGetNode("/sim/sound/working");
79     _sound_enabled = fgGetNode("/sim/sound/enabled");
80     _volume        = fgGetNode("/sim/sound/volume");
81
82     // we intentionally do _not_ call SGSoundMgr::bind here, we'll do this later
83 }
84
85 void FGSoundManager::activate(bool State)
86 {
87     if (_is_initialized &&
88         fgGetBool("/sim/sound/working"))
89     {
90         if (State)
91             SGSoundMgr::activate();
92     }
93 }
94
95 void FGSoundManager::runtime_init()
96 {
97     SGSoundMgr::bind();
98     SGSoundMgr::init(fgGetString("/sim/sound/device-name", NULL));
99
100     std::vector <const char*>devices = get_available_devices();
101     for (unsigned int i=0; i<devices.size(); i++) {
102         SGPropertyNode *p = fgGetNode("/sim/sound/devices/device", i, true);
103         p->setStringValue(devices[i]);
104     }
105     devices.clear();
106 }
107
108 // Update sound manager and propagate property values,
109 // since the sound manager doesn't read any properties itself.
110 // Actual sound update is triggered by the subsystem manager.
111 void FGSoundManager::update(double dt)
112 {
113     if (!_is_initialized) {
114         if (_sound_working->getBoolValue()) {
115             runtime_init();
116             _is_initialized = true;
117         }
118     } else {
119         if (!_sound_working->getBoolValue()) {   // request to reinit
120             SGSoundMgr::reinit();
121            _sound_working->setBoolValue(true);
122         }
123
124         if (_sound_enabled->getBoolValue()) {
125             set_volume(_volume->getFloatValue());
126             SGSoundMgr::update(dt);
127         }
128     }
129 }
130
131 #endif // ENABLE_AUDIO_SUPPORT