Loading...
Searching...
No Matches
Robotization.cpp
Go to the documentation of this file.
// Copyright (c) 2008-2019 OpenShot Studios, LLC
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "Robotization.h"
#include "Exceptions.h"
#include "Frame.h"
using namespace openshot;
using namespace juce;
Robotization::Robotization()
: Robotization::Robotization(FFT_SIZE_512, HOP_SIZE_2, RECTANGULAR) {}
Robotization::Robotization(openshot::FFTSize fft_size,
openshot::HopSize hop_size,
openshot::WindowType window_type) :
fft_size(fft_size), hop_size(hop_size),
window_type(window_type), stft(*this)
{
// Init effect properties
init_effect_details();
}
// Init effect settings
void Robotization::init_effect_details()
{
InitEffectInfo();
info.class_name = "Robotization";
info.name = "Robotization";
info.description = "Transform the voice present in an audio track into a robotic voice effect.";
info.has_audio = true;
info.has_video = false;
}
// This method is required for all derived classes of EffectBase, and returns a
// modified openshot::Frame object
std::shared_ptr<openshot::Frame> Robotization::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
{
const std::lock_guard<std::recursive_mutex> lock(mutex);
ScopedNoDenormals noDenormals;
const int num_input_channels = frame->audio->getNumChannels();
const int num_output_channels = frame->audio->getNumChannels();
const int num_samples = frame->audio->getNumSamples();
const int hop_size_value = 1 << ((int)hop_size + 1);
const int fft_size_value = 1 << ((int)fft_size + 5);
stft.setup(num_output_channels);
stft.updateParameters((int)fft_size_value,
(int)hop_size_value,
(int)window_type);
stft.process(*frame->audio);
// return the modified frame
return frame;
}
void Robotization::RobotizationEffect::modification(const int channel)
{
fft->perform(time_domain_buffer, frequency_domain_buffer, false);
for (int index = 0; index < fft_size; ++index) {
float magnitude = abs(frequency_domain_buffer[index]);
frequency_domain_buffer[index].real(magnitude);
frequency_domain_buffer[index].imag(0.0f);
}
fft->perform(frequency_domain_buffer, time_domain_buffer, true);
}
// Generate JSON string of this object
std::string Robotization::Json() const {
// Return formatted string
return JsonValue().toStyledString();
}
// Generate Json::Value for this object
Json::Value Robotization::JsonValue() const {
// Create root json object
Json::Value root = EffectBase::JsonValue(); // get parent properties
root["type"] = info.class_name;
root["fft_size"] = fft_size;
root["hop_size"] = hop_size;
root["window_type"] = window_type;
// return JsonValue
return root;
}
// Load JSON string into this object
void Robotization::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 Robotization::SetJsonValue(const Json::Value root) {
// Set parent data
EffectBase::SetJsonValue(root);
if (!root["fft_size"].isNull())
fft_size = (FFTSize)root["fft_size"].asInt();
if (!root["hop_size"].isNull())
hop_size = (HopSize)root["hop_size"].asInt();
if (!root["window_type"].isNull())
window_type = (WindowType)root["window_type"].asInt();
}
// Get all properties for a specific frame
std::string Robotization::PropertiesJSON(int64_t requested_frame) const {
// Generate JSON properties list
Json::Value root = BasePropertiesJSON(requested_frame);
// Keyframes
root["fft_size"] = add_property_json("FFT Size", fft_size, "int", "", NULL, 0, 8, false, requested_frame);
root["hop_size"] = add_property_json("Hop Size", hop_size, "int", "", NULL, 0, 2, false, requested_frame);
root["window_type"] = add_property_json("Window Type", window_type, "int", "", NULL, 0, 3, false, requested_frame);
// Add fft_size choices (dropdown style)
root["fft_size"]["choices"].append(add_property_choice_json("128", FFT_SIZE_128, fft_size));
root["fft_size"]["choices"].append(add_property_choice_json("256", FFT_SIZE_256, fft_size));
root["fft_size"]["choices"].append(add_property_choice_json("512", FFT_SIZE_512, fft_size));
root["fft_size"]["choices"].append(add_property_choice_json("1024", FFT_SIZE_1024, fft_size));
root["fft_size"]["choices"].append(add_property_choice_json("2048", FFT_SIZE_2048, fft_size));
// Add hop_size choices (dropdown style)
root["hop_size"]["choices"].append(add_property_choice_json("1/2", HOP_SIZE_2, hop_size));
root["hop_size"]["choices"].append(add_property_choice_json("1/4", HOP_SIZE_4, hop_size));
root["hop_size"]["choices"].append(add_property_choice_json("1/8", HOP_SIZE_8, hop_size));
// Add window_type choices (dropdown style)
root["window_type"]["choices"].append(add_property_choice_json("Rectangular", RECTANGULAR, window_type));
root["window_type"]["choices"].append(add_property_choice_json("Bart Lett", BART_LETT, window_type));
root["window_type"]["choices"].append(add_property_choice_json("Hann", HANN, window_type));
root["window_type"]["choices"].append(add_property_choice_json("Hamming", HAMMING, window_type));
// Return formatted string
return root.toStyledString();
}
Exceptions.h
Header file for all Exception classes.
Frame.h
Header file for Frame class.
Robotization.h
Header file for Robotization audio effect class.
openshot::ClipBase::add_property_choice_json
Json::Value add_property_choice_json(std::string name, int value, int selected_value) const
Generate JSON choice for a property (dropdown properties).
Definition ClipBase.cpp:132
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::EffectBase::JsonValue
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Definition EffectBase.cpp:79
openshot::EffectBase::InitEffectInfo
void InitEffectInfo()
Definition EffectBase.cpp:24
openshot::EffectBase::BasePropertiesJSON
Json::Value BasePropertiesJSON(int64_t requested_frame) const
Generate JSON object of base properties (recommended to be used by all effects).
Definition EffectBase.cpp:179
openshot::EffectBase::SetJsonValue
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition EffectBase.cpp:115
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::Robotization::Robotization
Robotization()
Default constructor.
Definition Robotization.cpp:20
openshot::Robotization::mutex
std::recursive_mutex mutex
Definition Robotization.h:88
openshot::Robotization::PropertiesJSON
std::string PropertiesJSON(int64_t requested_frame) const override
Definition Robotization.cpp:139
openshot::Robotization::SetJsonValue
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition Robotization.cpp:123
openshot::Robotization::hop_size
openshot::HopSize hop_size
Definition Robotization.h:50
openshot::Robotization::fft_size
openshot::FFTSize fft_size
Definition Robotization.h:49
openshot::Robotization::SetJson
void SetJson(const std::string value) override
Load JSON string into this object.
Definition Robotization.cpp:106
openshot::Robotization::window_type
openshot::WindowType window_type
Definition Robotization.h:51
openshot::Robotization::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 Robotization.h:61
openshot::Robotization::Json
std::string Json() const override
Generate JSON string of this object.
Definition Robotization.cpp:85
openshot::Robotization::JsonValue
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition Robotization.cpp:92
openshot::Robotization::stft
RobotizationEffect stft
Definition Robotization.h:89
openshot::STFT::frequency_domain_buffer
juce::HeapBlock< juce::dsp::Complex< float > > frequency_domain_buffer
Definition STFT.h:63
openshot::STFT::fft
std::unique_ptr< juce::dsp::FFT > fft
Definition STFT.h:53
openshot::STFT::fft_size
int fft_size
Definition STFT.h:52
openshot::STFT::time_domain_buffer
juce::HeapBlock< juce::dsp::Complex< float > > time_domain_buffer
Definition STFT.h:62
juce
Definition Robotization.h:29
openshot
This namespace is the default namespace for all code in the openshot library.
Definition Compressor.h:29
openshot::FFTSize
FFTSize
This enumeration determines the FFT size.
Definition Enums.h:130
openshot::FFT_SIZE_1024
@ FFT_SIZE_1024
Definition Enums.h:136
openshot::FFT_SIZE_512
@ FFT_SIZE_512
Definition Enums.h:135
openshot::FFT_SIZE_256
@ FFT_SIZE_256
Definition Enums.h:134
openshot::FFT_SIZE_2048
@ FFT_SIZE_2048
Definition Enums.h:137
openshot::FFT_SIZE_128
@ FFT_SIZE_128
Definition Enums.h:133
openshot::HopSize
HopSize
This enumeration determines the hop size.
Definition Enums.h:143
openshot::HOP_SIZE_2
@ HOP_SIZE_2
Definition Enums.h:144
openshot::HOP_SIZE_4
@ HOP_SIZE_4
Definition Enums.h:145
openshot::HOP_SIZE_8
@ HOP_SIZE_8
Definition Enums.h:146
openshot::WindowType
WindowType
This enumeration determines the window type.
Definition Enums.h:150
openshot::RECTANGULAR
@ RECTANGULAR
Definition Enums.h:151
openshot::HANN
@ HANN
Definition Enums.h:153
openshot::BART_LETT
@ BART_LETT
Definition Enums.h:152
openshot::HAMMING
@ HAMMING
Definition Enums.h:154
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