-
Notifications
You must be signed in to change notification settings - Fork 2
/
AudioFilter.cpp
147 lines (126 loc) · 2.65 KB
/
AudioFilter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "AudioFilter.h"
#include "AudioEngine.h"
using namespace audio;
Filter::Filter()
{
type = Type::BASIC;
generateFilter();
}
Filter::Filter(const Filter &s)
: Filter()
{
Filter::setParams(s.getParams());
}
Filter::~Filter()
{
deleteFilter();
}
Filter::Type Filter::getType() const
{
return type;
}
Filter *Filter::clone()
{
return new Filter(*this);
}
bool Filter::generateFilter()
{
#ifdef ALC_EXT_EFX
if (!alGenFilters)
return false;
if (filter != AL_FILTER_NULL)
return true;
alGenFilters(1, &filter);
if (alGetError() != AL_NO_ERROR)
{
//throw love::Exception("Failed to create sound Filter.");
return false;
}
return true;
#else
return false;
#endif
}
void Filter::deleteFilter()
{
#ifdef ALC_EXT_EFX
if (filter != AL_FILTER_NULL)
alDeleteFilters(1, &filter);
#endif
filter = AL_FILTER_NULL;
}
ALuint Filter::getFilter() const
{
return filter;
}
bool Filter::setParams(const ParamMap& params_)
{
params = params_;
if (params.find(Parameter::TYPE) == params.end())
return false;
type = (Type)(int)params[Parameter::TYPE];
if (!generateFilter())
return false;
#ifdef ALC_EXT_EFX
switch (type)
{
case Type::LOWPASS:
alFilteri(filter, AL_FILTER_TYPE, AL_FILTER_LOWPASS);
break;
case Type::HIGHPASS:
alFilteri(filter, AL_FILTER_TYPE, AL_FILTER_HIGHPASS);
break;
case Type::BANDPASS:
alFilteri(filter, AL_FILTER_TYPE, AL_FILTER_BANDPASS);
break;
case Type::BASIC:
case Type::MAX_ENUM:
break;
}
//failed to make filter specific type - not supported etc.
if (alGetError() != AL_NO_ERROR)
{
deleteFilter();
return false;
}
#define clampf(v,l,h) fmax(fmin((v),(h)),(l))
#define PARAMSTR(i,e,v) filter,AL_##e##_##v,clampf(getValue(Parameter::i,AL_##e##_DEFAULT_##v),AL_##e##_MIN_##v,AL_##e##_MAX_##v)
switch (type)
{
case Type::LOWPASS:
alFilterf(PARAMSTR(VOLUME, LOWPASS, GAIN));
alFilterf(PARAMSTR(HIGHGAIN, LOWPASS, GAINHF));
break;
case Type::HIGHPASS:
alFilterf(PARAMSTR(VOLUME, HIGHPASS, GAIN));
alFilterf(PARAMSTR(LOWGAIN, HIGHPASS, GAINLF));
break;
case Type::BANDPASS:
alFilterf(PARAMSTR(VOLUME, BANDPASS, GAIN));
alFilterf(PARAMSTR(LOWGAIN, BANDPASS, GAINLF));
alFilterf(PARAMSTR(HIGHGAIN, BANDPASS, GAINHF));
break;
case Type::BASIC:
case Type::MAX_ENUM:
break;
}
#undef clampf
#undef PARAMSTR
//alGetError();
return true;
#else
return false;
#endif
}
const Filter::ParamMap& Filter::getParams() const
{
return params;
}
float Filter::getValue(Parameter in, float def) const
{
return params.find(in) == params.end() ? def : params.at(in);
}
int Filter::getValue(Parameter in, int def) const
{
return params.find(in) == params.end() ? def : static_cast<int>(params.at(in));
}