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