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
Expander()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
Expander(Keyframe new_threshold,Keyframe new_ratio,Keyframe new_attack,Keyframe new_release,Keyframe new_makeup_gain,Keyframe new_bypass)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
init_effect_details()51 void Expander::init_effect_details()
52 {
53 	/// Initialize the values of the EffectInfo struct.
54 	InitEffectInfo();
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
GetFrame(std::shared_ptr<openshot::Frame> frame,int64_t frame_number)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 
calculateAttackOrRelease(float value)132 float Expander::calculateAttackOrRelease(float value)
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
Json() const141 std::string Expander::Json() const {
142 
143 	// Return formatted string
144 	return JsonValue().toStyledString();
145 }
146 
147 // Generate Json::Value for this object
JsonValue() const148 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
SetJson(const std::string value)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
SetJsonValue(const Json::Value root)182 void Expander::SetJsonValue(const Json::Value root) {
183 
184 	// Set parent data
185 	EffectBase::SetJsonValue(root);
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
PropertiesJSON(int64_t requested_frame) const208 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 }
229