File: RMS.cpp

package info (click to toggle)
supercollider-sc3-plugins 3.13.0~repack-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 20,104 kB
  • sloc: cpp: 303,352; lisp: 9,589; ansic: 3,547; sh: 96; makefile: 87; haskell: 21
file content (121 lines) | stat: -rw-r--r-- 2,665 bytes parent folder | download | duplicates (4)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//-----------------------------------------------------
// name: "RMS"
// version: "1.1"
// author: "Till Bovermann"
// license: "GPL2+"
// copyright: "(c) Till Bovermann 2016, based on LPF1 as found in BerlachUGens"
//-----------------------------------------------------

#include "SC_PlugIn.h"
// #include <stdio.h>

// #ifndef MAXFLOAT
// # include <float.h>
// # define MAXFLOAT FLT_MAX
// #endif

static inline float tanapprox (float x)
{
  return x*(x*(x*(0.96369f*x-0.865157f)+0.53576f)+0.93f);
}


// InterfaceTable contains pointers to functions in the host (server).
static InterfaceTable *ft;
typedef unsigned int uint;
// declare struct to hold unit generator state
struct RMS : public Unit
{
	float m_y1;
	float p;
	float last_lpf;
	float last_freq;
};

void RMS_Ctor(RMS *unit);
void RMS_next(RMS *unit, int inNumSamples);
void RMS_next_a(RMS *unit, int inNumSamples);


void
RMS_Ctor (RMS *unit)
{
  unit->m_y1 = 0.f;
  unit->last_lpf = 0;
  unit->last_freq = ZIN0(1);
  unit->p = (1.f-2.f*tanf((unit->last_freq/SAMPLERATE)));
  if (INRATE(1) == calc_FullRate) {
	  SETCALC(RMS_next_a);
  } else {
    unit->last_freq = ZIN0(1);
	  unit->p = (1.f-2.f*tanf((unit->last_freq/SAMPLERATE)));
	  SETCALC(RMS_next);
  }

  RMS_next(unit, 1);
}

void
RMS_next (RMS *unit, int inNumSamples)
{
  float *out = ZOUT(0);
  float *in = ZIN(0);
  float freq = ZIN0(1);
  float p = unit->p;
  float y1 = unit->m_y1;
  float lastLPF = unit->last_lpf;
  float inVal;

  if (unit->last_freq != freq) {
	  float newp = (1.f-2.f*tanf((freq/SAMPLERATE)));
	  float pinc = (newp-p)/inNumSamples;
	  unit->p=newp;
	  unit->last_freq=freq;
	  LOOP(inNumSamples,
	  	   inVal = ZXP(in);
	  	   lastLPF = (1.f-p)*(inVal*inVal)+ p*lastLPF;
	       ZXP(out) = y1 = zapgremlins(sqrt(lastLPF));
	       p += pinc;
	  );
  } else {
	  LOOP(inNumSamples,
	  		 inVal = ZXP(in);
	  		 lastLPF = (1.f-p)*(inVal*inVal)+ p*lastLPF;
	  	     ZXP(out) = y1 = zapgremlins(sqrt(lastLPF));
	  );
  }

  unit->m_y1 = y1;
  unit->last_lpf = lastLPF;
}


void
RMS_next_a (RMS *unit, int inNumSamples)
{
  float *out = ZOUT(0);
  float *in = ZIN(0);
  float *freq = ZIN(1);
  float y1 = unit->m_y1;
  float srate = SAMPLERATE;
  float lastLPF = unit->last_lpf;

  LOOP(inNumSamples,
       float p = (1.f-2.f*tanapprox((ZXP(freq)/srate)));
       float inVal = ZXP(in);
       lastLPF = (1.f-p)*(inVal*inVal)+ p*lastLPF;
       ZXP(out) = y1 = zapgremlins(sqrt(lastLPF));
  );

   unit->m_y1 = y1;
   unit->last_lpf = lastLPF;
}


//////////////////////////////
PluginLoad(RMS)
{
	// InterfaceTable *inTable implicitly given as argument to load function
	ft = inTable;
	DefineSimpleUnit(RMS);
}