OpenShot Library | libopenshot  0.3.2
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 
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
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 {
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
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 = 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 }
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::stringToJson
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:16
openshot::FFT_SIZE_1024
@ FFT_SIZE_1024
Definition: Enums.h:98
openshot::HOP_SIZE_2
@ HOP_SIZE_2
Definition: Enums.h:106
openshot::EffectBase::info
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:69
openshot::Robotization::fft_size
openshot::FFTSize fft_size
Definition: Robotization.h:49
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
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::HOP_SIZE_8
@ HOP_SIZE_8
Definition: Enums.h:108
openshot::EffectBase::JsonValue
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: EffectBase.cpp:79
openshot::RECTANGULAR
@ RECTANGULAR
Definition: Enums.h:113
openshot::Robotization::window_type
openshot::WindowType window_type
Definition: Robotization.h:51
openshot::Robotization::mutex
std::recursive_mutex mutex
Definition: Robotization.h:88
openshot::STFT::fft
std::unique_ptr< juce::dsp::FFT > fft
Definition: STFT.h:53
openshot::Robotization::SetJsonValue
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: Robotization.cpp:123
openshot::BART_LETT
@ BART_LETT
Definition: Enums.h:114
openshot::Robotization::PropertiesJSON
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: Robotization.cpp:139
openshot::Robotization::hop_size
openshot::HopSize hop_size
Definition: Robotization.h:50
openshot::STFT::frequency_domain_buffer
juce::HeapBlock< juce::dsp::Complex< float > > frequency_domain_buffer
Definition: STFT.h:63
openshot::FFT_SIZE_256
@ FFT_SIZE_256
Definition: Enums.h:96
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::Robotization::Json
std::string Json() const override
Generate JSON string of this object.
Definition: Robotization.cpp:85
openshot::InvalidJSON
Exception for invalid JSON.
Definition: Exceptions.h:217
Robotization.h
Header file for Robotization audio effect class.
openshot::EffectBase::InitEffectInfo
void InitEffectInfo()
Definition: EffectBase.cpp:24
openshot::EffectInfoStruct::has_audio
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:41
juce
Definition: Robotization.h:29
openshot::FFTSize
FFTSize
This enumeration determines the FFT size.
Definition: Enums.h:91
openshot::STFT::process
void process(juce::AudioBuffer< float > &block)
Definition: STFT.cpp:21
openshot::HAMMING
@ HAMMING
Definition: Enums.h:116
openshot::STFT::fft_size
int fft_size
Definition: STFT.h:52
Frame.h
Header file for Frame class.
openshot::HOP_SIZE_4
@ HOP_SIZE_4
Definition: Enums.h:107
openshot::FFT_SIZE_128
@ FFT_SIZE_128
Definition: Enums.h:95
openshot::EffectInfoStruct::class_name
std::string class_name
The class name of the effect.
Definition: EffectBase.h:36
openshot::EffectInfoStruct::description
std::string description
The description of this effect and what it does.
Definition: EffectBase.h:38
openshot::Robotization::SetJson
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: Robotization.cpp:106
openshot::EffectInfoStruct::has_video
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:40
openshot::Robotization
This class adds a robotization effect into the audio.
Definition: Robotization.h:42
openshot::Robotization::JsonValue
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Robotization.cpp:92
openshot::STFT::updateParameters
void updateParameters(const int new_fft_size, const int new_overlap, const int new_window_type)
Definition: STFT.cpp:14
openshot::STFT::time_domain_buffer
juce::HeapBlock< juce::dsp::Complex< float > > time_domain_buffer
Definition: STFT.h:62
openshot::FFT_SIZE_512
@ FFT_SIZE_512
Definition: Enums.h:97
openshot::HopSize
HopSize
This enumeration determines the hop size.
Definition: Enums.h:105
openshot::EffectInfoStruct::name
std::string name
The name of the effect.
Definition: EffectBase.h:37
openshot::Robotization::stft
RobotizationEffect stft
Definition: Robotization.h:89
openshot::HANN
@ HANN
Definition: Enums.h:115
openshot::Robotization::Robotization
Robotization()
Default constructor.
Definition: Robotization.cpp:20
openshot::STFT::setup
void setup(const int num_input_channels)
Definition: STFT.cpp:9
openshot::WindowType
WindowType
This enumeration determines the window type.
Definition: Enums.h:112
Exceptions.h
Header file for all Exception classes.
openshot::FFT_SIZE_2048
@ FFT_SIZE_2048
Definition: Enums.h:99
openshot::EffectBase::SetJsonValue
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: EffectBase.cpp:115
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