-
Notifications
You must be signed in to change notification settings - Fork 0
/
Distortion.cpp
275 lines (236 loc) · 6.41 KB
/
Distortion.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//
// Distortion.cpp
// FMOD Distortion
//
// Created by Max Walley on 13/05/2020.
// Copyright © 2020 Max Walley. All rights reserved.
//
#include "fmod.hpp"
#include "fmod_errors.h"
#include <stdio.h>
#include <cstring>
#include <math.h>
void errorCheck(FMOD_RESULT result);
extern "C"
{
F_EXPORT FMOD_DSP_DESCRIPTION* F_CALL FMODGetDSPDescription();
}
FMOD_RESULT F_CALLBACK create(FMOD_DSP_STATE* state);
FMOD_RESULT F_CALLBACK release(FMOD_DSP_STATE* state);
FMOD_RESULT F_CALLBACK reset(FMOD_DSP_STATE* state);
FMOD_RESULT F_CALLBACK read(FMOD_DSP_STATE *dsp_state, float *inbuffer, float *outbuffer, unsigned int length, int inchannels, int *outchannels);
FMOD_RESULT F_CALLBACK setFloat(FMOD_DSP_STATE* state, int index, float value);
FMOD_RESULT F_CALLBACK setBool(FMOD_DSP_STATE* state, int index, FMOD_BOOL value);
FMOD_RESULT F_CALLBACK getFloat(FMOD_DSP_STATE* state, int index, float* value, char* valuestr);
FMOD_RESULT F_CALLBACK getBool(FMOD_DSP_STATE* state, int index, FMOD_BOOL* value, char* valuestr);
FMOD_RESULT F_CALLBACK shouldIProcess(FMOD_DSP_STATE *dsp_state, FMOD_BOOL inputsidle, unsigned int length, FMOD_CHANNELMASK inmask, int inchannels, FMOD_SPEAKERMODE speakermode);
//Parameter definitions here
FMOD_DSP_PARAMETER_DESC gain;
FMOD_DSP_PARAMETER_DESC hardClipLevel;
FMOD_DSP_PARAMETER_DESC shapeOn;
//List of pointers to parameters
FMOD_DSP_PARAMETER_DESC* params[3]
{
&gain,
&hardClipLevel,
&shapeOn
};
FMOD_DSP_DESCRIPTION pluginDesc =
{
FMOD_PLUGIN_SDK_VERSION,
"MaxWalley Distortion",
1,
1,
1,
create,
release,
reset,
read,
0,
0,
3,
params,
setFloat,
0,
setBool,
0,
getFloat,
0,
getBool,
0,
shouldIProcess,
0,
0,
0,
0
};
void errorCheck(FMOD_RESULT result)
{
if(result != FMOD_OK)
{
printf("FMOD Error %d: %s\n", result, FMOD_ErrorString(result));
}
}
extern "C"
{
F_EXPORT FMOD_DSP_DESCRIPTION* F_CALL FMODGetDSPDescription()
{
FMOD_DSP_INIT_PARAMDESC_FLOAT(gain, "Gain", "dB", "Level Gain", -80.0, 10.0, 0.0);
FMOD_DSP_INIT_PARAMDESC_FLOAT(hardClipLevel, "Clip Level", "dB", "The level to clip audio at", -40.0, 0, 0);
FMOD_DSP_INIT_PARAMDESC_BOOL(shapeOn, "Shape?", "", "Whether to apply soft clipping to audio", 0, 0);
return &pluginDesc;
}
}
class pluginData
{
public:
pluginData();
void setGain(float newGain);
float getGain() const;
void setClipLevel(float newLevel);
float getClipLevel() const;
void setShapeOn(FMOD_BOOL on);
FMOD_BOOL getShapeOn() const;
private:
float pGain;
float clipLevel;
FMOD_BOOL shapeSetting;
};
pluginData::pluginData()
{
pGain = 1.0;
clipLevel = 1;
}
void pluginData::setGain(float newGain)
{
pGain = newGain;
}
float pluginData::getGain() const
{
return pGain;
}
void pluginData::setClipLevel(float newLevel)
{
clipLevel = newLevel;
}
float pluginData::getClipLevel() const
{
return clipLevel;
}
void pluginData::setShapeOn(FMOD_BOOL on)
{
shapeSetting = on;
}
FMOD_BOOL pluginData::getShapeOn() const
{
return shapeSetting;
}
//Referenced from FMOD gain example
FMOD_RESULT F_CALLBACK create(FMOD_DSP_STATE* state)
{
//Allocating memory for the plugin
state->plugindata = (pluginData*) FMOD_DSP_ALLOC(state, sizeof(pluginData));
//Checking that memory has been correctly allocated
if(state->plugindata == nullptr)
{
return FMOD_ERR_MEMORY;
}
return FMOD_OK;
}
//Referenced from FMOD gain example
FMOD_RESULT F_CALLBACK release(FMOD_DSP_STATE* state)
{
//Deallocating the memory for the plugin
pluginData* data = (pluginData*)state->plugindata;
FMOD_DSP_FREE(state, data);
return FMOD_OK;
}
FMOD_RESULT F_CALLBACK reset(FMOD_DSP_STATE* state)
{
return FMOD_OK;
}
FMOD_RESULT F_CALLBACK read(FMOD_DSP_STATE *dsp_state, float *inbuffer, float *outbuffer, unsigned int length, int inchannels, int *outchannels)
{
pluginData* data = (pluginData*)dsp_state->plugindata;
for(int i = 0; i < length; i++)
{
for(int j = 0; j < inchannels; j++)
{
float sample = *inbuffer++;
sample = sample * data->getGain();
if(sample > data->getClipLevel())
{
sample = data->getClipLevel();
}
if(data->getShapeOn() == true)
{
sample = sin(0.5 * M_PI * sample);
}
*outbuffer++ = sample;
}
}
return FMOD_OK;
}
FMOD_RESULT F_CALLBACK setFloat(FMOD_DSP_STATE* state, int index, float value)
{
if(index == 0)
{
pluginData* data = (pluginData*)state->plugindata;
data->setGain(pow(10, (value/20)));
return FMOD_OK;
}
else if(index == 1)
{
pluginData* data = (pluginData*)state->plugindata;
data->setClipLevel(pow(10, (value/20)));
return FMOD_OK;
}
return FMOD_ERR_INVALID_PARAM;
}
FMOD_RESULT F_CALLBACK setBool(FMOD_DSP_STATE* state, int index, FMOD_BOOL value)
{
if(index == 2)
{
pluginData* data = (pluginData*)state->plugindata;
data->setShapeOn(value);
return FMOD_OK;
}
return FMOD_ERR_INVALID_PARAM;
}
FMOD_RESULT F_CALLBACK getFloat(FMOD_DSP_STATE* state, int index, float* value, char* valuestr)
{
if(index == 0)
{
pluginData* data = (pluginData*)state->plugindata;
*value = data->getGain();
sprintf(valuestr, "%f", data->getGain());
return FMOD_OK;
}
else if(index == 1)
{
pluginData* data = (pluginData*)state->plugindata;
*value = data->getClipLevel();
sprintf(valuestr, "%f", data->getClipLevel());
return FMOD_OK;
}
return FMOD_ERR_INVALID_PARAM;
}
FMOD_RESULT F_CALLBACK getBool(FMOD_DSP_STATE* state, int index, FMOD_BOOL* value, char* valuestr)
{
if(index == 2)
{
pluginData* data = (pluginData*)state->plugindata;
*value = data->getShapeOn();
sprintf(valuestr, "%d", data->getShapeOn());
return FMOD_OK;
}
return FMOD_ERR_INVALID_PARAM;
}
FMOD_RESULT F_CALLBACK shouldIProcess(FMOD_DSP_STATE *dsp_state, FMOD_BOOL inputsidle, unsigned int length, FMOD_CHANNELMASK inmask, int inchannels, FMOD_SPEAKERMODE speakermode)
{
if(inputsidle == true)
{
return FMOD_ERR_DSP_DONTPROCESS;
}
return FMOD_OK;
}