OpenShot Library | libopenshot 0.3.2
Delay.cpp
Go to the documentation of this file.
1
9// Copyright (c) 2008-2019 OpenShot Studios, LLC
10//
11// SPDX-License-Identifier: LGPL-3.0-or-later
12
13#include "Delay.h"
14#include "Exceptions.h"
15#include "Frame.h"
16
17using namespace openshot;
18
19Delay::Delay() : Delay::Delay(1) { }
20
21Delay::Delay(Keyframe delay_time) : delay_time(delay_time)
22{
23 init_effect_details();
24}
25
26// Init effect settings
27void Delay::init_effect_details()
28{
31
33 info.class_name = "Delay";
34 info.name = "Delay";
35 info.description = "Adjust the synchronism between the audio and video track.";
36 info.has_audio = true;
37 info.has_video = false;
38 initialized = false;
39}
40
41void Delay::setup(std::shared_ptr<openshot::Frame> frame)
42{
43 if (!initialized)
44 {
45 const float max_delay_time = 5;
46 delay_buffer_samples = (int)(max_delay_time * (float)frame->SampleRate()) + 1;
47
50
51 delay_buffer_channels = frame->audio->getNumChannels();
53 delay_buffer.clear();
55 initialized = true;
56 }
57}
58
59// This method is required for all derived classes of EffectBase, and returns a
60// modified openshot::Frame object
61std::shared_ptr<openshot::Frame> Delay::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
62{
63 const float delay_time_value = (float)delay_time.GetValue(frame_number)*(float)frame->SampleRate();
64 int local_write_position;
65
66 setup(frame);
67
68 for (int channel = 0; channel < frame->audio->getNumChannels(); channel++)
69 {
70 float *channel_data = frame->audio->getWritePointer(channel);
71 float *delay_data = delay_buffer.getWritePointer(channel);
72 local_write_position = delay_write_position;
73
74 for (auto sample = 0; sample < frame->audio->getNumSamples(); ++sample)
75 {
76 const float in = (float)(channel_data[sample]);
77 float out = 0.0f;
78
79 float read_position = fmodf((float)local_write_position - delay_time_value + (float)delay_buffer_samples, delay_buffer_samples);
80 int local_read_position = floorf(read_position);
81
82 if (local_read_position != local_write_position)
83 {
84 float fraction = read_position - (float)local_read_position;
85 float delayed1 = delay_data[(local_read_position + 0)];
86 float delayed2 = delay_data[(local_read_position + 1) % delay_buffer_samples];
87 out = (float)(delayed1 + fraction * (delayed2 - delayed1));
88
89 channel_data[sample] = in + (out - in);
90 delay_data[local_write_position] = in;
91 }
92
93 if (++local_write_position >= delay_buffer_samples)
94 local_write_position -= delay_buffer_samples;
95 }
96 }
97
98 delay_write_position = local_write_position;
99
100 // return the modified frame
101 return frame;
102}
103
104// Generate JSON string of this object
105std::string Delay::Json() const {
106
107 // Return formatted string
108 return JsonValue().toStyledString();
109}
110
111// Generate Json::Value for this object
112Json::Value Delay::JsonValue() const {
113
114 // Create root json object
115 Json::Value root = EffectBase::JsonValue(); // get parent properties
116 root["type"] = info.class_name;
117 root["delay_time"] = delay_time.JsonValue();
118
119 // return JsonValue
120 return root;
121}
122
123// Load JSON string into this object
124void Delay::SetJson(const std::string value) {
125
126 // Parse JSON string into JSON objects
127 try
128 {
129 const Json::Value root = openshot::stringToJson(value);
130 // Set all values that match
131 SetJsonValue(root);
132 }
133 catch (const std::exception& e)
134 {
135 // Error parsing JSON (or missing keys)
136 throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
137 }
138}
139
140// Load Json::Value into this object
141void Delay::SetJsonValue(const Json::Value root) {
142
143 // Set parent data
145
146 // Set data from Json (if key is found)
147 if (!root["delay_time"].isNull())
148 delay_time.SetJsonValue(root["delay_time"]);
149}
150
151// Get all properties for a specific frame
152std::string Delay::PropertiesJSON(int64_t requested_frame) const {
153
154 // Generate JSON properties list
155 Json::Value root;
156 root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
157 root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
158 root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
159 root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
160 root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 1000 * 60 * 30, true, requested_frame);
161
162 // Keyframes
163 root["delay_time"] = add_property_json("Delay Time", delay_time.GetValue(requested_frame), "float", "", &delay_time, 0, 5, false, requested_frame);
164
165 // Return formatted string
166 return root.toStyledString();
167}
Header file for Delay audio effect class.
Header file for all Exception classes.
Header file for Frame class.
float Start() const
Get start position (in seconds) of clip (trim start of video)
Definition: ClipBase.h:88
float Duration() const
Get the length of this clip (in seconds)
Definition: ClipBase.h:90
virtual float End() const
Get end position (in seconds) of clip (trim end of video)
Definition: ClipBase.h:89
std::string Id() const
Get the Id of this clip object.
Definition: ClipBase.h:85
int Layer() const
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition: ClipBase.h:87
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:96
This class adds a delay into the audio.
Definition: Delay.h:36
juce::AudioBuffer< float > delay_buffer
Definition: Delay.h:44
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: Delay.cpp:141
Keyframe delay_time
Definition: Delay.h:42
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: Delay.h:56
Delay()
Default constructor.
Definition: Delay.cpp:19
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: Delay.cpp:124
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Delay.cpp:112
std::string Json() const override
Generate JSON string of this object.
Definition: Delay.cpp:105
int delay_write_position
Definition: Delay.h:47
int delay_buffer_samples
Definition: Delay.h:45
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: Delay.cpp:152
bool initialized
Definition: Delay.h:48
int delay_buffer_channels
Definition: Delay.h:46
void setup(std::shared_ptr< openshot::Frame > frame)
Definition: Delay.cpp:41
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: EffectBase.cpp:77
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: EffectBase.cpp:112
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:69
Exception for invalid JSON.
Definition: Exceptions.h:218
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
Definition: KeyFrame.h:53
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: KeyFrame.cpp:372
double GetValue(int64_t index) const
Get the value at a specific index.
Definition: KeyFrame.cpp:258
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: KeyFrame.cpp:339
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:29
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:16
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:40
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:41
std::string class_name
The class name of the effect.
Definition: EffectBase.h:36
std::string name
The name of the effect.
Definition: EffectBase.h:37
std::string description
The description of this effect and what it does.
Definition: EffectBase.h:38