OpenShot Library | libopenshot  0.2.7
Expander.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Source file for Expander 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 "Expander.h"
32 #include "Exceptions.h"
33 
34 using namespace openshot;
35 
36 /// Blank constructor, useful when using Json to load the effect properties
37 Expander::Expander() : threshold(-10), ratio(1), attack(1), release(1), makeup_gain(1), bypass(false) {
38  // Init effect properties
39  init_effect_details();
40 }
41 
42 // Default constructor
43 Expander::Expander(Keyframe new_threshold, Keyframe new_ratio, Keyframe new_attack, Keyframe new_release, Keyframe new_makeup_gain, Keyframe new_bypass) :
44  threshold(new_threshold), ratio(new_ratio), attack(new_attack), release(new_release), makeup_gain(new_makeup_gain), bypass(new_bypass)
45 {
46  // Init effect properties
47  init_effect_details();
48 }
49 
50 // Init effect settings
51 void Expander::init_effect_details()
52 {
53  /// Initialize the values of the EffectInfo struct.
55 
56  /// Set the effect info
57  info.class_name = "Expander";
58  info.name = "Expander";
59  info.description = "Louder parts of audio becomes relatively louder and quieter parts becomes quieter.";
60  info.has_audio = true;
61  info.has_video = false;
62 
63  input_level = 0.0f;
64  yl_prev = 0.0f;
65 
66 
67 }
68 
69 // This method is required for all derived classes of EffectBase, and returns a
70 // modified openshot::Frame object
71 std::shared_ptr<openshot::Frame> Expander::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
72 {
73  // Adding Expander
74  const int num_input_channels = frame->audio->getNumChannels();
75  const int num_output_channels = frame->audio->getNumChannels();
76  const int num_samples = frame->audio->getNumSamples();
77 
78  mixed_down_input.setSize(1, num_samples);
79  inverse_sample_rate = 1.0f / frame->SampleRate();
80  inverseE = 1.0f / M_E;
81 
82  if ((bool)bypass.GetValue(frame_number))
83  return frame;
84 
85  mixed_down_input.clear();
86 
87  for (int channel = 0; channel < num_input_channels; ++channel)
88  mixed_down_input.addFrom(0, 0, *frame->audio, channel, 0, num_samples, 1.0f / num_input_channels);
89 
90  for (int sample = 0; sample < num_samples; ++sample) {
91  float T = threshold.GetValue(frame_number);
92  float R = ratio.GetValue(frame_number);
93  float alphaA = calculateAttackOrRelease(attack.GetValue(frame_number));
94  float alphaR = calculateAttackOrRelease(release.GetValue(frame_number));
95  float gain = makeup_gain.GetValue(frame_number);
96  float input_squared = powf(mixed_down_input.getSample(0, sample), 2.0f);
97 
98  const float average_factor = 0.9999f;
99  input_level = average_factor * input_level + (1.0f - average_factor) * input_squared;
100 
101  xg = (input_level <= 1e-6f) ? -60.0f : 10.0f * log10f(input_level);
102 
103  if (xg > T)
104  yg = xg;
105  else
106  yg = T + (xg - T) * R;
107 
108  xl = xg - yg;
109 
110  if (xl < yl_prev)
111  yl = alphaA * yl_prev + (1.0f - alphaA) * xl;
112  else
113  yl = alphaR * yl_prev + (1.0f - alphaR) * xl;
114 
115 
116  control = powf (10.0f, (gain - yl) * 0.05f);
117  yl_prev = yl;
118 
119  for (int channel = 0; channel < num_input_channels; ++channel) {
120  float new_value = frame->audio->getSample(channel, sample)*control;
121  frame->audio->setSample(channel, sample, new_value);
122  }
123  }
124 
125  for (int channel = num_input_channels; channel < num_output_channels; ++channel)
126  frame->audio->clear(channel, 0, num_samples);
127 
128  // return the modified frame
129  return frame;
130 }
131 
133 {
134  if (value == 0.0f)
135  return 0.0f;
136  else
137  return pow (inverseE, inverse_sample_rate / value);
138 }
139 
140 // Generate JSON string of this object
141 std::string Expander::Json() const {
142 
143  // Return formatted string
144  return JsonValue().toStyledString();
145 }
146 
147 // Generate Json::Value for this object
148 Json::Value Expander::JsonValue() const {
149 
150  // Create root json object
151  Json::Value root = EffectBase::JsonValue(); // get parent properties
152  root["type"] = info.class_name;
153  root["threshold"] = threshold.JsonValue();
154  root["ratio"] = ratio.JsonValue();
155  root["attack"] = attack.JsonValue();
156  root["release"] = release.JsonValue();
157  root["makeup_gain"] = makeup_gain.JsonValue();
158  root["bypass"] = bypass.JsonValue();
159 
160  // return JsonValue
161  return root;
162 }
163 
164 // Load JSON string into this object
165 void Expander::SetJson(const std::string value) {
166 
167  // Parse JSON string into JSON objects
168  try
169  {
170  const Json::Value root = openshot::stringToJson(value);
171  // Set all values that match
172  SetJsonValue(root);
173  }
174  catch (const std::exception& e)
175  {
176  // Error parsing JSON (or missing keys)
177  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
178  }
179 }
180 
181 // Load Json::Value into this object
182 void Expander::SetJsonValue(const Json::Value root) {
183 
184  // Set parent data
186 
187  // Set data from Json (if key is found)
188  if (!root["threshold"].isNull())
189  threshold.SetJsonValue(root["threshold"]);
190 
191  if (!root["ratio"].isNull())
192  ratio.SetJsonValue(root["ratio"]);
193 
194  if (!root["attack"].isNull())
195  attack.SetJsonValue(root["attack"]);
196 
197  if (!root["release"].isNull())
198  release.SetJsonValue(root["release"]);
199 
200  if (!root["makeup_gain"].isNull())
201  makeup_gain.SetJsonValue(root["makeup_gain"]);
202 
203  if (!root["bypass"].isNull())
204  bypass.SetJsonValue(root["bypass"]);
205 }
206 
207 // Get all properties for a specific frame
208 std::string Expander::PropertiesJSON(int64_t requested_frame) const {
209 
210  // Generate JSON properties list
211  Json::Value root;
212  root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
213  root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
214  root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
215  root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
216  root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 1000 * 60 * 30, true, requested_frame);
217 
218  // Keyframes
219  root["threshold"] = add_property_json("Threshold (dB)", threshold.GetValue(requested_frame), "float", "", &threshold, -60, 0, false, requested_frame);
220  root["ratio"] = add_property_json("Ratio", ratio.GetValue(requested_frame), "float", "", &ratio, 1, 100, false, requested_frame);
221  root["attack"] = add_property_json("Attack (ms)", attack.GetValue(requested_frame), "float", "", &attack, 0.1, 100, false, requested_frame);
222  root["release"] = add_property_json("Release (ms)", release.GetValue(requested_frame), "float", "", &release, 10, 1000, false, requested_frame);
223  root["makeup_gain"] = add_property_json("Makeup gain (dB)", makeup_gain.GetValue(requested_frame), "float", "", &makeup_gain, -12, 12, false, requested_frame);
224  root["bypass"] = add_property_json("Bypass", bypass.GetValue(requested_frame), "bool", "", &bypass, 0, 1, false, requested_frame);
225 
226  // Return formatted string
227  return root.toStyledString();
228 }
Header file for all Exception classes.
Header file for Expander audio effect class.
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
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
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
Expander()
Blank constructor, useful when using Json to load the effect properties.
Definition: Expander.cpp:37
float inverse_sample_rate
Definition: Expander.h:78
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: Expander.cpp:208
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: Expander.cpp:182
Keyframe makeup_gain
Definition: Expander.h:65
Keyframe ratio
Definition: Expander.h:62
Keyframe release
Definition: Expander.h:64
float calculateAttackOrRelease(float value)
Definition: Expander.cpp:132
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: Expander.cpp:165
Keyframe threshold
Definition: Expander.h:61
Keyframe bypass
Definition: Expander.h:66
juce::AudioSampleBuffer mixed_down_input
Definition: Expander.h:68
Keyframe attack
Definition: Expander.h:63
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Expander.cpp:148
std::string Json() const override
Generate JSON string of this object.
Definition: Expander.cpp:141
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: Expander.h:97
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
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