MCPcopy Create free account
hub / github.com/OpenShot/libopenshot / GetFrame

Method GetFrame

src/audio_effects/Compressor.cpp:48–105  ·  view source on GitHub ↗

This method is required for all derived classes of EffectBase, and returns a modified openshot::Frame object

Source from the content-addressed store, hash-verified

46// This method is required for all derived classes of EffectBase, and returns a
47// modified openshot::Frame object
48std::shared_ptr<openshot::Frame> Compressor::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
49{
50 // Adding Compressor
51 const int num_input_channels = frame->audio->getNumChannels();
52 const int num_output_channels = frame->audio->getNumChannels();
53 const int num_samples = frame->audio->getNumSamples();
54
55 mixed_down_input.setSize(1, num_samples);
56 inverse_sample_rate = 1.0f / frame->SampleRate();
57 inverseE = 1.0f / M_E;
58
59 if ((bool)bypass.GetValue(frame_number))
60 return frame;
61
62 mixed_down_input.clear();
63
64 for (int channel = 0; channel < num_input_channels; ++channel)
65 mixed_down_input.addFrom(0, 0, *frame->audio, channel, 0, num_samples, 1.0f / num_input_channels);
66
67 for (int sample = 0; sample < num_samples; ++sample) {
68 float T = threshold.GetValue(frame_number);
69 float R = ratio.GetValue(frame_number);
70 float alphaA = calculateAttackOrRelease(attack.GetValue(frame_number));
71 float alphaR = calculateAttackOrRelease(release.GetValue(frame_number));
72 float gain = makeup_gain.GetValue(frame_number);
73 float input_squared = powf(mixed_down_input.getSample(0, sample), 2.0f);
74
75 input_level = input_squared;
76
77 xg = (input_level <= 1e-6f) ? -60.0f : 10.0f * log10f(input_level);
78
79 if (xg < T)
80 yg = xg;
81 else
82 yg = T + (xg - T) / R;
83
84 xl = xg - yg;
85
86 if (xl > yl_prev)
87 yl = alphaA * yl_prev + (1.0f - alphaA) * xl;
88 else
89 yl = alphaR * yl_prev + (1.0f - alphaR) * xl;
90
91 control = powf (10.0f, (gain - yl) * 0.05f);
92 yl_prev = yl;
93
94 for (int channel = 0; channel < num_input_channels; ++channel) {
95 float new_value = frame->audio->getSample(channel, sample)*control;
96 frame->audio->setSample(channel, sample, new_value);
97 }
98 }
99
100 for (int channel = num_input_channels; channel < num_output_channels; ++channel)
101 frame->audio->clear(channel, 0, num_samples);
102
103 // return the modified frame
104 return frame;
105}

Callers

nothing calls this directly

Calls 3

GetValueMethod · 0.80
SampleRateMethod · 0.45
clearMethod · 0.45

Tested by

no test coverage detected