OpenShot Library | libopenshot  0.3.3
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 = BasePropertiesJSON(requested_frame);
151 
152  // Keyframes
153  root["fft_size"] = add_property_json("FFT Size", fft_size, "int", "", NULL, 0, 8, false, requested_frame);
154  root["hop_size"] = add_property_json("Hop Size", hop_size, "int", "", NULL, 0, 2, false, requested_frame);
155  root["window_type"] = add_property_json("Window Type", window_type, "int", "", NULL, 0, 3, false, requested_frame);
156 
157  // Add fft_size choices (dropdown style)
158  root["fft_size"]["choices"].append(add_property_choice_json("128", FFT_SIZE_128, fft_size));
159  root["fft_size"]["choices"].append(add_property_choice_json("256", FFT_SIZE_256, fft_size));
160  root["fft_size"]["choices"].append(add_property_choice_json("512", FFT_SIZE_512, fft_size));
161  root["fft_size"]["choices"].append(add_property_choice_json("1024", FFT_SIZE_1024, fft_size));
162  root["fft_size"]["choices"].append(add_property_choice_json("2048", FFT_SIZE_2048, fft_size));
163 
164  // Add hop_size choices (dropdown style)
165  root["hop_size"]["choices"].append(add_property_choice_json("1/2", HOP_SIZE_2, hop_size));
166  root["hop_size"]["choices"].append(add_property_choice_json("1/4", HOP_SIZE_4, hop_size));
167  root["hop_size"]["choices"].append(add_property_choice_json("1/8", HOP_SIZE_8, hop_size));
168 
169  // Add window_type choices (dropdown style)
170  root["window_type"]["choices"].append(add_property_choice_json("Rectangular", RECTANGULAR, window_type));
171  root["window_type"]["choices"].append(add_property_choice_json("Bart Lett", BART_LETT, window_type));
172  root["window_type"]["choices"].append(add_property_choice_json("Hann", HANN, window_type));
173  root["window_type"]["choices"].append(add_property_choice_json("Hamming", HAMMING, window_type));
174 
175 
176  // Return formatted string
177  return root.toStyledString();
178 }
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::Whisperization::PropertiesJSON
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: Whisperization.cpp:147
openshot::Whisperization::JsonValue
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Whisperization.cpp:100
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
openshot::Whisperization::SetJsonValue
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: Whisperization.cpp:131
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::Whisperization::SetJson
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: Whisperization.cpp:114
openshot::STFT::fft
std::unique_ptr< juce::dsp::FFT > fft
Definition: STFT.h:53
openshot::BART_LETT
@ BART_LETT
Definition: Enums.h:114
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::Whisperization::mutex
std::recursive_mutex mutex
Definition: Whisperization.h:87
openshot::Whisperization
This class adds a whisperization effect into the audio.
Definition: Whisperization.h:41
openshot::InvalidJSON
Exception for invalid JSON.
Definition: Exceptions.h:217
openshot::Whisperization::Json
std::string Json() const override
Generate JSON string of this object.
Definition: Whisperization.cpp:93
openshot::EffectBase::InitEffectInfo
void InitEffectInfo()
Definition: EffectBase.cpp:24
openshot::Whisperization::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: Whisperization.h:60
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::Whisperization::window_type
openshot::WindowType window_type
Definition: Whisperization.h:50
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::Whisperization::Whisperization
Whisperization()
Default constructor.
Definition: Whisperization.cpp:20
openshot::EffectInfoStruct::has_video
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:40
openshot::Whisperization::hop_size
openshot::HopSize hop_size
Definition: Whisperization.h:49
openshot::Whisperization::fft_size
openshot::FFTSize fft_size
Definition: Whisperization.h:48
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
Whisperization.h
Header file for whisperization audio effect class.
openshot::EffectInfoStruct::name
std::string name
The name of the effect.
Definition: EffectBase.h:37
openshot::Whisperization::stft
WhisperizationEffect stft
Definition: Whisperization.h:88
openshot::HANN
@ HANN
Definition: Enums.h:115
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