Debian Codegraph @

plugins/LadspaEffect/swh/util/blo.h

raw
1 /*
2 Copyright (C) 2002 Steve Harris <[email protected]>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <math.h>
20 #include <stdio.h>
21 #include <unistd.h>
22
23 #include "ladspa-util.h"
24
25 #define BLO_N_WAVES 4
26 #define BLO_SINE 0
27 #define BLO_TRI 1
28 #define BLO_SQUARE 2
29 #define BLO_SAW 3
30
31 #define BLO_MMAP 0
32 #define BLO_MALLOC 1
33
34 #ifndef BLO_N_HARMONICS
35 #define BLO_N_HARMONICS 64
36 #endif
37
38 /* The wraparaound off the end of the basic wavetable, used by the
39 * interpolators. */
40 #define BLO_TABLE_WR 4
41
42 #define BLO_SIN_GEN(phase) sin((phase) * 2.0f * (float)M_PI / table_size_f)
43 #define BLO_NEXT_TABLE (all_tables + (table_count++ * (table_size + BLO_TABLE_WR)))
44
45 typedef struct {
46 float *h_tables[BLO_N_WAVES][BLO_N_HARMONICS];
47 float *alloc_space;
48 size_t alloc_size;
49 int table_size;
50 int table_mask;
51 int store_type;
52 } blo_h_tables;
53
54 typedef union {
55 int all;
56 struct {
57 unsigned short fr;
58 short in;
59 } part;
60 } blo_fixp;
61
62 typedef struct {
63 blo_h_tables *tables;
64 float sample_rate;
65 float nyquist;
66 unsigned int wave;
67 blo_fixp ph;
68 blo_fixp om;
69 float ph_coef;
70 int ph_mask;
71 int table_mask;
72 int table_size;
73 int topbit;
74 float *table;
75 float *table_b;
76 float xfade;
77 } blo_h_osc;
78
79 blo_h_tables *blo_h_tables_new(int table_size);
80
81 void blo_h_tables_free(blo_h_tables *tables);
82
83 blo_h_osc *blo_h_new(blo_h_tables *tables, unsigned int wave,
84 float sample_rate);
85
86 void blo_h_free(blo_h_osc *osc);
87
88 /* Set frequency for static oscilator, less cycles, but won't modulate as well
89 * You can't use blo_osc_hd_run_* until you've called the _hd version of this
90 * function.
91 */
92
93 static inline void blo_hs_set_freq(blo_h_osc *this, const float f)
94 {
95 unsigned int tab_num;
96 const float ff = fabs(f) + 0.00001f; // Prevent div by zero
97
98 // This needs to be a cast, no idea why
99 this->om.all = (int)(f * this->ph_coef);
100 tab_num = f_round(this->nyquist / ff - 0.5f);
101 if (tab_num >= BLO_N_HARMONICS) {
102 tab_num = BLO_N_HARMONICS - 1;
103 }
104 this->table_b = this->tables->h_tables[this->wave][tab_num];
105 }
106
107 /* Set frequency for dynamic oscilator, can only used with _hd run calls.
108 */
109
110 static inline void blo_hd_set_freq(blo_h_osc *this, const float f)
111 {
112 int tab_num;
113 const float ff = fabs(f) + 0.00001f; // Prevent div by zero
114
115 this->om.all = f_round(f * this->ph_coef);
116 tab_num = abs(f_round(this->nyquist / ff - 0.5f));
117 if (tab_num >= BLO_N_HARMONICS) {
118 tab_num = BLO_N_HARMONICS - 1;
119 } else if (tab_num < 0) {
120 tab_num = 0;
121 }
122 this->table = this->tables->h_tables[this->wave][tab_num];
123 this->xfade = this->nyquist / ff - tab_num;
124 if (this->xfade > 1.0f) {
125 this->xfade = 1.0f;
126 }
127 if (--tab_num < 0) {
128 tab_num = 0;
129 }
130 this->table_b = this->tables->h_tables[this->wave][tab_num];
131 }
132
133 /* Run static oscilator, returns amplitude for current phase and advances the
134 * phase. Uses linear interpoation */
135
136 static inline float blo_hs_run_lin(blo_h_osc *this)
137 {
138 const float frac = (float)(this->ph.part.fr) * 0.00001525878f;
139 const int idx = this->ph.part.in;
140
141 this->ph.all += this->om.all;
142 this->ph.all &= this->ph_mask;
143 if (this->topbit != (this->ph.all & this->table_size)) {
144 this->topbit = this->ph.all & this->table_size;
145 this->table = this->table_b;
146 }
147
148 return this->table[idx] * (1.0f - frac) + this->table[idx+1] * frac;
149 }
150
151 /* Run static oscilator, returns amplitude for current phase and advances the
152 * phase. Uses cubic interpoation */
153
154 static inline float blo_hs_run_cub(blo_h_osc *this)
155 {
156 const float frac = (float)(this->ph.part.fr) * 0.00001525878f;
157 const int idx = this->ph.part.in;
158 float *t = this->table;
159
160 this->ph.all += this->om.all;
161 this->ph.all &= this->ph_mask;
162
163 if (this->topbit != (this->ph.all & this->table_size)) {
164 this->topbit = this->ph.all & this->table_size;
165 this->table = this->table_b;
166 t = this->table_b;
167 }
168
169 return cube_interp(frac, t[idx], t[idx+1], t[idx+2], t[idx+3]);
170 }
171
172 /* Run dynamic oscilator, returns amplitude for current phase and advances the
173 * phase, ensures harmonics won't suddently pop into existence, takes more
174 * cycles and has slightly less high frequency partials than the static
175 * version */
176
177 static inline float blo_hd_run_lin(blo_h_osc * const this)
178 {
179 float low, high;
180 const float frac = (float)(this->ph.part.fr) * 0.00001525878f;
181 const int idx = this->ph.part.in;
182
183 this->ph.all += this->om.all;
184 this->ph.all &= this->ph_mask;
185
186 low = LIN_INTERP(frac, this->table_b[idx], this->table_b[idx+1]);
187 high = LIN_INTERP(frac, this->table[idx], this->table[idx+1]);
188
189 return LIN_INTERP(this->xfade, low, high);
190 }
191
192 /* Run dynamic oscilator, returns amplitude for current phase and advances the
193 * phase, ensures harmonics won't suddently pop into existence, takes more
194 * cycles and has slightly less high frequency partials than the static
195 * version. This one uses cubic interpolation. */
196
197 static inline float blo_hd_run_cub(blo_h_osc * const this)
198 {
199 float low, high;
200 const float frac = (float)(this->ph.part.fr) * 0.00001525878f;
201 const int idx = this->ph.part.in;
202 const float *tl = this->table_b;
203 const float *th = this->table;
204
205
206 this->ph.all += this->om.all;
207 this->ph.all &= this->ph_mask;
208
209 low = cube_interp(frac, tl[idx], tl[idx+1], tl[idx+2], tl[idx+3]);
210 high = cube_interp(frac, th[idx], th[idx+1], th[idx+2], th[idx+3]);
211
212 return LIN_INTERP(this->xfade, low, high);
213 }