OpenShot Library | libopenshot 0.3.1
Noise.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 "Noise.h"
14#include "Exceptions.h"
15#include "Frame.h"
16
17#include <AppConfig.h>
18#include <juce_audio_basics/juce_audio_basics.h>
19
20using namespace openshot;
21
22Noise::Noise(): Noise::Noise(30) { }
23
24Noise::Noise(Keyframe level) : level(level)
25{
26 // Init effect properties
27 init_effect_details();
28}
29
30// Init effect settings
31void Noise::init_effect_details()
32{
35
37 info.class_name = "Noise";
38 info.name = "Noise";
39 info.description = "Random signal having equal intensity at different frequencies.";
40 info.has_audio = true;
41 info.has_video = false;
42}
43
44// This method is required for all derived classes of EffectBase, and returns a
45// modified openshot::Frame object
46std::shared_ptr<openshot::Frame> Noise::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
47{
48 // Adding Noise
49 srand ( time(NULL) );
50 int noise = level.GetValue(frame_number);
51
52 for (int channel = 0; channel < frame->audio->getNumChannels(); channel++)
53 {
54 auto *buffer = frame->audio->getWritePointer(channel);
55
56 for (auto sample = 0; sample < frame->audio->getNumSamples(); ++sample)
57 {
58 buffer[sample] = buffer[sample]*(1 - (1+(float)noise)/100) + buffer[sample]*0.0001*(rand()%100+1)*noise;
59 }
60 }
61
62
63 // return the modified frame
64 return frame;
65}
66
67// Generate JSON string of this object
68std::string Noise::Json() const {
69
70 // Return formatted string
71 return JsonValue().toStyledString();
72}
73
74// Generate Json::Value for this object
75Json::Value Noise::JsonValue() const {
76
77 // Create root json object
78 Json::Value root = EffectBase::JsonValue(); // get parent properties
79 root["type"] = info.class_name;
80 root["level"] = level.JsonValue();
81
82 // return JsonValue
83 return root;
84}
85
86// Load JSON string into this object
87void Noise::SetJson(const std::string value) {
88
89 // Parse JSON string into JSON objects
90 try
91 {
92 const Json::Value root = openshot::stringToJson(value);
93 // Set all values that match
94 SetJsonValue(root);
95 }
96 catch (const std::exception& e)
97 {
98 // Error parsing JSON (or missing keys)
99 throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
100 }
101}
102
103// Load Json::Value into this object
104void Noise::SetJsonValue(const Json::Value root) {
105
106 // Set parent data
108
109 // Set data from Json (if key is found)
110 if (!root["level"].isNull())
111 level.SetJsonValue(root["level"]);
112}
113
114// Get all properties for a specific frame
115std::string Noise::PropertiesJSON(int64_t requested_frame) const {
116
117 // Generate JSON properties list
118 Json::Value root;
119 root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
120 root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
121 root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
122 root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
123 root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 1000 * 60 * 30, true, requested_frame);
124
125 // Keyframes
126 root["level"] = add_property_json("Level", level.GetValue(requested_frame), "int", "", &level, 0, 100, false, requested_frame);
127
128 // Return formatted string
129 return root.toStyledString();
130}
Header file for all Exception classes.
Header file for Frame class.
Header file for Noise 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
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
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
Definition: KeyFrame.h:53
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: KeyFrame.cpp:372
double GetValue(int64_t index) const
Get the value at a specific index.
Definition: KeyFrame.cpp:258
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: KeyFrame.cpp:339
This class adds a noise into the audio.
Definition: Noise.h:36
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: Noise.h:52
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: Noise.cpp:87
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Noise.cpp:75
std::string Json() const override
Generate JSON string of this object.
Definition: Noise.cpp:68
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: Noise.cpp:115
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: Noise.cpp:104
Noise()
Default constructor.
Definition: Noise.cpp:22
Keyframe level
Noise level keyframe. The amount of noise inserted on the audio.
Definition: Noise.h:42
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:29
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