In case of running out of prefetch pipes, truncate the sample to the preloaded part.
[calfbox.git] / sampler.h
blob8cdfe0b7a6e79d991d4d445c6b35553355fd9dab
1 /*
2 Calf Box, an open source musical instrument.
3 Copyright (C) 2010-2013 Krzysztof Foltman
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #ifndef CBOX_SAMPLER_H
20 #define CBOX_SAMPLER_H
22 #include "biquad-float.h"
23 #include "envelope.h"
24 #include "module.h"
25 #include "onepole-float.h"
26 #include "prefetch_pipe.h"
27 #include "sampler_layer.h"
28 #include "sampler_prg.h"
29 #include "wavebank.h"
30 #include <stdint.h>
32 #define MAX_SAMPLER_VOICES 128
34 #define CBOX_SAMPLER_ERROR cbox_sampler_error_quark()
36 enum CboxSamplerError
38 CBOX_SAMPLER_ERROR_FAILED,
39 CBOX_SAMPLER_ERROR_INVALID_LAYER,
40 CBOX_SAMPLER_ERROR_INVALID_WAVEFORM,
41 CBOX_SAMPLER_ERROR_NO_PROGRAMS
44 struct sampler_noteinitfunc;
45 struct sampler_voice;
47 #define GET_RT_FROM_sampler_channel(channel) ((channel)->module->module.rt)
49 struct sampler_channel
51 struct sampler_module *module;
52 int pitchwheel;
53 uint32_t switchmask[4];
54 uint32_t sustainmask[4];
55 uint32_t sostenutomask[4];
56 int previous_note;
57 uint8_t cc[smsrc_perchan_count];
58 struct sampler_program *program;
59 struct sampler_voice *voices_running;
60 int active_voices;
61 uint8_t prev_note_velocity[128];
62 uint32_t prev_note_start_time[128];
63 int channel_volume_cc, channel_pan_cc;
64 int output_shift;
67 struct sampler_lfo
69 uint32_t phase, delta;
70 uint32_t age, delay, fade;
73 struct sampler_gen
75 enum sampler_player_type mode;
76 int16_t *sample_data;
77 int16_t *scratch;
79 uint64_t bigpos, bigdelta;
80 uint32_t loop_start, loop_end;
81 uint32_t cur_sample_end;
82 float lgain, rgain;
83 float last_lgain, last_rgain;
85 // In-memory mode only
86 uint32_t loop_overlap;
87 float loop_overlap_step;
88 uint32_t play_count, loop_count;
89 int16_t scratch_bandlimited[2 * MAX_INTERPOLATION_ORDER * 2];
91 // Streaming mode only
92 int16_t *streaming_buffer;
93 uint32_t consumed, consumed_credit, streaming_buffer_frames;
94 gboolean prefetch_only_loop, in_streaming_buffer;
97 struct sampler_voice
99 struct sampler_voice *prev, *next;
100 struct sampler_layer_data *layer;
101 // Note: may be NULL when program is being deleted
102 struct sampler_program *program;
103 struct cbox_waveform *last_waveform;
104 struct sampler_gen gen;
105 struct cbox_prefetch_pipe *current_pipe;
106 int note;
107 int vel;
108 int released, released_with_sustain, released_with_sostenuto, captured_sostenuto;
109 int off_by;
110 int delay;
111 int age;
112 float pitch_shift;
113 float cutoff_shift;
114 float gain_shift, gain_fromvel;
115 struct cbox_biquadf_state filter_left, filter_right;
116 struct cbox_biquadf_state filter_left2, filter_right2;
117 struct cbox_biquadf_coeffs filter_coeffs, filter_coeffs_extra;
118 struct cbox_onepolef_state onepole_left, onepole_right;
119 struct cbox_onepolef_coeffs onepole_coeffs;
120 struct sampler_channel *channel;
121 struct cbox_envelope amp_env, filter_env, pitch_env;
122 struct sampler_lfo amp_lfo, filter_lfo, pitch_lfo;
123 enum sampler_loop_mode loop_mode;
124 int output_pair_no;
125 int send1bus, send2bus;
126 float send1gain, send2gain;
127 int serial_no;
128 struct cbox_envelope_shape dyn_envs[3]; // amp, filter, pitch
129 struct cbox_biquadf_state eq_left[3], eq_right[3];
130 struct cbox_biquadf_coeffs eq_coeffs[3];
131 gboolean layer_changed;
132 int last_level;
133 uint64_t last_level_min_rate;
134 uint32_t last_eq_bitmask;
135 float reloffset;
136 uint32_t offset;
139 struct sampler_module
141 struct cbox_module module;
143 struct sampler_voice *voices_free, voices_all[MAX_SAMPLER_VOICES];
144 struct sampler_channel channels[16];
145 struct sampler_program **programs;
146 int program_count;
147 int active_voices, max_voices;
148 int serial_no;
149 int output_pairs, aux_pairs;
150 uint32_t current_time;
151 gboolean deleting;
152 int disable_mixer_controls;
153 struct cbox_prefetch_stack *pipe_stack;
154 struct cbox_sincos sincos[12800];
157 #define MAX_RELEASED_GROUPS 4
159 extern GQuark cbox_sampler_error_quark(void);
161 extern void sampler_register_program(struct sampler_module *m, struct sampler_program *pgm);
162 extern gboolean sampler_select_program(struct sampler_module *m, int channel, const gchar *preset, GError **error);
163 extern void sampler_unselect_program(struct sampler_module *m, struct sampler_program *prg);
165 extern void sampler_channel_init(struct sampler_channel *c, struct sampler_module *m);
166 // This function may only be called from RT thread!
167 extern void sampler_channel_set_program_RT(struct sampler_channel *c, struct sampler_program *prg);
168 // ... and this one is RT-safe
169 extern void sampler_channel_set_program(struct sampler_channel *c, struct sampler_program *prg);
170 extern void sampler_channel_start_note(struct sampler_channel *c, int note, int vel, gboolean is_release_trigger);
171 extern void sampler_channel_stop_note(struct sampler_channel *c, int note, int vel, gboolean is_polyaft);
172 extern void sampler_channel_program_change(struct sampler_channel *c, int program);
173 extern void sampler_channel_stop_sustained(struct sampler_channel *c);
174 extern void sampler_channel_stop_sostenuto(struct sampler_channel *c);
175 extern void sampler_channel_capture_sostenuto(struct sampler_channel *c);
176 extern void sampler_channel_release_groups(struct sampler_channel *c, int note, int exgroups[MAX_RELEASED_GROUPS], int exgroupcount);
177 extern void sampler_channel_stop_all(struct sampler_channel *c);
178 extern void sampler_channel_process_cc(struct sampler_channel *c, int cc, int val);
180 extern void sampler_voice_start(struct sampler_voice *v, struct sampler_channel *c, struct sampler_layer_data *l, int note, int vel, int *exgroups, int *pexgroupcount);
181 extern void sampler_voice_release(struct sampler_voice *v, gboolean is_polyaft);
182 extern void sampler_voice_process(struct sampler_voice *v, struct sampler_module *m, cbox_sample_t **outputs);
183 extern void sampler_voice_link(struct sampler_voice **pv, struct sampler_voice *v);
184 extern void sampler_voice_unlink(struct sampler_voice **pv, struct sampler_voice *v);
185 extern void sampler_voice_inactivate(struct sampler_voice *v, gboolean expect_active);
186 extern void sampler_voice_update_params_from_layer(struct sampler_voice *v);
188 extern float sampler_sine_wave[2049];
190 static inline int sampler_channel_addcc(struct sampler_channel *c, int cc_no)
192 return (((int)c->cc[cc_no]) << 7) + c->cc[cc_no + 32];
195 #define FOREACH_VOICE(var, p) \
196 for (struct sampler_voice *p = (var), *p##_next = NULL; p && (p##_next = p->next, TRUE); p = p##_next)
199 #endif