Merge branch 'ryzom/ark-features' into main/gingo-test
[ryzomcore.git] / nel / src / sound / group_controller.cpp
blob838f486c271bf6119d52fbd44193fa1a3002ad43
1 /**
2 * \file group_controller.cpp
3 * \brief CGroupController
4 * \date 2012-04-10 09:29GMT
5 * \author Jan Boon (Kaetemi)
6 * CGroupController
7 */
9 // NeL - MMORPG Framework <https://wiki.ryzom.dev/>
10 // Copyright (C) 2012 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
12 // This program is free software: you can redistribute it and/or modify
13 // it under the terms of the GNU Affero General Public License as
14 // published by the Free Software Foundation, either version 3 of the
15 // License, or (at your option) any later version.
17 // This program is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU Affero General Public License for more details.
22 // You should have received a copy of the GNU Affero General Public License
23 // along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "stdsound.h"
26 #include <nel/sound/group_controller.h>
28 // STL includes
30 // NeL includes
31 // #include <nel/misc/debug.h>
32 #include <nel/sound/source_common.h>
34 // Project includes
36 using namespace std;
37 // using namespace NLMISC;
39 namespace NLSOUND {
41 CGroupController::CGroupController(CGroupController *parent) :
42 m_Parent(parent), m_Gain(1.0f), m_NbSourcesInclChild(0)
47 CGroupController::~CGroupController()
49 // If m_Sources is not empty, a crash is very likely.
50 nlassert(m_Sources.empty());
52 for (std::map<std::string, CGroupController *>::iterator it(m_Children.begin()), end(m_Children.end()); it != end; ++it)
54 delete it->second;
55 it->second = NULL;
57 m_Parent = NULL;
60 void CGroupController::addSource(CSourceCommon *source)
62 nlassert(this != NULL);
64 m_Sources.insert(source);
65 increaseSources();
68 void CGroupController::removeSource(CSourceCommon *source)
70 decreaseSources();
71 m_Sources.erase(source);
74 std::string CGroupController::getPath() // overridden by root
76 for (std::map<std::string, CGroupController *>::iterator it(m_Parent->m_Children.begin()), end(m_Parent->m_Children.end()); it != end; ++it)
78 if (it->second == this)
80 const std::string &name = it->first;
81 std::string returnPath = m_Parent->getPath() + ":" + name;
82 return returnPath;
85 nlerror("Group Controller not child of parent");
86 return "";
89 void CGroupController::calculateFinalGain() // overridden by root
91 m_FinalGain = calculateTotalGain() * m_Parent->getFinalGain();
94 void CGroupController::updateSourceGain()
96 // Dont update source gain when this controller is inactive.
97 if (m_NbSourcesInclChild)
99 calculateFinalGain();
100 for (TSourceContainer::iterator it(m_Sources.begin()), end(m_Sources.end()); it != end; ++it)
101 (*it)->updateFinalGain();
102 for (std::map<std::string, CGroupController *>::iterator it(m_Children.begin()), end(m_Children.end()); it != end; ++it)
103 (*it).second->updateSourceGain();
107 void CGroupController::increaseSources() // overridden by root
109 ++m_NbSourcesInclChild;
110 m_Parent->increaseSources();
112 // Update source gain when this controller was inactive before but the parent was active before.
113 // Thus, when this controller was the root of inactive controllers.
114 if (m_NbSourcesInclChild == 1 && m_Parent->m_NbSourcesInclChild > 1)
115 updateSourceGain();
118 void CGroupController::decreaseSources() // overridden by root
120 --m_NbSourcesInclChild;
121 m_Parent->decreaseSources();
124 } /* namespace NLSOUND */
126 /* end of file */