OpenShot Library | libopenshot  0.3.0
Whisperization.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 "Whisperization.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 Whisperization::init_effect_details()
35 {
38 
40  info.class_name = "Whisperization";
41  info.name = "Whisperization";
42  info.description = "Transform the voice present in an audio track into a whispering 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> Whisperization::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 Whisperization::WhisperizationEffect::modification(const int channel)
72 {
74 
75  for (int index = 0; index < fft_size / 2 + 1; ++index) {
76  float magnitude = abs(frequency_domain_buffer[index]);
77  float phase = 2.0f * M_PI * (float)rand() / (float)RAND_MAX;
78 
79  frequency_domain_buffer[index].real(magnitude * cosf(phase));
80  frequency_domain_buffer[index].imag(magnitude * sinf(phase));
81 
82  if (index > 0 && index < fft_size / 2) {
83  frequency_domain_buffer[fft_size - index].real(magnitude * cosf (phase));
84  frequency_domain_buffer[fft_size - index].imag(magnitude * sinf (-phase));
85  }
86  }
87 
89 }
90 
91 
92 // Generate JSON string of this object
93 std::string Whisperization::Json() const {
94 
95  // Return formatted string
96  return JsonValue().toStyledString();
97 }
98 
99 // Generate Json::Value for this object
100 Json::Value Whisperization::JsonValue() const {
101 
102  // Create root json object
103  Json::Value root = EffectBase::JsonValue(); // get parent properties
104  root["type"] = info.class_name;
105  root["fft_size"] = fft_size;
106  root["hop_size"] = hop_size;
107  root["window_type"] = window_type;
108 
109  // return JsonValue
110  return root;
111 }
112 
113 // Load JSON string into this object
114 void Whisperization::SetJson(const std::string value) {
115 
116  // Parse JSON string into JSON objects
117  try
118  {
119  const Json::Value root = openshot::stringToJson(value);
120  // Set all values that match
121  SetJsonValue(root);
122  }
123  catch (const std::exception& e)
124  {
125  // Error parsing JSON (or missing keys)
126  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
127  }
128 }
129 
130 // Load Json::Value into this object
131 void Whisperization::SetJsonValue(const Json::Value root) {
132 
133  // Set parent data
135 
136  if (!root["fft_size"].isNull())
137  fft_size = (FFTSize)root["fft_size"].asInt();
138 
139  if (!root["hop_size"].isNull())
140  hop_size = (HopSize)root["hop_size"].asInt();
141 
142  if (!root["window_type"].isNull())
143  window_type = (WindowType)root["window_type"].asInt();
144 }
145 
146 // Get all properties for a specific frame
147 std::string Whisperization::PropertiesJSON(int64_t requested_frame) const {
148 
149  // Generate JSON properties list
150  Json::Value root;
151  root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
152  root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
153  root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
154  root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
155  root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 1000 * 60 * 30, true, requested_frame);
156 
157  // Keyframes
158  root["fft_size"] = add_property_json("FFT Size", fft_size, "int", "", NULL, 0, 8, false, requested_frame);
159  root["hop_size"] = add_property_json("Hop Size", hop_size, "int", "", NULL, 0, 2, false, requested_frame);
160  root["window_type"] = add_property_json("Window Type", window_type, "int", "", NULL, 0, 3, false, requested_frame);
161 
162  // Add fft_size choices (dropdown style)
163  root["fft_size"]["choices"].append(add_property_choice_json("128", FFT_SIZE_128, fft_size));
164  root["fft_size"]["choices"].append(add_property_choice_json("256", FFT_SIZE_256, fft_size));
165  root["fft_size"]["choices"].append(add_property_choice_json("512", FFT_SIZE_512, fft_size));
166  root["fft_size"]["choices"].append(add_property_choice_json("1024", FFT_SIZE_1024, fft_size));
167  root["fft_size"]["choices"].append(add_property_choice_json("2048", FFT_SIZE_2048, fft_size));
168 
169  // Add hop_size choices (dropdown style)
170  root["hop_size"]["choices"].append(add_property_choice_json("1/2", HOP_SIZE_2, hop_size));
171  root["hop_size"]["choices"].append(add_property_choice_json("1/4", HOP_SIZE_4, hop_size));
172  root["hop_size"]["choices"].append(add_property_choice_json("1/8", HOP_SIZE_8, hop_size));
173 
174  // Add window_type choices (dropdown style)
175  root["window_type"]["choices"].append(add_property_choice_json("Rectangular", RECTANGULAR, window_type));
176  root["window_type"]["choices"].append(add_property_choice_json("Bart Lett", BART_LETT, window_type));
177  root["window_type"]["choices"].append(add_property_choice_json("Hann", HANN, window_type));
178  root["window_type"]["choices"].append(add_property_choice_json("Hamming", HAMMING, window_type));
179 
180 
181  // Return formatted string
182  return root.toStyledString();
183 }
Header file for all Exception classes.
Header file for Frame class.
Header file for whisperization audio effect class.
float Start() const
Get start position (in seconds) of clip (trim start of video)
Definition: ClipBase.h:88
float Duration() const
Get the length of this clip (in seconds)
Definition: ClipBase.h:90
virtual float End() const
Get end position (in seconds) of clip (trim end of video)
Definition: ClipBase.h:89
std::string Id() const
Get the Id of this clip object.
Definition: ClipBase.h:85
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
int Layer() const
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition: ClipBase.h:87
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.
Definition: EffectBase.cpp:77
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: EffectBase.cpp:112
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:69
Exception for invalid JSON.
Definition: Exceptions.h:218
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 class adds a whisperization effect into the audio.
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Whisperization()
Default constructor.
std::string Json() const override
Generate JSON string of this object.
openshot::WindowType window_type
WhisperizationEffect stft
std::recursive_mutex mutex
Json::Value JsonValue() const override
Generate Json::Value for this object.
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 PropertiesJSON(int64_t requested_frame) const override
void SetJson(const std::string value) override
Load JSON string into this object.
openshot::FFTSize fft_size
openshot::HopSize hop_size
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