OpenShot Library | libopenshot 0.2.7
Distortion.cpp
Go to the documentation of this file.
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
34using namespace openshot;
35
36/// Blank constructor, useful when using Json to load the effect properties
37Distortion::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
43Distortion::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
51void Distortion::init_effect_details()
52{
53 /// Initialize the values of the EffectInfo struct.
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
67std::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
153void 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
163std::string Distortion::Json() const {
164
165 // Return formatted string
166 return JsonValue().toStyledString();
167}
168
169void 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
184Json::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
199void 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
216void Distortion::SetJsonValue(const Json::Value root) {
217
218 // Set parent data
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
236std::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}
Header file for Distortion audio effect class.
Header file for all Exception classes.
float End() const
Get end position (in seconds) of clip (trim end of video)
Definition: ClipBase.h:111
float Start() const
Get start position (in seconds) of clip (trim start of video)
Definition: ClipBase.h:110
float Duration() const
Get the length of this clip (in seconds)
Definition: ClipBase.h:112
std::string Id() const
Get the Id of this clip object.
Definition: ClipBase.h:107
Json::Value add_property_choice_json(std::string name, int value, int selected_value) const
Generate JSON choice for a property (dropdown properties)
Definition: ClipBase.cpp:104
int Layer() const
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition: ClipBase.h:109
Json::Value add_property_json(std::string name, float value, std::string type, std::string memo, const Keyframe *keyframe, float min_value, float max_value, bool readonly, int64_t requested_frame) const
Generate JSON for a property.
Definition: ClipBase.cpp:68
void updateCoefficients(const double discrete_frequency, const double gain)
Definition: Distortion.cpp:169
openshot::DistortionType distortion_type
Definition: Distortion.h:62
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: Distortion.cpp:236
std::shared_ptr< openshot::Frame > GetFrame(int64_t frame_number) override
This method is required for all derived classes of ClipBase, and returns a new openshot::Frame object...
Definition: Distortion.h:81
juce::OwnedArray< Filter > filters
Definition: Distortion.h:112
Keyframe output_gain
Definition: Distortion.h:64
void updateFilters(int64_t frame_number)
Definition: Distortion.cpp:153
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Distortion.cpp:184
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: Distortion.cpp:199
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: Distortion.cpp:216
Distortion()
Blank constructor, useful when using Json to load the effect properties.
Definition: Distortion.cpp:37
std::string Json() const override
Generate JSON string of this object.
Definition: Distortion.cpp:163
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: EffectBase.cpp:92
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: EffectBase.cpp:127
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:87
Exception for invalid JSON.
Definition: Exceptions.h:206
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
Definition: KeyFrame.h:72
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: KeyFrame.cpp:368
double GetValue(int64_t index) const
Get the value at a specific index.
Definition: KeyFrame.cpp:268
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: KeyFrame.cpp:335
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:47
DistortionType
This enumeration determines the distortion type of Distortion Effect.
Definition: Enums.h:87
@ HALF_WAVE_RECTIFIER
Definition: Enums.h:92
@ FULL_WAVE_RECTIFIER
Definition: Enums.h:91
@ EXPONENTIAL
Definition: Enums.h:90
@ SOFT_CLIPPING
Definition: Enums.h:89
@ HARD_CLIPPING
Definition: Enums.h:88
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:34
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:58
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:59
std::string class_name
The class name of the effect.
Definition: EffectBase.h:54
std::string name
The name of the effect.
Definition: EffectBase.h:55
std::string description
The description of this effect and what it does.
Definition: EffectBase.h:56