File: follower.c

package info (click to toggle)
bristol 0.60.10-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 15,652 kB
  • sloc: ansic: 124,457; sh: 10,579; makefile: 111
file content (213 lines) | stat: -rw-r--r-- 5,647 bytes parent folder | download | duplicates (3)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213

/*
 *  Diverse Bristol audio routines.
 *  Copyright (c) by Nick Copeland <nickycopeland@hotmail.com> 1996,2012
 *
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   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 for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 */

/*#define DEBUG */

/*
 * This envelope follower will track the input signal to extract an amplitude
 * envelope from it. It will track the rectified signal.
 */

#include <math.h>

#include "bristol.h"
#include "follower.h"

#define OPNAME "Envelope Follower"
#define OPDESCRIPTION "Mono envelope follower"
#define PCOUNT 1
#define IOCOUNT 2

#define GAIN 0

#define FOLLOWER_LIN_IND 0
#define FOLLOWER_LOUT_IND 1

/*
 * Reset any local memory information.
 */
static int destroy(bristolOP *operator)
{
#ifdef BRISTOL_DBG
	printf("destroy(%x)\n", operator);
#endif

	bristolfree(operator->specs);

	cleanup(operator);
	return(0);
}

/*
 * This is called by the frontend when a parameter is changed.
static int param(param, value)
 */
static int param(bristolOP *operator, bristolOPParams *param,
	unsigned char index, float value)
{
#ifdef DEBUG
	printf("checkParams(%f)\n", value);
#endif

	param->param[index].float_val = value;

	if (index == 0)
	{
		int offset = (value * (CONTROLLER_RANGE - 1)) + 1;
		/*
		 * This will be used to control the tracking rate. Attack should be
		 * quite a lot faster than decay.
		 */
		param->param[1].float_val = gainTable[offset].rate;
		param->param[2].float_val = 1/(gainTable[offset].rate * 4);
	}
	return(0);
}

/*
 * Reset any local memory information.
 */
static int reset(bristolOP *operator, bristolOPParams *param)
{
#ifdef BRISTOL_DBG
	printf("reset(%x)\n", operator);
#endif

	param->param[0].float_val = 0;
	return(0);
}

static int operate(bristolOP *operator,
	bristolVoice *voice,
	bristolOPParams *param,
	void *lcl)
{
	bristolFOLLOWER *specs;
	register float *source, *dest, envelope, signal, attack, decay;
	register int count, i;

	specs = (bristolFOLLOWER *) operator->specs;

	count = specs->spec.io[FOLLOWER_LOUT_IND].samplecount;
	source = specs->spec.io[FOLLOWER_LIN_IND].buf;
	dest = specs->spec.io[FOLLOWER_LOUT_IND].buf;

	envelope = ((bristolFOLLOWERlocal *) lcl)->envelope;

	attack = param->param[1].float_val;
	decay = param->param[2].float_val;

#ifdef DEBUG
	printf("follower()\n");
#endif

	/*
	 * This should be something like a VU meter, and a root mean square with a
	 * gentle average function might fit the bill but could be quite intensive.
	 * Alternatively a rectifier and smoother would also work, but the single
	 * available parameter would really have to be the smoothing rate rather
	 * than overall gain - not an issue as the typical source would be the input
	 * signal that will already have gone through a preamp.
	 */
	for (i = 0; i < count; i++) {
		if ((signal = *source++) < 0)
			signal = -signal;

		if (signal > envelope)
			envelope *= attack;
		else if (signal < envelope)
			envelope *= decay;

		*dest++ = envelope;
	}

	((bristolFOLLOWERlocal *) lcl)->envelope = envelope;
	return(0);
}

/*
 * Setup any variables in our OP structure, in our IO structures, and malloc
 * any memory we need.
 */
bristolOP *
followerinit(bristolOP **operator, int index, int samplerate, int samplecount)
{
	bristolFOLLOWER *specs;

	*operator = bristolOPinit(operator, index, samplecount);

#ifdef BRISTOL_DBG
	printf("followerinit(%x(%x), %i, %i, %i)\n",
		operator, *operator, index, samplerate, samplecount);
#endif

	/*
	 * Then the local parameters specific to this operator. These will be
	 * the same for each operator, but must be inited in the local code.
	 */
	(*operator)->operate = operate;
	(*operator)->destroy = destroy;
	(*operator)->reset = reset;
	(*operator)->param = param;
	(*operator)->flags |= BRISTOL_FX_STEREO;

	specs = (bristolFOLLOWER *) bristolmalloc0(sizeof(bristolFOLLOWER));
	(*operator)->specs = (bristolOPSpec *) specs;
	(*operator)->size = sizeof(bristolFOLLOWER);

	/*
	 * These are specific to this operator, and will need to be altered for
	 * each operator.
	 */
	specs->spec.opname = OPNAME;
	specs->spec.description = OPDESCRIPTION;
	specs->spec.pcount = PCOUNT;
	specs->spec.iocount = IOCOUNT;
	specs->spec.localsize = sizeof(bristolFOLLOWERlocal);

	/*
	 * Now fill in the specs for this operator.
	 */
	specs->spec.param[0].pname = "rate";
	specs->spec.param[0].description= "follower envelope attack and decay rate";
	specs->spec.param[0].type = BRISTOL_FLOAT;
	specs->spec.param[0].low = 0;
	specs->spec.param[0].high = 1;
	specs->spec.param[0].flags = BRISTOL_ROTARY|BRISTOL_SLIDER;

	/*
	 * Now fill in the IO specs.
	 */
	specs->spec.io[0].ioname = "input buffer";
	specs->spec.io[0].description = "input signal";
	specs->spec.io[0].samplerate = samplerate;
	specs->spec.io[0].samplecount = samplecount;
	specs->spec.io[0].flags = BRISTOL_AC|BRISTOL_INPUT;

	specs->spec.io[1].ioname = "output buffer";
	specs->spec.io[1].description = "output signal";
	specs->spec.io[1].samplerate = samplerate;
	specs->spec.io[1].samplecount = samplecount;
	specs->spec.io[1].flags = BRISTOL_AC|BRISTOL_INPUT;

	return(*operator);
}