REALTYPE -> zyn_sample_type for VoiceOut buffers
[zyn.git] / addsynth.cpp
blobd6fa787be5a631c9478747c5d4f2c3bae5b7b800
1 /* -*- Mode: C++ ; c-basic-offset: 2 -*- */
2 /*****************************************************************************
4 * Copyright (C) 2006,2007 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 *****************************************************************************/
21 #include <stdlib.h>
22 #include <math.h>
23 #include <assert.h>
25 #include "common.h"
26 #include "addsynth.h"
27 #include "globals.h"
28 #include "resonance.h"
29 #include "fft.h"
30 #include "oscillator.h"
31 #include "envelope_parameters.h"
32 #include "envelope.h"
33 #include "lfo_parameters.h"
34 #include "lfo.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"
40 #include "filter.h"
41 #include "portamento.h"
42 #include "addsynth_internal.h"
43 #include "addsynth_voice.h"
44 #include "addnote.h"
45 #include "util.h"
47 #define LOG_LEVEL LOG_LEVEL_ERROR
48 #include "log.h"
50 #define ZYN_DEFAULT_POLYPHONY 60
52 // (94.0 / 64.0 - 1) * 5.0 = 2.34375
53 #define ZYN_GLOBAL_FILTER_INITIAL_FREQUENCY 2.34375
55 // 40.0 / 127.0 = 0.31496062992125984
56 #define ZYN_GLOBAL_FILTER_INITIAL_Q 0.31496062992125984
58 bool
59 zyn_addsynth_create(
60 float sample_rate,
61 unsigned int voices_count,
62 zyn_addsynth_handle * handle_ptr)
64 struct zyn_addsynth * zyn_addsynth_ptr;
65 unsigned int note_index;
66 unsigned int voice_index;
68 // printf("zyn_addsynth_create\n");
69 zyn_addsynth_ptr = (struct zyn_addsynth *)malloc(sizeof(struct zyn_addsynth));
70 if (zyn_addsynth_ptr == NULL)
72 goto fail;
75 zyn_addsynth_ptr->sample_rate = sample_rate;
77 zyn_addsynth_ptr->temporary_samples_ptr = (zyn_sample_type *)malloc(sizeof(zyn_sample_type) * OSCIL_SIZE);
78 zyn_fft_freqs_init(&zyn_addsynth_ptr->oscillator_fft_frequencies, OSCIL_SIZE / 2);
80 zyn_addsynth_ptr->polyphony = ZYN_DEFAULT_POLYPHONY;
81 zyn_addsynth_ptr->notes_array = (struct note_channel *)malloc(ZYN_DEFAULT_POLYPHONY * sizeof(struct note_channel));
83 zyn_addsynth_ptr->all_sound_off = false;
85 zyn_addsynth_ptr->velsns = 64;
86 zyn_addsynth_ptr->fft = zyn_fft_create(OSCIL_SIZE);
88 // ADnoteParameters temp begin
90 zyn_addsynth_ptr->m_frequency_envelope_params.init_asr(0, false, 64, 50, 64, 60);
92 zyn_addsynth_ptr->m_amplitude_envelope_params.init_adsr(64, true, 0, 40, 127, 25, false);
94 zyn_addsynth_ptr->filter_type = ZYN_FILTER_TYPE_ANALOG;
95 zyn_addsynth_ptr->m_filter_params.init(sample_rate, ZYN_FILTER_ANALOG_TYPE_LPF2, 94, 40);
96 if (!zyn_filter_sv_create(sample_rate, ZYN_GLOBAL_FILTER_INITIAL_FREQUENCY, ZYN_GLOBAL_FILTER_INITIAL_Q, &zyn_addsynth_ptr->filter_sv))
98 goto fail_free_synth;
101 zyn_addsynth_ptr->m_filter_envelope_params.init_adsr_filter(0, true, 64, 40, 64, 70, 60, 64);
103 zyn_resonance_init(&zyn_addsynth_ptr->GlobalPar.resonance);
105 zyn_addsynth_ptr->voices_count = voices_count;
106 zyn_addsynth_ptr->voices_params_ptr = (struct zyn_addnote_voice_parameters *)malloc(sizeof(struct zyn_addnote_voice_parameters) * voices_count);
108 for (voice_index = 0 ; voice_index < voices_count ; voice_index++)
110 zyn_oscillator_init(
111 &zyn_addsynth_ptr->voices_params_ptr[voice_index].oscillator,
112 sample_rate,
113 zyn_addsynth_ptr->fft,
114 &zyn_addsynth_ptr->GlobalPar.resonance,
115 zyn_addsynth_ptr->temporary_samples_ptr,
116 &zyn_addsynth_ptr->oscillator_fft_frequencies);
118 zyn_oscillator_init(
119 &zyn_addsynth_ptr->voices_params_ptr[voice_index].modulator_oscillator,
120 sample_rate,
121 zyn_addsynth_ptr->fft,
122 NULL,
123 zyn_addsynth_ptr->temporary_samples_ptr,
124 &zyn_addsynth_ptr->oscillator_fft_frequencies);
126 zyn_addsynth_ptr->voices_params_ptr[voice_index].m_amplitude_envelope_params.init_adsr(64, true, 0, 100, 127, 100, false);
128 zyn_addsynth_ptr->voices_params_ptr[voice_index].amplitude_lfo_params.frequency = 90.0 / 127.0;
129 zyn_addsynth_ptr->voices_params_ptr[voice_index].amplitude_lfo_params.depth = 32.0 / 127.0;
130 zyn_addsynth_ptr->voices_params_ptr[voice_index].amplitude_lfo_params.random_start_phase = false;
131 zyn_addsynth_ptr->voices_params_ptr[voice_index].amplitude_lfo_params.start_phase = 0.5;
132 zyn_addsynth_ptr->voices_params_ptr[voice_index].amplitude_lfo_params.depth_randomness_enabled = false;
133 zyn_addsynth_ptr->voices_params_ptr[voice_index].amplitude_lfo_params.depth = 0;
134 zyn_addsynth_ptr->voices_params_ptr[voice_index].amplitude_lfo_params.frequency_randomness_enabled = false;
135 zyn_addsynth_ptr->voices_params_ptr[voice_index].amplitude_lfo_params.frequency_randomness = 0;
136 zyn_addsynth_ptr->voices_params_ptr[voice_index].amplitude_lfo_params.delay = 30.0 / 127.0 * 4.0;
137 zyn_addsynth_ptr->voices_params_ptr[voice_index].amplitude_lfo_params.stretch = 0;
138 zyn_addsynth_ptr->voices_params_ptr[voice_index].amplitude_lfo_params.shape = ZYN_LFO_SHAPE_TYPE_SINE;
140 zyn_addsynth_ptr->voices_params_ptr[voice_index].m_frequency_envelope_params.init_asr(0, false, 30, 40, 64, 60);
142 zyn_addsynth_ptr->voices_params_ptr[voice_index].frequency_lfo_params.frequency = 50.0 / 127.0;
143 zyn_addsynth_ptr->voices_params_ptr[voice_index].frequency_lfo_params.depth = 40.0 / 127.0;
144 zyn_addsynth_ptr->voices_params_ptr[voice_index].frequency_lfo_params.random_start_phase = false;
145 zyn_addsynth_ptr->voices_params_ptr[voice_index].frequency_lfo_params.start_phase = 0;
146 zyn_addsynth_ptr->voices_params_ptr[voice_index].frequency_lfo_params.depth_randomness_enabled = false;
147 zyn_addsynth_ptr->voices_params_ptr[voice_index].frequency_lfo_params.depth = 0;
148 zyn_addsynth_ptr->voices_params_ptr[voice_index].frequency_lfo_params.frequency_randomness_enabled = false;
149 zyn_addsynth_ptr->voices_params_ptr[voice_index].frequency_lfo_params.frequency_randomness = 0;
150 zyn_addsynth_ptr->voices_params_ptr[voice_index].frequency_lfo_params.delay = 0;
151 zyn_addsynth_ptr->voices_params_ptr[voice_index].frequency_lfo_params.stretch = 0;
152 zyn_addsynth_ptr->voices_params_ptr[voice_index].frequency_lfo_params.shape = ZYN_LFO_SHAPE_TYPE_SINE;
154 zyn_addsynth_ptr->voices_params_ptr[voice_index].m_filter_params.init(sample_rate, ZYN_FILTER_ANALOG_TYPE_LPF2, 50, 60);
155 zyn_addsynth_ptr->voices_params_ptr[voice_index].m_filter_envelope_params.init_adsr_filter(0, false, 90, 70, 40, 70, 10, 40);
157 zyn_addsynth_ptr->voices_params_ptr[voice_index].filter_lfo_params.frequency = 50.0 / 127.0;
158 zyn_addsynth_ptr->voices_params_ptr[voice_index].filter_lfo_params.depth = 20.0 / 127.0;
159 zyn_addsynth_ptr->voices_params_ptr[voice_index].filter_lfo_params.random_start_phase = false;
160 zyn_addsynth_ptr->voices_params_ptr[voice_index].filter_lfo_params.start_phase = 0.5;
161 zyn_addsynth_ptr->voices_params_ptr[voice_index].filter_lfo_params.depth_randomness_enabled = false;
162 zyn_addsynth_ptr->voices_params_ptr[voice_index].filter_lfo_params.depth = 0;
163 zyn_addsynth_ptr->voices_params_ptr[voice_index].filter_lfo_params.frequency_randomness_enabled = false;
164 zyn_addsynth_ptr->voices_params_ptr[voice_index].filter_lfo_params.frequency_randomness = 0;
165 zyn_addsynth_ptr->voices_params_ptr[voice_index].filter_lfo_params.delay = 0;
166 zyn_addsynth_ptr->voices_params_ptr[voice_index].filter_lfo_params.stretch = 0;
167 zyn_addsynth_ptr->voices_params_ptr[voice_index].filter_lfo_params.shape = ZYN_LFO_SHAPE_TYPE_SINE;
169 zyn_addsynth_ptr->voices_params_ptr[voice_index].m_fm_frequency_envelope_params.init_asr(0, false, 20, 90, 40, 80);
170 zyn_addsynth_ptr->voices_params_ptr[voice_index].m_fm_amplitude_envelope_params.init_adsr(64, true, 80, 90, 127, 100, false);
173 /* Frequency Global Parameters */
174 // zyn_addsynth_ptr->GlobalPar.stereo = true; // Stereo
175 zyn_addsynth_ptr->GlobalPar.PDetune=8192;//zero
176 zyn_addsynth_ptr->GlobalPar.PCoarseDetune=0;
177 zyn_addsynth_ptr->GlobalPar.PDetuneType=1;
178 zyn_addsynth_ptr->GlobalPar.PBandwidth=64;
180 /* Amplitude Global Parameters */
181 zyn_addsynth_ptr->GlobalPar.PVolume=90;
182 zyn_addsynth_ptr->GlobalPar.PAmpVelocityScaleFunction=64;
183 zyn_addsynth_ptr->GlobalPar.PPunchStrength=0;
184 zyn_addsynth_ptr->GlobalPar.PPunchTime=60;
185 zyn_addsynth_ptr->GlobalPar.PPunchStretch=64;
186 zyn_addsynth_ptr->GlobalPar.PPunchVelocitySensing=72;
188 /* Filter Global Parameters*/
189 zyn_addsynth_ptr->m_filter_velocity_sensing_amount = 0.5;
190 zyn_addsynth_ptr->m_filter_velocity_scale_function = 0;
191 zyn_addsynth_ptr->m_filter_params.defaults();
193 for (voice_index = 0 ; voice_index < voices_count ; voice_index++)
195 zyn_addsynth_ptr->voices_params_ptr[voice_index].enabled = false;
196 zyn_addsynth_ptr->voices_params_ptr[voice_index].white_noise = false;
197 zyn_addsynth_ptr->voices_params_ptr[voice_index].Pfixedfreq=0;
198 zyn_addsynth_ptr->voices_params_ptr[voice_index].PfixedfreqET=0;
199 zyn_addsynth_ptr->voices_params_ptr[voice_index].resonance = true;
200 zyn_addsynth_ptr->voices_params_ptr[voice_index].Pfilterbypass=0;
201 zyn_addsynth_ptr->voices_params_ptr[voice_index].Pextoscil=-1;
202 zyn_addsynth_ptr->voices_params_ptr[voice_index].PextFMoscil=-1;
203 zyn_addsynth_ptr->voices_params_ptr[voice_index].Poscilphase=64;
204 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFMoscilphase=64;
205 zyn_addsynth_ptr->voices_params_ptr[voice_index].PDelay=0;
206 zyn_addsynth_ptr->voices_params_ptr[voice_index].PVolume=100;
207 zyn_addsynth_ptr->voices_params_ptr[voice_index].PVolumeminus=0;
208 zyn_addsynth_ptr->voices_params_ptr[voice_index].PPanning=64;//center
209 zyn_addsynth_ptr->voices_params_ptr[voice_index].PDetune=8192;//8192=0
210 zyn_addsynth_ptr->voices_params_ptr[voice_index].PCoarseDetune=0;
211 zyn_addsynth_ptr->voices_params_ptr[voice_index].PDetuneType=0;
212 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFreqLfoEnabled=0;
213 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFreqEnvelopeEnabled=0;
214 zyn_addsynth_ptr->voices_params_ptr[voice_index].PAmpEnvelopeEnabled=0;
215 zyn_addsynth_ptr->voices_params_ptr[voice_index].PAmpLfoEnabled=0;
216 zyn_addsynth_ptr->voices_params_ptr[voice_index].PAmpVelocityScaleFunction=127;
217 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFilterEnabled=0;
218 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFilterEnvelopeEnabled=0;
219 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFilterLfoEnabled=0;
220 zyn_addsynth_ptr->voices_params_ptr[voice_index].fm_type = ZYN_FM_TYPE_NONE;
222 //I use the internal oscillator (-1)
223 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFMVoice=-1;
225 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFMVolume=90;
226 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFMVolumeDamp=64;
227 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFMDetune=8192;
228 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFMCoarseDetune=0;
229 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFMDetuneType=0;
230 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFMFreqEnvelopeEnabled=0;
231 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFMAmpEnvelopeEnabled=0;
232 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFMVelocityScaleFunction=64;
234 zyn_addsynth_ptr->voices_params_ptr[voice_index].m_filter_params.defaults();
237 zyn_addsynth_ptr->voices_params_ptr[0].enabled = true;
239 // ADnoteParameters temp end
241 zyn_addsynth_ptr->bandwidth_depth = 64;
242 zyn_addsynth_ptr->bandwidth_exponential = false;
243 zyn_addsynth_ptr->modwheel_depth = 80;
244 zyn_addsynth_ptr->modwheel_exponential = false;
246 zyn_addsynth_set_bandwidth(zyn_addsynth_ptr, 64);
247 zyn_addsynth_set_modwheel(zyn_addsynth_ptr, 64);
249 zyn_portamento_init(&zyn_addsynth_ptr->portamento);
251 zyn_addsynth_ptr->pitch_bend_range = 200.0; // 200 cents = 2 halftones
252 zyn_addsynth_ptr->pitch_bend = 0; // center
253 ZYN_UPDATE_PITCH_BEND(zyn_addsynth_ptr);
255 zyn_addsynth_ptr->oldfreq = -1.0;
257 zyn_addsynth_ptr->random_panorama = false;
258 zyn_addsynth_ptr->panorama = 0.0;
260 zyn_addsynth_ptr->stereo = true;
262 zyn_addsynth_ptr->random_grouping = false;
264 zyn_addsynth_ptr->amplitude_lfo_params.frequency = 80.0 / 127.0;
265 zyn_addsynth_ptr->amplitude_lfo_params.depth = 0;
266 zyn_addsynth_ptr->amplitude_lfo_params.random_start_phase = false;
267 zyn_addsynth_ptr->amplitude_lfo_params.start_phase = 0.5;
268 zyn_addsynth_ptr->amplitude_lfo_params.depth_randomness_enabled = false;
269 zyn_addsynth_ptr->amplitude_lfo_params.depth_randomness = 0.5;
270 zyn_addsynth_ptr->amplitude_lfo_params.frequency_randomness_enabled = false;
271 zyn_addsynth_ptr->amplitude_lfo_params.frequency_randomness = 0.5;
272 zyn_addsynth_ptr->amplitude_lfo_params.delay = 0;
273 zyn_addsynth_ptr->amplitude_lfo_params.stretch = 0;
274 zyn_addsynth_ptr->amplitude_lfo_params.shape = ZYN_LFO_SHAPE_TYPE_SINE;
276 zyn_addsynth_ptr->filter_lfo_params.frequency = 80.0 / 127.0;
277 zyn_addsynth_ptr->filter_lfo_params.depth = 0;
278 zyn_addsynth_ptr->filter_lfo_params.random_start_phase = false;
279 zyn_addsynth_ptr->filter_lfo_params.start_phase = 0.5;
280 zyn_addsynth_ptr->filter_lfo_params.depth_randomness_enabled = false;
281 zyn_addsynth_ptr->filter_lfo_params.depth_randomness = 0.5;
282 zyn_addsynth_ptr->filter_lfo_params.frequency_randomness_enabled = false;
283 zyn_addsynth_ptr->filter_lfo_params.frequency_randomness = 0.5;
284 zyn_addsynth_ptr->filter_lfo_params.delay = 0;
285 zyn_addsynth_ptr->filter_lfo_params.stretch = 0;
286 zyn_addsynth_ptr->filter_lfo_params.shape = ZYN_LFO_SHAPE_TYPE_SINE;
288 zyn_addsynth_ptr->frequency_lfo_params.frequency = 70.0 / 127.0;
289 zyn_addsynth_ptr->frequency_lfo_params.depth = 0;
290 zyn_addsynth_ptr->frequency_lfo_params.random_start_phase = false;
291 zyn_addsynth_ptr->frequency_lfo_params.start_phase = 0.5;
292 zyn_addsynth_ptr->frequency_lfo_params.depth_randomness_enabled = false;
293 zyn_addsynth_ptr->frequency_lfo_params.depth_randomness = 0.5;
294 zyn_addsynth_ptr->frequency_lfo_params.frequency_randomness_enabled = false;
295 zyn_addsynth_ptr->frequency_lfo_params.frequency_randomness = 0.5;
296 zyn_addsynth_ptr->frequency_lfo_params.delay = 0;
297 zyn_addsynth_ptr->frequency_lfo_params.stretch = 0;
298 zyn_addsynth_ptr->frequency_lfo_params.shape = ZYN_LFO_SHAPE_TYPE_SINE;
300 for (note_index = 0 ; note_index < ZYN_DEFAULT_POLYPHONY ; note_index++)
302 zyn_addsynth_ptr->notes_array[note_index].note_ptr = new ADnote(zyn_addsynth_ptr);
303 zyn_addsynth_ptr->notes_array[note_index].midinote = -1;
306 // init global components
308 zyn_addsynth_component_init_amp_globals(
309 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_AMP_GLOBALS,
310 zyn_addsynth_ptr);
312 zyn_addsynth_component_init_amp_envelope(
313 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_AMP_ENV,
314 &zyn_addsynth_ptr->m_amplitude_envelope_params);
316 zyn_addsynth_component_init_lfo(
317 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_AMP_LFO,
318 &zyn_addsynth_ptr->amplitude_lfo_params);
320 zyn_addsynth_component_init_filter_globals(
321 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_FILTER_GLOBALS,
322 zyn_addsynth_ptr);
324 zyn_addsynth_component_init_filter_analog(
325 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_FILTER_ANALOG,
326 zyn_addsynth_ptr);
328 zyn_addsynth_component_init_filter_formant(
329 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_FILTER_FORMANT,
330 zyn_addsynth_ptr);
332 zyn_addsynth_component_init_filter_sv(
333 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_FILTER_SV,
334 zyn_addsynth_ptr->filter_sv);
336 zyn_addsynth_component_init_filter_envelope(
337 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_FILTER_ENV,
338 &zyn_addsynth_ptr->m_filter_envelope_params);
340 zyn_addsynth_component_init_lfo(
341 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_FILTER_LFO,
342 &zyn_addsynth_ptr->filter_lfo_params);
344 zyn_addsynth_component_init_frequency_globals(
345 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_FREQUENCY_GLOBALS);
347 zyn_addsynth_component_init_frequency_envelope(
348 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_FREQUENCY_ENV,
349 &zyn_addsynth_ptr->m_frequency_envelope_params);
351 zyn_addsynth_component_init_lfo(
352 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_FREQUENCY_LFO,
353 &zyn_addsynth_ptr->frequency_lfo_params);
355 zyn_addsynth_component_init_portamento(
356 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_PORTAMENTO,
357 &zyn_addsynth_ptr->portamento);
359 // init voices components
361 zyn_addsynth_ptr->voices_components =
362 (struct zyn_component_descriptor *)malloc(
363 sizeof(struct zyn_component_descriptor) * voices_count * ZYNADD_VOICE_COMPONENTS_COUNT);
365 for (voice_index = 0 ; voice_index < voices_count ; voice_index++)
367 zyn_addsynth_component_init_voice_globals(
368 zyn_addsynth_ptr->voices_components + voice_index * ZYNADD_VOICE_COMPONENTS_COUNT + ZYNADD_COMPONENT_VOICE_GLOBALS,
369 zyn_addsynth_ptr->voices_params_ptr + voice_index);
371 zyn_addsynth_component_init_oscillator(
372 zyn_addsynth_ptr->voices_components + voice_index * ZYNADD_VOICE_COMPONENTS_COUNT + ZYNADD_COMPONENT_VOICE_OSCILLATOR,
373 &zyn_addsynth_ptr->voices_params_ptr[voice_index].oscillator);
376 *handle_ptr = (zyn_addsynth_handle)zyn_addsynth_ptr;
378 // printf("zyn_addsynth_create(%08X)\n", (unsigned int)*handle_ptr);
380 return true;
382 fail_free_synth:
384 fail:
385 return false;
388 #define zyn_addsynth_ptr ((struct zyn_addsynth *)handle)
390 void
391 zyn_addsynth_get_audio_output(
392 zyn_addsynth_handle handle,
393 zyn_sample_type * buffer_left,
394 zyn_sample_type * buffer_right)
396 unsigned int note_index;
397 zyn_sample_type note_buffer_left[SOUND_BUFFER_SIZE];
398 zyn_sample_type note_buffer_right[SOUND_BUFFER_SIZE];
400 silence_two_buffers(buffer_left, buffer_right, SOUND_BUFFER_SIZE);
402 for (note_index = 0 ; note_index < zyn_addsynth_ptr->polyphony ; note_index++)
404 if (zyn_addsynth_ptr->notes_array[note_index].midinote != -1)
406 //printf("mixing note channel %u\n", note_index);
408 zyn_addsynth_ptr->notes_array[note_index].note_ptr->noteout(note_buffer_left, note_buffer_right);
410 mix_add_two_buffers(
411 buffer_left,
412 buffer_right,
413 note_buffer_left,
414 note_buffer_right,
415 SOUND_BUFFER_SIZE);
417 if (zyn_addsynth_ptr->notes_array[note_index].note_ptr->finished())
419 zyn_addsynth_ptr->notes_array[note_index].midinote = -1;
424 if (zyn_addsynth_ptr->all_sound_off)
426 fadeout_two_buffers(buffer_left, buffer_right, SOUND_BUFFER_SIZE);
428 for (note_index = 0 ; note_index < zyn_addsynth_ptr->polyphony ; note_index++)
430 if (zyn_addsynth_ptr->notes_array[note_index].midinote != -1)
432 zyn_addsynth_ptr->notes_array[note_index].note_ptr->force_disable();
433 zyn_addsynth_ptr->notes_array[note_index].midinote = -1;
437 zyn_addsynth_ptr->all_sound_off = false;
440 zyn_portamento_update(&zyn_addsynth_ptr->portamento);
443 void
444 zyn_addsynth_note_on(
445 zyn_addsynth_handle handle,
446 unsigned int note,
447 unsigned int velocity)
449 unsigned int note_index;
450 int masterkeyshift;
451 float vel;
452 zyn_sample_type notebasefreq;
454 for (note_index = 0 ; note_index < zyn_addsynth_ptr->polyphony ; note_index++)
456 if (zyn_addsynth_ptr->notes_array[note_index].midinote == -1)
458 goto unused_note_channel_found;
462 //printf("note on %u - ignored\n", note);
464 return;
466 unused_note_channel_found:
467 //printf("note on %u - channel %u\n", note, note_index);
468 masterkeyshift = 0;
470 vel = VelF(velocity/127.0, zyn_addsynth_ptr->velsns);
471 notebasefreq = 440.0*pow(2.0,(note-69.0)/12.0);
473 // Portamento
475 if (zyn_addsynth_ptr->oldfreq < 1.0) /* only when the first note is played */
477 zyn_addsynth_ptr->oldfreq = notebasefreq;
480 bool portamento = zyn_portamento_start(zyn_addsynth_ptr->sample_rate, &zyn_addsynth_ptr->portamento, zyn_addsynth_ptr->oldfreq, notebasefreq);
482 zyn_addsynth_ptr->oldfreq = notebasefreq;
484 zyn_addsynth_ptr->notes_array[note_index].midinote = note;
485 zyn_addsynth_ptr->notes_array[note_index].note_ptr->note_on(
486 zyn_addsynth_ptr->random_panorama ? RND : zyn_addsynth_ptr->panorama,
487 zyn_addsynth_ptr->random_grouping,
488 notebasefreq,
489 vel,
490 portamento,
491 note);
494 void
495 zyn_addsynth_note_off(
496 zyn_addsynth_handle handle,
497 unsigned int note)
499 unsigned int note_index;
501 //printf("note off %u\n", note);
503 for (note_index = 0 ; note_index < zyn_addsynth_ptr->polyphony ; note_index++)
505 if (zyn_addsynth_ptr->notes_array[note_index].midinote == (char)note)
507 zyn_addsynth_ptr->notes_array[note_index].note_ptr->relasekey();
512 void
513 zyn_addsynth_all_notes_off(
514 zyn_addsynth_handle handle)
516 unsigned int note_index;
518 for (note_index = 0 ; note_index < zyn_addsynth_ptr->polyphony ; note_index++)
520 if (zyn_addsynth_ptr->notes_array[note_index].midinote != -1)
522 zyn_addsynth_ptr->notes_array[note_index].note_ptr->relasekey();
527 void
528 zyn_addsynth_all_sound_off(
529 zyn_addsynth_handle handle)
531 zyn_addsynth_ptr->all_sound_off = true;
534 void
535 zyn_addsynth_destroy(
536 zyn_addsynth_handle handle)
538 unsigned int voice_index;
540 free(zyn_addsynth_ptr->voices_components);
542 // printf("zyn_addsynth_destroy(%08X)\n", (unsigned int)handle);
543 zyn_fft_destroy(zyn_addsynth_ptr->fft);
545 // ADnoteParameters temp begin
547 for (voice_index = 0 ; voice_index < zyn_addsynth_ptr->voices_count ; voice_index++)
549 zyn_oscillator_uninit(&zyn_addsynth_ptr->voices_params_ptr[voice_index].oscillator);
550 zyn_oscillator_uninit(&zyn_addsynth_ptr->voices_params_ptr[voice_index].modulator_oscillator);
553 zyn_filter_sv_destroy(zyn_addsynth_ptr->filter_sv);
555 // ADnoteParameters temp end
557 free(zyn_addsynth_ptr->voices_params_ptr);
559 free(zyn_addsynth_ptr->notes_array);
561 free(zyn_addsynth_ptr->temporary_samples_ptr);
563 delete zyn_addsynth_ptr;
566 zyn_addsynth_component
567 zyn_addsynth_get_global_component(
568 zyn_addsynth_handle handle,
569 unsigned int component)
571 if (component >= ZYNADD_GLOBAL_COMPONENTS_COUNT)
573 assert(0);
574 return NULL;
577 return zyn_addsynth_ptr->global_components + component;
580 zyn_addsynth_component
581 zyn_addsynth_get_voice_component(
582 zyn_addsynth_handle handle,
583 unsigned int component)
585 if (component >= ZYNADD_VOICE_COMPONENTS_COUNT)
587 assert(0);
588 return NULL;
591 return zyn_addsynth_ptr->voices_components + component;
594 float percent_from_0_127(unsigned char value)
596 return ((float)(value)/127.0)*100.0; // 0-127 -> percent
599 unsigned char percent_to_0_127(float value)
601 return (unsigned char)roundf(value / 100.0 * 127.0);
604 #define component_ptr ((struct zyn_component_descriptor *)component)
606 float
607 zyn_addsynth_get_float_parameter(
608 zyn_addsynth_component component,
609 unsigned int parameter)
611 return component_ptr->get_float(component_ptr->context, parameter);
614 void
615 zyn_addsynth_set_float_parameter(
616 zyn_addsynth_component component,
617 unsigned int parameter,
618 float value)
620 return component_ptr->set_float(component_ptr->context, parameter, value);
623 bool
624 zyn_addsynth_get_bool_parameter(
625 zyn_addsynth_component component,
626 unsigned int parameter)
628 //LOG_DEBUG("component %p, context %p", component_ptr, component_ptr->context);
629 return component_ptr->get_bool(component_ptr->context, parameter);
632 void
633 zyn_addsynth_set_bool_parameter(
634 zyn_addsynth_component component,
635 unsigned int parameter,
636 bool value)
638 return component_ptr->set_bool(component_ptr->context, parameter, value);
641 signed int
642 zyn_addsynth_get_int_parameter(
643 zyn_addsynth_component component,
644 unsigned int parameter)
646 return component_ptr->get_int(component_ptr->context, parameter);
649 void
650 zyn_addsynth_set_int_parameter(
651 zyn_addsynth_component component,
652 unsigned int parameter,
653 signed int value)
655 return component_ptr->set_int(component_ptr->context, parameter, value);
658 #undef zyn_addsynth_ptr
660 void
661 zyn_addsynth_set_bandwidth(struct zyn_addsynth * zyn_addsynth_ptr, int value)
663 float tmp;
665 if (!zyn_addsynth_ptr->bandwidth_exponential)
667 if (value < 64 && zyn_addsynth_ptr->bandwidth_depth >= 64)
669 tmp = 1.0;
671 else
673 tmp = pow(25.0, pow(zyn_addsynth_ptr->bandwidth_depth / 127.0, 1.5)) - 1.0;
676 zyn_addsynth_ptr->bandwidth_relbw = (value / 64.0 - 1.0) * tmp + 1.0;
677 if (zyn_addsynth_ptr->bandwidth_relbw < 0.01)
679 zyn_addsynth_ptr->bandwidth_relbw = 0.01;
682 else
684 zyn_addsynth_ptr->bandwidth_relbw = pow(25.0, (value - 64.0) / 64.0 * (zyn_addsynth_ptr->bandwidth_depth / 64.0));
688 void
689 zyn_addsynth_set_modwheel(struct zyn_addsynth * zyn_addsynth_ptr, int value)
691 float tmp;
693 if (!zyn_addsynth_ptr->modwheel_exponential)
695 if ((value < 64) && (zyn_addsynth_ptr->modwheel_depth >= 64))
697 tmp = 1.0;
699 else
701 tmp = pow(25.0, pow(zyn_addsynth_ptr->modwheel_depth / 127.0, 1.5) * 2.0) / 25.0;
704 zyn_addsynth_ptr->modwheel_relmod = (value / 64.0 - 1.0) * tmp + 1.0;
705 if (zyn_addsynth_ptr->modwheel_relmod < 0.0)
707 zyn_addsynth_ptr->modwheel_relmod = 0.0;
710 else
712 zyn_addsynth_ptr->modwheel_relmod = pow(25.0 , (value - 64.0) / 64.0 * (zyn_addsynth_ptr->modwheel_depth / 80.0));