File: Sustainer.C

package info (click to toggle)
rakarrack 0.6.1-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 10,136 kB
  • sloc: cpp: 27,705; sh: 794; makefile: 377
file content (191 lines) | stat: -rw-r--r-- 4,103 bytes parent folder | download | duplicates (5)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
  Rakarrack Guitar FX
 
  Sustainer.C - Simple compressor/sustainer effect with easy interface, minimal controls
  Copyright (C) 2010 Ryan Billing
  Author: Ryan Billing
  
  This program is free software; you can redistribute it and/or modify
  it under the terms of version 3 of the GNU General Public License 
  as published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License (version 2) for more details.

  You should have received a copy of the GNU General Public License (version 2)
  along with this program; if not, write to the Free Software Foundation,
  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA

*/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "Sustainer.h"

Sustainer::Sustainer (float * efxoutl_, float * efxoutr_)
{
  efxoutl = efxoutl_;
  efxoutr = efxoutr_;

  Pvolume = 64;
  Psustain = 64;
  fsustain = 0.5f;
  level = 0.5f;
  
  float tmp = 0.01f;  //10 ms decay time on peak detectors
  prls = 1.0f - (cSAMPLE_RATE/(cSAMPLE_RATE + tmp));

  tmp = 0.05f; //50 ms att/rel on compressor
  calpha =  cSAMPLE_RATE/(cSAMPLE_RATE + tmp);
  cbeta = 1.0f - calpha;
  cthresh = 0.25f;
  cratio = 0.25f;

  timer = 0;
  hold = (int) (SAMPLE_RATE*0.0125);  //12.5ms
  cleanup ();
};

Sustainer::~Sustainer ()
{
 
};

/*
 * Cleanup the effect
 */
void
Sustainer::cleanup ()
{
  compeak = 0.0f;
  compenv = 0.0f;  
  oldcompenv = 0.0f;
  cpthresh = cthresh; //dynamic threshold  
};




/*
 * Effect output
 */
void
Sustainer::out (float * smpsl, float * smpsr)
{
  int i;
  float auxtempl = 0.0f;
  float auxtempr = 0.0f;
  float auxcombi = 0.0f;
  
   for (i = 0; i<PERIOD; i++)    //apply compression to auxresampled
   {
   auxtempl = input * smpsl[i];
   auxtempr = input * smpsr[i];
   auxcombi = 0.5f * (auxtempl + auxtempr);
   if(fabs(auxcombi) > compeak) 
   {
   compeak = fabs(auxcombi);   //First do peak detection on the signal
   timer = 0;
   }
   if(timer>hold)
   {
   compeak *= prls;
   timer--;
   }
   timer++;
   compenv = cbeta * oldcompenv + calpha * compeak;       //Next average into envelope follower
   oldcompenv = compenv;
   
   if(compenv > cpthresh)                                //if envelope of signal exceeds thresh, then compress
   {
   compg = cpthresh + cpthresh*(compenv - cpthresh)/compenv; 
   cpthresh = cthresh + cratio*(compg - cpthresh);   //cpthresh changes dynamically
   tmpgain = compg/compenv;
   }
   else
   {
   tmpgain = 1.0f;
   }
   
   if(compenv < cpthresh) cpthresh = compenv;
   if(cpthresh < cthresh) cpthresh = cthresh;
   
   smpsl[i] = auxtempl * tmpgain * level;   
   smpsr[i] = auxtempr * tmpgain * level;
   };
      //End compression
};


/*
 * Parameter control
 */


void
Sustainer::setpreset (int npreset)
{
  const int PRESET_SIZE = 2;
  const int NUM_PRESETS = 3;
  int presets[NUM_PRESETS][PRESET_SIZE] = {
    //Moderate
    {79, 54},
    //Extreme
    {16, 127},
    //Mild
    {120, 15},

  };

  if(npreset>NUM_PRESETS-1)  
    {   
     Fpre->ReadPreset(36,npreset-NUM_PRESETS+1);    
     for (int n = 0; n < PRESET_SIZE; n++)    
     changepar (n, pdata[n]);    
    }    
  else                                      
  {     
  for (int n = 0; n < PRESET_SIZE; n++)
  changepar (n, presets[npreset][n]);
  }
  Ppreset = npreset;
};


void
Sustainer::changepar (int npar, int value)
{
  switch (npar)
    {
    case 0:
      Pvolume = value;
      level = dB2rap(-30.0f * (1.0f - ((float) Pvolume/127.0f)));
      break;
    case 1:
      Psustain = value;
      fsustain =  (float) Psustain/127.0f;
      cratio = 1.25f - fsustain;
      input = dB2rap (42.0f * fsustain - 6.0f);
      cthresh = 0.25 + fsustain;
      break;

    };
};

int
Sustainer::getpar (int npar)
{
  switch (npar)
    {
    case 0:
      return (Pvolume);
      break;
    case 1:
      return (Psustain);
      break;
    };
  return (0);			//in case of bogus parameter number
};