LMMS
Loading...
Searching...
No Matches
wavetable.h
Go to the documentation of this file.
1#ifndef __CALF_WAVETABLE_H
2#define __CALF_WAVETABLE_H
3
4#include <assert.h>
5#include "biquad.h"
6#include "onepole.h"
7#include "audio_fx.h"
8#include "inertia.h"
9#include "osc.h"
10#include "synth.h"
11#include "envelope.h"
12#include "modmatrix.h"
13
14namespace calf_plugins {
15
16#define WAVETABLE_WAVE_BITS 8
17
18class wavetable_audio_module;
19
21{
22 enum { SIZE = 1 << 8, MASK = SIZE - 1, SCALE = 1 << (32 - 8) };
23 int16_t (*tables)[256];
24 inline float get(uint16_t slice)
25 {
26 float fracslice = (slice & 255) * (1.0 / 256.0);
27 slice = slice >> 8;
28 int16_t *waveform = tables[slice];
29 int16_t *waveform2 = tables[slice + 1];
30 float value1 = 0.f, value2 = 0.f;
31 uint32_t cphase = phase, cphasedelta = phasedelta >> 3;
32 for (int j = 0; j < 8; j++)
33 {
34 uint32_t wpos = cphase >> (32 - 8);
35 uint32_t wpos2 = (wpos + 1) & MASK;
36 float frac = (cphase & (SCALE - 1)) * (1.0f / SCALE);
37 value1 += dsp::lerp((float)waveform[wpos], (float)waveform[wpos2], frac);
38 value2 += dsp::lerp((float)waveform2[wpos], (float)waveform2[wpos2], frac);
39 cphase += cphasedelta;
40 }
42 return dsp::lerp(value1, value2, fracslice) * (1.0 / 8.0) * (1.0 / 32768.0);;
43 }
44};
45
47{
48public:
51protected:
52 int note;
54 float **params;
59 float velocity;
69public:
71 void set_params_ptr(wavetable_audio_module *_parent, int _srate);
72 void reset();
73 void note_on(int note, int vel);
74 void note_off(int /* vel */);
76 void steal();
77 void render_block(int current_snapshot);
78 const int16_t *get_last_table(int osc) const;
79 virtual int get_current_note() {
80 return note;
81 }
82 virtual bool get_active() {
83 // printf("note %d getactive %d use_percussion %d pamp active %d\n", note, amp.get_active(), use_percussion(), pamp.get_active());
84 return (note != -1) && (amp.get_active()) && !envs[0].stopped();
85 }
86 inline void calc_derived_dests(float env0) {
87 float cv = dsp::clip<float>(0.5 + 0.01 * moddest[wavetable_metadata::moddest_oscmix], 0.f, 1.f);
88 float overall = *params[wavetable_metadata::par_eg1toamp] > 0 ? env0 : 1.0;
89 cur_oscamp[0] = (1 - cv) * *params[wavetable_metadata::par_o1level] * overall;
91 }
92};
93
94class wavetable_audio_module: public audio_module<wavetable_metadata>, public dsp::basic_synth, public dsp::block_allvoices_base<wavetable_voice>, public line_graph_iface, public mod_matrix_impl
95{
96public:
101
102protected:
105
106public:
107 int16_t tables[wt_count][129][256]; // one dummy level for interpolation
117
120 };
121
123
124public:
126
129 v->set_params_ptr(this, sample_rate);
130 return v;
131 }
132
133 uint32_t get_crate() const { return crate; }
134
136 uint32_t process(uint32_t offset, uint32_t nsamples, uint32_t inputs_mask, uint32_t outputs_mask) {
137 float *o[2] = { outs[0] + offset, outs[1] + offset };
138 if (panic_flag)
139 {
140 control_change(120, 0); // stop all sounds
141 control_change(121, 0); // reset all controllers
142 panic_flag = false;
143 }
144
145 fill_snapshots(nsamples);
146 float buf[MAX_SAMPLE_RUN][2];
147 dsp::zero(&buf[0][0], 2 * nsamples);
148 basic_synth::render_to(buf, nsamples);
149 if (!active_voices.empty())
151 float gain = 1.0f;
152 for (uint32_t i=0; i<nsamples; i++) {
153 o[0][i] = gain*buf[i][0];
154 o[1][i] = gain*buf[i][1];
155 }
156 return 3;
157 }
158
159 void make_snapshot(int index)
160 {
161 if (index)
162 control_snapshots[index].pitchbend = inertia_pitchbend.get();
163 else
164 control_snapshots[0].pitchbend = inertia_pitchbend.get_last();
165 }
166
168 setup(sr);
170 inertia_pitchbend.ramp.set_length(crate / 30); // 1/30s
171 inertia_pressure.ramp.set_length(crate / 30); // 1/30s - XXXKF monosynth needs that too
172 }
173 virtual void note_on(int channel, int note, int velocity) { if (*params[par_midi] && channel != *params[par_midi]) return; dsp::basic_synth::note_on(note, velocity); }
174 virtual void note_off(int channel, int note, int velocity) { if (*params[par_midi] && channel != *params[par_midi]) return; dsp::basic_synth::note_off(note, velocity); }
175 virtual void control_change(int channel, int controller, int value);
177 virtual void channel_pressure(int channel, int value);
179 virtual void pitch_bend(int channel, int value)
180 {
181 inertia_pitchbend.set_inertia((value * *params[par_pwhlrange]) * (1.0 / 8192.0));
182 }
183 bool get_graph(int index, int subindex, int phase, float *data, int points, cairo_iface *context, int *mode) const;
184 bool get_layers(int index, int generation, unsigned int &layers) const { layers = LG_REALTIME_GRAPH; return true; }
186 virtual char *configure(const char *key, const char *value);
188
189};
190
191
192};
193
194#endif
Controller controller
Definition main.C:5
static long waveform[8]
Definition adlibemu.c:197
float * params[wavetable_metadata::param_count]
Definition giface.h:527
float * outs[(wavetable_metadata::out_count !=0) ? wavetable_metadata::out_count :1]
Definition giface.h:526
mod_matrix_impl(dsp::modulation_entry *_matrix, calf_plugins::mod_matrix_metadata *_metadata)
Definition modmatrix.cpp:31
void send_configures(send_configure_iface *)
Definition modmatrix.cpp:113
Definition wavetable.h:95
void set_sample_rate(uint32_t sr)
Set sample rate for the plugin.
Definition wavetable.h:167
uint32_t process(uint32_t offset, uint32_t nsamples, uint32_t inputs_mask, uint32_t outputs_mask)
process function copied from Organ (will probably need some adjustments as well as implementing the p...
Definition wavetable.h:136
float modwheel_value
Unsmoothed mod wheel value.
Definition wavetable.h:115
dsp::inertia< dsp::linear_ramp > inertia_pitchbend
Smoothed pitch bend value.
Definition wavetable.h:111
dsp::modulation_entry mod_matrix_data[mod_matrix_slots]
Rows of the modulation matrix.
Definition wavetable.h:109
virtual void note_off(int channel, int note, int velocity)
Handle MIDI Note Off.
Definition wavetable.h:174
bool get_graph(int index, int subindex, int phase, float *data, int points, cairo_iface *context, int *mode) const
ControlSnapshot control_snapshots[MaxSnapshots]
Definition wavetable.h:122
int16_t tables[wt_count][129][256]
Definition wavetable.h:107
virtual char * configure(const char *key, const char *value)
DSSI configure call.
wavetable_voice * last_voice
Definition wavetable.h:116
virtual void pitch_bend(int channel, int value)
Handle pitch bend message.
Definition wavetable.h:179
virtual void channel_pressure(int channel, int value)
Handle MIDI Channel Pressure.
bool panic_flag
Definition wavetable.h:104
virtual const dsp::modulation_entry * get_default_mod_matrix_value(int row) const
virtual void control_change(int channel, int controller, int value)
Handle MIDI Control Change.
dsp::inertia< dsp::linear_ramp > inertia_pressure
Smoothed channel pressure value.
Definition wavetable.h:113
virtual void send_configures(send_configure_iface *sci)
Send all understood configure vars (none by default).
Definition wavetable.h:185
uint32_t get_crate() const
Definition wavetable.h:133
void make_snapshot(int index)
Definition wavetable.h:159
bool get_layers(int index, int generation, unsigned int &layers) const
Definition wavetable.h:184
virtual void note_on(int channel, int note, int velocity)
Handle MIDI Note On.
Definition wavetable.h:173
uint32_t crate
Definition wavetable.h:103
dsp::voice * alloc_voice()
Definition wavetable.h:127
Definition wavetable.h:47
virtual bool get_active()
check if voice can be removed from active voice list
Definition wavetable.h:82
void channel_pressure(int value)
virtual int get_current_note()
return the note used by this voice
Definition wavetable.h:79
const int16_t * get_last_table(int osc) const
dsp::triangle_lfo lfo2
Definition wavetable.h:68
void note_on(int note, int vel)
a note was pressed
void steal()
very fast note off
void set_params_ptr(wavetable_audio_module *_parent, int _srate)
float ** params
Definition wavetable.h:54
dsp::triangle_lfo lfo1
Definition wavetable.h:68
float last_oscshift[OscCount]
Last oscillator shift (wavetable index) of each oscillator.
Definition wavetable.h:63
float velocity
Current MIDI velocity.
Definition wavetable.h:59
wavetable_audio_module * parent
Definition wavetable.h:53
void note_off(int)
a note was released
float cur_oscamp[OscCount]
Current osc amplitude.
Definition wavetable.h:67
void reset()
reset voice to default state (used when a voice is to be reused)
void render_block(int current_snapshot)
int note
Definition wavetable.h:52
float last_oscamp[OscCount]
Last oscillator amplitude of each oscillator.
Definition wavetable.h:65
float moddest[wavetable_metadata::moddest_count]
Current calculated mod matrix outputs.
Definition wavetable.h:61
@ MaxSampleRun
Definition wavetable.h:49
@ OscCount
Definition wavetable.h:49
@ BlockSize
Definition wavetable.h:49
@ Channels
Definition wavetable.h:49
@ EnvCount
Definition wavetable.h:49
wavetable_oscillator oscs[OscCount]
Definition wavetable.h:56
dsp::decay amp
Definition wavetable.h:55
float output_buffer[BlockSize][Channels]
Definition wavetable.h:50
dsp::adsr envs[EnvCount]
Definition wavetable.h:57
void calc_derived_dests(float env0)
Definition wavetable.h:86
Definition envelope.h:33
Definition synth.h:135
void fill_snapshots(int nsamples)
Definition synth.h:140
Definition synth.h:161
Definition primitives.h:281
Definition inertia.h:108
Definition synth.h:105
register unsigned j
Definition inflate.c:1576
unsigned v[N_MAX]
Definition inflate.c:1584
register unsigned i
Definition inflate.c:1575
static PuglViewHint int value
Definition pugl.h:1708
JSAMPIMAGE data
Definition jpeglib.h:945
unsigned short uint16_t
Definition mid.cpp:99
unsigned int uint32_t
Definition mid.cpp:100
short int16_t
Definition mid.cpp:96
Definition benchmark.cpp:53
@ MAX_SAMPLE_RUN
Definition giface.h:38
@ LG_REALTIME_GRAPH
Definition giface.h:181
T lerp(T v1, T v2, U mix)
Definition primitives.h:267
void zero(float &v)
Set a float to zero.
Definition primitives.h:41
T clip(T value, T min, T max)
Clip a value to [min, max].
Definition primitives.h:231
png_structrp int mode
Definition png.h:1139
Definition giface.h:160
'provides live line graph values' interface
Definition giface.h:198
'may receive configure variables' interface
Definition giface.h:291
@ wt_count
Definition metadata.h:996
@ par_pwhlrange
Definition metadata.h:1030
@ par_o2level
Definition metadata.h:1026
@ par_o1level
Definition metadata.h:1025
@ par_eg1toamp
Definition metadata.h:1031
@ par_midi
Definition metadata.h:1034
@ moddest_oscmix
Definition metadata.h:1014
@ moddest_count
Definition metadata.h:1022
@ mod_matrix_slots
Definition metadata.h:1037
Definition wavetable.h:21
int16_t(* tables)[256]
Definition wavetable.h:23
@ SCALE
Definition wavetable.h:22
@ SIZE
Definition wavetable.h:22
@ MASK
Definition wavetable.h:22
float get(uint16_t slice)
Definition wavetable.h:24
Definition synth.h:278
voice_array active_voices
Voices currently playing.
Definition synth.h:290
int sample_rate
Current sample rate.
Definition synth.h:282
virtual void control_change(int ctl, int val)
Definition synth.cpp:156
virtual void setup(int sr)
Definition synth.h:302
virtual void note_off(int note, int vel)
Definition synth.cpp:123
virtual void note_on(int note, int vel)
Definition synth.cpp:102
virtual void pitch_bend(int amt)
amt = -8192 to 8191
Definition synth.h:317
Single entry in modulation matrix.
Definition modmatrix.h:31
Definition osc.h:36
uint32_t phasedelta
Per-sample phase delta (phase increment), equal to 2^32*freq/sr.
Definition osc.h:40
uint32_t phase
Phase (from 0 to 0xFFFFFFFF).
Definition osc.h:38
Definition osc.h:303
ZCONST char * key
Definition crypt.c:587