]> git.tdb.fi Git - ext/openal.git/blob - al/effects/pshifter.cpp
Import OpenAL Soft 1.23.1 sources
[ext/openal.git] / al / effects / pshifter.cpp
1
2 #include "config.h"
3
4 #include "AL/al.h"
5 #include "AL/efx.h"
6
7 #include "alc/effects/base.h"
8 #include "effects.h"
9
10 #ifdef ALSOFT_EAX
11 #include "alnumeric.h"
12 #include "al/eax/exception.h"
13 #include "al/eax/utils.h"
14 #endif // ALSOFT_EAX
15
16
17 namespace {
18
19 void Pshifter_setParamf(EffectProps*, ALenum param, float)
20 { throw effect_exception{AL_INVALID_ENUM, "Invalid pitch shifter float property 0x%04x", param}; }
21 void Pshifter_setParamfv(EffectProps*, ALenum param, const float*)
22 {
23     throw effect_exception{AL_INVALID_ENUM, "Invalid pitch shifter float-vector property 0x%04x",
24         param};
25 }
26
27 void Pshifter_setParami(EffectProps *props, ALenum param, int val)
28 {
29     switch(param)
30     {
31     case AL_PITCH_SHIFTER_COARSE_TUNE:
32         if(!(val >= AL_PITCH_SHIFTER_MIN_COARSE_TUNE && val <= AL_PITCH_SHIFTER_MAX_COARSE_TUNE))
33             throw effect_exception{AL_INVALID_VALUE, "Pitch shifter coarse tune out of range"};
34         props->Pshifter.CoarseTune = val;
35         break;
36
37     case AL_PITCH_SHIFTER_FINE_TUNE:
38         if(!(val >= AL_PITCH_SHIFTER_MIN_FINE_TUNE && val <= AL_PITCH_SHIFTER_MAX_FINE_TUNE))
39             throw effect_exception{AL_INVALID_VALUE, "Pitch shifter fine tune out of range"};
40         props->Pshifter.FineTune = val;
41         break;
42
43     default:
44         throw effect_exception{AL_INVALID_ENUM, "Invalid pitch shifter integer property 0x%04x",
45             param};
46     }
47 }
48 void Pshifter_setParamiv(EffectProps *props, ALenum param, const int *vals)
49 { Pshifter_setParami(props, param, vals[0]); }
50
51 void Pshifter_getParami(const EffectProps *props, ALenum param, int *val)
52 {
53     switch(param)
54     {
55     case AL_PITCH_SHIFTER_COARSE_TUNE:
56         *val = props->Pshifter.CoarseTune;
57         break;
58     case AL_PITCH_SHIFTER_FINE_TUNE:
59         *val = props->Pshifter.FineTune;
60         break;
61
62     default:
63         throw effect_exception{AL_INVALID_ENUM, "Invalid pitch shifter integer property 0x%04x",
64             param};
65     }
66 }
67 void Pshifter_getParamiv(const EffectProps *props, ALenum param, int *vals)
68 { Pshifter_getParami(props, param, vals); }
69
70 void Pshifter_getParamf(const EffectProps*, ALenum param, float*)
71 { throw effect_exception{AL_INVALID_ENUM, "Invalid pitch shifter float property 0x%04x", param}; }
72 void Pshifter_getParamfv(const EffectProps*, ALenum param, float*)
73 {
74     throw effect_exception{AL_INVALID_ENUM, "Invalid pitch shifter float vector-property 0x%04x",
75         param};
76 }
77
78 EffectProps genDefaultProps() noexcept
79 {
80     EffectProps props{};
81     props.Pshifter.CoarseTune = AL_PITCH_SHIFTER_DEFAULT_COARSE_TUNE;
82     props.Pshifter.FineTune   = AL_PITCH_SHIFTER_DEFAULT_FINE_TUNE;
83     return props;
84 }
85
86 } // namespace
87
88 DEFINE_ALEFFECT_VTABLE(Pshifter);
89
90 const EffectProps PshifterEffectProps{genDefaultProps()};
91
92 #ifdef ALSOFT_EAX
93 namespace {
94
95 using PitchShifterCommitter = EaxCommitter<EaxPitchShifterCommitter>;
96
97 struct CoarseTuneValidator {
98     void operator()(long lCoarseTune) const
99     {
100         eax_validate_range<PitchShifterCommitter::Exception>(
101             "Coarse Tune",
102             lCoarseTune,
103             EAXPITCHSHIFTER_MINCOARSETUNE,
104             EAXPITCHSHIFTER_MAXCOARSETUNE);
105     }
106 }; // CoarseTuneValidator
107
108 struct FineTuneValidator {
109     void operator()(long lFineTune) const
110     {
111         eax_validate_range<PitchShifterCommitter::Exception>(
112             "Fine Tune",
113             lFineTune,
114             EAXPITCHSHIFTER_MINFINETUNE,
115             EAXPITCHSHIFTER_MAXFINETUNE);
116     }
117 }; // FineTuneValidator
118
119 struct AllValidator {
120     void operator()(const EAXPITCHSHIFTERPROPERTIES& all) const
121     {
122         CoarseTuneValidator{}(all.lCoarseTune);
123         FineTuneValidator{}(all.lFineTune);
124     }
125 }; // AllValidator
126
127 } // namespace
128
129 template<>
130 struct PitchShifterCommitter::Exception : public EaxException {
131     explicit Exception(const char *message) : EaxException{"EAX_PITCH_SHIFTER_EFFECT", message}
132     { }
133 };
134
135 template<>
136 [[noreturn]] void PitchShifterCommitter::fail(const char *message)
137 {
138     throw Exception{message};
139 }
140
141 template<>
142 bool PitchShifterCommitter::commit(const EaxEffectProps &props)
143 {
144     if(props.mType == mEaxProps.mType
145         && mEaxProps.mPitchShifter.lCoarseTune == props.mPitchShifter.lCoarseTune
146         && mEaxProps.mPitchShifter.lFineTune == props.mPitchShifter.lFineTune)
147         return false;
148
149     mEaxProps = props;
150
151     mAlProps.Pshifter.CoarseTune = static_cast<int>(mEaxProps.mPitchShifter.lCoarseTune);
152     mAlProps.Pshifter.FineTune = static_cast<int>(mEaxProps.mPitchShifter.lFineTune);
153
154     return true;
155 }
156
157 template<>
158 void PitchShifterCommitter::SetDefaults(EaxEffectProps &props)
159 {
160     props.mType = EaxEffectType::PitchShifter;
161     props.mPitchShifter.lCoarseTune = EAXPITCHSHIFTER_DEFAULTCOARSETUNE;
162     props.mPitchShifter.lFineTune = EAXPITCHSHIFTER_DEFAULTFINETUNE;
163 }
164
165 template<>
166 void PitchShifterCommitter::Get(const EaxCall &call, const EaxEffectProps &props)
167 {
168     switch(call.get_property_id())
169     {
170     case EAXPITCHSHIFTER_NONE: break;
171     case EAXPITCHSHIFTER_ALLPARAMETERS: call.set_value<Exception>(props.mPitchShifter); break;
172     case EAXPITCHSHIFTER_COARSETUNE: call.set_value<Exception>(props.mPitchShifter.lCoarseTune); break;
173     case EAXPITCHSHIFTER_FINETUNE: call.set_value<Exception>(props.mPitchShifter.lFineTune); break;
174     default: fail_unknown_property_id();
175     }
176 }
177
178 template<>
179 void PitchShifterCommitter::Set(const EaxCall &call, EaxEffectProps &props)
180 {
181     switch(call.get_property_id())
182     {
183     case EAXPITCHSHIFTER_NONE: break;
184     case EAXPITCHSHIFTER_ALLPARAMETERS: defer<AllValidator>(call, props.mPitchShifter); break;
185     case EAXPITCHSHIFTER_COARSETUNE: defer<CoarseTuneValidator>(call, props.mPitchShifter.lCoarseTune); break;
186     case EAXPITCHSHIFTER_FINETUNE: defer<FineTuneValidator>(call, props.mPitchShifter.lFineTune); break;
187     default: fail_unknown_property_id();
188     }
189 }
190
191 #endif // ALSOFT_EAX