LMMS
Loading...
Searching...
No Matches
Delay.h
Go to the documentation of this file.
1/*
2 dsp/Delay.h
3
4 Copyright 2003-4, 2010 Tim Goetze <tim@quitte.de>
5
6 http://quitte.de/dsp/
7
8 delay lines with fractional (linear or cubic interpolation) lookup
9 and an allpass interpolating tap (which needs more work).
10
11 delay line storage is aligned to powers of two for simplified wrapping
12 checks (no conditional or modulo, binary and suffices instead).
13
14*/
15/*
16 This program is free software; you can redistribute it and/or
17 modify it under the terms of the GNU General Public License
18 as published by the Free Software Foundation; either version 2
19 of the License, or (at your option) any later version.
20
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
25
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
29 02111-1307, USA or point your web browser to http://www.gnu.org.
30*/
31
32#ifndef _DSP_DELAY_H_
33#define _DSP_DELAY_H_
34
35#include "util.h"
36#include "FPTruncateMode.h"
37
38namespace DSP {
39
40class Delay
41{
42 public:
43 int size;
45 int read, write;
46
48 {
49 read = write = 0;
50 data = 0;
51 }
52
54 {
55 if (data) free (data);
56 }
57
58 void init (int n)
59 {
61 data = (sample_t *) calloc (sizeof (sample_t), size);
62 size -= 1;
63 write = n;
64 }
65
66 void reset()
67 {
68 memset (data, 0, (size + 1) * sizeof (sample_t));
69 }
70
71 sample_t &
73 {
74 return data [(write - i) & size];
75 }
76
77 inline void
79 {
80 data [write] = x;
81 write = (write + 1) & size;
82 }
83
84 inline sample_t
86 {
87 sample_t x = data [read];
88 read = (read + 1) & size;
89 return x;
90 }
91
92 inline sample_t
94 {
95 put (x);
96 return get();
97 }
98
99 /* fractional lookup, linear interpolation */
100 inline sample_t
101 get_at (float f)
102 {
103 int n;
104 fistp (f, n); /* read: i = (int) f; relies on FPTruncateMode */
105 f -= n;
106
107 return (1 - f) * (*this) [n] + f * (*this) [n + 1];
108 }
109
110 /* fractional lookup, cubic interpolation */
111 inline sample_t
112 get_cubic (float f)
113 {
114 int n;
115 fistp (f, n); /* see FPTruncateMode */
116 f -= n;
117
118 sample_t x_1 = (*this) [n - 1];
119 sample_t x0 = (*this) [n];
120 sample_t x1 = (*this) [n + 1];
121 sample_t x2 = (*this) [n + 2];
122
123 /* sample_t (32bit) quicker than double here */
124 sample_t a =
125 (3 * (x0 - x1) - x_1 + x2) * .5;
126 sample_t b =
127 2 * x1 + x_1 - (5 * x0 + x2) * .5;
128 sample_t c =
129 (x1 - x_1) * .5;
130
131 return x0 + (((a * f) + b) * f + c) * f;
132 }
133};
134
135/* allpass variant */
136
138{
139 public:
141
143 {
144 reset();
145 }
146
147 void reset()
148 {
149 x1 = y1 = 0;
150 }
151
152 sample_t get (Delay & d, float f)
153 {
154 int n;
155 fistp (f, n); /* read: n = (int) f; relies on FPTruncateMode */
156 f -= n;
157
158 sample_t x = d[n];
159 f = (1 - f) / (1 + f);
160 y1 = x1 + f * x - f * y1;
161 x1 = x;
162 return y1;
163 }
164};
165
166}; /* namespace DSP */
167
168#endif /* _DSP_DELAY_H_ */
void fistp(float f, int32_t &i)
Definition FPTruncateMode.h:29
uint8_t a
Definition Spc_Cpu.h:141
LADSPA_Data sample_t
Definition basics.h:100
Definition Delay.h:41
sample_t get()
Definition Delay.h:85
void put(sample_t x)
Definition Delay.h:78
int read
Definition Delay.h:45
~Delay()
Definition Delay.h:53
sample_t get_at(float f)
Definition Delay.h:101
Delay()
Definition Delay.h:47
int size
Definition Delay.h:43
int write
Definition Delay.h:45
sample_t get_cubic(float f)
Definition Delay.h:112
sample_t & operator[](int i)
Definition Delay.h:72
void reset()
Definition Delay.h:66
sample_t * data
Definition Delay.h:44
sample_t putget(sample_t x)
Definition Delay.h:93
void init(int n)
Definition Delay.h:58
sample_t get(Delay &d, float f)
Definition Delay.h:152
sample_t y1
Definition Delay.h:140
sample_t x1
Definition Delay.h:140
DelayTapA()
Definition Delay.h:142
void reset()
Definition Delay.h:147
unsigned d
Definition inflate.c:940
register unsigned i
Definition inflate.c:1575
unsigned x[BMAX+1]
Definition inflate.c:1586
unsigned f
Definition inflate.c:1572
Definition BiQuad.h:31
int next_power_of_2(int n)
Definition util.h:33
int n
Definition crypt.c:458
return c
Definition crypt.c:175
b
Definition crypt.c:628