Loading...
Searching...
No Matches
Echo.cpp
Go to the documentation of this file.
// Copyright (c) 2008-2019 OpenShot Studios, LLC
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "Echo.h"
#include "Exceptions.h"
#include "Frame.h"
using namespace openshot;
Echo::Echo() : Echo::Echo(0.1, 0.5, 0.5) { }
Echo::Echo(Keyframe echo_time, Keyframe feedback, Keyframe mix) :
echo_time(echo_time), feedback(feedback), mix(mix)
{
// Init effect properties
init_effect_details();
}
// Init effect settings
void Echo::init_effect_details()
{
InitEffectInfo();
info.class_name = "Echo";
info.name = "Echo";
info.description = "Reflection of sound with a delay after the direct sound.";
info.has_audio = true;
info.has_video = false;
initialized = false;
}
void Echo::setup(std::shared_ptr<openshot::Frame> frame)
{
if (!initialized)
{
const float max_echo_time = 5;
echo_buffer_samples = (int)(max_echo_time * (float)frame->SampleRate()) + 1;
if (echo_buffer_samples < 1)
echo_buffer_samples = 1;
echo_buffer_channels = frame->audio->getNumChannels();
echo_buffer.setSize(echo_buffer_channels, echo_buffer_samples);
echo_buffer.clear();
echo_write_position = 0;
initialized = true;
}
}
// This method is required for all derived classes of EffectBase, and returns a
// modified openshot::Frame object
std::shared_ptr<openshot::Frame> Echo::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
{
const float echo_time_value = (float)echo_time.GetValue(frame_number)*(float)frame->SampleRate();
const float feedback_value = feedback.GetValue(frame_number);
const float mix_value = mix.GetValue(frame_number);
int local_write_position;
setup(frame);
for (int channel = 0; channel < frame->audio->getNumChannels(); channel++)
{
float *channel_data = frame->audio->getWritePointer(channel);
float *echo_data = echo_buffer.getWritePointer(channel);
local_write_position = echo_write_position;
for (auto sample = 0; sample < frame->audio->getNumSamples(); ++sample)
{
const float in = (float)(channel_data[sample]);
float out = 0.0f;
float read_position = fmodf((float)local_write_position - echo_time_value + (float)echo_buffer_samples, echo_buffer_samples);
int local_read_position = floorf(read_position);
if (local_read_position != local_write_position)
{
float fraction = read_position - (float)local_read_position;
float echoed1 = echo_data[(local_read_position + 0)];
float echoed2 = echo_data[(local_read_position + 1) % echo_buffer_samples];
out = (float)(echoed1 + fraction * (echoed2 - echoed1));
channel_data[sample] = in + mix_value*(out - in);
echo_data[local_write_position] = in + out*feedback_value;
}
if (++local_write_position >= echo_buffer_samples)
local_write_position -= echo_buffer_samples;
}
}
echo_write_position = local_write_position;
// return the modified frame
return frame;
}
// Generate JSON string of this object
std::string Echo::Json() const {
// Return formatted string
return JsonValue().toStyledString();
}
// Generate Json::Value for this object
Json::Value Echo::JsonValue() const {
// Create root json object
Json::Value root = EffectBase::JsonValue(); // get parent properties
root["type"] = info.class_name;
root["echo_time"] = echo_time.JsonValue();
root["feedback"] = feedback.JsonValue();
root["mix"] = mix.JsonValue();
// return JsonValue
return root;
}
// Load JSON string into this object
void Echo::SetJson(const std::string value) {
// Parse JSON string into JSON objects
try
{
const Json::Value root = openshot::stringToJson(value);
// Set all values that match
SetJsonValue(root);
}
catch (const std::exception& e)
{
// Error parsing JSON (or missing keys)
throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
}
}
// Load Json::Value into this object
void Echo::SetJsonValue(const Json::Value root) {
// Set parent data
EffectBase::SetJsonValue(root);
// Set data from Json (if key is found)
if (!root["echo_time"].isNull())
echo_time.SetJsonValue(root["echo_time"]);
if (!root["feedback"].isNull())
feedback.SetJsonValue(root["feedback"]);
if (!root["mix"].isNull())
mix.SetJsonValue(root["mix"]);
}
// Get all properties for a specific frame
std::string Echo::PropertiesJSON(int64_t requested_frame) const {
// Generate JSON properties list
Json::Value root;
root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 1000 * 60 * 30, true, requested_frame);
// Keyframes
root["echo_time"] = add_property_json("Time", echo_time.GetValue(requested_frame), "float", "", &echo_time, 0, 5, false, requested_frame);
root["feedback"] = add_property_json("Feedback", feedback.GetValue(requested_frame), "float", "", &feedback, 0, 1, false, requested_frame);
root["mix"] = add_property_json("Mix", mix.GetValue(requested_frame), "float", "", &mix, 0, 1, false, requested_frame);
// Return formatted string
return root.toStyledString();
}
Echo.h
Header file for Echo audio effect class.
Exceptions.h
Header file for all Exception classes.
Frame.h
Header file for Frame class.
openshot::ClipBase::Start
float Start() const
Get start position (in seconds) of clip (trim start of video)
Definition ClipBase.h:88
openshot::ClipBase::Duration
float Duration() const
Get the length of this clip (in seconds)
Definition ClipBase.h:90
openshot::ClipBase::End
virtual float End() const
Get end position (in seconds) of clip (trim end of video)
Definition ClipBase.h:89
openshot::ClipBase::Id
std::string Id() const
Get the Id of this clip object.
Definition ClipBase.h:85
openshot::ClipBase::Layer
int Layer() const
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition ClipBase.h:87
openshot::ClipBase::add_property_json
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
openshot::Echo
This class adds a echo into the audio.
Definition Echo.h:36
openshot::Echo::mix
Keyframe mix
Definition Echo.h:44
openshot::Echo::SetJson
void SetJson(const std::string value) override
Load JSON string into this object.
Definition Echo.cpp:129
openshot::Echo::echo_buffer_channels
int echo_buffer_channels
Definition Echo.h:48
openshot::Echo::echo_time
Keyframe echo_time
Definition Echo.h:42
openshot::Echo::Json
std::string Json() const override
Generate JSON string of this object.
Definition Echo.cpp:108
openshot::Echo::echo_write_position
int echo_write_position
Definition Echo.h:49
openshot::Echo::SetJsonValue
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition Echo.cpp:146
openshot::Echo::echo_buffer
juce::AudioBuffer< float > echo_buffer
Definition Echo.h:46
openshot::Echo::GetFrame
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:58
openshot::Echo::initialized
bool initialized
Definition Echo.h:50
openshot::Echo::JsonValue
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition Echo.cpp:115
openshot::Echo::Echo
Echo()
Default constructor.
Definition Echo.cpp:19
openshot::Echo::echo_buffer_samples
int echo_buffer_samples
Definition Echo.h:47
openshot::Echo::PropertiesJSON
std::string PropertiesJSON(int64_t requested_frame) const override
Definition Echo.cpp:161
openshot::Echo::setup
void setup(std::shared_ptr< openshot::Frame > frame)
Definition Echo.cpp:43
openshot::Echo::feedback
Keyframe feedback
Definition Echo.h:43
openshot::EffectBase::JsonValue
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Definition EffectBase.cpp:77
openshot::EffectBase::InitEffectInfo
void InitEffectInfo()
Definition EffectBase.cpp:24
openshot::EffectBase::SetJsonValue
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition EffectBase.cpp:112
openshot::EffectBase::info
EffectInfoStruct info
Information about the current effect.
Definition EffectBase.h:69
openshot::InvalidJSON
Exception for invalid JSON.
Definition Exceptions.h:218
openshot::Keyframe
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
Definition KeyFrame.h:53
openshot::Keyframe::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition KeyFrame.cpp:372
openshot::Keyframe::GetValue
double GetValue(int64_t index) const
Get the value at a specific index.
Definition KeyFrame.cpp:258
openshot::Keyframe::JsonValue
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition KeyFrame.cpp:339
openshot
This namespace is the default namespace for all code in the openshot library.
Definition Compressor.h:29
openshot::stringToJson
const Json::Value stringToJson(const std::string value)
Definition Json.cpp:16
openshot::EffectInfoStruct::has_video
bool has_video
Determines if this effect manipulates the image of a frame.
Definition EffectBase.h:40
openshot::EffectInfoStruct::has_audio
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition EffectBase.h:41
openshot::EffectInfoStruct::class_name
std::string class_name
The class name of the effect.
Definition EffectBase.h:36
openshot::EffectInfoStruct::name
std::string name
The name of the effect.
Definition EffectBase.h:37
openshot::EffectInfoStruct::description
std::string description
The description of this effect and what it does.
Definition EffectBase.h:38