Add some foaf and doap info to zynadd.ttl
[zyn.git] / addsynth.cpp
blob93839bfe6d5bdbdeef103c8923e30b8af631c3c7
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->velsns = 64;
84 zyn_addsynth_ptr->fft = zyn_fft_create(OSCIL_SIZE);
86 // ADnoteParameters temp begin
88 zyn_addsynth_ptr->m_frequency_envelope_params.init_asr(0, false, 64, 50, 64, 60);
90 zyn_addsynth_ptr->m_amplitude_envelope_params.init_adsr(64, true, 0, 40, 127, 25, false);
92 zyn_addsynth_ptr->filter_type = ZYN_FILTER_TYPE_ANALOG;
93 zyn_addsynth_ptr->m_filter_params.init(sample_rate, ZYN_FILTER_ANALOG_TYPE_LPF2, 94, 40);
94 if (!zyn_filter_sv_create(sample_rate, ZYN_GLOBAL_FILTER_INITIAL_FREQUENCY, ZYN_GLOBAL_FILTER_INITIAL_Q, &zyn_addsynth_ptr->filter_sv))
96 goto fail_free_synth;
99 zyn_addsynth_ptr->m_filter_envelope_params.init_adsr_filter(0, true, 64, 40, 64, 70, 60, 64);
101 zyn_resonance_init(&zyn_addsynth_ptr->GlobalPar.resonance);
103 zyn_addsynth_ptr->voices_count = voices_count;
104 zyn_addsynth_ptr->voices_params_ptr = (struct zyn_addnote_voice_parameters *)malloc(sizeof(struct zyn_addnote_voice_parameters) * voices_count);
106 for (voice_index = 0 ; voice_index < voices_count ; voice_index++)
108 zyn_oscillator_init(
109 &zyn_addsynth_ptr->voices_params_ptr[voice_index].oscillator,
110 sample_rate,
111 zyn_addsynth_ptr->fft,
112 &zyn_addsynth_ptr->GlobalPar.resonance,
113 zyn_addsynth_ptr->temporary_samples_ptr,
114 &zyn_addsynth_ptr->oscillator_fft_frequencies);
116 zyn_oscillator_init(
117 &zyn_addsynth_ptr->voices_params_ptr[voice_index].modulator_oscillator,
118 sample_rate,
119 zyn_addsynth_ptr->fft,
120 NULL,
121 zyn_addsynth_ptr->temporary_samples_ptr,
122 &zyn_addsynth_ptr->oscillator_fft_frequencies);
124 zyn_addsynth_ptr->voices_params_ptr[voice_index].m_amplitude_envelope_params.init_adsr(64, true, 0, 100, 127, 100, false);
126 zyn_addsynth_ptr->voices_params_ptr[voice_index].amplitude_lfo_params.frequency = 90.0 / 127.0;
127 zyn_addsynth_ptr->voices_params_ptr[voice_index].amplitude_lfo_params.depth = 32.0 / 127.0;
128 zyn_addsynth_ptr->voices_params_ptr[voice_index].amplitude_lfo_params.random_start_phase = false;
129 zyn_addsynth_ptr->voices_params_ptr[voice_index].amplitude_lfo_params.start_phase = 0.5;
130 zyn_addsynth_ptr->voices_params_ptr[voice_index].amplitude_lfo_params.depth_randomness_enabled = false;
131 zyn_addsynth_ptr->voices_params_ptr[voice_index].amplitude_lfo_params.depth = 0;
132 zyn_addsynth_ptr->voices_params_ptr[voice_index].amplitude_lfo_params.frequency_randomness_enabled = false;
133 zyn_addsynth_ptr->voices_params_ptr[voice_index].amplitude_lfo_params.frequency_randomness = 0;
134 zyn_addsynth_ptr->voices_params_ptr[voice_index].amplitude_lfo_params.delay = 30.0 / 127.0 * 4.0;
135 zyn_addsynth_ptr->voices_params_ptr[voice_index].amplitude_lfo_params.stretch = 0;
136 zyn_addsynth_ptr->voices_params_ptr[voice_index].amplitude_lfo_params.shape = ZYN_LFO_SHAPE_TYPE_SINE;
138 zyn_addsynth_ptr->voices_params_ptr[voice_index].m_frequency_envelope_params.init_asr(0, false, 30, 40, 64, 60);
140 zyn_addsynth_ptr->voices_params_ptr[voice_index].frequency_lfo_params.frequency = 50.0 / 127.0;
141 zyn_addsynth_ptr->voices_params_ptr[voice_index].frequency_lfo_params.depth = 40.0 / 127.0;
142 zyn_addsynth_ptr->voices_params_ptr[voice_index].frequency_lfo_params.random_start_phase = false;
143 zyn_addsynth_ptr->voices_params_ptr[voice_index].frequency_lfo_params.start_phase = 0;
144 zyn_addsynth_ptr->voices_params_ptr[voice_index].frequency_lfo_params.depth_randomness_enabled = false;
145 zyn_addsynth_ptr->voices_params_ptr[voice_index].frequency_lfo_params.depth = 0;
146 zyn_addsynth_ptr->voices_params_ptr[voice_index].frequency_lfo_params.frequency_randomness_enabled = false;
147 zyn_addsynth_ptr->voices_params_ptr[voice_index].frequency_lfo_params.frequency_randomness = 0;
148 zyn_addsynth_ptr->voices_params_ptr[voice_index].frequency_lfo_params.delay = 0;
149 zyn_addsynth_ptr->voices_params_ptr[voice_index].frequency_lfo_params.stretch = 0;
150 zyn_addsynth_ptr->voices_params_ptr[voice_index].frequency_lfo_params.shape = ZYN_LFO_SHAPE_TYPE_SINE;
152 zyn_addsynth_ptr->voices_params_ptr[voice_index].m_filter_params.init(sample_rate, ZYN_FILTER_ANALOG_TYPE_LPF2, 50, 60);
153 zyn_addsynth_ptr->voices_params_ptr[voice_index].m_filter_envelope_params.init_adsr_filter(0, false, 90, 70, 40, 70, 10, 40);
155 zyn_addsynth_ptr->voices_params_ptr[voice_index].filter_lfo_params.frequency = 50.0 / 127.0;
156 zyn_addsynth_ptr->voices_params_ptr[voice_index].filter_lfo_params.depth = 20.0 / 127.0;
157 zyn_addsynth_ptr->voices_params_ptr[voice_index].filter_lfo_params.random_start_phase = false;
158 zyn_addsynth_ptr->voices_params_ptr[voice_index].filter_lfo_params.start_phase = 0.5;
159 zyn_addsynth_ptr->voices_params_ptr[voice_index].filter_lfo_params.depth_randomness_enabled = false;
160 zyn_addsynth_ptr->voices_params_ptr[voice_index].filter_lfo_params.depth = 0;
161 zyn_addsynth_ptr->voices_params_ptr[voice_index].filter_lfo_params.frequency_randomness_enabled = false;
162 zyn_addsynth_ptr->voices_params_ptr[voice_index].filter_lfo_params.frequency_randomness = 0;
163 zyn_addsynth_ptr->voices_params_ptr[voice_index].filter_lfo_params.delay = 0;
164 zyn_addsynth_ptr->voices_params_ptr[voice_index].filter_lfo_params.stretch = 0;
165 zyn_addsynth_ptr->voices_params_ptr[voice_index].filter_lfo_params.shape = ZYN_LFO_SHAPE_TYPE_SINE;
167 zyn_addsynth_ptr->voices_params_ptr[voice_index].m_fm_frequency_envelope_params.init_asr(0, false, 20, 90, 40, 80);
168 zyn_addsynth_ptr->voices_params_ptr[voice_index].m_fm_amplitude_envelope_params.init_adsr(64, true, 80, 90, 127, 100, false);
171 /* Frequency Global Parameters */
172 // zyn_addsynth_ptr->GlobalPar.stereo = true; // Stereo
173 zyn_addsynth_ptr->GlobalPar.PDetune=8192;//zero
174 zyn_addsynth_ptr->GlobalPar.PCoarseDetune=0;
175 zyn_addsynth_ptr->GlobalPar.PDetuneType=1;
176 zyn_addsynth_ptr->GlobalPar.PBandwidth=64;
178 /* Amplitude Global Parameters */
179 zyn_addsynth_ptr->GlobalPar.PVolume=90;
180 zyn_addsynth_ptr->GlobalPar.PAmpVelocityScaleFunction=64;
181 zyn_addsynth_ptr->GlobalPar.PPunchStrength=0;
182 zyn_addsynth_ptr->GlobalPar.PPunchTime=60;
183 zyn_addsynth_ptr->GlobalPar.PPunchStretch=64;
184 zyn_addsynth_ptr->GlobalPar.PPunchVelocitySensing=72;
186 /* Filter Global Parameters*/
187 zyn_addsynth_ptr->m_filter_velocity_sensing_amount = 0.5;
188 zyn_addsynth_ptr->m_filter_velocity_scale_function = 0;
189 zyn_addsynth_ptr->m_filter_params.defaults();
191 for (voice_index = 0 ; voice_index < voices_count ; voice_index++)
193 zyn_addsynth_ptr->voices_params_ptr[voice_index].enabled = false;
194 zyn_addsynth_ptr->voices_params_ptr[voice_index].white_noise = false;
195 zyn_addsynth_ptr->voices_params_ptr[voice_index].Pfixedfreq=0;
196 zyn_addsynth_ptr->voices_params_ptr[voice_index].PfixedfreqET=0;
197 zyn_addsynth_ptr->voices_params_ptr[voice_index].resonance = true;
198 zyn_addsynth_ptr->voices_params_ptr[voice_index].Pfilterbypass=0;
199 zyn_addsynth_ptr->voices_params_ptr[voice_index].Pextoscil=-1;
200 zyn_addsynth_ptr->voices_params_ptr[voice_index].PextFMoscil=-1;
201 zyn_addsynth_ptr->voices_params_ptr[voice_index].Poscilphase=64;
202 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFMoscilphase=64;
203 zyn_addsynth_ptr->voices_params_ptr[voice_index].PDelay=0;
204 zyn_addsynth_ptr->voices_params_ptr[voice_index].PVolume=100;
205 zyn_addsynth_ptr->voices_params_ptr[voice_index].PVolumeminus=0;
206 zyn_addsynth_ptr->voices_params_ptr[voice_index].PPanning=64;//center
207 zyn_addsynth_ptr->voices_params_ptr[voice_index].PDetune=8192;//8192=0
208 zyn_addsynth_ptr->voices_params_ptr[voice_index].PCoarseDetune=0;
209 zyn_addsynth_ptr->voices_params_ptr[voice_index].PDetuneType=0;
210 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFreqLfoEnabled=0;
211 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFreqEnvelopeEnabled=0;
212 zyn_addsynth_ptr->voices_params_ptr[voice_index].PAmpEnvelopeEnabled=0;
213 zyn_addsynth_ptr->voices_params_ptr[voice_index].PAmpLfoEnabled=0;
214 zyn_addsynth_ptr->voices_params_ptr[voice_index].PAmpVelocityScaleFunction=127;
215 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFilterEnabled=0;
216 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFilterEnvelopeEnabled=0;
217 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFilterLfoEnabled=0;
218 zyn_addsynth_ptr->voices_params_ptr[voice_index].fm_type = ZYN_FM_TYPE_NONE;
220 //I use the internal oscillator (-1)
221 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFMVoice=-1;
223 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFMVolume=90;
224 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFMVolumeDamp=64;
225 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFMDetune=8192;
226 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFMCoarseDetune=0;
227 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFMDetuneType=0;
228 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFMFreqEnvelopeEnabled=0;
229 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFMAmpEnvelopeEnabled=0;
230 zyn_addsynth_ptr->voices_params_ptr[voice_index].PFMVelocityScaleFunction=64;
232 zyn_addsynth_ptr->voices_params_ptr[voice_index].m_filter_params.defaults();
235 zyn_addsynth_ptr->voices_params_ptr[0].enabled = true;
237 // ADnoteParameters temp end
239 zyn_addsynth_ptr->bandwidth_depth = 64;
240 zyn_addsynth_ptr->bandwidth_exponential = false;
241 zyn_addsynth_ptr->modwheel_depth = 80;
242 zyn_addsynth_ptr->modwheel_exponential = false;
244 zyn_addsynth_set_bandwidth(zyn_addsynth_ptr, 64);
245 zyn_addsynth_set_modwheel(zyn_addsynth_ptr, 64);
247 zyn_portamento_init(&zyn_addsynth_ptr->portamento);
249 zyn_addsynth_ptr->pitch_bend_range = 200.0; // 200 cents = 2 halftones
250 zyn_addsynth_ptr->pitch_bend = 0; // center
251 ZYN_UPDATE_PITCH_BEND(zyn_addsynth_ptr);
253 zyn_addsynth_ptr->oldfreq = -1.0;
255 zyn_addsynth_ptr->random_panorama = false;
256 zyn_addsynth_ptr->panorama = 0.0;
258 zyn_addsynth_ptr->stereo = true;
260 zyn_addsynth_ptr->random_grouping = false;
262 zyn_addsynth_ptr->amplitude_lfo_params.frequency = 80.0 / 127.0;
263 zyn_addsynth_ptr->amplitude_lfo_params.depth = 0;
264 zyn_addsynth_ptr->amplitude_lfo_params.random_start_phase = false;
265 zyn_addsynth_ptr->amplitude_lfo_params.start_phase = 0.5;
266 zyn_addsynth_ptr->amplitude_lfo_params.depth_randomness_enabled = false;
267 zyn_addsynth_ptr->amplitude_lfo_params.depth_randomness = 0.5;
268 zyn_addsynth_ptr->amplitude_lfo_params.frequency_randomness_enabled = false;
269 zyn_addsynth_ptr->amplitude_lfo_params.frequency_randomness = 0.5;
270 zyn_addsynth_ptr->amplitude_lfo_params.delay = 0;
271 zyn_addsynth_ptr->amplitude_lfo_params.stretch = 0;
272 zyn_addsynth_ptr->amplitude_lfo_params.shape = ZYN_LFO_SHAPE_TYPE_SINE;
274 zyn_addsynth_ptr->filter_lfo_params.frequency = 80.0 / 127.0;
275 zyn_addsynth_ptr->filter_lfo_params.depth = 0;
276 zyn_addsynth_ptr->filter_lfo_params.random_start_phase = false;
277 zyn_addsynth_ptr->filter_lfo_params.start_phase = 0.5;
278 zyn_addsynth_ptr->filter_lfo_params.depth_randomness_enabled = false;
279 zyn_addsynth_ptr->filter_lfo_params.depth_randomness = 0.5;
280 zyn_addsynth_ptr->filter_lfo_params.frequency_randomness_enabled = false;
281 zyn_addsynth_ptr->filter_lfo_params.frequency_randomness = 0.5;
282 zyn_addsynth_ptr->filter_lfo_params.delay = 0;
283 zyn_addsynth_ptr->filter_lfo_params.stretch = 0;
284 zyn_addsynth_ptr->filter_lfo_params.shape = ZYN_LFO_SHAPE_TYPE_SINE;
286 zyn_addsynth_ptr->frequency_lfo_params.frequency = 70.0 / 127.0;
287 zyn_addsynth_ptr->frequency_lfo_params.depth = 0;
288 zyn_addsynth_ptr->frequency_lfo_params.random_start_phase = false;
289 zyn_addsynth_ptr->frequency_lfo_params.start_phase = 0.5;
290 zyn_addsynth_ptr->frequency_lfo_params.depth_randomness_enabled = false;
291 zyn_addsynth_ptr->frequency_lfo_params.depth_randomness = 0.5;
292 zyn_addsynth_ptr->frequency_lfo_params.frequency_randomness_enabled = false;
293 zyn_addsynth_ptr->frequency_lfo_params.frequency_randomness = 0.5;
294 zyn_addsynth_ptr->frequency_lfo_params.delay = 0;
295 zyn_addsynth_ptr->frequency_lfo_params.stretch = 0;
296 zyn_addsynth_ptr->frequency_lfo_params.shape = ZYN_LFO_SHAPE_TYPE_SINE;
298 for (note_index = 0 ; note_index < ZYN_DEFAULT_POLYPHONY ; note_index++)
300 zyn_addsynth_ptr->notes_array[note_index].note_ptr = new ADnote(zyn_addsynth_ptr);
301 zyn_addsynth_ptr->notes_array[note_index].midinote = -1;
304 // init global components
306 zyn_addsynth_component_init_amp_globals(
307 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_AMP_GLOBALS,
308 zyn_addsynth_ptr);
310 zyn_addsynth_component_init_amp_envelope(
311 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_AMP_ENV,
312 &zyn_addsynth_ptr->m_amplitude_envelope_params);
314 zyn_addsynth_component_init_lfo(
315 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_AMP_LFO,
316 &zyn_addsynth_ptr->amplitude_lfo_params);
318 zyn_addsynth_component_init_filter_globals(
319 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_FILTER_GLOBALS,
320 zyn_addsynth_ptr);
322 zyn_addsynth_component_init_filter_analog(
323 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_FILTER_ANALOG,
324 zyn_addsynth_ptr);
326 zyn_addsynth_component_init_filter_formant(
327 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_FILTER_FORMANT,
328 zyn_addsynth_ptr);
330 zyn_addsynth_component_init_filter_sv(
331 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_FILTER_SV,
332 zyn_addsynth_ptr->filter_sv);
334 zyn_addsynth_component_init_filter_envelope(
335 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_FILTER_ENV,
336 &zyn_addsynth_ptr->m_filter_envelope_params);
338 zyn_addsynth_component_init_lfo(
339 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_FILTER_LFO,
340 &zyn_addsynth_ptr->filter_lfo_params);
342 zyn_addsynth_component_init_frequency_globals(
343 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_FREQUENCY_GLOBALS);
345 zyn_addsynth_component_init_frequency_envelope(
346 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_FREQUENCY_ENV,
347 &zyn_addsynth_ptr->m_frequency_envelope_params);
349 zyn_addsynth_component_init_lfo(
350 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_FREQUENCY_LFO,
351 &zyn_addsynth_ptr->frequency_lfo_params);
353 zyn_addsynth_component_init_portamento(
354 zyn_addsynth_ptr->global_components + ZYNADD_COMPONENT_PORTAMENTO,
355 &zyn_addsynth_ptr->portamento);
357 // init voices components
359 zyn_addsynth_ptr->voices_components =
360 (struct zyn_component_descriptor *)malloc(
361 sizeof(struct zyn_component_descriptor) * voices_count * ZYNADD_VOICE_COMPONENTS_COUNT);
363 for (voice_index = 0 ; voice_index < voices_count ; voice_index++)
365 zyn_addsynth_component_init_voice_globals(
366 zyn_addsynth_ptr->voices_components + voice_index * ZYNADD_VOICE_COMPONENTS_COUNT + ZYNADD_COMPONENT_VOICE_GLOBALS,
367 zyn_addsynth_ptr->voices_params_ptr + voice_index);
369 zyn_addsynth_component_init_oscillator(
370 zyn_addsynth_ptr->voices_components + voice_index * ZYNADD_VOICE_COMPONENTS_COUNT + ZYNADD_COMPONENT_VOICE_OSCILLATOR,
371 &zyn_addsynth_ptr->voices_params_ptr[voice_index].oscillator);
374 *handle_ptr = (zyn_addsynth_handle)zyn_addsynth_ptr;
376 // printf("zyn_addsynth_create(%08X)\n", (unsigned int)*handle_ptr);
378 return true;
380 fail_free_synth:
382 fail:
383 return false;
386 #define zyn_addsynth_ptr ((struct zyn_addsynth *)handle)
388 void
389 zyn_addsynth_get_audio_output(
390 zyn_addsynth_handle handle,
391 zyn_sample_type * buffer_left,
392 zyn_sample_type * buffer_right)
394 unsigned int note_index;
395 zyn_sample_type note_buffer_left[SOUND_BUFFER_SIZE];
396 zyn_sample_type note_buffer_right[SOUND_BUFFER_SIZE];
398 silence_two_buffers(buffer_left, buffer_right, SOUND_BUFFER_SIZE);
400 for (note_index = 0 ; note_index < zyn_addsynth_ptr->polyphony ; note_index++)
402 if (zyn_addsynth_ptr->notes_array[note_index].midinote != -1)
404 //printf("mixing note channel %u\n", note_index);
406 zyn_addsynth_ptr->notes_array[note_index].note_ptr->noteout(note_buffer_left, note_buffer_right);
408 mix_add_two_buffers(
409 buffer_left,
410 buffer_right,
411 note_buffer_left,
412 note_buffer_right,
413 SOUND_BUFFER_SIZE);
415 if (zyn_addsynth_ptr->notes_array[note_index].note_ptr->finished())
417 zyn_addsynth_ptr->notes_array[note_index].midinote = -1;
422 zyn_portamento_update(&zyn_addsynth_ptr->portamento);
425 void
426 zyn_addsynth_note_on(
427 zyn_addsynth_handle handle,
428 unsigned int note,
429 unsigned int velocity)
431 unsigned int note_index;
432 int masterkeyshift;
433 float vel;
434 zyn_sample_type notebasefreq;
436 for (note_index = 0 ; note_index < zyn_addsynth_ptr->polyphony ; note_index++)
438 if (zyn_addsynth_ptr->notes_array[note_index].midinote == -1)
440 goto unused_note_channel_found;
444 //printf("note on %u - ignored\n", note);
446 return;
448 unused_note_channel_found:
449 //printf("note on %u - channel %u\n", note, note_index);
450 masterkeyshift = 0;
452 vel = VelF(velocity/127.0, zyn_addsynth_ptr->velsns);
453 notebasefreq = 440.0*pow(2.0,(note-69.0)/12.0);
455 // Portamento
457 if (zyn_addsynth_ptr->oldfreq < 1.0) /* only when the first note is played */
459 zyn_addsynth_ptr->oldfreq = notebasefreq;
462 bool portamento = zyn_portamento_start(zyn_addsynth_ptr->sample_rate, &zyn_addsynth_ptr->portamento, zyn_addsynth_ptr->oldfreq, notebasefreq);
464 zyn_addsynth_ptr->oldfreq = notebasefreq;
466 zyn_addsynth_ptr->notes_array[note_index].midinote = note;
467 zyn_addsynth_ptr->notes_array[note_index].note_ptr->note_on(
468 zyn_addsynth_ptr->random_panorama ? RND : zyn_addsynth_ptr->panorama,
469 zyn_addsynth_ptr->random_grouping,
470 notebasefreq,
471 vel,
472 portamento,
473 note);
476 void
477 zyn_addsynth_note_off(
478 zyn_addsynth_handle handle,
479 unsigned int note)
481 unsigned int note_index;
483 //printf("note off %u\n", note);
485 for (note_index = 0 ; note_index < zyn_addsynth_ptr->polyphony ; note_index++)
487 if (zyn_addsynth_ptr->notes_array[note_index].midinote == (char)note)
489 zyn_addsynth_ptr->notes_array[note_index].note_ptr->relasekey();
494 void
495 zyn_addsynth_destroy(
496 zyn_addsynth_handle handle)
498 unsigned int voice_index;
500 free(zyn_addsynth_ptr->voices_components);
502 // printf("zyn_addsynth_destroy(%08X)\n", (unsigned int)handle);
503 zyn_fft_destroy(zyn_addsynth_ptr->fft);
505 // ADnoteParameters temp begin
507 for (voice_index = 0 ; voice_index < zyn_addsynth_ptr->voices_count ; voice_index++)
509 zyn_oscillator_uninit(&zyn_addsynth_ptr->voices_params_ptr[voice_index].oscillator);
510 zyn_oscillator_uninit(&zyn_addsynth_ptr->voices_params_ptr[voice_index].modulator_oscillator);
513 zyn_filter_sv_destroy(zyn_addsynth_ptr->filter_sv);
515 // ADnoteParameters temp end
517 free(zyn_addsynth_ptr->voices_params_ptr);
519 free(zyn_addsynth_ptr->notes_array);
521 free(zyn_addsynth_ptr->temporary_samples_ptr);
523 delete zyn_addsynth_ptr;
526 zyn_addsynth_component
527 zyn_addsynth_get_global_component(
528 zyn_addsynth_handle handle,
529 unsigned int component)
531 if (component >= ZYNADD_GLOBAL_COMPONENTS_COUNT)
533 assert(0);
534 return NULL;
537 return zyn_addsynth_ptr->global_components + component;
540 zyn_addsynth_component
541 zyn_addsynth_get_voice_component(
542 zyn_addsynth_handle handle,
543 unsigned int component)
545 if (component >= ZYNADD_VOICE_COMPONENTS_COUNT)
547 assert(0);
548 return NULL;
551 return zyn_addsynth_ptr->voices_components + component;
554 float percent_from_0_127(unsigned char value)
556 return ((float)(value)/127.0)*100.0; // 0-127 -> percent
559 unsigned char percent_to_0_127(float value)
561 return (unsigned char)roundf(value / 100.0 * 127.0);
564 #define component_ptr ((struct zyn_component_descriptor *)component)
566 float
567 zyn_addsynth_get_float_parameter(
568 zyn_addsynth_component component,
569 unsigned int parameter)
571 return component_ptr->get_float(component_ptr->context, parameter);
574 void
575 zyn_addsynth_set_float_parameter(
576 zyn_addsynth_component component,
577 unsigned int parameter,
578 float value)
580 return component_ptr->set_float(component_ptr->context, parameter, value);
583 bool
584 zyn_addsynth_get_bool_parameter(
585 zyn_addsynth_component component,
586 unsigned int parameter)
588 //LOG_DEBUG("component %p, context %p", component_ptr, component_ptr->context);
589 return component_ptr->get_bool(component_ptr->context, parameter);
592 void
593 zyn_addsynth_set_bool_parameter(
594 zyn_addsynth_component component,
595 unsigned int parameter,
596 bool value)
598 return component_ptr->set_bool(component_ptr->context, parameter, value);
601 signed int
602 zyn_addsynth_get_int_parameter(
603 zyn_addsynth_component component,
604 unsigned int parameter)
606 return component_ptr->get_int(component_ptr->context, parameter);
609 void
610 zyn_addsynth_set_int_parameter(
611 zyn_addsynth_component component,
612 unsigned int parameter,
613 signed int value)
615 return component_ptr->set_int(component_ptr->context, parameter, value);
618 #undef zyn_addsynth_ptr
620 void
621 zyn_addsynth_set_bandwidth(struct zyn_addsynth * zyn_addsynth_ptr, int value)
623 float tmp;
625 if (!zyn_addsynth_ptr->bandwidth_exponential)
627 if (value < 64 && zyn_addsynth_ptr->bandwidth_depth >= 64)
629 tmp = 1.0;
631 else
633 tmp = pow(25.0, pow(zyn_addsynth_ptr->bandwidth_depth / 127.0, 1.5)) - 1.0;
636 zyn_addsynth_ptr->bandwidth_relbw = (value / 64.0 - 1.0) * tmp + 1.0;
637 if (zyn_addsynth_ptr->bandwidth_relbw < 0.01)
639 zyn_addsynth_ptr->bandwidth_relbw = 0.01;
642 else
644 zyn_addsynth_ptr->bandwidth_relbw = pow(25.0, (value - 64.0) / 64.0 * (zyn_addsynth_ptr->bandwidth_depth / 64.0));
648 void
649 zyn_addsynth_set_modwheel(struct zyn_addsynth * zyn_addsynth_ptr, int value)
651 float tmp;
653 if (!zyn_addsynth_ptr->modwheel_exponential)
655 if ((value < 64) && (zyn_addsynth_ptr->modwheel_depth >= 64))
657 tmp = 1.0;
659 else
661 tmp = pow(25.0, pow(zyn_addsynth_ptr->modwheel_depth / 127.0, 1.5) * 2.0) / 25.0;
664 zyn_addsynth_ptr->modwheel_relmod = (value / 64.0 - 1.0) * tmp + 1.0;
665 if (zyn_addsynth_ptr->modwheel_relmod < 0.0)
667 zyn_addsynth_ptr->modwheel_relmod = 0.0;
670 else
672 zyn_addsynth_ptr->modwheel_relmod = pow(25.0 , (value - 64.0) / 64.0 * (zyn_addsynth_ptr->modwheel_depth / 80.0));