-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathURCompressor.cpp
More file actions
83 lines (46 loc) · 1.47 KB
/
URCompressor.cpp
File metadata and controls
83 lines (46 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
==============================================================================
URCompressor.cpp
Created: 25 Jun 2021 7:53:02pm
Author: asla_
==============================================================================
*/
#include "URCompressor.h"
Compressor::Compressor(){
m_Threshold = 0.f;
m_Ratio = 1.f;
}
void Compressor::processAudioSample(float& sample){
float detectionSignal = sample;
detectionSignal = fabs(detectionSignal);
m_EnvelopeShaper.processAudioSample(detectionSignal);
detectionSignal = AmplitudeToDecibel(detectionSignal);
if(detectionSignal > m_Threshold){
float scale = 1.f-(1.f/m_Ratio);
float gain = scale * (m_Threshold - detectionSignal);
gain = decibelToAmplitude(gain);
sample *= gain;
}
}
float Compressor::decibelToAmplitude(float db) {
return pow(10.f, db / 20.f);
}
float Compressor::AmplitudeToDecibel(float amplitude) {
amplitude = std::max(amplitude,BOUND_LIN);
return 20.f*log10(amplitude);
}
void Compressor::setTreshold(float thresh) {
m_Threshold = thresh;
}
void Compressor::setRatio(float ratio){
m_Ratio = ratio;
}
void Compressor::setAttack(float attack){
m_EnvelopeShaper.setAttack(attack);
}
void Compressor::setRelease(float release){
m_EnvelopeShaper.setRelease(release);
}
void Compressor::setSampleRate(float sampleRate) {
m_EnvelopeShaper.setSampleRate(sampleRate);
}