LMMS
Loading...
Searching...
No Matches
VCO.h
Go to the documentation of this file.
1/*
2 dsp/VCO.h
3
4 Copyright 2004, 2010 Tim Goetze <tim@quitte.de>
5
6 oscillators for triangle/sawtooth/square waves, and a combination
7 for detuning and hard sync.
8
9 NB: these oscillators are *not* bandlimited. oversample if needed.
10
11 */
12/*
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License
15 as published by the Free Software Foundation; either version 2
16 of the License, or (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 02111-1307, USA or point your web browser to http://www.gnu.org.
27*/
28
29
30#ifndef _DSP_VCO_H_
31#define _DSP_VCO_H_
32
33namespace DSP {
34
35/* variable triangle to sawtooth generator. you can use two of these to
36 * generate a square, but we prefer the integrated solution below.
37 */
38class TriSaw
39{
40 public:
41 /* doubles for maximum stability */
42 double phase, inc;
43
44 double tri, tri1, tri2;
45
46 public:
48 {
49 phase = 0;
50 tri = .5;
51 }
52
53 inline void set_f (double f, double fs)
54 {
55 set_inc (f / fs);
56 }
57
58 inline void set_inc (double i)
59 {
60 inc = i;
61 }
62
63 /* 0: triangle, 1: saw */
64 inline void set_saw (double t)
65 {
66 tri = .5 + .5 * t;
67 tri1 = 2. / tri;
68 tri2 = 2. / (1 - tri);
69 }
70
71 /* advance and return 1 sample.
72 * many conditionals, but quicker than a solution based on fmod()
73 */
74 inline float get()
75 {
76 phase += inc;
77
78 /* the good thing is that tri is always > .5, which implies
79 * that this first conditional is true more often than not. */
80 if (phase <= tri)
81 return -1 + phase * tri1;
82 if (phase < 1)
83 return 1 - (phase - tri) * tri2;
84
85 phase -= 1;
86 return -1 + phase * tri1;
87 }
88};
89
90/* variable triangle to sawtooth to square generator */
92{
93 public:
94 /* doubles for maximum stability, using floats here increases
95 * cycle need on my athlon */
96 double phase, inc;
97 double * sync;
98
100
101 /* using doubles here increases cycle need significantly */
102 float square_i;
103 float tri, tri1, tri2;
104 float st1, st2;
105
106 public:
108 {
109 reset();
110 }
111
112 void reset()
113 {
114 phase = 0;
115 sync = &phase;
116 sync_phase = 0;
117 set_saw_square (.5, .5);
118 }
119
120 inline void set_f (double f, double fs)
121 {
122 set_inc (f / fs);
123 }
124
125 inline void set_inc (double i)
126 {
127 inc = i;
128 }
129
130 inline void set_sync (TriSawSquare & tss, float p)
131 {
132 sync = &tss.phase;
133 sync_phase = p;
134 }
135
136 /* t = 0: tri - 1: saw,
137 * s = 0: tri/saw - 1: square
138 */
139 inline void set_saw_square (float t, float s)
140 {
141 tri = .5 + .5 * t;
142 square_i = 1 - s;
143
144 float si2 = 2 * square_i;
145 float one_m_t = 1 - tri;
146
147 tri1 = si2 / tri;
148 tri2 = si2 / one_m_t;
149
150 st1 = s * one_m_t;
151 st2 = s * tri;
152 }
153
154 /* advance and return 1 sample.
155 * many branching instructions but on this intel chip faster than
156 * a version using floor() to keep the phase within [0..1].
157 */
158 inline float get()
159 {
160 phase += inc;
161
162 if (phase <= tri)
163 first_half:
164 /* raw version:
165 return (1 - square) * (-1 + phase * 2 / tri) - square * (1 - tri);
166 */
167 return -square_i + phase * tri1 - st1;
168
169 if (phase < 1)
170 /* raw version:
171 return (1 - square) * (1 - (phase - tri) * 2 / (1 - tri)) + square * tri;
172 */
173 return square_i - (phase - tri) * tri2 + st2;
174
175 phase -= 1;
176 *sync = phase + sync_phase;
177 goto first_half;
178 }
179};
180
181class VCO2
182{
183 public:
186
187 public:
189 {
190 set_blend (.5);
191 }
192
193 void reset()
194 {
195 set_blend (.5);
196 vco[0].reset();
197 vco[1].reset();
198 }
199
200 void set_f (double f, double fs, double detune)
201 {
202 vco[0].set_f (f, fs);
203 vco[1].set_f (f * pow (2, detune / 12.), fs);
204 }
205
206 inline void set_blend (float b)
207 {
208 blend = b;
209 i_blend = 1 - fabs (b);
210 }
211
212 inline void set_sync (float sync)
213 {
214 vco[0].set_sync (sync ? vco[1] : vco[0], sync);
215 }
216
217 inline float get()
218 {
219 return vco[0].get() * blend + vco[1].get() * i_blend;
220 }
221};
222
223} /* namespace DSP */
224
225#endif /* _DSP_VCO_H_ */
void set_inc(double i)
Definition VCO.h:58
double tri2
Definition VCO.h:44
void set_f(double f, double fs)
Definition VCO.h:53
double tri
Definition VCO.h:44
TriSaw()
Definition VCO.h:47
double phase
Definition VCO.h:42
double inc
Definition VCO.h:42
double tri1
Definition VCO.h:44
float get()
Definition VCO.h:74
void set_saw(double t)
Definition VCO.h:64
Definition VCO.h:92
double inc
Definition VCO.h:96
void reset()
Definition VCO.h:112
float sync_phase
Definition VCO.h:99
float tri1
Definition VCO.h:103
float st1
Definition VCO.h:104
void set_inc(double i)
Definition VCO.h:125
float tri
Definition VCO.h:103
float tri2
Definition VCO.h:103
void set_saw_square(float t, float s)
Definition VCO.h:139
float st2
Definition VCO.h:104
float square_i
Definition VCO.h:102
float get()
Definition VCO.h:158
double phase
Definition VCO.h:96
void set_sync(TriSawSquare &tss, float p)
Definition VCO.h:130
double * sync
Definition VCO.h:97
TriSawSquare()
Definition VCO.h:107
void set_f(double f, double fs)
Definition VCO.h:120
void set_sync(float sync)
Definition VCO.h:212
float get()
Definition VCO.h:217
void reset()
Definition VCO.h:193
TriSawSquare vco[2]
Definition VCO.h:184
void set_f(double f, double fs, double detune)
Definition VCO.h:200
void set_blend(float b)
Definition VCO.h:206
VCO2()
Definition VCO.h:188
float blend
Definition VCO.h:185
float i_blend
Definition VCO.h:185
struct huft * t
Definition inflate.c:943
register unsigned i
Definition inflate.c:1575
unsigned s
Definition inflate.c:1555
unsigned f
Definition inflate.c:1572
Definition BiQuad.h:31
uch * p
Definition crypt.c:594
b
Definition crypt.c:628