Minot coding style fix
[zyn.git] / addsynth.cpp
blobcedb7b7db60e4d03bea68e73f416db29bbd8009f
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 *****************************************************************************/
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 "addnote.h"
44 #include "util.h"
46 #define LOG_LEVEL LOG_LEVEL_ERROR
47 #include "log.h"
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
57 struct note_channel
59 int midinote; // MIDI note, -1 when note "channel" is not playing
60 zyn_addnote_handle note_handle;
63 bool
64 zyn_addsynth_create(
65 float sample_rate,
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)
77 goto fail;
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++)
115 zyn_oscillator_init(
116 &zyn_addsynth_ptr->voices_params_ptr[voice_index].oscillator,
117 sample_rate,
118 zyn_addsynth_ptr->fft,
119 &zyn_addsynth_ptr->resonance,
120 zyn_addsynth_ptr->temporary_samples_ptr,
121 &zyn_addsynth_ptr->oscillator_fft_frequencies);
123 zyn_oscillator_init(
124 &zyn_addsynth_ptr->voices_params_ptr[voice_index].modulator_oscillator,
125 sample_rate,
126 zyn_addsynth_ptr->fft,
127 NULL,
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 = 8192;//zero
181 zyn_addsynth_ptr->detune.coarse = 0;
182 zyn_addsynth_ptr->detune.type = ZYN_DETUNE_TYPE_L35CENTS;
183 zyn_addsynth_ptr->detune_bandwidth = 0.0;
185 /* Amplitude Global Parameters */
186 zyn_addsynth_ptr->PVolume=90;
187 zyn_addsynth_ptr->PAmpVelocityScaleFunction=64;
188 zyn_addsynth_ptr->PPunchStrength=0;
189 zyn_addsynth_ptr->PPunchTime=60;
190 zyn_addsynth_ptr->PPunchStretch=64;
191 zyn_addsynth_ptr->PPunchVelocitySensing=72;
193 /* Filter Global Parameters*/
194 zyn_addsynth_ptr->m_filter_velocity_sensing_amount = 0.5;
195 zyn_addsynth_ptr->m_filter_velocity_scale_function = 0;
196 zyn_addsynth_ptr->m_filter_params.defaults();
198 for (voice_index = 0 ; voice_index < voices_count ; voice_index++)
200 zyn_addsynth_ptr->voices_params_ptr[voice_index].enabled = false;
201 zyn_addsynth_ptr->voices_params_ptr[voice_index].white_noise = false;
202 zyn_addsynth_ptr->voices_params_ptr[voice_index].fixed_detune.mode = ZYN_DETUNE_NORMAL;
203 zyn_addsynth_ptr->voices_params_ptr[voice_index].fixed_detune.equal_temperate = 0;
204 zyn_addsynth_ptr->voices_params_ptr[voice_index].resonance = true;
205 zyn_addsynth_ptr->voices_params_ptr[voice_index].Pfilterbypass=0;
206 zyn_addsynth_ptr->voices_params_ptr[voice_index].Pextoscil=-1;
207 zyn_addsynth_ptr->voices_params_ptr[voice_index].PextFMoscil=-1;
208 zyn_addsynth_ptr->voices_params_ptr[voice_index].Poscilphase=64;
209 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFMoscilphase=64;
210 zyn_addsynth_ptr->voices_params_ptr[voice_index].PDelay=0;
211 zyn_addsynth_ptr->voices_params_ptr[voice_index].PVolume=100;
212 zyn_addsynth_ptr->voices_params_ptr[voice_index].PVolumeminus=0;
213 zyn_addsynth_ptr->voices_params_ptr[voice_index].PPanning=64;//center
214 zyn_addsynth_ptr->voices_params_ptr[voice_index].detune.fine = 8192;//8192=0
215 zyn_addsynth_ptr->voices_params_ptr[voice_index].detune.coarse = 0;
216 zyn_addsynth_ptr->voices_params_ptr[voice_index].detune.type = ZYN_DETUNE_TYPE_GLOBAL;
217 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFreqLfoEnabled=0;
218 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFreqEnvelopeEnabled=0;
219 zyn_addsynth_ptr->voices_params_ptr[voice_index].PAmpEnvelopeEnabled=0;
220 zyn_addsynth_ptr->voices_params_ptr[voice_index].PAmpLfoEnabled=0;
221 zyn_addsynth_ptr->voices_params_ptr[voice_index].PAmpVelocityScaleFunction=127;
222 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFilterEnabled=0;
223 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFilterEnvelopeEnabled=0;
224 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFilterLfoEnabled=0;
225 zyn_addsynth_ptr->voices_params_ptr[voice_index].fm_type = ZYN_FM_TYPE_NONE;
227 //I use the internal oscillator (-1)
228 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFMVoice=-1;
230 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFMVolume=90;
231 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFMVolumeDamp=64;
232 zyn_addsynth_ptr->voices_params_ptr[voice_index].fm_detune.fine = 8192;
233 zyn_addsynth_ptr->voices_params_ptr[voice_index].fm_detune.coarse = 0;
234 zyn_addsynth_ptr->voices_params_ptr[voice_index].fm_detune.type = ZYN_DETUNE_TYPE_GLOBAL;
235 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFMFreqEnvelopeEnabled=0;
236 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFMAmpEnvelopeEnabled=0;
237 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFMVelocityScaleFunction=64;
239 zyn_addsynth_ptr->voices_params_ptr[voice_index].m_filter_params.defaults();
242 zyn_addsynth_ptr->voices_params_ptr[0].enabled = true;
244 // ADnoteParameters temp end
246 zyn_addsynth_ptr->bandwidth_depth = 64;
247 zyn_addsynth_ptr->bandwidth_exponential = false;
248 zyn_addsynth_ptr->modwheel_depth = 80;
249 zyn_addsynth_ptr->modwheel_exponential = false;
251 zyn_addsynth_set_bandwidth(zyn_addsynth_ptr, 64);
252 zyn_addsynth_set_modwheel(zyn_addsynth_ptr, 64);
254 zyn_portamento_init(&zyn_addsynth_ptr->portamento);
256 zyn_addsynth_ptr->pitch_bend_range = 200.0; // 200 cents = 2 halftones
257 zyn_addsynth_ptr->pitch_bend = 0; // center
258 ZYN_UPDATE_PITCH_BEND(zyn_addsynth_ptr);
260 zyn_addsynth_ptr->oldfreq = -1.0;
262 zyn_addsynth_ptr->random_panorama = false;
263 zyn_addsynth_ptr->panorama = 0.0;
265 zyn_addsynth_ptr->stereo = true;
267 zyn_addsynth_ptr->random_grouping = false;
269 zyn_addsynth_ptr->amplitude_lfo_params.frequency = 80.0 / 127.0;
270 zyn_addsynth_ptr->amplitude_lfo_params.depth = 0;
271 zyn_addsynth_ptr->amplitude_lfo_params.random_start_phase = false;
272 zyn_addsynth_ptr->amplitude_lfo_params.start_phase = 0.5;
273 zyn_addsynth_ptr->amplitude_lfo_params.depth_randomness_enabled = false;
274 zyn_addsynth_ptr->amplitude_lfo_params.depth_randomness = 0.5;
275 zyn_addsynth_ptr->amplitude_lfo_params.frequency_randomness_enabled = false;
276 zyn_addsynth_ptr->amplitude_lfo_params.frequency_randomness = 0.5;
277 zyn_addsynth_ptr->amplitude_lfo_params.delay = 0;
278 zyn_addsynth_ptr->amplitude_lfo_params.stretch = 0;
279 zyn_addsynth_ptr->amplitude_lfo_params.shape = ZYN_LFO_SHAPE_TYPE_SINE;
281 zyn_addsynth_ptr->filter_lfo_params.frequency = 80.0 / 127.0;
282 zyn_addsynth_ptr->filter_lfo_params.depth = 0;
283 zyn_addsynth_ptr->filter_lfo_params.random_start_phase = false;
284 zyn_addsynth_ptr->filter_lfo_params.start_phase = 0.5;
285 zyn_addsynth_ptr->filter_lfo_params.depth_randomness_enabled = false;
286 zyn_addsynth_ptr->filter_lfo_params.depth_randomness = 0.5;
287 zyn_addsynth_ptr->filter_lfo_params.frequency_randomness_enabled = false;
288 zyn_addsynth_ptr->filter_lfo_params.frequency_randomness = 0.5;
289 zyn_addsynth_ptr->filter_lfo_params.delay = 0;
290 zyn_addsynth_ptr->filter_lfo_params.stretch = 0;
291 zyn_addsynth_ptr->filter_lfo_params.shape = ZYN_LFO_SHAPE_TYPE_SINE;
293 zyn_addsynth_ptr->frequency_lfo_params.frequency = 70.0 / 127.0;
294 zyn_addsynth_ptr->frequency_lfo_params.depth = 0;
295 zyn_addsynth_ptr->frequency_lfo_params.random_start_phase = false;
296 zyn_addsynth_ptr->frequency_lfo_params.start_phase = 0.5;
297 zyn_addsynth_ptr->frequency_lfo_params.depth_randomness_enabled = false;
298 zyn_addsynth_ptr->frequency_lfo_params.depth_randomness = 0.5;
299 zyn_addsynth_ptr->frequency_lfo_params.frequency_randomness_enabled = false;
300 zyn_addsynth_ptr->frequency_lfo_params.frequency_randomness = 0.5;
301 zyn_addsynth_ptr->frequency_lfo_params.delay = 0;
302 zyn_addsynth_ptr->frequency_lfo_params.stretch = 0;
303 zyn_addsynth_ptr->frequency_lfo_params.shape = ZYN_LFO_SHAPE_TYPE_SINE;
305 for (note_index = 0 ; note_index < ZYN_DEFAULT_POLYPHONY ; note_index++)
307 if (!zyn_addnote_create(
308 zyn_addsynth_ptr,
309 &zyn_addsynth_ptr->notes_array[note_index].note_handle))
313 zyn_addsynth_ptr->notes_array[note_index].midinote = -1;
316 // init global components
318 zyn_addsynth_component_init_amp_globals(
319 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_AMP_GLOBALS,
320 zyn_addsynth_ptr);
322 zyn_addsynth_component_init_amp_envelope(
323 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_AMP_ENV,
324 &zyn_addsynth_ptr->m_amplitude_envelope_params);
326 zyn_addsynth_component_init_lfo(
327 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_AMP_LFO,
328 &zyn_addsynth_ptr->amplitude_lfo_params);
330 zyn_addsynth_component_init_filter_globals(
331 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_FILTER_GLOBALS,
332 zyn_addsynth_ptr);
334 zyn_addsynth_component_init_filter_analog(
335 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_FILTER_ANALOG,
336 zyn_addsynth_ptr);
338 zyn_addsynth_component_init_filter_formant(
339 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_FILTER_FORMANT,
340 zyn_addsynth_ptr);
342 zyn_addsynth_component_init_filter_sv(
343 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_FILTER_SV,
344 zyn_addsynth_ptr->filter_sv);
346 zyn_addsynth_component_init_filter_envelope(
347 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_FILTER_ENV,
348 &zyn_addsynth_ptr->m_filter_envelope_params);
350 zyn_addsynth_component_init_lfo(
351 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_FILTER_LFO,
352 &zyn_addsynth_ptr->filter_lfo_params);
354 zyn_addsynth_component_init_frequency_globals(
355 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_FREQUENCY_GLOBALS);
357 zyn_addsynth_component_init_frequency_envelope(
358 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_FREQUENCY_ENV,
359 &zyn_addsynth_ptr->m_frequency_envelope_params);
361 zyn_addsynth_component_init_lfo(
362 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_FREQUENCY_LFO,
363 &zyn_addsynth_ptr->frequency_lfo_params);
365 zyn_addsynth_component_init_portamento(
366 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_PORTAMENTO,
367 &zyn_addsynth_ptr->portamento);
369 // init voices components
371 zyn_addsynth_ptr->voices_components =
372 (struct zyn_component_descriptor *)malloc(
373 sizeof(struct zyn_component_descriptor) * voices_count * ZYNADD_VOICE_COMPONENTS_COUNT);
375 for (voice_index = 0 ; voice_index < voices_count ; voice_index++)
377 zyn_addsynth_component_init_voice_globals(
378 zyn_addsynth_ptr->voices_components + voice_index * ZYNADD_VOICE_COMPONENTS_COUNT + ZYNADD_COMPONENT_VOICE_GLOBALS,
379 zyn_addsynth_ptr->voices_params_ptr + voice_index);
381 zyn_addsynth_component_init_oscillator(
382 zyn_addsynth_ptr->voices_components + voice_index * ZYNADD_VOICE_COMPONENTS_COUNT + ZYNADD_COMPONENT_VOICE_OSCILLATOR,
383 &zyn_addsynth_ptr->voices_params_ptr[voice_index].oscillator);
386 *handle_ptr = (zyn_addsynth_handle)zyn_addsynth_ptr;
388 // printf("zyn_addsynth_create(%08X)\n", (unsigned int)*handle_ptr);
390 return true;
392 fail_free_synth:
394 fail:
395 return false;
398 #define zyn_addsynth_ptr ((struct zyn_addsynth *)handle)
400 void
401 zyn_addsynth_get_audio_output(
402 zyn_addsynth_handle handle,
403 zyn_sample_type * buffer_left,
404 zyn_sample_type * buffer_right)
406 unsigned int note_index;
407 zyn_sample_type note_buffer_left[SOUND_BUFFER_SIZE];
408 zyn_sample_type note_buffer_right[SOUND_BUFFER_SIZE];
409 bool note_active;
411 silence_two_buffers(buffer_left, buffer_right, SOUND_BUFFER_SIZE);
413 for (note_index = 0 ; note_index < zyn_addsynth_ptr->polyphony ; note_index++)
415 if (zyn_addsynth_ptr->notes_array[note_index].midinote != -1)
417 //printf("mixing note channel %u\n", note_index);
419 note_active = zyn_addnote_noteout(
420 zyn_addsynth_ptr->notes_array[note_index].note_handle,
421 note_buffer_left,
422 note_buffer_right);
424 mix_add_two_buffers(
425 buffer_left,
426 buffer_right,
427 note_buffer_left,
428 note_buffer_right,
429 SOUND_BUFFER_SIZE);
431 if (!note_active)
433 zyn_addsynth_ptr->notes_array[note_index].midinote = -1;
438 if (zyn_addsynth_ptr->all_sound_off)
440 fadeout_two_buffers(buffer_left, buffer_right, SOUND_BUFFER_SIZE);
442 for (note_index = 0 ; note_index < zyn_addsynth_ptr->polyphony ; note_index++)
444 if (zyn_addsynth_ptr->notes_array[note_index].midinote != -1)
446 zyn_addnote_force_disable(zyn_addsynth_ptr->notes_array[note_index].note_handle);
447 zyn_addsynth_ptr->notes_array[note_index].midinote = -1;
451 zyn_addsynth_ptr->all_sound_off = false;
454 zyn_portamento_update(&zyn_addsynth_ptr->portamento);
457 void
458 zyn_addsynth_note_on(
459 zyn_addsynth_handle handle,
460 unsigned int note,
461 unsigned int velocity)
463 unsigned int note_index;
464 int masterkeyshift;
465 float vel;
466 zyn_sample_type notebasefreq;
468 for (note_index = 0 ; note_index < zyn_addsynth_ptr->polyphony ; note_index++)
470 if (zyn_addsynth_ptr->notes_array[note_index].midinote == -1)
472 goto unused_note_channel_found;
476 //printf("note on %u - ignored\n", note);
478 return;
480 unused_note_channel_found:
481 //printf("note on %u - channel %u\n", note, note_index);
482 masterkeyshift = 0;
484 vel = VelF(velocity/127.0, zyn_addsynth_ptr->velsns);
485 notebasefreq = 440.0*pow(2.0,(note-69.0)/12.0);
487 // Portamento
489 if (zyn_addsynth_ptr->oldfreq < 1.0) /* only when the first note is played */
491 zyn_addsynth_ptr->oldfreq = notebasefreq;
494 bool portamento = zyn_portamento_start(zyn_addsynth_ptr->sample_rate, &zyn_addsynth_ptr->portamento, zyn_addsynth_ptr->oldfreq, notebasefreq);
496 zyn_addsynth_ptr->oldfreq = notebasefreq;
498 zyn_addsynth_ptr->notes_array[note_index].midinote = note;
500 zyn_addnote_note_on(
501 zyn_addsynth_ptr->notes_array[note_index].note_handle,
502 zyn_addsynth_ptr->random_panorama ? RND : zyn_addsynth_ptr->panorama,
503 zyn_addsynth_ptr->random_grouping,
504 notebasefreq,
505 vel,
506 portamento,
507 note);
510 void
511 zyn_addsynth_note_off(
512 zyn_addsynth_handle handle,
513 unsigned int note)
515 unsigned int note_index;
517 //printf("note off %u\n", note);
519 for (note_index = 0 ; note_index < zyn_addsynth_ptr->polyphony ; note_index++)
521 if (zyn_addsynth_ptr->notes_array[note_index].midinote == (char)note)
523 zyn_addnote_note_off(zyn_addsynth_ptr->notes_array[note_index].note_handle);
528 void
529 zyn_addsynth_all_notes_off(
530 zyn_addsynth_handle handle)
532 unsigned int note_index;
534 for (note_index = 0 ; note_index < zyn_addsynth_ptr->polyphony ; note_index++)
536 if (zyn_addsynth_ptr->notes_array[note_index].midinote != -1)
538 zyn_addnote_note_off(zyn_addsynth_ptr->notes_array[note_index].note_handle);
543 void
544 zyn_addsynth_all_sound_off(
545 zyn_addsynth_handle handle)
547 zyn_addsynth_ptr->all_sound_off = true;
550 void
551 zyn_addsynth_destroy(
552 zyn_addsynth_handle handle)
554 unsigned int voice_index;
556 free(zyn_addsynth_ptr->voices_components);
558 // printf("zyn_addsynth_destroy(%08X)\n", (unsigned int)handle);
559 zyn_fft_destroy(zyn_addsynth_ptr->fft);
561 // ADnoteParameters temp begin
563 for (voice_index = 0 ; voice_index < zyn_addsynth_ptr->voices_count ; voice_index++)
565 zyn_oscillator_uninit(&zyn_addsynth_ptr->voices_params_ptr[voice_index].oscillator);
566 zyn_oscillator_uninit(&zyn_addsynth_ptr->voices_params_ptr[voice_index].modulator_oscillator);
569 zyn_filter_sv_destroy(zyn_addsynth_ptr->filter_sv);
571 // ADnoteParameters temp end
573 free(zyn_addsynth_ptr->voices_params_ptr);
575 free(zyn_addsynth_ptr->notes_array);
577 free(zyn_addsynth_ptr->temporary_samples_ptr);
579 delete zyn_addsynth_ptr;
582 zyn_addsynth_component
583 zyn_addsynth_get_global_component(
584 zyn_addsynth_handle handle,
585 unsigned int component)
587 if (component >= ZYNADD_GLOBAL_COMPONENTS_COUNT)
589 assert(0);
590 return NULL;
593 return zyn_addsynth_ptr->global_components + component;
596 zyn_addsynth_component
597 zyn_addsynth_get_voice_component(
598 zyn_addsynth_handle handle,
599 unsigned int voice,
600 unsigned int component)
602 if (voice >= zyn_addsynth_ptr->voices_count)
604 assert(0);
605 return NULL;
608 if (component >= ZYNADD_VOICE_COMPONENTS_COUNT)
610 assert(0);
611 return NULL;
614 return zyn_addsynth_ptr->voices_components + voice * ZYNADD_VOICE_COMPONENTS_COUNT + component;
617 float percent_from_0_127(unsigned char value)
619 return ((float)(value)/127.0)*100.0; // 0-127 -> percent
622 unsigned char percent_to_0_127(float value)
624 return (unsigned char)roundf(value / 100.0 * 127.0);
627 #define component_ptr ((struct zyn_component_descriptor *)component)
629 float
630 zyn_addsynth_get_float_parameter(
631 zyn_addsynth_component component,
632 unsigned int parameter)
634 return component_ptr->get_float(component_ptr->context, parameter);
637 void
638 zyn_addsynth_set_float_parameter(
639 zyn_addsynth_component component,
640 unsigned int parameter,
641 float value)
643 return component_ptr->set_float(component_ptr->context, parameter, value);
646 bool
647 zyn_addsynth_get_bool_parameter(
648 zyn_addsynth_component component,
649 unsigned int parameter)
651 //LOG_DEBUG("component %p, context %p", component_ptr, component_ptr->context);
652 return component_ptr->get_bool(component_ptr->context, parameter);
655 void
656 zyn_addsynth_set_bool_parameter(
657 zyn_addsynth_component component,
658 unsigned int parameter,
659 bool value)
661 return component_ptr->set_bool(component_ptr->context, parameter, value);
664 signed int
665 zyn_addsynth_get_int_parameter(
666 zyn_addsynth_component component,
667 unsigned int parameter)
669 return component_ptr->get_int(component_ptr->context, parameter);
672 void
673 zyn_addsynth_set_int_parameter(
674 zyn_addsynth_component component,
675 unsigned int parameter,
676 signed int value)
678 return component_ptr->set_int(component_ptr->context, parameter, value);
681 #undef zyn_addsynth_ptr
683 void
684 zyn_addsynth_set_bandwidth(struct zyn_addsynth * zyn_addsynth_ptr, int value)
686 float tmp;
688 if (!zyn_addsynth_ptr->bandwidth_exponential)
690 if (value < 64 && zyn_addsynth_ptr->bandwidth_depth >= 64)
692 tmp = 1.0;
694 else
696 tmp = pow(25.0, pow(zyn_addsynth_ptr->bandwidth_depth / 127.0, 1.5)) - 1.0;
699 zyn_addsynth_ptr->bandwidth_relbw = (value / 64.0 - 1.0) * tmp + 1.0;
700 if (zyn_addsynth_ptr->bandwidth_relbw < 0.01)
702 zyn_addsynth_ptr->bandwidth_relbw = 0.01;
705 else
707 zyn_addsynth_ptr->bandwidth_relbw = pow(25.0, (value - 64.0) / 64.0 * (zyn_addsynth_ptr->bandwidth_depth / 64.0));
711 void
712 zyn_addsynth_set_modwheel(struct zyn_addsynth * zyn_addsynth_ptr, int value)
714 float tmp;
716 if (!zyn_addsynth_ptr->modwheel_exponential)
718 if ((value < 64) && (zyn_addsynth_ptr->modwheel_depth >= 64))
720 tmp = 1.0;
722 else
724 tmp = pow(25.0, pow(zyn_addsynth_ptr->modwheel_depth / 127.0, 1.5) * 2.0) / 25.0;
727 zyn_addsynth_ptr->modwheel_relmod = (value / 64.0 - 1.0) * tmp + 1.0;
728 if (zyn_addsynth_ptr->modwheel_relmod < 0.0)
730 zyn_addsynth_ptr->modwheel_relmod = 0.0;
733 else
735 zyn_addsynth_ptr->modwheel_relmod = pow(25.0 , (value - 64.0) / 64.0 * (zyn_addsynth_ptr->modwheel_depth / 80.0));