OpenShot Library | libopenshot 0.2.7
Robotization.cpp
Go to the documentation of this file.
1/**
2 * @file
3 * @brief Source file for Robotization 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 "Robotization.h"
32#include "Exceptions.h"
33
34using namespace openshot;
35
36
37/// Blank constructor, useful when using Json to load the effect properties
38Robotization::Robotization() : fft_size(FFT_SIZE_512), hop_size(HOP_SIZE_2), window_type(RECTANGULAR), stft(*this) {
39 // Init effect properties
40 init_effect_details();
41}
42
43// Default constructor
45 fft_size(new_fft_size), hop_size(new_hop_size), window_type(new_window_type), stft(*this)
46{
47 // Init effect properties
48 init_effect_details();
49}
50
51// Init effect settings
52void Robotization::init_effect_details()
53{
54 /// Initialize the values of the EffectInfo struct.
56
57 /// Set the effect info
58 info.class_name = "Robotization";
59 info.name = "Robotization";
60 info.description = "Transform the voice present in an audio track into a robotic voice effect.";
61 info.has_audio = true;
62 info.has_video = false;
63}
64
65// This method is required for all derived classes of EffectBase, and returns a
66// modified openshot::Frame object
67std::shared_ptr<openshot::Frame> Robotization::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
68{
69 const ScopedLock sl (lock);
70 ScopedNoDenormals noDenormals;
71
72 const int num_input_channels = frame->audio->getNumChannels();
73 const int num_output_channels = frame->audio->getNumChannels();
74 const int num_samples = frame->audio->getNumSamples();
75 const int hop_size_value = 1 << ((int)hop_size + 1);
76 const int fft_size_value = 1 << ((int)fft_size + 5);
77
78 stft.setup(num_output_channels);
79 stft.updateParameters((int)fft_size_value,
80 (int)hop_size_value,
81 (int)window_type);
82
83 stft.process(*frame->audio);
84
85 // return the modified frame
86 return frame;
87}
88
89void Robotization::RobotizationEffect::modification(const int channel)
90{
92
93 for (int index = 0; index < fft_size; ++index) {
94 float magnitude = abs(frequency_domain_buffer[index]);
95 frequency_domain_buffer[index].real(magnitude);
96 frequency_domain_buffer[index].imag(0.0f);
97 }
98
100}
101
102// Generate JSON string of this object
103std::string Robotization::Json() const {
104
105 // Return formatted string
106 return JsonValue().toStyledString();
107}
108
109// Generate Json::Value for this object
110Json::Value Robotization::JsonValue() const {
111
112 // Create root json object
113 Json::Value root = EffectBase::JsonValue(); // get parent properties
114 root["type"] = info.class_name;
115 root["fft_size"] = fft_size;
116 root["hop_size"] = hop_size;
117 root["window_type"] = window_type;
118
119 // return JsonValue
120 return root;
121}
122
123// Load JSON string into this object
124void Robotization::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 Robotization::SetJsonValue(const Json::Value root) {
142
143 // Set parent data
145
146 if (!root["fft_size"].isNull())
147 fft_size = (FFTSize)root["fft_size"].asInt();
148
149 if (!root["hop_size"].isNull())
150 hop_size = (HopSize)root["hop_size"].asInt();
151
152 if (!root["window_type"].isNull())
153 window_type = (WindowType)root["window_type"].asInt();
154}
155
156// Get all properties for a specific frame
157std::string Robotization::PropertiesJSON(int64_t requested_frame) const {
158
159 // Generate JSON properties list
160 Json::Value root;
161 root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
162 root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
163 root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
164 root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
165 root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 1000 * 60 * 30, true, requested_frame);
166
167 // Keyframes
168 root["fft_size"] = add_property_json("FFT Size", fft_size, "int", "", NULL, 0, 8, false, requested_frame);
169 root["hop_size"] = add_property_json("Hop Size", hop_size, "int", "", NULL, 0, 2, false, requested_frame);
170 root["window_type"] = add_property_json("Window Type", window_type, "int", "", NULL, 0, 3, false, requested_frame);
171
172 // Add fft_size choices (dropdown style)
173 root["fft_size"]["choices"].append(add_property_choice_json("128", FFT_SIZE_128, fft_size));
174 root["fft_size"]["choices"].append(add_property_choice_json("256", FFT_SIZE_256, fft_size));
175 root["fft_size"]["choices"].append(add_property_choice_json("512", FFT_SIZE_512, fft_size));
176 root["fft_size"]["choices"].append(add_property_choice_json("1024", FFT_SIZE_1024, fft_size));
177 root["fft_size"]["choices"].append(add_property_choice_json("2048", FFT_SIZE_2048, fft_size));
178
179 // Add hop_size choices (dropdown style)
180 root["hop_size"]["choices"].append(add_property_choice_json("1/2", HOP_SIZE_2, hop_size));
181 root["hop_size"]["choices"].append(add_property_choice_json("1/4", HOP_SIZE_4, hop_size));
182 root["hop_size"]["choices"].append(add_property_choice_json("1/8", HOP_SIZE_8, hop_size));
183
184 // Add window_type choices (dropdown style)
185 root["window_type"]["choices"].append(add_property_choice_json("Rectangular", RECTANGULAR, window_type));
186 root["window_type"]["choices"].append(add_property_choice_json("Bart Lett", BART_LETT, window_type));
187 root["window_type"]["choices"].append(add_property_choice_json("Hann", HANN, window_type));
188 root["window_type"]["choices"].append(add_property_choice_json("Hamming", HAMMING, window_type));
189
190 // Return formatted string
191 return root.toStyledString();
192}
Header file for all Exception classes.
Header file for Robotization audio effect class.
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
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:104
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
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
Robotization()
Blank constructor, useful when using Json to load the effect properties.
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
Definition: Robotization.h:64
openshot::FFTSize fft_size
Definition: Robotization.h:63
void SetJson(const std::string value) override
Load JSON string into this object.
juce::CriticalSection lock
Definition: Robotization.h:118
openshot::WindowType window_type
Definition: Robotization.h:65
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:81
std::string Json() const override
Generate JSON string of this object.
Json::Value JsonValue() const override
Generate Json::Value for this object.
RobotizationEffect stft
Definition: Robotization.h:119
void setup(const int num_input_channels)
Definition: STFT.cpp:5
void process(juce::AudioSampleBuffer &block)
Definition: STFT.cpp:17
juce::HeapBlock< juce::dsp::Complex< float > > frequency_domain_buffer
Definition: STFT.h:55
std::unique_ptr< juce::dsp::FFT > fft
Definition: STFT.h:45
void updateParameters(const int new_fft_size, const int new_overlap, const int new_window_type)
Definition: STFT.cpp:10
int fft_size
Definition: STFT.h:44
juce::HeapBlock< juce::dsp::Complex< float > > time_domain_buffer
Definition: STFT.h:54
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:47
FFTSize
This enumeration determines the FFT size.
Definition: Enums.h:109
@ FFT_SIZE_1024
Definition: Enums.h:115
@ FFT_SIZE_512
Definition: Enums.h:114
@ FFT_SIZE_256
Definition: Enums.h:113
@ FFT_SIZE_2048
Definition: Enums.h:116
@ FFT_SIZE_128
Definition: Enums.h:112
HopSize
This enumeration determines the hop size.
Definition: Enums.h:122
@ HOP_SIZE_2
Definition: Enums.h:123
@ HOP_SIZE_4
Definition: Enums.h:124
@ HOP_SIZE_8
Definition: Enums.h:125
WindowType
This enumeration determines the window type.
Definition: Enums.h:129
@ RECTANGULAR
Definition: Enums.h:130
@ HANN
Definition: Enums.h:132
@ BART_LETT
Definition: Enums.h:131
@ HAMMING
Definition: Enums.h:133
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