1 /**
2 * @file
3 * @brief Source file for Distortion audio effect class
4 * @author
5 *
6 * @ref License
7 */
8
9 /* LICENSE
10 *
11 * Copyright (c) 2008-2019 OpenShot Studios, LLC
12 * <http://www.openshotstudios.com/>. This file is part of
13 * OpenShot Library (libopenshot), an open-source project dedicated to
14 * delivering high quality video editing and animation solutions to the
15 * world. For more information visit <http://www.openshot.org/>.
16 *
17 * OpenShot Library (libopenshot) is free software: you can redistribute it
18 * and/or modify it under the terms of the GNU Lesser General Public License
19 * as published by the Free Software Foundation, either version 3 of the
20 * License, or (at your option) any later version.
21 *
22 * OpenShot Library (libopenshot) is distributed in the hope that it will be
23 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU Lesser General Public License for more details.
26 *
27 * You should have received a copy of the GNU Lesser General Public License
28 * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
29 */
30
31 #include "Distortion.h"
32 #include "Exceptions.h"
33
34 using namespace openshot;
35
36 /// Blank constructor, useful when using Json to load the effect properties
Distortion()37 Distortion::Distortion() : distortion_type(HARD_CLIPPING), input_gain(10), output_gain(-10), tone(5) {
38 // Init effect properties
39 init_effect_details();
40 }
41
42 // Default constructor
Distortion(openshot::DistortionType new_distortion_type,Keyframe new_input_gain,Keyframe new_output_gain,Keyframe new_tone)43 Distortion::Distortion(openshot::DistortionType new_distortion_type, Keyframe new_input_gain, Keyframe new_output_gain, Keyframe new_tone) :
44 distortion_type(new_distortion_type), input_gain(new_input_gain), output_gain(new_output_gain), tone(new_tone)
45 {
46 // Init effect properties
47 init_effect_details();
48 }
49
50 // Init effect settings
init_effect_details()51 void Distortion::init_effect_details()
52 {
53 /// Initialize the values of the EffectInfo struct.
54 InitEffectInfo();
55
56 /// Set the effect info
57 info.class_name = "Distortion";
58 info.name = "Distortion";
59 info.description = "Alter the audio by clipping the signal.";
60 info.has_audio = true;
61 info.has_video = false;
62 }
63
64
65 // This method is required for all derived classes of EffectBase, and returns a
66 // modified openshot::Frame object
GetFrame(std::shared_ptr<openshot::Frame> frame,int64_t frame_number)67 std::shared_ptr<openshot::Frame> Distortion::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
68 {
69 filters.clear();
70
71 for (int i = 0; i < frame->audio->getNumChannels(); ++i) {
72 Filter* filter;
73 filters.add (filter = new Filter());
74 }
75
76 updateFilters(frame_number);
77
78 // Add distortion
79 for (int channel = 0; channel < frame->audio->getNumChannels(); channel++)
80 {
81 auto *channel_data = frame->audio->getWritePointer(channel);
82 float out;
83
84 for (auto sample = 0; sample < frame->audio->getNumSamples(); ++sample)
85 {
86
87 const int input_gain_value = (int)input_gain.GetValue(frame_number);
88 const int output_gain_value = (int)output_gain.GetValue(frame_number);
89 const float in = channel_data[sample]*powf(10.0f, input_gain_value * 0.05f);
90
91 // Use the current distortion type
92 switch (distortion_type) {
93
94 case HARD_CLIPPING: {
95 float threshold = 0.5f;
96 if (in > threshold)
97 out = threshold;
98 else if (in < -threshold)
99 out = -threshold;
100 else
101 out = in;
102 break;
103 }
104
105 case SOFT_CLIPPING: {
106 float threshold1 = 1.0f / 3.0f;
107 float threshold2 = 2.0f / 3.0f;
108 if (in > threshold2)
109 out = 1.0f;
110 else if (in > threshold1)
111 out = 1.0f - powf (2.0f - 3.0f * in, 2.0f) / 3.0f;
112 else if (in < -threshold2)
113 out = -1.0f;
114 else if (in < -threshold1)
115 out = -1.0f + powf (2.0f + 3.0f * in, 2.0f) / 3.0f;
116 else
117 out = 2.0f * in;
118 out *= 0.5f;
119 break;
120 }
121
122 case EXPONENTIAL: {
123 if (in > 0.0f)
124 out = 1.0f - expf (-in);
125 else
126 out = -1.0f + expf (in);
127 break;
128 }
129
130 case FULL_WAVE_RECTIFIER: {
131 out = fabsf (in);
132 break;
133 }
134
135 case HALF_WAVE_RECTIFIER: {
136 if (in > 0.0f)
137 out = in;
138 else
139 out = 0.0f;
140 break;
141 }
142 }
143
144 float filtered = filters[channel]->processSingleSampleRaw(out);
145 channel_data[sample] = filtered*powf(10.0f, output_gain_value * 0.05f);
146 }
147 }
148
149 // return the modified frame
150 return frame;
151 }
152
updateFilters(int64_t frame_number)153 void Distortion::updateFilters(int64_t frame_number)
154 {
155 double discrete_frequency = M_PI * 0.01;
156 double gain = pow(10.0, (float)tone.GetValue(frame_number) * 0.05);
157
158 for (int i = 0; i < filters.size(); ++i)
159 filters[i]->updateCoefficients(discrete_frequency, gain);
160 }
161
162 // Generate JSON string of this object
Json() const163 std::string Distortion::Json() const {
164
165 // Return formatted string
166 return JsonValue().toStyledString();
167 }
168
updateCoefficients(const double discrete_frequency,const double gain)169 void Distortion::Filter::updateCoefficients(const double discrete_frequency, const double gain)
170 {
171 double tan_half_wc = tan(discrete_frequency / 2.0);
172 double sqrt_gain = sqrt(gain);
173
174 coefficients = juce::IIRCoefficients(/* b0 */ sqrt_gain * tan_half_wc + gain,
175 /* b1 */ sqrt_gain * tan_half_wc - gain,
176 /* b2 */ 0.0,
177 /* a0 */ sqrt_gain * tan_half_wc + 1.0,
178 /* a1 */ sqrt_gain * tan_half_wc - 1.0,
179 /* a2 */ 0.0);
180 setCoefficients(coefficients);
181 }
182
183 // Generate Json::Value for this object
JsonValue() const184 Json::Value Distortion::JsonValue() const {
185
186 // Create root json object
187 Json::Value root = EffectBase::JsonValue(); // get parent properties
188 root["type"] = info.class_name;
189 root["distortion_type"] = distortion_type;
190 root["input_gain"] = input_gain.JsonValue();
191 root["output_gain"] = output_gain.JsonValue();
192 root["tone"] = tone.JsonValue();
193
194 // return JsonValue
195 return root;
196 }
197
198 // Load JSON string into this object
SetJson(const std::string value)199 void Distortion::SetJson(const std::string value) {
200
201 // Parse JSON string into JSON objects
202 try
203 {
204 const Json::Value root = openshot::stringToJson(value);
205 // Set all values that match
206 SetJsonValue(root);
207 }
208 catch (const std::exception& e)
209 {
210 // Error parsing JSON (or missing keys)
211 throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
212 }
213 }
214
215 // Load Json::Value into this object
SetJsonValue(const Json::Value root)216 void Distortion::SetJsonValue(const Json::Value root) {
217
218 // Set parent data
219 EffectBase::SetJsonValue(root);
220
221 // Set data from Json (if key is found)
222 if (!root["distortion_type"].isNull())
223 distortion_type = (DistortionType)root["distortion_type"].asInt();
224
225 if (!root["input_gain"].isNull())
226 input_gain.SetJsonValue(root["input_gain"]);
227
228 if (!root["output_gain"].isNull())
229 output_gain.SetJsonValue(root["output_gain"]);
230
231 if (!root["tone"].isNull())
232 tone.SetJsonValue(root["tone"]);
233 }
234
235 // Get all properties for a specific frame
PropertiesJSON(int64_t requested_frame) const236 std::string Distortion::PropertiesJSON(int64_t requested_frame) const {
237
238 // Generate JSON properties list
239 Json::Value root;
240 root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
241 root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
242 root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
243 root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
244 root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 1000 * 60 * 30, true, requested_frame);
245
246 // Keyframes
247 root["distortion_type"] = add_property_json("Distortion Type", distortion_type, "int", "", NULL, 0, 3, false, requested_frame);
248 root["input_gain"] = add_property_json("Input Gain (dB)", input_gain.GetValue(requested_frame), "int", "", &input_gain, -24, 24, false, requested_frame);
249 root["output_gain"] = add_property_json("Output Gain (dB)", output_gain.GetValue(requested_frame), "int", "", &output_gain, -24, 24, false, requested_frame);
250 root["tone"] = add_property_json("Tone (dB)", tone.GetValue(requested_frame), "int", "", &tone, -24, 24, false, requested_frame);
251
252 // Add distortion_type choices (dropdown style)
253 root["distortion_type"]["choices"].append(add_property_choice_json("Hard Clipping", HARD_CLIPPING, distortion_type));
254 root["distortion_type"]["choices"].append(add_property_choice_json("Soft Clipping", SOFT_CLIPPING, distortion_type));
255 root["distortion_type"]["choices"].append(add_property_choice_json("Exponential", EXPONENTIAL, distortion_type));
256 root["distortion_type"]["choices"].append(add_property_choice_json("Full Wave Rectifier", FULL_WAVE_RECTIFIER, distortion_type));
257 root["distortion_type"]["choices"].append(add_property_choice_json("Half Wave Rectifier", HALF_WAVE_RECTIFIER, distortion_type));
258
259 // Return formatted string
260 return root.toStyledString();
261 }
262