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