1 /**
2 * Copyright 2017 Cut Through Recordings
3 * License: MIT License
4 * Author(s): Ethan Reker
5 */6 moduleddsp.effect.dynamics;
7 8 importddsp.effect.effect;
9 importddsp.util.envelope;
10 importddsp.util.functions;
11 12 importdplug.core.nogc;
13 14 importstd.algorithm;
15 importstd.math;
16 17 /// Base class for dynamics processors such as compressor, limiter, expander, and gate.18 /// This class is useless on it's own. It should be inherited from and have getNextSample overriden.19 classDynamicsProcessor : AudioEffect20 {
21 public:
22 nothrow:
23 @nogc:
24 25 /// Tracks the input level to trigger compression.26 EnvelopeDetectordetector;
27 28 /// Will point to the detector of a processor that is stereo linked29 EnvelopeDetector *linkedDetector;
30 31 this()
32 {
33 x = mallocSlice!float(2);
34 y = mallocSlice!float(2);
35 detector = mallocNew!EnvelopeDetector;
36 }
37 38 voidsetParams(floatattackTime, floatreleaseTime, floatthreshold, floatratio, floatknee)
39 {
40 detector.setEnvelope(attackTime, releaseTime);
41 _threshold = threshold;
42 _ratio = ratio;
43 _kneeWidth = knee;
44 }
45 46 overridefloatgetNextSample(constfloatinput)
47 {
48 return0;
49 }
50 51 overridevoidreset() nothrow @nogc52 {
53 54 }
55 56 overridevoidsetSampleRate(floatsampleRate)
57 {
58 _sampleRate = sampleRate;
59 detector.setSampleRate(_sampleRate);
60 }
61 62 /// Allows this processors envelope to be linked to another processor.63 /// This way the two will act as a single unit. Both processors must call64 /// this on each other to function properly65 voidlinkStereo(DynamicsProcessorstereoProcessor) nothrow @nogc66 {
67 linkedDetector = &stereoProcessor.detector;
68 stereoProcessor.linkedDetector = &this.detector;
69 }
70 71 protected:
72 /// Amount of input gain in decibels73 float_inputGain;
74 75 /// Level in decibels that the input signal must cross before compression begins76 float_threshold;
77 78 /// Time in milliseconds before compression begins after threshold has been79 /// crossed80 float_attTime;
81 82 /// Time in milliseconds before the compression releases after the input signal83 /// has fallen below the threshold84 float_relTime;
85 86 /// Ratio of compression, higher ratio = more compression87 float_ratio;
88 89 /// Amount of output gain in decibels90 float_outputGain;
91 92 /// width of the curve that interpolates between input and output. Unit in93 /// decibels94 float_kneeWidth;
95 96 /// Holds the points used for interpolation;97 float[] x, y;
98 99 //DynamicsProcessor _linkedProcessor;100 }