OpenShot Library | libopenshot  0.3.2
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 
20 using namespace openshot;
21 
23 
24 Noise::Noise(Keyframe level) : level(level)
25 {
26  // Init effect properties
27  init_effect_details();
28 }
29 
30 // Init effect settings
31 void 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
46 std::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
68 std::string Noise::Json() const {
69 
70  // Return formatted string
71  return JsonValue().toStyledString();
72 }
73 
74 // Generate Json::Value for this object
75 Json::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
87 void 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
104 void 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
115 std::string Noise::PropertiesJSON(int64_t requested_frame) const {
116 
117  // Generate JSON properties list
118  Json::Value root = BasePropertiesJSON(requested_frame);
119 
120  // Keyframes
121  root["level"] = add_property_json("Level", level.GetValue(requested_frame), "int", "", &level, 0, 100, false, requested_frame);
122 
123  // Return formatted string
124  return root.toStyledString();
125 }
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::Noise::PropertiesJSON
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: Noise.cpp:115
openshot::EffectBase::info
EffectInfoStruct info
Information about the current effect.
Definition: EffectBase.h:69
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
openshot::Noise::level
Keyframe level
Noise level keyframe. The amount of noise inserted on the audio.
Definition: Noise.h:42
openshot::EffectBase::JsonValue
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: EffectBase.cpp:79
openshot::Keyframe::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: KeyFrame.cpp:372
openshot::Noise::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: Noise.h:52
openshot::Noise::SetJsonValue
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: Noise.cpp:104
openshot::Noise::Noise
Noise()
Default constructor.
Definition: Noise.cpp:22
openshot::Keyframe::JsonValue
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: KeyFrame.cpp:339
openshot::Noise::JsonValue
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Noise.cpp:75
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::Keyframe
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
Definition: KeyFrame.h:53
openshot::InvalidJSON
Exception for invalid JSON.
Definition: Exceptions.h:217
openshot::EffectBase::InitEffectInfo
void InitEffectInfo()
Definition: EffectBase.cpp:24
openshot::EffectInfoStruct::has_audio
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition: EffectBase.h:41
Frame.h
Header file for Frame class.
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::EffectInfoStruct::has_video
bool has_video
Determines if this effect manipulates the image of a frame.
Definition: EffectBase.h:40
openshot::EffectInfoStruct::name
std::string name
The name of the effect.
Definition: EffectBase.h:37
openshot::Noise::Json
std::string Json() const override
Generate JSON string of this object.
Definition: Noise.cpp:68
Noise.h
Header file for Noise audio effect class.
openshot::Noise::SetJson
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: Noise.cpp:87
openshot::Noise
This class adds a noise into the audio.
Definition: Noise.h:35
Exceptions.h
Header file for all Exception classes.
openshot::EffectBase::SetJsonValue
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: EffectBase.cpp:115
openshot::Keyframe::GetValue
double GetValue(int64_t index) const
Get the value at a specific index.
Definition: KeyFrame.cpp:258