MuseScore  3.4
Music composition and notation
compressor.h
Go to the documentation of this file.
1 //=============================================================================
2 // MuseScore
3 // Music Composition & Notation
4 //
5 // Copyright (C) 2015 Werner Schweer
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License version 2
9 // as published by the Free Software Foundation and appearing in
10 // the file LICENCE.GPL
11 //=============================================================================
12 
13 #ifndef __COMPRESSOR_H__
14 #define __COMPRESSOR_H__
15 
16 #include "effects/effect.h"
17 
18 namespace Ms {
19 
20 #define RMS_BUF_SIZE 64
21 static const int A_TBL = 256;
22 
23 //---------------------------------------------------------
24 // RmsEnv
25 //---------------------------------------------------------
26 
27 class RmsEnv {
29  unsigned int pos;
30  float sum;
31 
32  public:
33  RmsEnv() {
34  for (int i=0; i<RMS_BUF_SIZE; i++)
35  buffer[i] = 0.0f;
36  pos = 0;
37  sum = 0.0f;
38  }
39  float process(const float x) {
40  sum -= buffer[pos];
41  sum += x;
42  if (sum < 1.0e-6)
43  sum = 0.0f;
44  buffer[pos] = x;
45  pos = (pos + 1) & (RMS_BUF_SIZE - 1);
46  return sqrt(sum / (float)RMS_BUF_SIZE);
47  }
48  };
49 
50 //---------------------------------------------------------
51 // Compressor
52 //---------------------------------------------------------
53 
54 class Compressor : public Effect
55  {
56  Q_OBJECT
57 
58  float sampleRate;
60  float sum;
61  float amp;
62  float gain;
63  float gain_t;
64  float env;
65  float env_rms;
66  float env_peak;
67  unsigned int count;
68  float as[A_TBL];
69 
70  // The balance between the RMS and peak envelope followers.
71  // RMS is generally better for subtle, musical compression and peak is better for heavier,
72  // fast compression and percussion.
73  // <range min="0" max="1"/>
74  float rms_peak { .5 };
75 
76  // Attack time (ms)
77  //The attack time in milliseconds.
78  // <range min="1.5" max="400"/>
79  float _attack {1.5 }; // 1.5 - 400 (ms)
80 
81  // <port label="release" dir="input" type="control" hint="default_middle">
82  // Release time (ms)
83  // The release time in milliseconds.
84  // <range min="2" max="800"/>
85  float _release = 400;
86 
87  // <port label="threshold" dir="input" type="control" hint="default_maximum">
88  // Threshold level (dB)
89  // The point at which the compressor will start to kick in.
90  // <range min="-30" max="0"/>
91  float _threshold = -10;
92 
93  // Ratio (1:n)
94  // The gain reduction ratio used when the signal level exceeds the threshold.
95  // <range min="1" max="20"/>
96  float _ratio = 5;
97 
98  // <port label="knee" dir="input" type="control" hint="default_low">
99  // Knee radius (dB)
100  // The distance from the threshold where the knee curve starts.
101  // <range min="1" max="10"/>
102  float _knee = 1.0;
103 
104  // Amplitude (dB)
105  // The level of the input signal, in decibels.
106  // <range min="-40" max="+12"/>
107 // float amplitude = 0.0;
108 
109  // Gain reduction (dB)
110  // The degree of gain reduction applied to the input signal, in decibels.
111  // <range min="-24" max="0"/>
112 // float gain_red = -20;
113 
114  float _makeupGain = 1.0;
115 
116  void setRmsPeak(float v) { rms_peak = v; }
117  void setAttack(float v) { _attack = v; }
118  void setRelease(float v) { _release = v; }
119  void setThreshold(float v) { _threshold = v; }
120  void setRatio(float v) { _ratio = v; }
121  void setKnee(float v) { _knee = v; }
122  void setMakeupGain(float v) { _makeupGain = v; }
123 
124  float rmsPeak() const { return rms_peak; }
125  float attack() const { return _attack; }
126  float release() const { return _release; }
127  float threshold() const { return _threshold; }
128  float ratio() const { return _ratio; }
129  float knee() const { return _knee; }
130  float makeupGain() const { return _makeupGain; }
131 
132  public:
133  virtual void init(float fsamp);
134  virtual void process(int n, float* inp, float* out);
135  virtual const char* name() const { return "SC4"; }
136  virtual EffectGui* gui();
137  virtual const std::vector<ParDescr>& parDescr() const;
138 
139  virtual void setNValue(int parameter, double value);
140  virtual double nvalue(int idx) const;
141 
142  virtual SynthesizerGroup state() const;
143  virtual void setState(const SynthesizerGroup&);
144  };
145 
146 }
147 
148 #endif
149 
Definition: effect.h:41
float env_rms
Definition: compressor.h:65
float makeupGain() const
Definition: compressor.h:130
void setThreshold(float v)
Definition: compressor.h:119
float knee() const
Definition: compressor.h:129
#define RMS_BUF_SIZE
Definition: compressor.h:20
void setKnee(float v)
Definition: compressor.h:121
float env
Definition: compressor.h:64
Definition: effectgui.h:24
float sum
Definition: compressor.h:30
void setRatio(float v)
Definition: compressor.h:120
float amp
Definition: compressor.h:61
float env_peak
Definition: compressor.h:66
float sum
Definition: compressor.h:60
void setRmsPeak(float v)
Definition: compressor.h:116
float process(const float x)
Definition: compressor.h:39
unsigned int pos
Definition: compressor.h:29
float buffer[RMS_BUF_SIZE]
Definition: compressor.h:28
float sampleRate
Definition: compressor.h:58
float attack() const
Definition: compressor.h:125
float gain_t
Definition: compressor.h:63
float threshold() const
Definition: compressor.h:127
virtual const char * name() const
Definition: compressor.h:135
Definition: compressor.h:27
Definition: aeolus.cpp:26
float ratio() const
Definition: compressor.h:128
RmsEnv()
Definition: compressor.h:33
unsigned int count
Definition: compressor.h:67
void setAttack(float v)
Definition: compressor.h:117
Definition: synthesizerstate.h:40
void setRelease(float v)
Definition: compressor.h:118
Definition: compressor.h:54
void setMakeupGain(float v)
Definition: compressor.h:122
RmsEnv rms
Definition: compressor.h:59
float rmsPeak() const
Definition: compressor.h:124
float gain
Definition: compressor.h:62
float release() const
Definition: compressor.h:126