FIFE  6e1afdbeda11afe9ac53e6023a4be96ef88f1dc6
soundfilter.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2017 by the FIFE team *
3  * http://www.fifengine.net *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 // Standard C++ library includes
23 #include <algorithm>
24 
25 // Platform specific includes
26 
27 // 3rd party library includes
28 
29 // FIFE includes
30 // These includes are split up in two parts, separated by one empty line
31 // First block: files included from the FIFE root src directory
32 // Second block: files included from the same folder
33 #include "util/log/logger.h"
34 #include "util/base/exception.h"
35 
36 #include "soundfilter.h"
37 
38 namespace FIFE {
42  static Logger _log(LM_AUDIO);
43 
45  m_filter(0),
46  m_type(SF_FILTER_NULL),
47  m_enabled(false),
48  m_gain(1.0),
49  m_hGain(1.0),
50  m_lGain(1.0) {
51 
53  CHECK_OPENAL_LOG(_log, LogManager::LEVEL_ERROR, "error creating filter");
54  setFilterType(type);
55  }
56 
59  }
60 
61  ALuint SoundFilter::getFilterId() const {
62  return m_filter;
63  }
64 
66  if (m_type == type || isEnabled()) {
67  return;
68  }
69  m_type = type;
70  if (m_type == SF_FILTER_NULL) {
71  alFilteri(m_filter, AL_FILTER_TYPE, AL_FILTER_NULL);
72  } else if (m_type == SF_FILTER_LOWPASS) {
73  alFilteri(m_filter, AL_FILTER_TYPE, AL_FILTER_LOWPASS);
74  } else if (m_type == SF_FILTER_HIGHPASS) {
75  alFilteri(m_filter, AL_FILTER_TYPE, AL_FILTER_HIGHPASS);
76  } else if (m_type == SF_FILTER_BANDPASS) {
77  alFilteri(m_filter, AL_FILTER_TYPE, AL_FILTER_BANDPASS);
78  }
79  CHECK_OPENAL_LOG(_log, LogManager::LEVEL_ERROR, "error setting filter");
80  }
81 
83  return m_type;
84  }
85 
86  void SoundFilter::setEnabled(bool enabled) {
87  m_enabled = enabled;
88  }
89 
90  bool SoundFilter::isEnabled() const {
91  return m_enabled;
92  }
93 
94  void SoundFilter::setGain(float gain) {
95  gain = std::min(gain, 1.0f);
96  gain = std::max(gain, 0.0f);
97  m_gain = gain;
98 
99  if (m_type == SF_FILTER_LOWPASS) {
100  alFilterf(m_filter, AL_LOWPASS_GAIN, m_gain);
101  } else if (m_type == SF_FILTER_HIGHPASS) {
102  alFilterf(m_filter, AL_HIGHPASS_GAIN, m_gain);
103  } else if (m_type == SF_FILTER_BANDPASS) {
104  alFilterf(m_filter, AL_BANDPASS_GAIN, m_gain);
105  }
106  }
107 
108  float SoundFilter::getGain() const {
109  return m_gain;
110  }
111 
112  void SoundFilter::setGainHf(float gain) {
113  gain = std::min(gain, 1.0f);
114  gain = std::max(gain, 0.0f);
115  m_hGain = gain;
116 
117  if (m_type == SF_FILTER_LOWPASS) {
118  alFilterf(m_filter, AL_LOWPASS_GAINHF, m_hGain);
119  } else if (m_type == SF_FILTER_BANDPASS) {
120  alFilterf(m_filter, AL_BANDPASS_GAINHF, m_hGain);
121  }
122  }
123 
124  float SoundFilter::getGainHf() const {
125  return m_hGain;
126  }
127 
128  void SoundFilter::setGainLf(float gain) {
129  gain = std::min(gain, 1.0f);
130  gain = std::max(gain, 0.0f);
131  m_lGain = gain;
132 
133  if (m_type == SF_FILTER_HIGHPASS) {
134  alFilterf(m_filter, AL_HIGHPASS_GAINLF, m_lGain);
135  }
136  else if (m_type == SF_FILTER_BANDPASS) {
137  alFilterf(m_filter, AL_BANDPASS_GAINLF, m_lGain);
138  }
139  }
140 
141  float SoundFilter::getGainLf() const {
142  return m_lGain;
143  }
144 
145 } //FIFE
LPALFILTERI alFilteri
void setFilterType(SoundFilterType type)
Sets the filter type.
Definition: soundfilter.cpp:65
LPALFILTERF alFilterf
void setGain(float gain)
Sets filter gain.
Definition: soundfilter.cpp:94
static Logger _log(LM_AUDIO)
#define CHECK_OPENAL_LOG(logger, level, msg)
Definition: fife_openal.h:49
SoundFilter(SoundFilterType type)
Constructor.
Definition: soundfilter.cpp:44
LPALGENFILTERS alGenFilters
LPALDELETEFILTERS alDeleteFilters
float getGainHf() const
Return filter high frequency gain.
ALuint m_filter
Filter object id.
Definition: soundfilter.h:106
bool isEnabled() const
Return true if the filter is enabled, false otherwise.
Definition: soundfilter.cpp:90
void setGainLf(float gain)
Sets filter low frequency gain.
float m_lGain
Low frequency gain.
Definition: soundfilter.h:116
float getGain() const
Return filter gain.
void setGainHf(float gain)
Sets filter high frequency gain.
~SoundFilter()
Destructor.
Definition: soundfilter.cpp:57
SoundFilterType m_type
Filter type.
Definition: soundfilter.h:108
float getGainLf() const
Return filter low frequency gain.
float m_hGain
High frequency gain.
Definition: soundfilter.h:114
float m_gain
Gain.
Definition: soundfilter.h:112
bool m_enabled
Filter enabled.
Definition: soundfilter.h:110
SoundFilterType
Sound filter type.
Definition: soundconfig.h:39
SoundFilterType getFilterType() const
Return the filter type.
Definition: soundfilter.cpp:82
ALuint getFilterId() const
Return the OpenAL filter handle.
Definition: soundfilter.cpp:61
void setEnabled(bool enabled)
Enables or disables the filter.
Definition: soundfilter.cpp:86