OpenShot Library | libopenshot  0.3.1
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 
17 using namespace openshot;
18 using namespace juce;
19 
22 
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
34 void 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
49 std::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 
71 void Robotization::RobotizationEffect::modification(const int channel)
72 {
73  fft->perform(time_domain_buffer, frequency_domain_buffer, false);
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 
81  fft->perform(frequency_domain_buffer, time_domain_buffer, true);
82 }
83 
84 // Generate JSON string of this object
85 std::string Robotization::Json() const {
86 
87  // Return formatted string
88  return JsonValue().toStyledString();
89 }
90 
91 // Generate Json::Value for this object
92 Json::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
106 void 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
123 void 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
139 std::string Robotization::PropertiesJSON(int64_t requested_frame) const {
140 
141  // Generate JSON properties list
142  Json::Value root;
143  root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
144  root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
145  root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
146  root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
147  root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 1000 * 60 * 30, true, requested_frame);
148 
149  // Keyframes
150  root["fft_size"] = add_property_json("FFT Size", fft_size, "int", "", NULL, 0, 8, false, requested_frame);
151  root["hop_size"] = add_property_json("Hop Size", hop_size, "int", "", NULL, 0, 2, false, requested_frame);
152  root["window_type"] = add_property_json("Window Type", window_type, "int", "", NULL, 0, 3, false, requested_frame);
153 
154  // Add fft_size choices (dropdown style)
155  root["fft_size"]["choices"].append(add_property_choice_json("128", FFT_SIZE_128, fft_size));
156  root["fft_size"]["choices"].append(add_property_choice_json("256", FFT_SIZE_256, fft_size));
157  root["fft_size"]["choices"].append(add_property_choice_json("512", FFT_SIZE_512, fft_size));
158  root["fft_size"]["choices"].append(add_property_choice_json("1024", FFT_SIZE_1024, fft_size));
159  root["fft_size"]["choices"].append(add_property_choice_json("2048", FFT_SIZE_2048, fft_size));
160 
161  // Add hop_size choices (dropdown style)
162  root["hop_size"]["choices"].append(add_property_choice_json("1/2", HOP_SIZE_2, hop_size));
163  root["hop_size"]["choices"].append(add_property_choice_json("1/4", HOP_SIZE_4, hop_size));
164  root["hop_size"]["choices"].append(add_property_choice_json("1/8", HOP_SIZE_8, hop_size));
165 
166  // Add window_type choices (dropdown style)
167  root["window_type"]["choices"].append(add_property_choice_json("Rectangular", RECTANGULAR, window_type));
168  root["window_type"]["choices"].append(add_property_choice_json("Bart Lett", BART_LETT, window_type));
169  root["window_type"]["choices"].append(add_property_choice_json("Hann", HANN, window_type));
170  root["window_type"]["choices"].append(add_property_choice_json("Hamming", HAMMING, window_type));
171 
172  // Return formatted string
173  return root.toStyledString();
174 }
Json::Value JsonValue() const override
Generate Json::Value for this object.
std::string Id() const
Get the Id of this clip object.
Definition: ClipBase.h:85
void updateParameters(const int new_fft_size, const int new_overlap, const int new_window_type)
Definition: STFT.cpp:14
float Start() const
Get start position (in seconds) of clip (trim start of video)
Definition: ClipBase.h:88
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: EffectBase.cpp:112
void SetJson(const std::string value) override
Load JSON string into this object.
virtual float End() const
Get end position (in seconds) of clip (trim end of video)
Definition: ClipBase.h:89
FFTSize
This enumeration determines the FFT size.
Definition: Enums.h:91
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:16
std::string Json() const override
Generate JSON string of this object.
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: EffectBase.cpp:77
void setup(const int num_input_channels)
Definition: STFT.cpp:9
openshot::WindowType window_type
Definition: Robotization.h:51
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:41
Header file for all Exception classes.
openshot::HopSize hop_size
Definition: Robotization.h:50
Header file for Frame class.
std::recursive_mutex mutex
Definition: Robotization.h:88
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
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
RobotizationEffect stft
Definition: Robotization.h:89
float Duration() const
Get the length of this clip (in seconds)
Definition: ClipBase.h:90
HopSize
This enumeration determines the hop size.
Definition: Enums.h:105
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
std::string description
The description of this effect and what it does.
Definition: EffectBase.h:38
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:40
Exception for invalid JSON.
Definition: Exceptions.h:217
WindowType
This enumeration determines the window type.
Definition: Enums.h:112
Robotization()
Default constructor.
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::FFTSize fft_size
Definition: Robotization.h:49
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 robotization effect into the audio.
Definition: Robotization.h:42
std::string PropertiesJSON(int64_t requested_frame) const override
int Layer() const
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition: ClipBase.h:87
std::unique_ptr< juce::dsp::FFT > fft
Definition: Robotization.h:90
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:69
Header file for Robotization audio effect class.
void process(juce::AudioBuffer< float > &block)
Definition: STFT.cpp:21