File: Scape.cc

package info (click to toggle)
caps 0.9.26-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 924 kB
  • sloc: cpp: 10,867; ansic: 1,324; makefile: 78; python: 38
file content (249 lines) | stat: -rw-r--r-- 4,908 bytes parent folder | download
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
	Scape.cc
	
	Copyright 2004-13 Tim Goetze <tim@quitte.de>
	
	http://quitte.de/dsp/

*/
/*
	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, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
	02111-1307, USA or point your web browser to http://www.gnu.org.
*/

#include "basics.h"

#include "Scape.h"
#include "Descriptor.h"

static double 
dividers [] = {
	1 /* not used, 0 sentinel */, 
	1, 0.5, 0.66666666666666666667,	0.75
};

static float 
frandom2()
{
	float f = frandom();
	return f*f*f;
}

void 
Scape::init()
{
	delay.init ((int) (2.01 * fs)); /* two seconds = 30 bpm + */
	for (int i = 0; i < 2; ++i)
	{
		lfo[i].lorenz.init(),
		lfo[i].lorenz.set_rate (.00000001*fs);
		lfo[i].lp.set_f (3*over_fs);
	}
}

void
Scape::activate()
{
	time = 0;
	fb = 0;

	for (int i = 0; i < 4; ++i)
		svf[i].reset(),
		svf[i].set_out (1), /* band pass */
		hipass[i].set_f (250*over_fs);

	delay.reset();
	period = 0;
}

inline double
pick_f (float range, float tune)
{
	static float over_12 = 1./12;
	int n = 48 + (int) (4*12*range*frandom());
	return tune*pow(2,(n-69)*over_12);
}


void
Scape::cycle (uint frames)
{
	/* delay times */
	double t1 = 60*fs/getport(0);
	int div = (int) getport(1);
	double t2 = t1*dividers[div];


	float q = .99*getport(2);
	float blend = pow(getport(3),.2);
	float dry = sqrt(1-blend*blend);
	fb = .94*getport(4);

	float tune = 440;

	sample_t * s = ports[5];
	sample_t * dl = ports[6];
	sample_t * dr = ports[7];

	DSP::FPTruncateMode truncate;

	while (frames)
	{
		/* retune filters */
		if (period <= 1)
		{
			period = .5*t2;
			float f;

			f = frandom2();
			svf[0].set_f_Q (pick_f(.3,tune)*over_fs, q);
			svf[3].set_f_Q (pick_f(.5,tune)*over_fs, q); /* LP */
			
			f = frandom2();
			svf[1].set_f_Q (pick_f(.8,tune)*over_fs, f*q);
			svf[2].set_f_Q (pick_f(.9,tune)*over_fs, .5*f*q);
		}
		
		uint n = min((uint) period, frames);
		for (uint i=0; i < n; ++i)
		{
			sample_t x = s[i] + normal;

			sample_t x1 = delay.get_linear (t1);
			sample_t x2 = delay.get_linear (t2);

			delay.put (x + fb*x1);
			x = dry*x + .1*svf[0].process (x) + .2*svf[3].process(x);

			x1 = svf[1].process(x1 - normal);
			x2 = svf[2].process(x2 - normal);

			x1 = hipass[1].process(x1);
			x2 = hipass[2].process(x2);

			sample_t x1l, x1r, x2l, x2r;
			x1l = .7*fabs(lfo[0].get());
			x1r = 1 - x1l;
			x2r = .7*fabs(lfo[1].get());
			x2l = 1 - x2r;

			dl[i] = x + blend*(x1*x1l + x2*x2l);
			dr[i] = x + blend*(x1*x1r + x2*x2r);
		}

		frames -= n;
		period -= n;
		s += n;
		dl += n;
		dr += n;
	}
}

/* //////////////////////////////////////////////////////////////////////// */

PortInfo
Scape::port_info [] =
{
	{ "bpm", CTRL_IN, {DEFAULT_MID, 30, 182} }, 
	{	"div", CTRL_IN, {INTEGER | DEFAULT_MIN, 2, 4}, 
		"{2:'♪♪',3:'♪♪♪',4:'♬♬'}" }, 

	{ "Q", CTRL_IN | GROUP, {DEFAULT_HIGH, 0, 1} }, 
	{ "blend", CTRL_IN, {DEFAULT_MID, 0, 1} }, 
	{ "feedback", CTRL_IN, {DEFAULT_LOW, 0, 1} }, 

	{ "in", AUDIO_IN }, 
	{ "out.l", AUDIO_OUT }, 
	{ "out.r", AUDIO_OUT }
};

template <> void
Descriptor<Scape>::setup()
{
	Label = "Scape";
	Name = CAPS "Scape - Stereo delay with chromatic resonances";
	autogen();
}

/* //////////////////////////////////////////////////////////////////////// */

void 
DDDelay::init()
{
	float l = 2*fs; /* one beat at 30 bpm */
	for(int i=0; i<4; ++i)
	{
		step[i].delay.init((int) (l + .5)); 
		step[i].lp.set(.001);
	}
}

void
DDDelay::activate()
{
	for(int i=0; i<4; ++i)
	{
		step[i].delay.reset();
		step[i].lp.reset();
	}
}

void
DDDelay::cycle (uint frames)
{
	/* delay times */
	int div = (int) getport(1);
	int t = -1 + (int) (60*fs/getport(0));

	sample_t * s = ports[2];
	sample_t * d = ports[3];

	sample_t g[4] = {.4,.7,.8,.7};
	for(uint i=0; i<frames; ++i)
	{
		sample_t x=s[i], y=x;

		for(int j=0; j<div; ++j)
		{
			step[j].delay.put(y);
			y = step[j].delay[t];
			x += g[j]*y;
		}

		d[i] = x;
	}
}

/* //////////////////////////////////////////////////////////////////////// */

PortInfo
DDDelay::port_info [] =
{
	{ "bpm", CTRL_IN, {DEFAULT_MID, 30, 182} }, 
	{	"div", CTRL_IN, {INTEGER | DEFAULT_HIGH, 2, 4}, 
		"{2:'♪♪',3:'♪♪♪',4:'♬♬'}" }, 

	{ "in", AUDIO_IN }, 
	{ "out", AUDIO_OUT }, 
};

template <> void
Descriptor<DDDelay>::setup()
{
	Label = "DDDelay";
	Name = CAPS "DDDelay - Delay with fixed repetition count";
	autogen();
}