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
Whisperization()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
Whisperization(openshot::FFTSize new_fft_size,openshot::HopSize new_hop_size,openshot::WindowType new_window_type)43 Whisperization::Whisperization(openshot::FFTSize new_fft_size, openshot::HopSize new_hop_size, openshot::WindowType new_window_type) :
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
init_effect_details()51 void Whisperization::init_effect_details()
52 {
53 	/// Initialize the values of the EffectInfo struct.
54 	InitEffectInfo();
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
GetFrame(std::shared_ptr<openshot::Frame> frame,int64_t frame_number)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 
modification(const int channel)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
Json() const110 std::string Whisperization::Json() const {
111 
112 	// Return formatted string
113 	return JsonValue().toStyledString();
114 }
115 
116 // Generate Json::Value for this object
JsonValue() const117 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
SetJson(const std::string value)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
SetJsonValue(const Json::Value root)148 void Whisperization::SetJsonValue(const Json::Value root) {
149 
150 	// Set parent data
151 	EffectBase::SetJsonValue(root);
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
PropertiesJSON(int64_t requested_frame) const164 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 }
201