LMMS
Loading...
Searching...
No Matches
blo.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002 Steve Harris <steve@plugin.org.uk>
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#ifndef _MSC_VER
22#include <unistd.h>
23#endif
24
25#include "ladspa-util.h"
26
27#define BLO_N_WAVES 4
28#define BLO_SINE 0
29#define BLO_TRI 1
30#define BLO_SQUARE 2
31#define BLO_SAW 3
32
33#define BLO_MMAP 0
34#define BLO_MALLOC 1
35
36#ifndef BLO_N_HARMONICS
37#define BLO_N_HARMONICS 64
38#endif
39
40/* The wraparaound off the end of the basic wavetable, used by the
41 * interpolators. */
42#define BLO_TABLE_WR 4
43
44#define BLO_SIN_GEN(phase) sin((phase) * 2.0f * (float)M_PI / table_size_f)
45#define BLO_NEXT_TABLE (all_tables + (table_count++ * (table_size + BLO_TABLE_WR)))
46
55
56typedef union {
57 int all;
58 struct {
59 unsigned short fr;
60 short in;
61 } part;
62} blo_fixp;
63
64typedef struct {
67 float nyquist;
68 unsigned int wave;
71 float ph_coef;
75 int topbit;
76 float *table;
77 float *table_b;
78 float xfade;
79} blo_h_osc;
80
81blo_h_tables *blo_h_tables_new(int table_size);
82
84
85blo_h_osc *blo_h_new(blo_h_tables *tables, unsigned int wave,
86 float sample_rate);
87
88void blo_h_free(blo_h_osc *osc);
89
90/* Set frequency for static oscilator, less cycles, but won't modulate as well
91 * You can't use blo_osc_hd_run_* until you've called the _hd version of this
92 * function.
93 */
94
95static inline void blo_hs_set_freq(blo_h_osc *this, const float f)
96{
97 unsigned int tab_num;
98 const float ff = fabs(f) + 0.00001f; // Prevent div by zero
99
100 // This needs to be a cast, no idea why
101 this->om.all = (int)(f * this->ph_coef);
102 tab_num = f_round(this->nyquist / ff - 0.5f);
103 if (tab_num >= BLO_N_HARMONICS) {
104 tab_num = BLO_N_HARMONICS - 1;
105 }
106 this->table_b = this->tables->h_tables[this->wave][tab_num];
107}
108
109/* Set frequency for dynamic oscilator, can only used with _hd run calls.
110 */
111
112static inline void blo_hd_set_freq(blo_h_osc *this, const float f)
113{
114 int tab_num;
115 const float ff = fabs(f) + 0.00001f; // Prevent div by zero
116
117 this->om.all = f_round(f * this->ph_coef);
118 tab_num = f_abs(f_round(this->nyquist / ff - 0.5f));
119 if (tab_num >= BLO_N_HARMONICS) {
120 tab_num = BLO_N_HARMONICS - 1;
121 } else if (tab_num < 0) {
122 tab_num = 0;
123 }
124 this->table = this->tables->h_tables[this->wave][tab_num];
125 this->xfade = this->nyquist / ff - tab_num;
126 if (this->xfade > 1.0f) {
127 this->xfade = 1.0f;
128 }
129 if (--tab_num < 0) {
130 tab_num = 0;
131 }
132 this->table_b = this->tables->h_tables[this->wave][tab_num];
133}
134
135/* Run static oscilator, returns amplitude for current phase and advances the
136 * phase. Uses linear interpoation */
137
138static inline float blo_hs_run_lin(blo_h_osc *this)
139{
140 const float frac = (float)(this->ph.part.fr) * 0.00001525878f;
141 const int idx = this->ph.part.in;
142
143 this->ph.all += this->om.all;
144 this->ph.all &= this->ph_mask;
145 if (this->topbit != (this->ph.all & this->table_size)) {
146 this->topbit = this->ph.all & this->table_size;
147 this->table = this->table_b;
148 }
149
150 return this->table[idx] * (1.0f - frac) + this->table[idx+1] * frac;
151}
152
153/* Run static oscilator, returns amplitude for current phase and advances the
154 * phase. Uses cubic interpoation */
155
156static inline float blo_hs_run_cub(blo_h_osc *this)
157{
158 const float frac = (float)(this->ph.part.fr) * 0.00001525878f;
159 const int idx = this->ph.part.in;
160 float *t = this->table;
161
162 this->ph.all += this->om.all;
163 this->ph.all &= this->ph_mask;
164
165 if (this->topbit != (this->ph.all & this->table_size)) {
166 this->topbit = this->ph.all & this->table_size;
167 this->table = this->table_b;
168 t = this->table_b;
169 }
170
171 return cube_interp(frac, t[idx], t[idx+1], t[idx+2], t[idx+3]);
172}
173
174/* Run dynamic oscilator, returns amplitude for current phase and advances the
175 * phase, ensures harmonics won't suddently pop into existence, takes more
176 * cycles and has slightly less high frequency partials than the static
177 * version */
178
179static inline float blo_hd_run_lin(blo_h_osc * const this)
180{
181 float low, high;
182 const float frac = (float)(this->ph.part.fr) * 0.00001525878f;
183 const int idx = this->ph.part.in;
184
185 this->ph.all += this->om.all;
186 this->ph.all &= this->ph_mask;
187
188 low = LIN_INTERP(frac, this->table_b[idx], this->table_b[idx+1]);
189 high = LIN_INTERP(frac, this->table[idx], this->table[idx+1]);
190
191 return LIN_INTERP(this->xfade, low, high);
192}
193
194/* Run dynamic oscilator, returns amplitude for current phase and advances the
195 * phase, ensures harmonics won't suddently pop into existence, takes more
196 * cycles and has slightly less high frequency partials than the static
197 * version. This one uses cubic interpolation. */
198
199static inline float blo_hd_run_cub(blo_h_osc * const this)
200{
201 float low, high;
202 const float frac = (float)(this->ph.part.fr) * 0.00001525878f;
203 const int idx = this->ph.part.in;
204 const float *tl = this->table_b;
205 const float *th = this->table;
206
207
208 this->ph.all += this->om.all;
209 this->ph.all &= this->ph_mask;
210
211 low = cube_interp(frac, tl[idx], tl[idx+1], tl[idx+2], tl[idx+3]);
212 high = cube_interp(frac, th[idx], th[idx+1], th[idx+2], th[idx+3]);
213
214 return LIN_INTERP(this->xfade, low, high);
215}
static void blo_hd_set_freq(blo_h_osc *this, const float f)
Definition blo.h:112
static float blo_hs_run_cub(blo_h_osc *this)
Definition blo.h:156
void blo_h_free(blo_h_osc *osc)
Definition blo.c:274
void blo_h_tables_free(blo_h_tables *tables)
Definition blo.c:243
static float blo_hs_run_lin(blo_h_osc *this)
Definition blo.h:138
blo_h_osc * blo_h_new(blo_h_tables *tables, unsigned int wave, float sample_rate)
Definition blo.c:255
blo_h_tables * blo_h_tables_new(int table_size)
Definition blo.c:39
#define BLO_N_HARMONICS
Definition blo.h:37
#define BLO_N_WAVES
Definition blo.h:27
static float blo_hd_run_cub(blo_h_osc *const this)
Definition blo.h:199
static float blo_hd_run_lin(blo_h_osc *const this)
Definition blo.h:179
static void blo_hs_set_freq(blo_h_osc *this, const float f)
Definition blo.h:95
struct huft * t
Definition inflate.c:943
unsigned f
Definition inflate.c:1572
#define LIN_INTERP(f, a, b)
Definition ladspa-util.h:133
#define f_abs(j)
Definition ladspa-util.h:185
static float cube_interp(const float fr, const float inm1, const float in, const float inp1, const float inp2)
Definition ladspa-util.h:136
static int f_round(float f)
Definition ladspa-util.h:175
Definition blo.h:64
float sample_rate
Definition blo.h:66
int ph_mask
Definition blo.h:72
blo_fixp ph
Definition blo.h:69
int table_mask
Definition blo.h:73
int topbit
Definition blo.h:75
unsigned int wave
Definition blo.h:68
float * table
Definition blo.h:76
int table_size
Definition blo.h:74
blo_fixp om
Definition blo.h:70
blo_h_tables * tables
Definition blo.h:65
float * table_b
Definition blo.h:77
float ph_coef
Definition blo.h:71
float nyquist
Definition blo.h:67
float xfade
Definition blo.h:78
Definition blo.h:47
size_t alloc_size
Definition blo.h:50
int table_size
Definition blo.h:51
float * h_tables[BLO_N_WAVES][BLO_N_HARMONICS]
Definition blo.h:48
int store_type
Definition blo.h:53
float * alloc_space
Definition blo.h:49
int table_mask
Definition blo.h:52
Definition blo.h:56
unsigned short fr
Definition blo.h:59
short in
Definition blo.h:60
int all
Definition blo.h:57
typedef int(UZ_EXP MsgFn)()