1 /* -*- Mode: C++ ; c-basic-offset: 2 -*- */
2 /*****************************************************************************
4 * Copyright (C) 2006,2007,2008,2009 Nedko Arnaudov <nedko@arnaudov.name>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License
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, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *****************************************************************************/
28 #include "resonance.h"
30 #include "oscillator.h"
31 #include "envelope_parameters.h"
33 #include "lfo_parameters.h"
35 #include "filter_parameters.h"
36 #include "filter_base.h"
37 #include "analog_filter.h"
38 #include "sv_filter.h"
39 #include "formant_filter.h"
41 #include "portamento.h"
42 #include "addsynth_internal.h"
46 #define LOG_LEVEL LOG_LEVEL_ERROR
49 #define ZYN_DEFAULT_POLYPHONY 60
51 // (94.0 / 64.0 - 1) * 5.0 = 2.34375
52 #define ZYN_GLOBAL_FILTER_INITIAL_FREQUENCY 2.34375
54 // 40.0 / 127.0 = 0.31496062992125984
55 #define ZYN_GLOBAL_FILTER_INITIAL_Q 0.31496062992125984
59 int midinote
; // MIDI note, -1 when note "channel" is not playing
60 zyn_addnote_handle note_handle
;
66 unsigned int voices_count
,
67 zyn_addsynth_handle
* handle_ptr
)
69 struct zyn_addsynth
* zyn_addsynth_ptr
;
70 unsigned int note_index
;
71 unsigned int voice_index
;
73 // printf("zyn_addsynth_create\n");
74 zyn_addsynth_ptr
= (struct zyn_addsynth
*)malloc(sizeof(struct zyn_addsynth
));
75 if (zyn_addsynth_ptr
== NULL
)
80 zyn_addsynth_ptr
->sample_rate
= sample_rate
;
82 zyn_addsynth_ptr
->temporary_samples_ptr
= (zyn_sample_type
*)malloc(sizeof(zyn_sample_type
) * OSCIL_SIZE
);
83 zyn_fft_freqs_init(&zyn_addsynth_ptr
->oscillator_fft_frequencies
, OSCIL_SIZE
/ 2);
85 zyn_addsynth_ptr
->polyphony
= ZYN_DEFAULT_POLYPHONY
;
86 zyn_addsynth_ptr
->notes_array
= (struct note_channel
*)malloc(ZYN_DEFAULT_POLYPHONY
* sizeof(struct note_channel
));
88 zyn_addsynth_ptr
->all_sound_off
= false;
90 zyn_addsynth_ptr
->velsns
= 64;
91 zyn_addsynth_ptr
->fft
= zyn_fft_create(OSCIL_SIZE
);
93 // ADnoteParameters temp begin
95 zyn_addsynth_ptr
->m_frequency_envelope_params
.init_asr(0, false, 64, 50, 64, 60);
97 zyn_addsynth_ptr
->m_amplitude_envelope_params
.init_adsr(64, true, 0, 40, 127, 25, false);
99 zyn_addsynth_ptr
->filter_type
= ZYN_FILTER_TYPE_ANALOG
;
100 zyn_addsynth_ptr
->m_filter_params
.init(sample_rate
, ZYN_FILTER_ANALOG_TYPE_LPF2
, 94, 40);
101 if (!zyn_filter_sv_create(sample_rate
, ZYN_GLOBAL_FILTER_INITIAL_FREQUENCY
, ZYN_GLOBAL_FILTER_INITIAL_Q
, &zyn_addsynth_ptr
->filter_sv
))
103 goto fail_free_synth
;
106 zyn_addsynth_ptr
->m_filter_envelope_params
.init_adsr_filter(0, true, 64, 40, 64, 70, 60, 64);
108 zyn_resonance_init(&zyn_addsynth_ptr
->resonance
);
110 zyn_addsynth_ptr
->voices_count
= voices_count
;
111 zyn_addsynth_ptr
->voices_params_ptr
= (struct zyn_addnote_voice_parameters
*)malloc(sizeof(struct zyn_addnote_voice_parameters
) * voices_count
);
113 for (voice_index
= 0 ; voice_index
< voices_count
; voice_index
++)
116 &zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].oscillator
,
118 zyn_addsynth_ptr
->fft
,
119 &zyn_addsynth_ptr
->resonance
,
120 zyn_addsynth_ptr
->temporary_samples_ptr
,
121 &zyn_addsynth_ptr
->oscillator_fft_frequencies
);
124 &zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].modulator_oscillator
,
126 zyn_addsynth_ptr
->fft
,
128 zyn_addsynth_ptr
->temporary_samples_ptr
,
129 &zyn_addsynth_ptr
->oscillator_fft_frequencies
);
131 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].m_amplitude_envelope_params
.init_adsr(64, true, 0, 100, 127, 100, false);
133 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].amplitude_lfo_params
.frequency
= 90.0 / 127.0;
134 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].amplitude_lfo_params
.depth
= 32.0 / 127.0;
135 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].amplitude_lfo_params
.random_start_phase
= false;
136 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].amplitude_lfo_params
.start_phase
= 0.5;
137 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].amplitude_lfo_params
.depth_randomness_enabled
= false;
138 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].amplitude_lfo_params
.depth
= 0;
139 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].amplitude_lfo_params
.frequency_randomness_enabled
= false;
140 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].amplitude_lfo_params
.frequency_randomness
= 0;
141 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].amplitude_lfo_params
.delay
= 30.0 / 127.0 * 4.0;
142 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].amplitude_lfo_params
.stretch
= 0;
143 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].amplitude_lfo_params
.shape
= ZYN_LFO_SHAPE_TYPE_SINE
;
145 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].m_frequency_envelope_params
.init_asr(0, false, 30, 40, 64, 60);
147 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].frequency_lfo_params
.frequency
= 50.0 / 127.0;
148 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].frequency_lfo_params
.depth
= 40.0 / 127.0;
149 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].frequency_lfo_params
.random_start_phase
= false;
150 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].frequency_lfo_params
.start_phase
= 0;
151 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].frequency_lfo_params
.depth_randomness_enabled
= false;
152 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].frequency_lfo_params
.depth
= 0;
153 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].frequency_lfo_params
.frequency_randomness_enabled
= false;
154 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].frequency_lfo_params
.frequency_randomness
= 0;
155 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].frequency_lfo_params
.delay
= 0;
156 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].frequency_lfo_params
.stretch
= 0;
157 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].frequency_lfo_params
.shape
= ZYN_LFO_SHAPE_TYPE_SINE
;
159 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].m_filter_params
.init(sample_rate
, ZYN_FILTER_ANALOG_TYPE_LPF2
, 50, 60);
160 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].m_filter_envelope_params
.init_adsr_filter(0, false, 90, 70, 40, 70, 10, 40);
162 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].filter_lfo_params
.frequency
= 50.0 / 127.0;
163 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].filter_lfo_params
.depth
= 20.0 / 127.0;
164 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].filter_lfo_params
.random_start_phase
= false;
165 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].filter_lfo_params
.start_phase
= 0.5;
166 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].filter_lfo_params
.depth_randomness_enabled
= false;
167 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].filter_lfo_params
.depth
= 0;
168 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].filter_lfo_params
.frequency_randomness_enabled
= false;
169 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].filter_lfo_params
.frequency_randomness
= 0;
170 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].filter_lfo_params
.delay
= 0;
171 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].filter_lfo_params
.stretch
= 0;
172 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].filter_lfo_params
.shape
= ZYN_LFO_SHAPE_TYPE_SINE
;
174 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].m_fm_frequency_envelope_params
.init_asr(0, false, 20, 90, 40, 80);
175 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].m_fm_amplitude_envelope_params
.init_adsr(64, true, 80, 90, 127, 100, false);
178 /* Frequency Global Parameters */
179 // zyn_addsynth_ptr->GlobalPar.stereo = true; // Stereo
180 zyn_addsynth_ptr
->detune
.fine
= 0.0;
181 zyn_addsynth_ptr
->detune
.coarse
= 0;
182 zyn_addsynth_ptr
->detune
.octave
= 0;
183 zyn_addsynth_ptr
->detune
.type
= ZYN_DETUNE_TYPE_L35CENTS
;
184 zyn_addsynth_ptr
->detune_bandwidth
= 0.0;
186 /* Amplitude Global Parameters */
187 zyn_addsynth_ptr
->PVolume
=90;
188 zyn_addsynth_ptr
->PAmpVelocityScaleFunction
=64;
189 zyn_addsynth_ptr
->PPunchStrength
=0;
190 zyn_addsynth_ptr
->PPunchTime
=60;
191 zyn_addsynth_ptr
->PPunchStretch
=64;
192 zyn_addsynth_ptr
->PPunchVelocitySensing
=72;
194 /* Filter Global Parameters*/
195 zyn_addsynth_ptr
->m_filter_velocity_sensing_amount
= 0.5;
196 zyn_addsynth_ptr
->m_filter_velocity_scale_function
= 0;
197 zyn_addsynth_ptr
->m_filter_params
.defaults();
199 for (voice_index
= 0 ; voice_index
< voices_count
; voice_index
++)
201 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].enabled
= false;
202 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].white_noise
= false;
203 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].fixed_detune
.mode
= ZYN_DETUNE_MODE_NORMAL
;
204 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].fixed_detune
.equal_temperate
= 0;
205 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].resonance
= true;
206 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].Pfilterbypass
=0;
207 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].Pextoscil
=-1;
208 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].PextFMoscil
=-1;
209 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].Poscilphase
=64;
210 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].PFMoscilphase
=64;
211 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].PDelay
=0;
212 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].PVolume
=100;
213 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].PVolumeminus
=0;
214 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].PPanning
=64;//center
215 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].detune
.fine
= 0.0;
216 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].detune
.coarse
= 0;
217 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].detune
.octave
= 0;
218 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].detune
.type
= ZYN_DETUNE_TYPE_GLOBAL
;
219 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].PFreqLfoEnabled
=0;
220 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].PFreqEnvelopeEnabled
=0;
221 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].PAmpEnvelopeEnabled
=0;
222 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].PAmpLfoEnabled
=0;
223 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].PAmpVelocityScaleFunction
=127;
224 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].PFilterEnabled
=0;
225 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].PFilterEnvelopeEnabled
=0;
226 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].PFilterLfoEnabled
=0;
227 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].fm_type
= ZYN_FM_TYPE_NONE
;
229 //I use the internal oscillator (-1)
230 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].PFMVoice
=-1;
232 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].PFMVolume
=90;
233 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].PFMVolumeDamp
=64;
234 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].fm_detune
.fine
= 0.0;
235 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].fm_detune
.coarse
= 0;
236 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].fm_detune
.octave
= 0;
237 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].fm_detune
.type
= ZYN_DETUNE_TYPE_GLOBAL
;
238 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].PFMFreqEnvelopeEnabled
=0;
239 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].PFMAmpEnvelopeEnabled
=0;
240 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].PFMVelocityScaleFunction
=64;
242 zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].m_filter_params
.defaults();
245 zyn_addsynth_ptr
->voices_params_ptr
[0].enabled
= true;
247 // ADnoteParameters temp end
249 zyn_addsynth_ptr
->bandwidth_depth
= 64;
250 zyn_addsynth_ptr
->bandwidth_exponential
= false;
251 zyn_addsynth_ptr
->modwheel_depth
= 80;
252 zyn_addsynth_ptr
->modwheel_exponential
= false;
254 zyn_addsynth_set_bandwidth(zyn_addsynth_ptr
, 64);
255 zyn_addsynth_set_modwheel(zyn_addsynth_ptr
, 64);
257 zyn_portamento_init(&zyn_addsynth_ptr
->portamento
);
259 zyn_addsynth_ptr
->pitch_bend_range
= 200.0; // 200 cents = 2 halftones
260 zyn_addsynth_ptr
->pitch_bend
= 0; // center
261 ZYN_UPDATE_PITCH_BEND(zyn_addsynth_ptr
);
263 zyn_addsynth_ptr
->oldfreq
= -1.0;
265 zyn_addsynth_ptr
->random_panorama
= false;
266 zyn_addsynth_ptr
->panorama
= 0.0;
268 zyn_addsynth_ptr
->stereo
= true;
270 zyn_addsynth_ptr
->random_grouping
= false;
272 zyn_addsynth_ptr
->amplitude_lfo_params
.frequency
= 80.0 / 127.0;
273 zyn_addsynth_ptr
->amplitude_lfo_params
.depth
= 0;
274 zyn_addsynth_ptr
->amplitude_lfo_params
.random_start_phase
= false;
275 zyn_addsynth_ptr
->amplitude_lfo_params
.start_phase
= 0.5;
276 zyn_addsynth_ptr
->amplitude_lfo_params
.depth_randomness_enabled
= false;
277 zyn_addsynth_ptr
->amplitude_lfo_params
.depth_randomness
= 0.5;
278 zyn_addsynth_ptr
->amplitude_lfo_params
.frequency_randomness_enabled
= false;
279 zyn_addsynth_ptr
->amplitude_lfo_params
.frequency_randomness
= 0.5;
280 zyn_addsynth_ptr
->amplitude_lfo_params
.delay
= 0;
281 zyn_addsynth_ptr
->amplitude_lfo_params
.stretch
= 0;
282 zyn_addsynth_ptr
->amplitude_lfo_params
.shape
= ZYN_LFO_SHAPE_TYPE_SINE
;
284 zyn_addsynth_ptr
->filter_lfo_params
.frequency
= 80.0 / 127.0;
285 zyn_addsynth_ptr
->filter_lfo_params
.depth
= 0;
286 zyn_addsynth_ptr
->filter_lfo_params
.random_start_phase
= false;
287 zyn_addsynth_ptr
->filter_lfo_params
.start_phase
= 0.5;
288 zyn_addsynth_ptr
->filter_lfo_params
.depth_randomness_enabled
= false;
289 zyn_addsynth_ptr
->filter_lfo_params
.depth_randomness
= 0.5;
290 zyn_addsynth_ptr
->filter_lfo_params
.frequency_randomness_enabled
= false;
291 zyn_addsynth_ptr
->filter_lfo_params
.frequency_randomness
= 0.5;
292 zyn_addsynth_ptr
->filter_lfo_params
.delay
= 0;
293 zyn_addsynth_ptr
->filter_lfo_params
.stretch
= 0;
294 zyn_addsynth_ptr
->filter_lfo_params
.shape
= ZYN_LFO_SHAPE_TYPE_SINE
;
296 zyn_addsynth_ptr
->frequency_lfo_params
.frequency
= 70.0 / 127.0;
297 zyn_addsynth_ptr
->frequency_lfo_params
.depth
= 0;
298 zyn_addsynth_ptr
->frequency_lfo_params
.random_start_phase
= false;
299 zyn_addsynth_ptr
->frequency_lfo_params
.start_phase
= 0.5;
300 zyn_addsynth_ptr
->frequency_lfo_params
.depth_randomness_enabled
= false;
301 zyn_addsynth_ptr
->frequency_lfo_params
.depth_randomness
= 0.5;
302 zyn_addsynth_ptr
->frequency_lfo_params
.frequency_randomness_enabled
= false;
303 zyn_addsynth_ptr
->frequency_lfo_params
.frequency_randomness
= 0.5;
304 zyn_addsynth_ptr
->frequency_lfo_params
.delay
= 0;
305 zyn_addsynth_ptr
->frequency_lfo_params
.stretch
= 0;
306 zyn_addsynth_ptr
->frequency_lfo_params
.shape
= ZYN_LFO_SHAPE_TYPE_SINE
;
308 for (note_index
= 0 ; note_index
< ZYN_DEFAULT_POLYPHONY
; note_index
++)
310 if (!zyn_addnote_create(
312 &zyn_addsynth_ptr
->notes_array
[note_index
].note_handle
))
316 zyn_addsynth_ptr
->notes_array
[note_index
].midinote
= -1;
319 // init global components
321 zyn_addsynth_component_init_amp_globals(
322 zyn_addsynth_ptr
->global_components
+ ZYNADD_COMPONENT_AMP_GLOBALS
,
325 zyn_addsynth_component_init_detune(
326 zyn_addsynth_ptr
->global_components
+ ZYNADD_COMPONENT_DETUNE
,
327 &zyn_addsynth_ptr
->detune
);
329 zyn_addsynth_component_init_amp_envelope(
330 zyn_addsynth_ptr
->global_components
+ ZYNADD_COMPONENT_AMP_ENV
,
331 &zyn_addsynth_ptr
->m_amplitude_envelope_params
);
333 zyn_addsynth_component_init_lfo(
334 zyn_addsynth_ptr
->global_components
+ ZYNADD_COMPONENT_AMP_LFO
,
335 &zyn_addsynth_ptr
->amplitude_lfo_params
);
337 zyn_addsynth_component_init_filter_globals(
338 zyn_addsynth_ptr
->global_components
+ ZYNADD_COMPONENT_FILTER_GLOBALS
,
341 zyn_addsynth_component_init_filter_analog(
342 zyn_addsynth_ptr
->global_components
+ ZYNADD_COMPONENT_FILTER_ANALOG
,
345 zyn_addsynth_component_init_filter_formant(
346 zyn_addsynth_ptr
->global_components
+ ZYNADD_COMPONENT_FILTER_FORMANT
,
349 zyn_addsynth_component_init_filter_sv(
350 zyn_addsynth_ptr
->global_components
+ ZYNADD_COMPONENT_FILTER_SV
,
351 zyn_addsynth_ptr
->filter_sv
);
353 zyn_addsynth_component_init_filter_envelope(
354 zyn_addsynth_ptr
->global_components
+ ZYNADD_COMPONENT_FILTER_ENV
,
355 &zyn_addsynth_ptr
->m_filter_envelope_params
);
357 zyn_addsynth_component_init_lfo(
358 zyn_addsynth_ptr
->global_components
+ ZYNADD_COMPONENT_FILTER_LFO
,
359 &zyn_addsynth_ptr
->filter_lfo_params
);
361 zyn_addsynth_component_init_frequency_globals(
362 zyn_addsynth_ptr
->global_components
+ ZYNADD_COMPONENT_FREQUENCY_GLOBALS
);
364 zyn_addsynth_component_init_frequency_envelope(
365 zyn_addsynth_ptr
->global_components
+ ZYNADD_COMPONENT_FREQUENCY_ENV
,
366 &zyn_addsynth_ptr
->m_frequency_envelope_params
);
368 zyn_addsynth_component_init_lfo(
369 zyn_addsynth_ptr
->global_components
+ ZYNADD_COMPONENT_FREQUENCY_LFO
,
370 &zyn_addsynth_ptr
->frequency_lfo_params
);
372 zyn_addsynth_component_init_portamento(
373 zyn_addsynth_ptr
->global_components
+ ZYNADD_COMPONENT_PORTAMENTO
,
374 &zyn_addsynth_ptr
->portamento
);
376 // init voices components
378 zyn_addsynth_ptr
->voices_components
=
379 (struct zyn_component_descriptor
*)malloc(
380 sizeof(struct zyn_component_descriptor
) * voices_count
* ZYNADD_VOICE_COMPONENTS_COUNT
);
382 for (voice_index
= 0 ; voice_index
< voices_count
; voice_index
++)
384 zyn_addsynth_component_init_voice_globals(
385 zyn_addsynth_ptr
->voices_components
+ voice_index
* ZYNADD_VOICE_COMPONENTS_COUNT
+ ZYNADD_COMPONENT_VOICE_GLOBALS
,
386 zyn_addsynth_ptr
->voices_params_ptr
+ voice_index
);
388 zyn_addsynth_component_init_oscillator(
389 zyn_addsynth_ptr
->voices_components
+ voice_index
* ZYNADD_VOICE_COMPONENTS_COUNT
+ ZYNADD_COMPONENT_VOICE_OSCILLATOR
,
390 &zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].oscillator
);
392 zyn_addsynth_component_init_detune(
393 zyn_addsynth_ptr
->voices_components
+ voice_index
* ZYNADD_VOICE_COMPONENTS_COUNT
+ ZYNADD_COMPONENT_VOICE_DETUNE
,
394 &zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].detune
);
396 zyn_addsynth_component_init_fixed_detune(
397 zyn_addsynth_ptr
->voices_components
+ voice_index
* ZYNADD_VOICE_COMPONENTS_COUNT
+ ZYNADD_COMPONENT_VOICE_FIXED_DETUNE
,
398 &zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].fixed_detune
);
400 zyn_addsynth_component_init_detune(
401 zyn_addsynth_ptr
->voices_components
+ voice_index
* ZYNADD_VOICE_COMPONENTS_COUNT
+ ZYNADD_COMPONENT_VOICE_MODULATOR_DETUNE
,
402 &zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].fm_detune
);
405 *handle_ptr
= (zyn_addsynth_handle
)zyn_addsynth_ptr
;
407 // printf("zyn_addsynth_create(%08X)\n", (unsigned int)*handle_ptr);
417 #define zyn_addsynth_ptr ((struct zyn_addsynth *)handle)
420 zyn_addsynth_get_audio_output(
421 zyn_addsynth_handle handle
,
422 zyn_sample_type
* buffer_left
,
423 zyn_sample_type
* buffer_right
)
425 unsigned int note_index
;
426 zyn_sample_type note_buffer_left
[SOUND_BUFFER_SIZE
];
427 zyn_sample_type note_buffer_right
[SOUND_BUFFER_SIZE
];
430 silence_two_buffers(buffer_left
, buffer_right
, SOUND_BUFFER_SIZE
);
432 for (note_index
= 0 ; note_index
< zyn_addsynth_ptr
->polyphony
; note_index
++)
434 if (zyn_addsynth_ptr
->notes_array
[note_index
].midinote
!= -1)
436 //printf("mixing note channel %u\n", note_index);
438 note_active
= zyn_addnote_noteout(
439 zyn_addsynth_ptr
->notes_array
[note_index
].note_handle
,
452 zyn_addsynth_ptr
->notes_array
[note_index
].midinote
= -1;
457 if (zyn_addsynth_ptr
->all_sound_off
)
459 fadeout_two_buffers(buffer_left
, buffer_right
, SOUND_BUFFER_SIZE
);
461 for (note_index
= 0 ; note_index
< zyn_addsynth_ptr
->polyphony
; note_index
++)
463 if (zyn_addsynth_ptr
->notes_array
[note_index
].midinote
!= -1)
465 zyn_addnote_force_disable(zyn_addsynth_ptr
->notes_array
[note_index
].note_handle
);
466 zyn_addsynth_ptr
->notes_array
[note_index
].midinote
= -1;
470 zyn_addsynth_ptr
->all_sound_off
= false;
473 zyn_portamento_update(&zyn_addsynth_ptr
->portamento
);
477 zyn_addsynth_note_on(
478 zyn_addsynth_handle handle
,
480 unsigned int velocity
)
482 unsigned int note_index
;
485 zyn_sample_type notebasefreq
;
487 for (note_index
= 0 ; note_index
< zyn_addsynth_ptr
->polyphony
; note_index
++)
489 if (zyn_addsynth_ptr
->notes_array
[note_index
].midinote
== -1)
491 goto unused_note_channel_found
;
495 //printf("note on %u - ignored\n", note);
499 unused_note_channel_found
:
500 //printf("note on %u - channel %u\n", note, note_index);
503 vel
= VelF(velocity
/127.0, zyn_addsynth_ptr
->velsns
);
504 notebasefreq
= 440.0*pow(2.0,(note
-69.0)/12.0);
508 if (zyn_addsynth_ptr
->oldfreq
< 1.0) /* only when the first note is played */
510 zyn_addsynth_ptr
->oldfreq
= notebasefreq
;
513 bool portamento
= zyn_portamento_start(zyn_addsynth_ptr
->sample_rate
, &zyn_addsynth_ptr
->portamento
, zyn_addsynth_ptr
->oldfreq
, notebasefreq
);
515 zyn_addsynth_ptr
->oldfreq
= notebasefreq
;
517 zyn_addsynth_ptr
->notes_array
[note_index
].midinote
= note
;
520 zyn_addsynth_ptr
->notes_array
[note_index
].note_handle
,
521 zyn_addsynth_ptr
->random_panorama
? RND
: zyn_addsynth_ptr
->panorama
,
522 zyn_addsynth_ptr
->random_grouping
,
530 zyn_addsynth_note_off(
531 zyn_addsynth_handle handle
,
534 unsigned int note_index
;
536 //printf("note off %u\n", note);
538 for (note_index
= 0 ; note_index
< zyn_addsynth_ptr
->polyphony
; note_index
++)
540 if (zyn_addsynth_ptr
->notes_array
[note_index
].midinote
== (char)note
)
542 zyn_addnote_note_off(zyn_addsynth_ptr
->notes_array
[note_index
].note_handle
);
548 zyn_addsynth_all_notes_off(
549 zyn_addsynth_handle handle
)
551 unsigned int note_index
;
553 for (note_index
= 0 ; note_index
< zyn_addsynth_ptr
->polyphony
; note_index
++)
555 if (zyn_addsynth_ptr
->notes_array
[note_index
].midinote
!= -1)
557 zyn_addnote_note_off(zyn_addsynth_ptr
->notes_array
[note_index
].note_handle
);
563 zyn_addsynth_all_sound_off(
564 zyn_addsynth_handle handle
)
566 zyn_addsynth_ptr
->all_sound_off
= true;
570 zyn_addsynth_destroy(
571 zyn_addsynth_handle handle
)
573 unsigned int voice_index
;
575 free(zyn_addsynth_ptr
->voices_components
);
577 // printf("zyn_addsynth_destroy(%08X)\n", (unsigned int)handle);
578 zyn_fft_destroy(zyn_addsynth_ptr
->fft
);
580 // ADnoteParameters temp begin
582 for (voice_index
= 0 ; voice_index
< zyn_addsynth_ptr
->voices_count
; voice_index
++)
584 zyn_oscillator_uninit(&zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].oscillator
);
585 zyn_oscillator_uninit(&zyn_addsynth_ptr
->voices_params_ptr
[voice_index
].modulator_oscillator
);
588 zyn_filter_sv_destroy(zyn_addsynth_ptr
->filter_sv
);
590 // ADnoteParameters temp end
592 free(zyn_addsynth_ptr
->voices_params_ptr
);
594 free(zyn_addsynth_ptr
->notes_array
);
596 free(zyn_addsynth_ptr
->temporary_samples_ptr
);
598 delete zyn_addsynth_ptr
;
601 zyn_addsynth_component
602 zyn_addsynth_get_global_component(
603 zyn_addsynth_handle handle
,
604 unsigned int component
)
606 if (component
>= ZYNADD_GLOBAL_COMPONENTS_COUNT
)
612 return zyn_addsynth_ptr
->global_components
+ component
;
615 zyn_addsynth_component
616 zyn_addsynth_get_voice_component(
617 zyn_addsynth_handle handle
,
619 unsigned int component
)
621 if (voice
>= zyn_addsynth_ptr
->voices_count
)
627 if (component
>= ZYNADD_VOICE_COMPONENTS_COUNT
)
633 return zyn_addsynth_ptr
->voices_components
+ voice
* ZYNADD_VOICE_COMPONENTS_COUNT
+ component
;
636 float percent_from_0_127(unsigned char value
)
638 return ((float)(value
)/127.0)*100.0; // 0-127 -> percent
641 unsigned char percent_to_0_127(float value
)
643 return (unsigned char)roundf(value
/ 100.0 * 127.0);
646 #define component_ptr ((struct zyn_component_descriptor *)component)
649 zyn_addsynth_get_float_parameter(
650 zyn_addsynth_component component
,
651 unsigned int parameter
)
653 return component_ptr
->get_float(component_ptr
->context
, parameter
);
657 zyn_addsynth_set_float_parameter(
658 zyn_addsynth_component component
,
659 unsigned int parameter
,
662 return component_ptr
->set_float(component_ptr
->context
, parameter
, value
);
666 zyn_addsynth_get_bool_parameter(
667 zyn_addsynth_component component
,
668 unsigned int parameter
)
670 //LOG_DEBUG("component %p, context %p", component_ptr, component_ptr->context);
671 return component_ptr
->get_bool(component_ptr
->context
, parameter
);
675 zyn_addsynth_set_bool_parameter(
676 zyn_addsynth_component component
,
677 unsigned int parameter
,
680 return component_ptr
->set_bool(component_ptr
->context
, parameter
, value
);
684 zyn_addsynth_get_int_parameter(
685 zyn_addsynth_component component
,
686 unsigned int parameter
)
688 return component_ptr
->get_int(component_ptr
->context
, parameter
);
692 zyn_addsynth_set_int_parameter(
693 zyn_addsynth_component component
,
694 unsigned int parameter
,
697 return component_ptr
->set_int(component_ptr
->context
, parameter
, value
);
700 #undef zyn_addsynth_ptr
703 zyn_addsynth_set_bandwidth(struct zyn_addsynth
* zyn_addsynth_ptr
, int value
)
707 if (!zyn_addsynth_ptr
->bandwidth_exponential
)
709 if (value
< 64 && zyn_addsynth_ptr
->bandwidth_depth
>= 64)
715 tmp
= pow(25.0, pow(zyn_addsynth_ptr
->bandwidth_depth
/ 127.0, 1.5)) - 1.0;
718 zyn_addsynth_ptr
->bandwidth_relbw
= (value
/ 64.0 - 1.0) * tmp
+ 1.0;
719 if (zyn_addsynth_ptr
->bandwidth_relbw
< 0.01)
721 zyn_addsynth_ptr
->bandwidth_relbw
= 0.01;
726 zyn_addsynth_ptr
->bandwidth_relbw
= pow(25.0, (value
- 64.0) / 64.0 * (zyn_addsynth_ptr
->bandwidth_depth
/ 64.0));
731 zyn_addsynth_set_modwheel(struct zyn_addsynth
* zyn_addsynth_ptr
, int value
)
735 if (!zyn_addsynth_ptr
->modwheel_exponential
)
737 if ((value
< 64) && (zyn_addsynth_ptr
->modwheel_depth
>= 64))
743 tmp
= pow(25.0, pow(zyn_addsynth_ptr
->modwheel_depth
/ 127.0, 1.5) * 2.0) / 25.0;
746 zyn_addsynth_ptr
->modwheel_relmod
= (value
/ 64.0 - 1.0) * tmp
+ 1.0;
747 if (zyn_addsynth_ptr
->modwheel_relmod
< 0.0)
749 zyn_addsynth_ptr
->modwheel_relmod
= 0.0;
754 zyn_addsynth_ptr
->modwheel_relmod
= pow(25.0 , (value
- 64.0) / 64.0 * (zyn_addsynth_ptr
->modwheel_depth
/ 80.0));