1 /**
2  * @file
3  * @brief Source file for Delay 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 "Delay.h"
32 #include "Exceptions.h"
33 
34 using namespace openshot;
35 
36 /// Blank constructor, useful when using Json to load the effect properties
Delay()37 Delay::Delay() : delay_time(1) {
38 	// Init effect properties
39 	init_effect_details();
40 }
41 
42 // Default constructor
Delay(Keyframe new_delay_time)43 Delay::Delay(Keyframe new_delay_time) : delay_time(new_delay_time)
44 {
45 	// Init effect properties
46 	init_effect_details();
47 }
48 
49 // Init effect settings
init_effect_details()50 void Delay::init_effect_details()
51 {
52 	/// Initialize the values of the EffectInfo struct.
53 	InitEffectInfo();
54 
55 	/// Set the effect info
56 	info.class_name = "Delay";
57 	info.name = "Delay";
58 	info.description = "Adjust the synchronism between the audio and video track.";
59 	info.has_audio = true;
60 	info.has_video = false;
61 	initialized = false;
62 }
63 
setup(std::shared_ptr<openshot::Frame> frame)64 void Delay::setup(std::shared_ptr<openshot::Frame> frame)
65 {
66 	if (!initialized)
67 	{
68 		const float max_delay_time = 5;
69 		delay_buffer_samples = (int)(max_delay_time * (float)frame->SampleRate()) + 1;
70 
71 		if (delay_buffer_samples < 1)
72 			delay_buffer_samples = 1;
73 
74 		delay_buffer_channels = frame->audio->getNumChannels();
75 		delay_buffer.setSize(delay_buffer_channels, delay_buffer_samples);
76 		delay_buffer.clear();
77 		delay_write_position = 0;
78 		initialized = true;
79 	}
80 }
81 
82 // This method is required for all derived classes of EffectBase, and returns a
83 // modified openshot::Frame object
GetFrame(std::shared_ptr<openshot::Frame> frame,int64_t frame_number)84 std::shared_ptr<openshot::Frame> Delay::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
85 {
86 	const float delay_time_value = (float)delay_time.GetValue(frame_number)*(float)frame->SampleRate();
87 	int local_write_position;
88 
89 	setup(frame);
90 
91 	for (int channel = 0; channel < frame->audio->getNumChannels(); channel++)
92 	{
93 		float *channel_data = frame->audio->getWritePointer(channel);
94         float *delay_data = delay_buffer.getWritePointer(channel);
95         local_write_position = delay_write_position;
96 
97 		for (auto sample = 0; sample < frame->audio->getNumSamples(); ++sample)
98 		{
99 			const float in = (float)(channel_data[sample]);
100             float out = 0.0f;
101 
102             float read_position = fmodf((float)local_write_position - delay_time_value + (float)delay_buffer_samples, delay_buffer_samples);
103             int local_read_position = floorf(read_position);
104 
105             if (local_read_position != local_write_position)
106 			{
107                 float fraction = read_position - (float)local_read_position;
108                 float delayed1 = delay_data[(local_read_position + 0)];
109                 float delayed2 = delay_data[(local_read_position + 1) % delay_buffer_samples];
110                 out = (float)(delayed1 + fraction * (delayed2 - delayed1));
111 
112                 channel_data[sample] = in + (out - in);
113 				delay_data[local_write_position] = in;
114             }
115 
116             if (++local_write_position >= delay_buffer_samples)
117                 local_write_position -= delay_buffer_samples;
118 		}
119 	}
120 
121     delay_write_position = local_write_position;
122 
123 	// return the modified frame
124 	return frame;
125 }
126 
127 // Generate JSON string of this object
Json() const128 std::string Delay::Json() const {
129 
130 	// Return formatted string
131 	return JsonValue().toStyledString();
132 }
133 
134 // Generate Json::Value for this object
JsonValue() const135 Json::Value Delay::JsonValue() const {
136 
137 	// Create root json object
138 	Json::Value root = EffectBase::JsonValue(); // get parent properties
139 	root["type"] = info.class_name;
140 	root["delay_time"] = delay_time.JsonValue();
141 
142 	// return JsonValue
143 	return root;
144 }
145 
146 // Load JSON string into this object
SetJson(const std::string value)147 void Delay::SetJson(const std::string value) {
148 
149 	// Parse JSON string into JSON objects
150 	try
151 	{
152 		const Json::Value root = openshot::stringToJson(value);
153 		// Set all values that match
154 		SetJsonValue(root);
155 	}
156 	catch (const std::exception& e)
157 	{
158 		// Error parsing JSON (or missing keys)
159 		throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
160 	}
161 }
162 
163 // Load Json::Value into this object
SetJsonValue(const Json::Value root)164 void Delay::SetJsonValue(const Json::Value root) {
165 
166 	// Set parent data
167 	EffectBase::SetJsonValue(root);
168 
169 	// Set data from Json (if key is found)
170 	if (!root["delay_time"].isNull())
171 		delay_time.SetJsonValue(root["delay_time"]);
172 }
173 
174 // Get all properties for a specific frame
PropertiesJSON(int64_t requested_frame) const175 std::string Delay::PropertiesJSON(int64_t requested_frame) const {
176 
177 	// Generate JSON properties list
178 	Json::Value root;
179 	root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
180 	root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
181 	root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
182 	root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
183 	root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 1000 * 60 * 30, true, requested_frame);
184 
185 	// Keyframes
186 	root["delay_time"] = add_property_json("Delay Time", delay_time.GetValue(requested_frame), "float", "", &delay_time, 0, 5, false, requested_frame);
187 
188 	// Return formatted string
189 	return root.toStyledString();
190 }
191