OpenShot Library | libopenshot 0.3.3
Loading...
Searching...
No Matches
Robotization.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 "Robotization.h"
14#include "Exceptions.h"
15#include "Frame.h"
16
17using namespace openshot;
18using namespace juce;
19
22
24 openshot::HopSize hop_size,
25 openshot::WindowType window_type) :
26 fft_size(fft_size), hop_size(hop_size),
27 window_type(window_type), stft(*this)
28{
29 // Init effect properties
30 init_effect_details();
31}
32
33// Init effect settings
34void Robotization::init_effect_details()
35{
38
40 info.class_name = "Robotization";
41 info.name = "Robotization";
42 info.description = "Transform the voice present in an audio track into a robotic voice effect.";
43 info.has_audio = true;
44 info.has_video = false;
45}
46
47// This method is required for all derived classes of EffectBase, and returns a
48// modified openshot::Frame object
49std::shared_ptr<openshot::Frame> Robotization::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
50{
51 const std::lock_guard<std::recursive_mutex> lock(mutex);
52 ScopedNoDenormals noDenormals;
53
54 const int num_input_channels = frame->audio->getNumChannels();
55 const int num_output_channels = frame->audio->getNumChannels();
56 const int num_samples = frame->audio->getNumSamples();
57 const int hop_size_value = 1 << ((int)hop_size + 1);
58 const int fft_size_value = 1 << ((int)fft_size + 5);
59
60 stft.setup(num_output_channels);
61 stft.updateParameters((int)fft_size_value,
62 (int)hop_size_value,
63 (int)window_type);
64
65 stft.process(*frame->audio);
66
67 // return the modified frame
68 return frame;
69}
70
71void Robotization::RobotizationEffect::modification(const int channel)
72{
74
75 for (int index = 0; index < fft_size; ++index) {
76 float magnitude = abs(frequency_domain_buffer[index]);
77 frequency_domain_buffer[index].real(magnitude);
78 frequency_domain_buffer[index].imag(0.0f);
79 }
80
82}
83
84// Generate JSON string of this object
85std::string Robotization::Json() const {
86
87 // Return formatted string
88 return JsonValue().toStyledString();
89}
90
91// Generate Json::Value for this object
92Json::Value Robotization::JsonValue() const {
93
94 // Create root json object
95 Json::Value root = EffectBase::JsonValue(); // get parent properties
96 root["type"] = info.class_name;
97 root["fft_size"] = fft_size;
98 root["hop_size"] = hop_size;
99 root["window_type"] = window_type;
100
101 // return JsonValue
102 return root;
103}
104
105// Load JSON string into this object
106void Robotization::SetJson(const std::string value) {
107
108 // Parse JSON string into JSON objects
109 try
110 {
111 const Json::Value root = openshot::stringToJson(value);
112 // Set all values that match
113 SetJsonValue(root);
114 }
115 catch (const std::exception& e)
116 {
117 // Error parsing JSON (or missing keys)
118 throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
119 }
120}
121
122// Load Json::Value into this object
123void Robotization::SetJsonValue(const Json::Value root) {
124
125 // Set parent data
127
128 if (!root["fft_size"].isNull())
129 fft_size = (FFTSize)root["fft_size"].asInt();
130
131 if (!root["hop_size"].isNull())
132 hop_size = (HopSize)root["hop_size"].asInt();
133
134 if (!root["window_type"].isNull())
135 window_type = (WindowType)root["window_type"].asInt();
136}
137
138// Get all properties for a specific frame
139std::string Robotization::PropertiesJSON(int64_t requested_frame) const {
140
141 // Generate JSON properties list
142 Json::Value root = BasePropertiesJSON(requested_frame);
143
144 // Keyframes
145 root["fft_size"] = add_property_json("FFT Size", fft_size, "int", "", NULL, 0, 8, false, requested_frame);
146 root["hop_size"] = add_property_json("Hop Size", hop_size, "int", "", NULL, 0, 2, false, requested_frame);
147 root["window_type"] = add_property_json("Window Type", window_type, "int", "", NULL, 0, 3, false, requested_frame);
148
149 // Add fft_size choices (dropdown style)
150 root["fft_size"]["choices"].append(add_property_choice_json("128", FFT_SIZE_128, fft_size));
151 root["fft_size"]["choices"].append(add_property_choice_json("256", FFT_SIZE_256, fft_size));
152 root["fft_size"]["choices"].append(add_property_choice_json("512", FFT_SIZE_512, fft_size));
153 root["fft_size"]["choices"].append(add_property_choice_json("1024", FFT_SIZE_1024, fft_size));
154 root["fft_size"]["choices"].append(add_property_choice_json("2048", FFT_SIZE_2048, fft_size));
155
156 // Add hop_size choices (dropdown style)
157 root["hop_size"]["choices"].append(add_property_choice_json("1/2", HOP_SIZE_2, hop_size));
158 root["hop_size"]["choices"].append(add_property_choice_json("1/4", HOP_SIZE_4, hop_size));
159 root["hop_size"]["choices"].append(add_property_choice_json("1/8", HOP_SIZE_8, hop_size));
160
161 // Add window_type choices (dropdown style)
162 root["window_type"]["choices"].append(add_property_choice_json("Rectangular", RECTANGULAR, window_type));
163 root["window_type"]["choices"].append(add_property_choice_json("Bart Lett", BART_LETT, window_type));
164 root["window_type"]["choices"].append(add_property_choice_json("Hann", HANN, window_type));
165 root["window_type"]["choices"].append(add_property_choice_json("Hamming", HAMMING, window_type));
166
167 // Return formatted string
168 return root.toStyledString();
169}
Header file for all Exception classes.
Header file for Frame class.
Header file for Robotization audio effect class.
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
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
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Json::Value BasePropertiesJSON(int64_t requested_frame) const
Generate JSON object of base properties (recommended to be used by all effects)
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
EffectInfoStruct info
Information about the current effect.
Definition EffectBase.h:69
Exception for invalid JSON.
Definition Exceptions.h:218
This class adds a robotization effect into the audio.
Robotization()
Default constructor.
std::recursive_mutex mutex
std::string PropertiesJSON(int64_t requested_frame) const override
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
openshot::HopSize hop_size
openshot::FFTSize fft_size
void SetJson(const std::string value) override
Load JSON string into this object.
openshot::WindowType window_type
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...
std::string Json() const override
Generate JSON string of this object.
Json::Value JsonValue() const override
Generate Json::Value for this object.
RobotizationEffect stft
void process(juce::AudioBuffer< float > &block)
Definition STFT.cpp:21
void setup(const int num_input_channels)
Definition STFT.cpp:9
juce::HeapBlock< juce::dsp::Complex< float > > frequency_domain_buffer
Definition STFT.h:63
std::unique_ptr< juce::dsp::FFT > fft
Definition STFT.h:53
void updateParameters(const int new_fft_size, const int new_overlap, const int new_window_type)
Definition STFT.cpp:14
int fft_size
Definition STFT.h:52
juce::HeapBlock< juce::dsp::Complex< float > > time_domain_buffer
Definition STFT.h:62
This namespace is the default namespace for all code in the openshot library.
Definition Compressor.h:29
FFTSize
This enumeration determines the FFT size.
Definition Enums.h:92
@ FFT_SIZE_1024
Definition Enums.h:98
@ FFT_SIZE_512
Definition Enums.h:97
@ FFT_SIZE_256
Definition Enums.h:96
@ FFT_SIZE_2048
Definition Enums.h:99
@ FFT_SIZE_128
Definition Enums.h:95
HopSize
This enumeration determines the hop size.
Definition Enums.h:105
@ HOP_SIZE_2
Definition Enums.h:106
@ HOP_SIZE_4
Definition Enums.h:107
@ HOP_SIZE_8
Definition Enums.h:108
WindowType
This enumeration determines the window type.
Definition Enums.h:112
@ RECTANGULAR
Definition Enums.h:113
@ HANN
Definition Enums.h:115
@ BART_LETT
Definition Enums.h:114
@ HAMMING
Definition Enums.h:116
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