1 // Licensed GNU LGPL v3 or later: http://www.gnu.org/licenses/lgpl.html
2
3 #include "smlivedecoder.hh"
4 #include "smmath.hh"
5 #include "smleakdebugger.hh"
6 #include "smutils.hh"
7
8 #include <stdio.h>
9 #include <assert.h>
10
11 using namespace SpectMorph;
12
13 using std::vector;
14 using std::min;
15 using std::max;
16
17 static LeakDebugger leak_debugger ("SpectMorph::LiveDecoder");
18
19 #define ANTIALIAS_FILTER_TABLE_SIZE 256
20
21 #define DEBUG (0)
22
23 static vector<float> antialias_filter_table;
24
25 static void
init_aa_filter()26 init_aa_filter()
27 {
28 if (antialias_filter_table.empty())
29 {
30 antialias_filter_table.resize (ANTIALIAS_FILTER_TABLE_SIZE);
31
32 const double db_at_nyquist = -60;
33
34 for (size_t i = 0; i < antialias_filter_table.size(); i++)
35 antialias_filter_table[i] = db_to_factor (double (i) / ANTIALIAS_FILTER_TABLE_SIZE * db_at_nyquist);
36 }
37 }
38
39 static float
freq_to_note(float freq)40 freq_to_note (float freq)
41 {
42 return 69 + 12 * log (freq / 440) / log (2);
43 }
44 static inline double
fmatch(double f1,double f2)45 fmatch (double f1, double f2)
46 {
47 return f2 < (f1 * 1.05) && f2 > (f1 * 0.95);
48 }
49
LiveDecoder()50 LiveDecoder::LiveDecoder() :
51 smset (NULL),
52 audio (NULL),
53 ifft_synth (NULL),
54 noise_decoder (NULL),
55 source (NULL),
56 sines_enabled (true),
57 noise_enabled (true),
58 debug_fft_perf_enabled (false),
59 original_samples_enabled (false),
60 loop_enabled (true),
61 start_skip_enabled (false),
62 noise_seed (-1),
63 sse_samples (NULL),
64 vibrato_enabled (false)
65 {
66 init_aa_filter();
67 set_unison_voices (1, 0);
68 leak_debugger.add (this);
69 }
70
LiveDecoder(WavSet * smset)71 LiveDecoder::LiveDecoder (WavSet *smset) :
72 LiveDecoder()
73 {
74 this->smset = smset;
75 }
76
LiveDecoder(LiveDecoderSource * source)77 LiveDecoder::LiveDecoder (LiveDecoderSource *source) :
78 LiveDecoder()
79 {
80 this->source = source;
81 }
82
~LiveDecoder()83 LiveDecoder::~LiveDecoder()
84 {
85 if (ifft_synth)
86 {
87 delete ifft_synth;
88 ifft_synth = NULL;
89 }
90 if (noise_decoder)
91 {
92 delete noise_decoder;
93 noise_decoder = NULL;
94 }
95 if (sse_samples)
96 {
97 delete sse_samples;
98 sse_samples = NULL;
99 }
100 leak_debugger.del (this);
101 }
102
103 void
retrigger(int channel,float freq,int midi_velocity,float mix_freq)104 LiveDecoder::retrigger (int channel, float freq, int midi_velocity, float mix_freq)
105 {
106 Audio *best_audio = 0;
107 double best_diff = 1e10;
108
109 if (source)
110 {
111 source->retrigger (channel, freq, midi_velocity, mix_freq);
112 best_audio = source->audio();
113 }
114 else
115 {
116 if (smset)
117 {
118 float note = freq_to_note (freq);
119
120 // find best audio candidate
121 for (vector<WavSetWave>::iterator wi = smset->waves.begin(); wi != smset->waves.end(); wi++)
122 {
123 Audio *audio = wi->audio;
124 if (audio && wi->channel == channel &&
125 wi->velocity_range_min <= midi_velocity &&
126 wi->velocity_range_max >= midi_velocity)
127 {
128 float audio_note = freq_to_note (audio->fundamental_freq);
129
130 if (fabs (audio_note - note) < best_diff)
131 {
132 best_diff = fabs (audio_note - note);
133 best_audio = audio;
134 }
135 }
136 }
137 }
138 }
139 audio = best_audio;
140
141 if (best_audio)
142 {
143 frame_size = audio->frame_size_ms * mix_freq / 1000;
144 frame_step = audio->frame_step_ms * mix_freq / 1000;
145 zero_values_at_start_scaled = audio->zero_values_at_start * mix_freq / audio->mix_freq;
146 loop_start_scaled = audio->loop_start * mix_freq / audio->mix_freq;
147 loop_end_scaled = audio->loop_end * mix_freq / audio->mix_freq;
148 loop_point = (get_loop_type() == Audio::LOOP_NONE) ? -1 : audio->loop_start;
149
150 block_size = NoiseDecoder::preferred_block_size (mix_freq);
151
152 /* start skip: skip the first half block to avoid fade-in at start
153 * this will produce clicks unless an external envelope is applied
154 */
155 if (start_skip_enabled)
156 zero_values_at_start_scaled += block_size / 2;
157
158 if (noise_decoder)
159 delete noise_decoder;
160 noise_decoder = new NoiseDecoder (mix_freq, block_size);
161
162 if (noise_seed != -1)
163 noise_decoder->set_seed (noise_seed);
164
165 if (ifft_synth)
166 delete ifft_synth;
167 ifft_synth = new IFFTSynth (block_size, mix_freq, IFFTSynth::WIN_HANNING);
168
169 if (sse_samples)
170 delete sse_samples;
171 sse_samples = new AlignedArray<float, 16> (block_size);
172
173 pp_inter = PolyPhaseInter::the(); // do not delete
174
175 have_samples = 0;
176 pos = 0;
177 frame_idx = 0;
178 env_pos = 0;
179 original_sample_pos = 0;
180 original_samples_norm_factor = db_to_factor (audio->original_samples_norm_db);
181
182 done_state = DoneState::ACTIVE;
183
184 // reset partial state vectors
185 pstate[0].clear();
186 pstate[1].clear();
187 last_pstate = &pstate[0];
188
189 // reset unison phases
190 unison_phases[0].clear();
191 unison_phases[1].clear();
192
193 // setup portamento state
194 assert (PortamentoState::DELTA >= pp_inter->get_min_padding());
195
196 portamento_state.pos = PortamentoState::DELTA;
197 portamento_state.buffer.resize (PortamentoState::DELTA);
198 portamento_state.active = false;
199
200 // setup vibrato state
201 vibrato_phase = 0;
202 vibrato_env = 0;
203 }
204 current_freq = freq;
205 current_mix_freq = mix_freq;
206 }
207
208 size_t
compute_loop_frame_index(size_t frame_idx,Audio * audio)209 LiveDecoder::compute_loop_frame_index (size_t frame_idx, Audio *audio)
210 {
211 if (int (frame_idx) > audio->loop_start)
212 {
213 g_return_val_if_fail (audio->loop_end >= audio->loop_start, frame_idx);
214
215 if (audio->loop_type == Audio::LOOP_FRAME_FORWARD)
216 {
217 size_t loop_len = audio->loop_end + 1 - audio->loop_start;
218 frame_idx = audio->loop_start + (frame_idx - audio->loop_start) % loop_len;
219 }
220 else if (audio->loop_type == Audio::LOOP_FRAME_PING_PONG)
221 {
222 size_t loop_len = audio->loop_end - audio->loop_start;
223 if (loop_len > 0)
224 {
225 size_t ping_pong_len = loop_len * 2;
226 size_t ping_pong_pos = (frame_idx - audio->loop_start) % ping_pong_len;
227
228 if (ping_pong_pos < loop_len) // ping part of the ping-pong loop (forward)
229 {
230 frame_idx = audio->loop_start + ping_pong_pos;
231 }
232 else // pong part of the ping-pong loop (backward)
233 {
234 frame_idx = audio->loop_end - (ping_pong_pos - loop_len);
235 }
236 }
237 else
238 {
239 frame_idx = audio->loop_start;
240 }
241 }
242 }
243 return frame_idx;
244 }
245
246 void
process_internal(size_t n_values,float * audio_out,float portamento_stretch)247 LiveDecoder::process_internal (size_t n_values, float *audio_out, float portamento_stretch)
248 {
249 assert (audio); // need selected (triggered) audio to use this function
250
251 if (original_samples_enabled)
252 {
253 /* we can skip the resampler if the phase increment is always 1.0
254 * this ensures that the original samples are reproduced exactly
255 * in this case
256 */
257 bool need_resample = true;
258 const double phase_inc = (current_freq / audio->fundamental_freq) *
259 (audio->mix_freq / current_mix_freq);
260 if (fabs (phase_inc - 1.0) < 1e-6)
261 need_resample = false;
262
263 for (unsigned int i = 0; i < n_values; i++)
264 {
265 double want_freq = current_freq;
266 double phase_inc = (want_freq / audio->fundamental_freq) *
267 (audio->mix_freq / current_mix_freq);
268
269 int ipos = original_sample_pos;
270 float frac = original_sample_pos - ipos;
271
272 if (get_loop_type() == Audio::LOOP_TIME_FORWARD)
273 {
274 while (ipos >= (audio->loop_end - audio->zero_values_at_start))
275 ipos -= (audio->loop_end - audio->loop_start);
276 }
277
278 if (need_resample)
279 {
280 audio_out[i] = pp_inter->get_sample (audio->original_samples, ipos + frac) * original_samples_norm_factor;
281 }
282 else
283 {
284 if (ipos >= 0 && size_t (ipos) < audio->original_samples.size())
285 audio_out[i] = audio->original_samples[ipos] * original_samples_norm_factor;
286 else
287 audio_out[i] = 0;
288 }
289
290 original_sample_pos += phase_inc;
291 }
292 if (original_sample_pos > audio->original_samples.size() && get_loop_type() != Audio::LOOP_TIME_FORWARD)
293 {
294 if (done_state == DoneState::ACTIVE)
295 done_state = DoneState::ALMOST_DONE;
296 }
297 return;
298 }
299
300 const double portamento_env_step = 1 / portamento_stretch;
301 unsigned int i = 0;
302 while (i < n_values)
303 {
304 if (have_samples == 0)
305 {
306 double want_freq = current_freq;
307
308 std::copy (&(*sse_samples)[block_size / 2], &(*sse_samples)[block_size], &(*sse_samples)[0]);
309 zero_float_block (block_size / 2, &(*sse_samples)[block_size / 2]);
310
311 if (get_loop_type() == Audio::LOOP_TIME_FORWARD)
312 {
313 size_t xenv_pos = env_pos;
314
315 if (xenv_pos > loop_start_scaled)
316 {
317 xenv_pos = (xenv_pos - loop_start_scaled) % (loop_end_scaled - loop_start_scaled);
318 xenv_pos += loop_start_scaled;
319 }
320 frame_idx = xenv_pos / frame_step;
321 }
322 else if (get_loop_type() == Audio::LOOP_FRAME_FORWARD || get_loop_type() == Audio::LOOP_FRAME_PING_PONG)
323 {
324 frame_idx = compute_loop_frame_index (env_pos / frame_step, audio);
325 }
326 else
327 {
328 frame_idx = env_pos / frame_step;
329 if (loop_point != -1 && frame_idx > size_t (loop_point)) /* if in loop mode: loop current frame */
330 frame_idx = loop_point;
331 }
332
333 AudioBlock *audio_block_ptr = NULL;
334 if (source)
335 {
336 audio_block_ptr = source->audio_block (frame_idx);
337 }
338 else if (frame_idx < audio->contents.size())
339 {
340 audio_block_ptr = &audio->contents[frame_idx];
341 }
342 if (audio_block_ptr)
343 {
344 const AudioBlock& audio_block = *audio_block_ptr;
345
346 assert (audio_block.freqs.size() == audio_block.mags.size());
347
348 ifft_synth->clear_partials();
349
350 // point n_pstate to pstate[0] and pstate[1] alternately (one holds points to last state and the other points to new state)
351 bool lps_zero = (last_pstate == &pstate[0]);
352 vector<PartialState>& new_pstate = lps_zero ? pstate[1] : pstate[0];
353 const vector<PartialState>& old_pstate = lps_zero ? pstate[0] : pstate[1];
354 vector<float>& unison_new_phases = lps_zero ? unison_phases[1] : unison_phases[0];
355 const vector<float>& unison_old_phases = lps_zero ? unison_phases[0] : unison_phases[1];
356
357 if (unison_voices != 1)
358 {
359 // check unison phases size corresponds to old partial state size
360 assert (unison_voices * old_pstate.size() == unison_old_phases.size());
361 }
362 new_pstate.clear(); // clear old partial state
363 unison_new_phases.clear(); // and old unison phase information
364
365 if (sines_enabled)
366 {
367 const double phase_factor = block_size * M_PI / current_mix_freq;
368 const double filter_fact = 18000.0 / 44100.0; // for 44.1 kHz, filter at 18 kHz (higher mix freq => higher filter)
369 const double filter_min_freq = filter_fact * current_mix_freq;
370
371 size_t old_partial = 0;
372 for (size_t partial = 0; partial < audio_block.freqs.size(); partial++)
373 {
374 const double freq = audio_block.freqs_f (partial) * want_freq;
375
376 // anti alias filter:
377 double mag = audio_block.mags_f (partial);
378 double phase = 0; //atan2 (smag, cmag); FIXME: Does initial phase matter? I think not.
379
380 // portamento:
381 // - portamento_stretch > 1 means we read out faster
382 // => this means the aliasing starts at lower frequencies
383 const double portamento_freq = freq * max (portamento_stretch, 1.0f);
384 if (portamento_freq > filter_min_freq)
385 {
386 double norm_freq = portamento_freq / current_mix_freq;
387 if (norm_freq > 0.5)
388 {
389 // above nyquist freq -> since partials are sorted, there is nothing more to do for this frame
390 break;
391 }
392 else
393 {
394 // between filter_fact and 0.5 (db linear filter)
395 int index = sm_round_positive (ANTIALIAS_FILTER_TABLE_SIZE * (norm_freq - filter_fact) / (0.5 - filter_fact));
396 if (index >= 0)
397 {
398 if (index < ANTIALIAS_FILTER_TABLE_SIZE)
399 mag *= antialias_filter_table[index];
400 else
401 mag = 0;
402 }
403 else
404 {
405 // filter magnitude is supposed to be 1.0
406 }
407 }
408 }
409
410 /*
411 * increment old_partial as long as there is a better candidate (closer to freq)
412 */
413 bool freq_match = false;
414 if (!old_pstate.empty())
415 {
416 double best_fdiff = fabs (old_pstate[old_partial].freq - freq);
417
418 while ((old_partial + 1) < old_pstate.size())
419 {
420 double fdiff = fabs (old_pstate[old_partial + 1].freq - freq);
421 if (fdiff < best_fdiff)
422 {
423 old_partial++;
424 best_fdiff = fdiff;
425 }
426 else
427 {
428 break;
429 }
430 }
431 const double lfreq = old_pstate[old_partial].freq;
432 freq_match = fmatch (lfreq, freq);
433 }
434 if (DEBUG)
435 printf ("%d:F %.17g %.17g\n", int (env_pos), freq, mag);
436
437 if (unison_voices == 1)
438 {
439 if (freq_match)
440 {
441 // matching freq -> compute new phase
442 const double lfreq = old_pstate[old_partial].freq;
443 const double lphase = old_pstate[old_partial].phase;
444
445 phase = fmod (lphase + lfreq * phase_factor, 2 * M_PI);
446
447 if (DEBUG)
448 printf ("%d:L %.17g %.17g %.17g\n", int (env_pos), lfreq, freq, mag);
449 }
450 ifft_synth->render_partial (freq, mag, phase);
451 }
452 else
453 {
454 mag *= unison_gain;
455
456 for (int i = 0; i < unison_voices; i++)
457 {
458 if (freq_match)
459 {
460 const double lfreq = old_pstate[old_partial].freq;
461 const double lphase = unison_old_phases[old_partial * unison_voices + i];
462
463 phase = fmod (lphase + lfreq * phase_factor * unison_freq_factor[i], 2 * M_PI);
464 }
465 else
466 {
467 // randomize start phase for unison
468
469 phase = unison_phase_random_gen.random_double_range (0, 2 * M_PI);
470 }
471
472 ifft_synth->render_partial (freq * unison_freq_factor[i], mag, phase);
473
474 unison_new_phases.push_back (phase);
475 }
476 }
477
478 PartialState ps;
479 ps.freq = freq;
480 ps.phase = phase;
481 new_pstate.push_back (ps);
482 }
483 }
484 last_pstate = &new_pstate;
485
486 if (noise_enabled)
487 noise_decoder->process (audio_block, ifft_synth->fft_buffer(), NoiseDecoder::FFT_SPECTRUM, portamento_stretch);
488
489 if (noise_enabled || sines_enabled || debug_fft_perf_enabled)
490 {
491 float *samples = &(*sse_samples)[0];
492 ifft_synth->get_samples (samples, IFFTSynth::ADD);
493 }
494 }
495 else
496 {
497 if (done_state == DoneState::ACTIVE)
498 done_state = DoneState::ALMOST_DONE;
499 }
500 pos = 0;
501 have_samples = block_size / 2;
502 }
503
504 g_assert (have_samples > 0);
505 if (env_pos >= zero_values_at_start_scaled)
506 {
507 // decode envelope
508 const double time_ms = env_pos * 1000.0 / current_mix_freq;
509 if (time_ms < audio->attack_start_ms)
510 {
511 audio_out[i++] = 0;
512 pos++;
513 env_pos += portamento_env_step;
514 have_samples--;
515 }
516 else if (time_ms < audio->attack_end_ms)
517 {
518 audio_out[i++] = (*sse_samples)[pos] * (time_ms - audio->attack_start_ms) / (audio->attack_end_ms - audio->attack_start_ms);
519 pos++;
520 env_pos += portamento_env_step;
521 have_samples--;
522 }
523 else // envelope is 1 -> copy data efficiently
524 {
525 size_t can_copy = min (have_samples, n_values - i);
526
527 memcpy (audio_out + i, &(*sse_samples)[pos], sizeof (float) * can_copy);
528 i += can_copy;
529 pos += can_copy;
530 env_pos += can_copy * portamento_env_step;
531 have_samples -= can_copy;
532 }
533 }
534 else
535 {
536 // skip sample
537 pos++;
538 env_pos += portamento_env_step;
539 have_samples--;
540 }
541 }
542 }
543
544 static bool
portamento_check(size_t n_values,const float * freq_in,float current_freq)545 portamento_check (size_t n_values, const float *freq_in, float current_freq)
546 {
547 /* if any value in freq_in differs from current_freq => need portamento */
548 for (size_t i = 0; i < n_values; i++)
549 {
550 if (fabs (freq_in[i] / current_freq - 1) > 0.0001) // very small frequency difference (less than one cent)
551 return true;
552 }
553
554 /* all freq_in[i] are (approximately) current_freq => no portamento */
555 return false;
556 }
557
558 void
portamento_grow(double end_pos,float portamento_stretch)559 LiveDecoder::portamento_grow (double end_pos, float portamento_stretch)
560 {
561 /* produce input samples until current_pos */
562 const int TODO = int (end_pos) + PortamentoState::DELTA - int (portamento_state.buffer.size());
563 if (TODO > 0)
564 {
565 const size_t START = portamento_state.buffer.size();
566
567 portamento_state.buffer.resize (portamento_state.buffer.size() + TODO);
568 process_internal (TODO, &portamento_state.buffer[START], portamento_stretch);
569 }
570 portamento_state.pos = end_pos;
571 }
572
573 void
portamento_shrink()574 LiveDecoder::portamento_shrink()
575 {
576 vector<float>& buffer = portamento_state.buffer;
577
578 /* avoid infinite state */
579 if (buffer.size() > 256)
580 {
581 const int shrink_buffer = buffer.size() - 2 * PortamentoState::DELTA; // only keep 2 * DELTA samples
582
583 buffer.erase (buffer.begin(), buffer.begin() + shrink_buffer);
584 portamento_state.pos -= shrink_buffer;
585 }
586 }
587
588 void
process_portamento(size_t n_values,const float * freq_in,float * audio_out)589 LiveDecoder::process_portamento (size_t n_values, const float *freq_in, float *audio_out)
590 {
591 assert (audio); // need selected (triggered) audio to use this function
592
593 const double start_pos = portamento_state.pos;
594 const vector<float>& buffer = portamento_state.buffer;
595
596 if (!portamento_state.active)
597 {
598 if (freq_in && portamento_check (n_values, freq_in, current_freq))
599 portamento_state.active = true;
600 }
601 if (portamento_state.active)
602 {
603 float fake_freq_in[n_values];
604 if (!freq_in)
605 {
606 std::fill (fake_freq_in, fake_freq_in + n_values, current_freq);
607 freq_in = fake_freq_in;
608 }
609
610 double pos[n_values], end_pos = start_pos, current_step = 1;
611
612 for (size_t i = 0; i < n_values; i++)
613 {
614 pos[i] = end_pos;
615
616 current_step = freq_in[i] / current_freq;
617 end_pos += current_step;
618 }
619 portamento_grow (end_pos, current_step);
620
621 /* interpolate from buffer (portamento) */
622 for (size_t i = 0; i < n_values; i++)
623 audio_out[i] = pp_inter->get_sample_no_check (buffer, pos[i]);
624 }
625 else
626 {
627 /* no portamento: just compute & copy values */
628 portamento_grow (start_pos + n_values, 1);
629
630 const float *start = &buffer[sm_round_positive (start_pos)];
631 std::copy (start, start + n_values, audio_out);
632 }
633 portamento_shrink();
634 }
635
636 void
process_vibrato(size_t n_values,const float * freq_in,float * audio_out)637 LiveDecoder::process_vibrato (size_t n_values, const float *freq_in, float *audio_out)
638 {
639 float vib_freq_in[n_values];
640
641 /* how many samples has the attack phase? */
642 const float attack_samples = vibrato_attack / 1000.0 * current_mix_freq;
643
644 /* compute per sample envelope increment */
645 const float vibrato_env_inc = attack_samples > 1.0 ? 1.0 / attack_samples : 1.0;
646
647 const float vibrato_phase_inc = vibrato_frequency / current_mix_freq * 2 * M_PI;
648 const float vibrato_depth_factor = pow (2, vibrato_depth / 1200.0) - 1;
649
650 for (size_t i = 0; i < n_values; i++)
651 {
652 vib_freq_in[i] = freq_in ? freq_in[i] : current_freq;
653
654 if (vibrato_env > 1.0) // attack phase done?
655 {
656 vib_freq_in[i] *= 1 + sin (vibrato_phase) * vibrato_depth_factor;
657 }
658 else
659 {
660 vibrato_env += vibrato_env_inc;
661
662 vib_freq_in[i] *= 1 + sin (vibrato_phase) * vibrato_depth_factor * vibrato_env;
663 }
664
665 vibrato_phase += vibrato_phase_inc;
666 }
667 vibrato_phase = fmod (vibrato_phase, 2 * M_PI);
668
669 process_portamento (n_values, vib_freq_in, audio_out);
670 }
671
672 void
process(size_t n_values,const float * freq_in,float * audio_out)673 LiveDecoder::process (size_t n_values, const float *freq_in, float *audio_out)
674 {
675 if (!audio) // nothing loaded
676 {
677 std::fill (audio_out, audio_out + n_values, 0);
678 done_state = DoneState::DONE;
679 return;
680 }
681 /* ensure that time_offset_ms() is only called during live decoder process */
682 assert (!in_process);
683 in_process = true;
684 start_env_pos = env_pos;
685 /*
686 * split processing into small blocks, max 10 ms
687 * -> limit n_values to keep portamento stretch settings up-to-date
688 */
689 const size_t max_n_values = current_mix_freq * 0.010;
690 const size_t orig_n_values = n_values;
691 const float *orig_audio_out = audio_out;
692
693 while (n_values > 0)
694 {
695 size_t todo_values = min (n_values, max_n_values);
696
697 if (vibrato_enabled)
698 {
699 process_vibrato (todo_values, freq_in, audio_out);
700 }
701 else
702 {
703 process_portamento (todo_values, freq_in, audio_out);
704 }
705
706
707 if (freq_in)
708 freq_in += todo_values;
709
710 audio_out += todo_values;
711 n_values -= todo_values;
712 }
713
714 /* update done state ALMOST_DONE => DONE: we need
715 * - done state ALMOST_DONE
716 * - a silent output buffer
717 */
718 if (done_state == DoneState::ALMOST_DONE)
719 {
720 size_t i = 0;
721 while (i < orig_n_values)
722 {
723 if (orig_audio_out[i] != 0.0)
724 break;
725
726 i++;
727 }
728 if (i == orig_n_values)
729 done_state = DoneState::DONE;
730 }
731 in_process = false;
732 }
733
734
735 void
enable_noise(bool en)736 LiveDecoder::enable_noise (bool en)
737 {
738 noise_enabled = en;
739 }
740
741 void
enable_sines(bool es)742 LiveDecoder::enable_sines (bool es)
743 {
744 sines_enabled = es;
745 }
746
747 void
enable_debug_fft_perf(bool dfp)748 LiveDecoder::enable_debug_fft_perf (bool dfp)
749 {
750 debug_fft_perf_enabled = dfp;
751 }
752
753 void
enable_original_samples(bool eos)754 LiveDecoder::enable_original_samples (bool eos)
755 {
756 original_samples_enabled = eos;
757 }
758
759 void
enable_loop(bool eloop)760 LiveDecoder::enable_loop (bool eloop)
761 {
762 loop_enabled = eloop;
763 }
764
765 void
enable_start_skip(bool ess)766 LiveDecoder::enable_start_skip (bool ess)
767 {
768 start_skip_enabled = ess;
769 }
770
771 void
precompute_tables(float mix_freq)772 LiveDecoder::precompute_tables (float mix_freq)
773 {
774 /* computing one sample (from the source) will ensure that tables (like
775 * anti-alias filter table and IFFTSynth window table and FFTW plan) will be
776 * available once RT synthesis is needed
777 */
778 float out;
779
780 retrigger (0, 440, 127, mix_freq);
781 process (1, nullptr, &out);
782 }
783
784 void
set_noise_seed(int seed)785 LiveDecoder::set_noise_seed (int seed)
786 {
787 assert (seed >= -1);
788 noise_seed = seed;
789 }
790
791 Audio::LoopType
get_loop_type()792 LiveDecoder::get_loop_type()
793 {
794 assert (audio);
795
796 if (loop_enabled)
797 {
798 return audio->loop_type;
799 }
800 else
801 {
802 return Audio::LOOP_NONE;
803 }
804 }
805
806 void
set_unison_voices(int voices,float detune)807 LiveDecoder::set_unison_voices (int voices, float detune)
808 {
809 assert (voices > 0);
810
811 unison_voices = voices;
812
813 if (voices == 1)
814 return;
815
816 /* setup unison frequency factors for unison voices */
817 unison_freq_factor.resize (voices);
818
819 for (size_t i = 0; i < unison_freq_factor.size(); i++)
820 {
821 const float detune_cent = -detune/2 + i / float (voices - 1) * detune;
822 unison_freq_factor[i] = pow (2, detune_cent / 1200);
823 }
824
825 /* take into account the more unison voices we add up, the louder the result
826 * will be, so compensate for this:
827 *
828 * -> each time the number of voices is doubled, the signal level is increased by
829 * a factor of sqrt (2)
830 */
831 unison_gain = 1 / sqrt (voices);
832
833 /* resize unison phase array to match pstate */
834 const bool lps_zero = (last_pstate == &pstate[0]);
835 const vector<PartialState>& old_pstate = lps_zero ? pstate[0] : pstate[1];
836 vector<float>& unison_old_phases = lps_zero ? unison_phases[0] : unison_phases[1];
837
838 if (unison_old_phases.size() != old_pstate.size() * unison_voices)
839 {
840 unison_old_phases.resize (old_pstate.size() * unison_voices);
841
842 for (float& phase : unison_old_phases)
843 {
844 /* since the position of the partials changed, randomization is really
845 * the best we can do here */
846 phase = unison_phase_random_gen.random_double_range (0, 2 * M_PI);
847 }
848 }
849 }
850
851 void
set_vibrato(bool enabled,float depth,float frequency,float attack)852 LiveDecoder::set_vibrato (bool enabled, float depth, float frequency, float attack)
853 {
854 vibrato_enabled = enabled;
855 vibrato_depth = depth;
856 vibrato_frequency = frequency;
857 vibrato_attack = attack;
858 }
859
860 double
current_pos() const861 LiveDecoder::current_pos() const
862 {
863 if (!audio)
864 return -1;
865
866 if (original_samples_enabled)
867 return original_sample_pos * 1000.0 / audio->mix_freq;
868
869 return frame_idx * audio->frame_step_ms - audio->zero_values_at_start * 1000.0 / audio->mix_freq;
870 }
871
872 double
fundamental_note() const873 LiveDecoder::fundamental_note() const
874 {
875 if (!audio)
876 return -1;
877
878 return freq_to_note (audio->fundamental_freq);
879 }
880
881 bool
done() const882 LiveDecoder::done() const
883 {
884 return done_state == DoneState::DONE;
885 }
886
887 double
time_offset_ms() const888 LiveDecoder::time_offset_ms() const
889 {
890 /* LiveDecoder::process() produces samples by IFFTSynth - possibly more than once per block
891 *
892 * This function provides the time offset of the IFFTSynth call relative to the start of
893 * the sample block generated by process(). LFOs (and other operators) can use this
894 * information for jitter-free timing.
895 */
896 assert (in_process);
897 return 1000 * (env_pos - start_env_pos) / current_mix_freq;
898 }
899