[go: up one dir, main page]

File: Delay.h

package info (click to toggle)
caps 0.3.0-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 888 kB
  • ctags: 1,273
  • sloc: cpp: 9,837; ansic: 584; makefile: 62
file content (171 lines) | stat: -rw-r--r-- 3,337 bytes parent folder | download | duplicates (7)
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
/*
	dsp/Delay.h
	
	Copyright 2003-4 Tim Goetze <tim@quitte.de>
	
	http://quitte.de/dsp/

	delay lines with fractional (linear or cubica interpolation) lookup 
	and an allpass interpolating tap (which needs more work).

	delay line storage is aligned to powers of two for simplified wrapping
	checks (no conditional or modulo, binary and suffices instead).

*/
/*
	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 2
	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.
*/

#ifndef _DSP_DELAY_H_
#define _DSP_DELAY_H_

#include "util.h"
#include "FPTruncateMode.h"

namespace DSP {
	
class Delay
{
	public:
		int size;
		d_sample * data;
		int read, write;

		Delay()
			{
				read = write = 0;
				data = 0;
			}

		~Delay()
			{
				if (data) free (data);
			}

		void init (int n)
			{
				size = next_power_of_2 (n);
				data = (d_sample *) calloc (sizeof (d_sample), size);
				size -= 1;
				write = n;
			}

		void reset()
			{
				memset (data, 0, (size + 1) * sizeof (d_sample));
			}

		d_sample & 
		operator [] (int i)
			{
				return data [(write - i) & size];
			}

		inline void 
		put (d_sample x)
			{
				data [write] = x;
				write = (write + 1) & size;
			}

		inline d_sample 
		get()
			{
				d_sample x = data [read];
				read = (read + 1) & size;
				return x;
			}

		inline d_sample
		putget (d_sample x)
			{
				put (x);
				return get();
			}

		/* fractional lookup, linear interpolation */
		inline d_sample 
		get_at (float f)
			{
				int n;
				fistp (f, n); /* read: i = (int) f; relies on FPTruncateMode */
				f -= n;
				
				return (1 - f) * (*this) [n] + f * (*this) [n + 1];
			}

		/* fractional lookup, cubic interpolation */
		inline d_sample
		get_cubic (float f)
			{
				int n;
				fistp (f, n); /* see FPTruncateMode */
				f -= n;

				d_sample x_1 = (*this) [n - 1];
				d_sample x0 = (*this) [n];
				d_sample x1 = (*this) [n + 1];
				d_sample x2 = (*this) [n + 2];

				/* d_sample (32bit) quicker than double here */
				register d_sample a = 
						(3 * (x0 - x1) - x_1 + x2) * .5;
				register d_sample b =
						2 * x1 + x_1 - (5 * x0 + x2) * .5;
				register d_sample c = 
						(x1 - x_1) * .5;

				return x0 + (((a * f) + b) * f + c) * f;
			}
};

/* allpass variant */

class DelayTapA
{
	public:
		d_sample x1, y1;

		DelayTapA()
			{
				reset();
			}

		void reset()
			{
				x1 = y1 = 0;
			}

		d_sample get (Delay & d, float f)
			{
				int n;
				fistp (f, n); /* read: i = (int) f; relies on FPTruncateMode */
				f -= n;
				if (0 && f < .5)
					f += 1,
					n -= 1;

				d_sample x = d[n];
				f = (1 - f) / (1 + f);
				y1 = x1 + f * x - f * y1;
				x1 = x;
				return y1;
			}
};

}; /* namespace DSP */

#endif /* _DSP_DELAY_H_ */