23 distortion_type(distortion_type), input_gain(input_gain),
24 output_gain(output_gain), tone(tone)
27 init_effect_details();
31 void Distortion::init_effect_details()
47 std::shared_ptr<openshot::Frame>
Distortion::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
51 for (
int i = 0; i < frame->audio->getNumChannels(); ++i) {
59 for (
int channel = 0; channel < frame->audio->getNumChannels(); channel++)
61 auto *channel_data = frame->audio->getWritePointer(channel);
64 for (
auto sample = 0; sample < frame->audio->getNumSamples(); ++sample)
69 const float in = channel_data[sample]*powf(10.0f, input_gain_value * 0.05f);
75 float threshold = 0.5f;
78 else if (in < -threshold)
86 float threshold1 = 1.0f / 3.0f;
87 float threshold2 = 2.0f / 3.0f;
90 else if (in > threshold1)
91 out = 1.0f - powf (2.0f - 3.0f * in, 2.0f) / 3.0f;
92 else if (in < -threshold2)
94 else if (in < -threshold1)
95 out = -1.0f + powf (2.0f + 3.0f * in, 2.0f) / 3.0f;
104 out = 1.0f - expf (-in);
106 out = -1.0f + expf (in);
124 float filtered =
filters[channel]->processSingleSampleRaw(out);
125 channel_data[sample] = filtered*powf(10.0f, output_gain_value * 0.05f);
135 double discrete_frequency = M_PI * 0.01;
136 double gain = pow(10.0, (
float)
tone.
GetValue(frame_number) * 0.05);
138 for (
int i = 0; i <
filters.size(); ++i)
139 filters[i]->updateCoefficients(discrete_frequency, gain);
151 double tan_half_wc = tan(discrete_frequency / 2.0);
152 double sqrt_gain = sqrt(gain);
154 coefficients = juce::IIRCoefficients( sqrt_gain * tan_half_wc + gain,
155 sqrt_gain * tan_half_wc - gain,
157 sqrt_gain * tan_half_wc + 1.0,
158 sqrt_gain * tan_half_wc - 1.0,
160 setCoefficients(coefficients);
188 catch (
const std::exception& e)
191 throw InvalidJSON(
"JSON is invalid (missing keys or invalid data types)");
202 if (!root[
"distortion_type"].isNull())
205 if (!root[
"input_gain"].isNull())
208 if (!root[
"output_gain"].isNull())
211 if (!root[
"tone"].isNull())
220 root[
"id"] =
add_property_json(
"ID", 0.0,
"string",
Id(), NULL, -1, -1,
true, requested_frame);
222 root[
"start"] =
add_property_json(
"Start",
Start(),
"float",
"", NULL, 0, 1000 * 60 * 30,
false, requested_frame);
223 root[
"end"] =
add_property_json(
"End",
End(),
"float",
"", NULL, 0, 1000 * 60 * 30,
false, requested_frame);
224 root[
"duration"] =
add_property_json(
"Duration",
Duration(),
"float",
"", NULL, 0, 1000 * 60 * 30,
true, requested_frame);
240 return root.toStyledString();
Header file for Distortion audio effect class.
Header file for all Exception classes.
float Start() const
Get start position (in seconds) of clip (trim start of video)
float Duration() const
Get the length of this clip (in seconds)
virtual float End() const
Get end position (in seconds) of clip (trim end of video)
std::string Id() const
Get the Id of this clip object.
Json::Value add_property_choice_json(std::string name, int value, int selected_value) const
Generate JSON choice for a property (dropdown properties)
int Layer() const
Get layer of clip on timeline (lower number is covered by higher numbers)
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.
void updateCoefficients(const double discrete_frequency, const double gain)
This class adds a distortion into the audio.
openshot::DistortionType distortion_type
std::string PropertiesJSON(int64_t requested_frame) const override
juce::OwnedArray< Filter > filters
void updateFilters(int64_t frame_number)
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...
Json::Value JsonValue() const override
Generate Json::Value for this object.
void SetJson(const std::string value) override
Load JSON string into this object.
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Distortion()
Default constructor.
std::string Json() const override
Generate JSON string of this object.
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
EffectInfoStruct info
Information about the current effect.
Exception for invalid JSON.
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
double GetValue(int64_t index) const
Get the value at a specific index.
Json::Value JsonValue() const
Generate Json::Value for this object.
This namespace is the default namespace for all code in the openshot library.
DistortionType
This enumeration determines the distortion type of Distortion Effect.
const Json::Value stringToJson(const std::string value)
bool has_video
Determines if this effect manipulates the image of a frame.
bool has_audio
Determines if this effect manipulates the audio of a frame.
std::string class_name
The class name of the effect.
std::string name
The name of the effect.
std::string description
The description of this effect and what it does.