37Distortion::Distortion() : distortion_type(
HARD_CLIPPING), input_gain(10), output_gain(-10), tone(5) {
39 init_effect_details();
44 distortion_type(new_distortion_type), input_gain(new_input_gain), output_gain(new_output_gain), tone(new_tone)
47 init_effect_details();
51void Distortion::init_effect_details()
67std::shared_ptr<openshot::Frame>
Distortion::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
71 for (
int i = 0; i < frame->audio->getNumChannels(); ++i) {
79 for (
int channel = 0; channel < frame->audio->getNumChannels(); channel++)
81 auto *channel_data = frame->audio->getWritePointer(channel);
84 for (
auto sample = 0; sample < frame->audio->getNumSamples(); ++sample)
89 const float in = channel_data[sample]*powf(10.0f, input_gain_value * 0.05f);
95 float threshold = 0.5f;
98 else if (in < -threshold)
106 float threshold1 = 1.0f / 3.0f;
107 float threshold2 = 2.0f / 3.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)
114 else if (in < -threshold1)
115 out = -1.0f + powf (2.0f + 3.0f * in, 2.0f) / 3.0f;
124 out = 1.0f - expf (-in);
126 out = -1.0f + expf (in);
144 float filtered =
filters[channel]->processSingleSampleRaw(out);
145 channel_data[sample] = filtered*powf(10.0f, output_gain_value * 0.05f);
155 double discrete_frequency = M_PI * 0.01;
156 double gain = pow(10.0, (
float)
tone.
GetValue(frame_number) * 0.05);
158 for (
int i = 0; i <
filters.size(); ++i)
159 filters[i]->updateCoefficients(discrete_frequency, gain);
171 double tan_half_wc = tan(discrete_frequency / 2.0);
172 double sqrt_gain = sqrt(gain);
174 coefficients = juce::IIRCoefficients( sqrt_gain * tan_half_wc + gain,
175 sqrt_gain * tan_half_wc - gain,
177 sqrt_gain * tan_half_wc + 1.0,
178 sqrt_gain * tan_half_wc - 1.0,
180 setCoefficients(coefficients);
208 catch (
const std::exception& e)
211 throw InvalidJSON(
"JSON is invalid (missing keys or invalid data types)");
222 if (!root[
"distortion_type"].isNull())
225 if (!root[
"input_gain"].isNull())
228 if (!root[
"output_gain"].isNull())
231 if (!root[
"tone"].isNull())
240 root[
"id"] =
add_property_json(
"ID", 0.0,
"string",
Id(), NULL, -1, -1,
true, 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);
260 return root.toStyledString();
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)
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)
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)
openshot::DistortionType distortion_type
std::string PropertiesJSON(int64_t requested_frame) const override
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...
juce::OwnedArray< Filter > filters
void updateFilters(int64_t frame_number)
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()
Blank constructor, useful when using Json to load the effect properties.
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.