1/**
2 * Copyright (c) 2006-2023 LOVE Development Team
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty. In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 *
8 * Permission is granted to anyone to use this software for any purpose,
9 * including commercial applications, and to alter it and redistribute it
10 * freely, subject to the following restrictions:
11 *
12 * 1. The origin of this software must not be misrepresented; you must not
13 * claim that you wrote the original software. If you use this software
14 * in a product, an acknowledgment in the product documentation would be
15 * appreciated but is not required.
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 * 3. This notice may not be removed or altered from any source distribution.
19 **/
20
21#ifndef LOVE_AUDIO_FILTERS_H
22#define LOVE_AUDIO_FILTERS_H
23
24#include "common/Object.h"
25#include "common/StringMap.h"
26#include <map>
27
28template<typename T>
29class LazierAndSlowerButEasilyArrayableStringMap2
30{
31public:
32 struct Entry
33 {
34 const char *key;
35 T value;
36 };
37 LazierAndSlowerButEasilyArrayableStringMap2()
38 {
39 }
40
41 LazierAndSlowerButEasilyArrayableStringMap2(const std::vector<Entry> &entries)
42 {
43 for (auto entry : entries)
44 {
45 forward[entry.key] = entry.value;
46 reverse[entry.value] = entry.key;
47 }
48 }
49
50 bool find(const char *key, T &t)
51 {
52 if (forward.find(key) == forward.end())
53 return false;
54 t = forward[key];
55 return true;
56 }
57
58 bool find(T key, const char *&str)
59 {
60 if (reverse.find(key) == reverse.end())
61 return false;
62 str = reverse[key];
63 return true;
64 }
65
66private:
67 std::map<std::string, T> forward;
68 std::map<T, const char*> reverse;
69};
70
71namespace love
72{
73namespace audio
74{
75
76class Filter
77{
78public:
79 enum Type
80 {
81 TYPE_BASIC,
82 TYPE_LOWPASS,
83 TYPE_HIGHPASS,
84 TYPE_BANDPASS,
85 TYPE_MAX_ENUM
86 };
87
88 enum Parameter
89 {
90 FILTER_TYPE,
91 FILTER_VOLUME,
92
93 FILTER_LOWGAIN,
94 FILTER_HIGHGAIN,
95
96 FILTER_MAX_ENUM
97 };
98
99 enum ParameterType
100 {
101 PARAM_TYPE,
102 PARAM_FLOAT,
103 PARAM_MAX_ENUM
104 };
105
106 Filter();
107 virtual ~Filter();
108 Type getType() const;
109
110 static bool getConstant(const char *in, Type &out);
111 static bool getConstant(Type in, const char *&out);
112 static std::vector<std::string> getConstants(Type);
113 static bool getConstant(const char *in, Parameter &out, Type t);
114 static bool getConstant(Parameter in, const char *&out, Type t);
115 static ParameterType getParameterType(Parameter in);
116
117protected:
118 Type type;
119
120private:
121 static StringMap<Type, TYPE_MAX_ENUM>::Entry typeEntries[];
122 static StringMap<Type, TYPE_MAX_ENUM> types;
123#define StringMap LazierAndSlowerButEasilyArrayableStringMap2
124 static std::vector<StringMap<Filter::Parameter>::Entry> basicParameters;
125 static std::vector<StringMap<Filter::Parameter>::Entry> lowpassParameters;
126 static std::vector<StringMap<Filter::Parameter>::Entry> highpassParameters;
127 static std::vector<StringMap<Filter::Parameter>::Entry> bandpassParameters;
128 static std::map<Type, StringMap<Parameter>> parameterNames;
129#undef StringMap
130 static std::map<Parameter, ParameterType> parameterTypes;
131};
132
133} //audio
134} //love
135
136#endif //LOVE_AUDIO_FILTERS_H
137