1 /* FluidSynth - A Software Synthesizer
3 * Copyright (C) 2003 Peter Hanappe and others.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 2.1 of
8 * the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free
17 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #include "fluid_rvoice_mixer.h"
22 #include "fluid_rvoice.h"
23 #include "fluid_sys.h"
24 #include "fluid_rev.h"
25 #include "fluid_chorus.h"
26 #include "fluid_synth.h"
29 // If less than x voices, the thread overhead is larger than the gain,
30 // so don't activate the thread(s).
31 #define VOICES_PER_THREAD 8
33 typedef struct _fluid_mixer_buffers_t fluid_mixer_buffers_t;
35 struct _fluid_mixer_buffers_t
37 fluid_rvoice_mixer_t *mixer; /**< Owner of object */
38 #if ENABLE_MIXER_THREADS
39 fluid_thread_t *thread; /**< Thread object */
40 fluid_atomic_int_t ready; /**< Atomic: buffers are ready for mixing */
43 fluid_rvoice_t **finished_voices; /* List of voices who have finished */
44 int finished_voice_count;
46 fluid_real_t *local_buf;
51 /** buffer to store the left part of a stereo channel to.
52 * Specifically a two dimensional array, containing \c buf_count sample buffers
53 * (i.e. for each synth.audio-channels), of which each contains
54 * FLUID_BUFSIZE * FLUID_MIXER_MAX_BUFFERS_DEFAULT audio items (=samples)
55 * @note Each sample buffer is aligned to the FLUID_DEFAULT_ALIGNMENT
56 * boundary provided that this pointer points to an aligned buffer.
57 * So make sure to access the sample buffer by first aligning this
58 * pointer using fluid_align_ptr()
60 fluid_real_t *left_buf;
62 /** dito, but for right part of a stereo channel */
63 fluid_real_t *right_buf;
65 /** buffer to store the left part of a stereo effects channel to.
66 * Specifically a two dimensional array, containing \c fx_buf_count buffers
67 * (i.e. for each synth.effects-channels), of which each buffer contains
68 * FLUID_BUFSIZE * FLUID_MIXER_MAX_BUFFERS_DEFAULT audio items (=samples)
70 fluid_real_t *fx_left_buf;
71 fluid_real_t *fx_right_buf;
74 typedef struct _fluid_mixer_fx_t fluid_mixer_fx_t;
76 struct _fluid_mixer_fx_t
78 fluid_revmodel_t *reverb; /**< Reverb unit */
79 fluid_chorus_t *chorus; /**< Chorus unit */
82 struct _fluid_rvoice_mixer_t
86 fluid_mixer_buffers_t buffers; /**< Used by mixer only: own buffers */
87 fluid_rvoice_eventhandler_t *eventhandler;
89 fluid_rvoice_t **rvoices; /**< Read-only: Voices array, sorted so that all nulls are last */
90 int polyphony; /**< Read-only: Length of voices array */
91 int active_voices; /**< Read-only: Number of non-null voices */
92 int current_blockcount; /**< Read-only: how many blocks to process this time */
94 int with_reverb; /**< Should the synth use the built-in reverb unit? */
95 int with_chorus; /**< Should the synth use the built-in chorus unit? */
96 int mix_fx_to_out; /**< Should the effects be mixed in with the primary output? */
99 fluid_ladspa_fx_t *ladspa_fx; /**< Used by mixer only: Effects unit for LADSPA support. Never created or freed */
102 #if ENABLE_MIXER_THREADS
103 // int sleeping_threads; /**< Atomic: number of threads currently asleep */
104 // int active_threads; /**< Atomic: number of threads in the thread loop */
105 fluid_atomic_int_t threads_should_terminate; /**< Atomic: Set to TRUE when threads should terminate */
106 fluid_atomic_int_t current_rvoice; /**< Atomic: for the threads to know next voice to */
107 fluid_cond_t *wakeup_threads; /**< Signalled when the threads should wake up */
108 fluid_cond_mutex_t *wakeup_threads_m; /**< wakeup_threads mutex companion */
109 fluid_cond_t *thread_ready; /**< Signalled from thread, when the thread has a buffer ready for mixing */
110 fluid_cond_mutex_t *thread_ready_m; /**< thread_ready mutex companion */
112 int thread_count; /**< Number of extra mixer threads for multi-core rendering */
113 fluid_mixer_buffers_t *threads; /**< Array of mixer threads (thread_count in length) */
117 #if ENABLE_MIXER_THREADS
118 static void delete_rvoice_mixer_threads(fluid_rvoice_mixer_t *mixer);
119 static int fluid_rvoice_mixer_set_threads(fluid_rvoice_mixer_t *mixer, int thread_count, int prio_level);
122 static FLUID_INLINE void
123 fluid_rvoice_mixer_process_fx(fluid_rvoice_mixer_t *mixer, int current_blockcount)
125 const int fx_channels_per_unit = mixer->buffers.fx_buf_count / mixer->fx_units;
128 void (*reverb_process_func)(fluid_revmodel_t *rev, const fluid_real_t *in, fluid_real_t *left_out, fluid_real_t *right_out);
129 void (*chorus_process_func)(fluid_chorus_t *chorus, const fluid_real_t *in, fluid_real_t *left_out, fluid_real_t *right_out);
131 fluid_real_t *out_rev_l, *out_rev_r, *out_ch_l, *out_ch_r;
133 // all dry unprocessed mono input is stored in the left channel
134 fluid_real_t *in_rev = fluid_align_ptr(mixer->buffers.fx_left_buf, FLUID_DEFAULT_ALIGNMENT);
135 fluid_real_t *in_ch = in_rev;
137 fluid_profile_ref_var(prof_ref);
140 if(mixer->mix_fx_to_out)
142 // mix effects to first stereo channel
143 out_ch_l = out_rev_l = fluid_align_ptr(mixer->buffers.left_buf, FLUID_DEFAULT_ALIGNMENT);
144 out_ch_r = out_rev_r = fluid_align_ptr(mixer->buffers.right_buf, FLUID_DEFAULT_ALIGNMENT);
146 reverb_process_func = fluid_revmodel_processmix;
147 chorus_process_func = fluid_chorus_processmix;
152 // replace effects into respective stereo effects channel
153 out_ch_l = out_rev_l = fluid_align_ptr(mixer->buffers.fx_left_buf, FLUID_DEFAULT_ALIGNMENT);
154 out_ch_r = out_rev_r = fluid_align_ptr(mixer->buffers.fx_right_buf, FLUID_DEFAULT_ALIGNMENT);
156 reverb_process_func = fluid_revmodel_processreplace;
157 chorus_process_func = fluid_chorus_processreplace;
161 if(mixer->with_reverb)
163 for(f = 0; f < mixer->fx_units; f++)
165 int buf_idx = f * fx_channels_per_unit + SYNTH_REVERB_CHANNEL;
167 for(i = 0; i < current_blockcount * FLUID_BUFSIZE; i += FLUID_BUFSIZE)
169 int samp_idx = buf_idx * FLUID_MIXER_MAX_BUFFERS_DEFAULT * FLUID_BUFSIZE + i;
171 reverb_process_func(mixer->fx[f].reverb,
173 mixer->mix_fx_to_out ? &out_rev_l[i] : &out_rev_l[samp_idx],
174 mixer->mix_fx_to_out ? &out_rev_r[i] : &out_rev_r[samp_idx]);
178 fluid_profile(FLUID_PROF_ONE_BLOCK_REVERB, prof_ref, 0,
179 current_blockcount * FLUID_BUFSIZE);
182 if(mixer->with_chorus)
184 for(f = 0; f < mixer->fx_units; f++)
186 int buf_idx = f * fx_channels_per_unit + SYNTH_CHORUS_CHANNEL;
188 for(i = 0; i < current_blockcount * FLUID_BUFSIZE; i += FLUID_BUFSIZE)
190 int samp_idx = buf_idx * FLUID_MIXER_MAX_BUFFERS_DEFAULT * FLUID_BUFSIZE + i;
192 chorus_process_func(mixer->fx[f].chorus,
194 mixer->mix_fx_to_out ? &out_ch_l[i] : &out_ch_l[samp_idx],
195 mixer->mix_fx_to_out ? &out_ch_r[i] : &out_ch_r[samp_idx]);
199 fluid_profile(FLUID_PROF_ONE_BLOCK_CHORUS, prof_ref, 0,
200 current_blockcount * FLUID_BUFSIZE);
205 /* Run the signal through the LADSPA Fx unit. The buffers have already been
206 * set up in fluid_rvoice_mixer_set_ladspa. */
209 fluid_ladspa_run(mixer->ladspa_fx, current_blockcount, FLUID_BUFSIZE);
210 fluid_check_fpe("LADSPA");
217 * Glue to get fluid_rvoice_buffers_mix what it wants
218 * Note: Make sure outbufs has 2 * (buf_count + fx_buf_count) elements before calling
220 static FLUID_INLINE int
221 fluid_mixer_buffers_prepare(fluid_mixer_buffers_t *buffers, fluid_real_t **outbufs)
223 fluid_real_t *base_ptr;
225 const int fx_channels_per_unit = buffers->fx_buf_count / buffers->mixer->fx_units;
226 const int offset = buffers->buf_count * 2;
227 int with_reverb = buffers->mixer->with_reverb;
228 int with_chorus = buffers->mixer->with_chorus;
230 /* Set up the reverb and chorus buffers only when the effect is enabled or
231 * when LADSPA is active. Nonexisting buffers are detected in the DSP loop.
232 * Not sending the effect signals saves some time in that case. */
234 int with_ladspa = (buffers->mixer->ladspa_fx != NULL);
235 with_reverb = (with_reverb | with_ladspa);
236 with_chorus = (with_chorus | with_ladspa);
239 // all the dry, non-processed mono audio for effects is to be stored in the left buffers
240 base_ptr = fluid_align_ptr(buffers->fx_left_buf, FLUID_DEFAULT_ALIGNMENT);
242 for(i = 0; i < buffers->mixer->fx_units; i++)
244 int fx_idx = i * fx_channels_per_unit;
246 outbufs[offset + fx_idx + SYNTH_REVERB_CHANNEL] =
248 ? &base_ptr[(fx_idx + SYNTH_REVERB_CHANNEL) * FLUID_BUFSIZE * FLUID_MIXER_MAX_BUFFERS_DEFAULT]
251 outbufs[offset + fx_idx + SYNTH_CHORUS_CHANNEL] =
253 ? &base_ptr[(fx_idx + SYNTH_CHORUS_CHANNEL) * FLUID_BUFSIZE * FLUID_MIXER_MAX_BUFFERS_DEFAULT]
257 /* The output associated with a MIDI channel is wrapped around
258 * using the number of audio groups as modulo divider. This is
259 * typically the number of output channels on the 'sound card',
260 * as long as the LADSPA Fx unit is not used. In case of LADSPA
261 * unit, think of it as subgroups on a mixer.
263 * For example: Assume that the number of groups is set to 2.
264 * Then MIDI channel 1, 3, 5, 7 etc. go to output 1, channels 2,
265 * 4, 6, 8 etc to output 2. Or assume 3 groups: Then MIDI
266 * channels 1, 4, 7, 10 etc go to output 1; 2, 5, 8, 11 etc to
267 * output 2, 3, 6, 9, 12 etc to output 3.
269 base_ptr = fluid_align_ptr(buffers->left_buf, FLUID_DEFAULT_ALIGNMENT);
271 for(i = 0; i < buffers->buf_count; i++)
273 outbufs[i * 2] = &base_ptr[i * FLUID_BUFSIZE * FLUID_MIXER_MAX_BUFFERS_DEFAULT];
276 base_ptr = fluid_align_ptr(buffers->right_buf, FLUID_DEFAULT_ALIGNMENT);
278 for(i = 0; i < buffers->buf_count; i++)
280 outbufs[i * 2 + 1] = &base_ptr[i * FLUID_BUFSIZE * FLUID_MIXER_MAX_BUFFERS_DEFAULT];
283 return offset + buffers->fx_buf_count;
287 static FLUID_INLINE void
288 fluid_finish_rvoice(fluid_mixer_buffers_t *buffers, fluid_rvoice_t *rvoice)
290 if(buffers->finished_voice_count < buffers->mixer->polyphony)
292 buffers->finished_voices[buffers->finished_voice_count++] = rvoice;
296 FLUID_LOG(FLUID_ERR, "Exceeded finished voices array, try increasing polyphony");
301 fluid_mixer_buffer_process_finished_voices(fluid_mixer_buffers_t *buffers)
305 for(i = 0; i < buffers->finished_voice_count; i++)
307 fluid_rvoice_t *v = buffers->finished_voices[i];
308 int av = buffers->mixer->active_voices;
310 for(j = 0; j < av; j++)
312 if(v == buffers->mixer->rvoices[j])
319 buffers->mixer->rvoices[j] = buffers->mixer->rvoices[av];
324 buffers->mixer->active_voices = av;
326 fluid_rvoice_eventhandler_finished_voice_callback(buffers->mixer->eventhandler, v);
329 buffers->finished_voice_count = 0;
332 static FLUID_INLINE void fluid_rvoice_mixer_process_finished_voices(fluid_rvoice_mixer_t *mixer)
334 #if ENABLE_MIXER_THREADS
337 for(i = 0; i < mixer->thread_count; i++)
339 fluid_mixer_buffer_process_finished_voices(&mixer->threads[i]);
343 fluid_mixer_buffer_process_finished_voices(&mixer->buffers);
347 static FLUID_INLINE fluid_real_t *
348 get_dest_buf(fluid_rvoice_buffers_t *buffers, int index,
349 fluid_real_t **dest_bufs, int dest_bufcount)
351 int j = buffers->bufs[index].mapping;
353 if(j >= dest_bufcount || j < 0)
362 * Mix data down to buffers
364 * @param buffers Destination buffer(s)
365 * @param dsp_buf Mono sample source
366 * @param start_block Block to start mixing at
367 * @param sample_count number of samples to mix following \c start_block
368 * @param dest_bufs Array of buffers to mixdown to
369 * @param dest_bufcount Length of dest_bufs
372 fluid_rvoice_buffers_mix(fluid_rvoice_buffers_t *buffers,
373 const fluid_real_t *FLUID_RESTRICT dsp_buf,
374 int start_block, int sample_count,
375 fluid_real_t **dest_bufs, int dest_bufcount)
377 int bufcount = buffers->count;
380 if(sample_count <= 0 || dest_bufcount <= 0)
385 FLUID_ASSERT((uintptr_t)dsp_buf % FLUID_DEFAULT_ALIGNMENT == 0);
386 FLUID_ASSERT((uintptr_t)(&dsp_buf[start_block * FLUID_BUFSIZE]) % FLUID_DEFAULT_ALIGNMENT == 0);
388 for(i = 0; i < bufcount; i++)
390 fluid_real_t *FLUID_RESTRICT buf = get_dest_buf(buffers, i, dest_bufs, dest_bufcount);
391 fluid_real_t amp = buffers->bufs[i].amp;
393 if(buf == NULL || amp == 0.0f)
398 FLUID_ASSERT((uintptr_t)buf % FLUID_DEFAULT_ALIGNMENT == 0);
400 #pragma omp simd aligned(dsp_buf,buf:FLUID_DEFAULT_ALIGNMENT)
402 for(dsp_i = (start_block * FLUID_BUFSIZE); dsp_i < sample_count; dsp_i++)
404 buf[dsp_i] += amp * dsp_buf[dsp_i];
410 * Synthesize one voice and add to buffer.
411 * NOTE: If return value is less than blockcount*FLUID_BUFSIZE, that means
412 * voice has been finished, removed and possibly replaced with another voice.
414 static FLUID_INLINE void
415 fluid_mixer_buffers_render_one(fluid_mixer_buffers_t *buffers,
416 fluid_rvoice_t *rvoice, fluid_real_t **dest_bufs,
417 unsigned int dest_bufcount, fluid_real_t *src_buf, int blockcount)
419 int i, total_samples = 0, start_block = 0;
421 for(i = 0; i < blockcount; i++)
423 int s = fluid_rvoice_write(rvoice, &src_buf[FLUID_BUFSIZE * i]);
433 if(s < FLUID_BUFSIZE)
439 fluid_rvoice_buffers_mix(&rvoice->buffers, src_buf, -start_block, total_samples - ((-start_block)*FLUID_BUFSIZE), dest_bufs, dest_bufcount);
441 if(total_samples < blockcount * FLUID_BUFSIZE)
443 fluid_finish_rvoice(buffers, rvoice);
447 DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_mixer_add_voice)
450 fluid_rvoice_mixer_t *mixer = obj;
451 fluid_rvoice_t *voice = param[0].ptr;
453 if(mixer->active_voices < mixer->polyphony)
455 mixer->rvoices[mixer->active_voices++] = voice;
459 /* See if any voices just finished, if so, take its place.
460 This can happen in voice overflow conditions. */
461 for(i = 0; i < mixer->active_voices; i++)
463 if(mixer->rvoices[i] == voice)
465 FLUID_LOG(FLUID_ERR, "Internal error: Trying to replace an existing rvoice in fluid_rvoice_mixer_add_voice?!");
469 if(mixer->rvoices[i]->envlfo.volenv.section == FLUID_VOICE_ENVFINISHED)
471 fluid_finish_rvoice(&mixer->buffers, mixer->rvoices[i]);
472 mixer->rvoices[i] = voice;
477 /* This should never happen */
478 FLUID_LOG(FLUID_ERR, "Trying to exceed polyphony in fluid_rvoice_mixer_add_voice");
483 fluid_mixer_buffers_update_polyphony(fluid_mixer_buffers_t *buffers, int value)
487 if(buffers->finished_voice_count > value)
492 newptr = FLUID_REALLOC(buffers->finished_voices, value * sizeof(fluid_rvoice_t *));
494 if(newptr == NULL && value > 0)
499 buffers->finished_voices = newptr;
504 * Update polyphony - max number of voices (NOTE: not hard real-time capable)
505 * @return FLUID_OK or FLUID_FAILED
507 DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_mixer_set_polyphony)
510 fluid_rvoice_mixer_t *handler = obj;
511 int value = param[0].i;
513 if(handler->active_voices > value)
515 return /*FLUID_FAILED*/;
518 newptr = FLUID_REALLOC(handler->rvoices, value * sizeof(fluid_rvoice_t *));
522 return /*FLUID_FAILED*/;
525 handler->rvoices = newptr;
527 if(fluid_mixer_buffers_update_polyphony(&handler->buffers, value)
530 return /*FLUID_FAILED*/;
533 #if ENABLE_MIXER_THREADS
537 for(i = 0; i < handler->thread_count; i++)
539 if(fluid_mixer_buffers_update_polyphony(&handler->threads[i], value)
542 return /*FLUID_FAILED*/;
548 handler->polyphony = value;
554 fluid_render_loop_singlethread(fluid_rvoice_mixer_t *mixer, int blockcount)
557 FLUID_DECLARE_VLA(fluid_real_t *, bufs,
558 mixer->buffers.buf_count * 2 + mixer->buffers.fx_buf_count * 2);
559 int bufcount = fluid_mixer_buffers_prepare(&mixer->buffers, bufs);
561 fluid_real_t *local_buf = fluid_align_ptr(mixer->buffers.local_buf, FLUID_DEFAULT_ALIGNMENT);
563 fluid_profile_ref_var(prof_ref);
565 for(i = 0; i < mixer->active_voices; i++)
567 fluid_mixer_buffers_render_one(&mixer->buffers, mixer->rvoices[i], bufs,
568 bufcount, local_buf, blockcount);
569 fluid_profile(FLUID_PROF_ONE_BLOCK_VOICE, prof_ref, 1,
570 blockcount * FLUID_BUFSIZE);
574 static FLUID_INLINE void
575 fluid_mixer_buffers_zero(fluid_mixer_buffers_t *buffers, int current_blockcount)
577 int i, size = current_blockcount * FLUID_BUFSIZE * sizeof(fluid_real_t);
579 /* TODO: Optimize by only zero out the buffers we actually use later on. */
580 int buf_count = buffers->buf_count, fx_buf_count = buffers->fx_buf_count;
582 fluid_real_t *FLUID_RESTRICT buf_l = fluid_align_ptr(buffers->left_buf, FLUID_DEFAULT_ALIGNMENT);
583 fluid_real_t *FLUID_RESTRICT buf_r = fluid_align_ptr(buffers->right_buf, FLUID_DEFAULT_ALIGNMENT);
585 for(i = 0; i < buf_count; i++)
587 FLUID_MEMSET(&buf_l[i * FLUID_MIXER_MAX_BUFFERS_DEFAULT * FLUID_BUFSIZE], 0, size);
588 FLUID_MEMSET(&buf_r[i * FLUID_MIXER_MAX_BUFFERS_DEFAULT * FLUID_BUFSIZE], 0, size);
591 buf_l = fluid_align_ptr(buffers->fx_left_buf, FLUID_DEFAULT_ALIGNMENT);
592 buf_r = fluid_align_ptr(buffers->fx_right_buf, FLUID_DEFAULT_ALIGNMENT);
594 for(i = 0; i < fx_buf_count; i++)
596 FLUID_MEMSET(&buf_l[i * FLUID_MIXER_MAX_BUFFERS_DEFAULT * FLUID_BUFSIZE], 0, size);
597 FLUID_MEMSET(&buf_r[i * FLUID_MIXER_MAX_BUFFERS_DEFAULT * FLUID_BUFSIZE], 0, size);
602 fluid_mixer_buffers_init(fluid_mixer_buffers_t *buffers, fluid_rvoice_mixer_t *mixer)
604 const int samplecount = FLUID_BUFSIZE * FLUID_MIXER_MAX_BUFFERS_DEFAULT;
606 buffers->mixer = mixer;
607 buffers->buf_count = mixer->buffers.buf_count;
608 buffers->fx_buf_count = mixer->buffers.fx_buf_count;
610 /* Local mono voice buf */
611 buffers->local_buf = FLUID_ARRAY_ALIGNED(fluid_real_t, samplecount, FLUID_DEFAULT_ALIGNMENT);
613 /* Left and right audio buffers */
615 buffers->left_buf = FLUID_ARRAY_ALIGNED(fluid_real_t, buffers->buf_count * samplecount, FLUID_DEFAULT_ALIGNMENT);
616 buffers->right_buf = FLUID_ARRAY_ALIGNED(fluid_real_t, buffers->buf_count * samplecount, FLUID_DEFAULT_ALIGNMENT);
618 if((buffers->local_buf == NULL) || (buffers->left_buf == NULL) || (buffers->right_buf == NULL))
620 FLUID_LOG(FLUID_ERR, "Out of memory");
624 /* Effects audio buffers */
626 buffers->fx_left_buf = FLUID_ARRAY_ALIGNED(fluid_real_t, buffers->fx_buf_count * samplecount, FLUID_DEFAULT_ALIGNMENT);
627 buffers->fx_right_buf = FLUID_ARRAY_ALIGNED(fluid_real_t, buffers->fx_buf_count * samplecount, FLUID_DEFAULT_ALIGNMENT);
629 if((buffers->fx_left_buf == NULL) || (buffers->fx_right_buf == NULL))
631 FLUID_LOG(FLUID_ERR, "Out of memory");
635 buffers->finished_voices = NULL;
637 if(fluid_mixer_buffers_update_polyphony(buffers, mixer->polyphony)
640 FLUID_LOG(FLUID_ERR, "Out of memory");
648 * Note: Not hard real-time capable (calls malloc)
650 DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_mixer_set_samplerate)
652 fluid_rvoice_mixer_t *mixer = obj;
653 fluid_real_t samplerate = param[1].real; // becausee fluid_synth_update_mixer() puts real into arg2
656 for(i = 0; i < mixer->fx_units; i++)
658 if(mixer->fx[i].chorus)
660 delete_fluid_chorus(mixer->fx[i].chorus);
663 mixer->fx[i].chorus = new_fluid_chorus(samplerate);
665 if(mixer->fx[i].reverb)
667 fluid_revmodel_samplerate_change(mixer->fx[i].reverb, samplerate);
673 if(mixer->ladspa_fx != NULL)
675 fluid_ladspa_set_sample_rate(mixer->ladspa_fx, samplerate);
683 * @param buf_count number of primary stereo buffers
684 * @param fx_buf_count number of stereo effect buffers
686 fluid_rvoice_mixer_t *
687 new_fluid_rvoice_mixer(int buf_count, int fx_buf_count, int fx_units, fluid_real_t sample_rate, fluid_rvoice_eventhandler_t *evthandler, int extra_threads, int prio)
690 fluid_rvoice_mixer_t *mixer = FLUID_NEW(fluid_rvoice_mixer_t);
694 FLUID_LOG(FLUID_ERR, "Out of memory");
698 FLUID_MEMSET(mixer, 0, sizeof(fluid_rvoice_mixer_t));
699 mixer->eventhandler = evthandler;
700 mixer->fx_units = fx_units;
701 mixer->buffers.buf_count = buf_count;
702 mixer->buffers.fx_buf_count = fx_buf_count * fx_units;
704 /* allocate the reverb module */
705 mixer->fx = FLUID_ARRAY(fluid_mixer_fx_t, fx_units);
706 if(mixer->fx == NULL)
708 FLUID_LOG(FLUID_ERR, "Out of memory");
712 FLUID_MEMSET(mixer->fx, 0, fx_units * sizeof(*mixer->fx));
714 for(i = 0; i < fx_units; i++)
716 mixer->fx[i].reverb = new_fluid_revmodel(sample_rate);
717 mixer->fx[i].chorus = new_fluid_chorus(sample_rate);
719 if(mixer->fx[i].reverb == NULL || mixer->fx[i].chorus == NULL)
721 FLUID_LOG(FLUID_ERR, "Out of memory");
726 if(!fluid_mixer_buffers_init(&mixer->buffers, mixer))
731 #if ENABLE_MIXER_THREADS
732 mixer->thread_ready = new_fluid_cond();
733 mixer->wakeup_threads = new_fluid_cond();
734 mixer->thread_ready_m = new_fluid_cond_mutex();
735 mixer->wakeup_threads_m = new_fluid_cond_mutex();
737 if(!mixer->thread_ready || !mixer->wakeup_threads ||
738 !mixer->thread_ready_m || !mixer->wakeup_threads_m)
743 if(fluid_rvoice_mixer_set_threads(mixer, extra_threads, prio) != FLUID_OK)
753 delete_fluid_rvoice_mixer(mixer);
758 fluid_mixer_buffers_free(fluid_mixer_buffers_t *buffers)
760 FLUID_FREE(buffers->finished_voices);
762 /* free all the sample buffers */
763 FLUID_FREE(buffers->local_buf);
764 FLUID_FREE(buffers->left_buf);
765 FLUID_FREE(buffers->right_buf);
766 FLUID_FREE(buffers->fx_left_buf);
767 FLUID_FREE(buffers->fx_right_buf);
770 void delete_fluid_rvoice_mixer(fluid_rvoice_mixer_t *mixer)
774 fluid_return_if_fail(mixer != NULL);
776 #if ENABLE_MIXER_THREADS
777 delete_rvoice_mixer_threads(mixer);
779 if(mixer->thread_ready)
781 delete_fluid_cond(mixer->thread_ready);
784 if(mixer->wakeup_threads)
786 delete_fluid_cond(mixer->wakeup_threads);
789 if(mixer->thread_ready_m)
791 delete_fluid_cond_mutex(mixer->thread_ready_m);
794 if(mixer->wakeup_threads_m)
796 delete_fluid_cond_mutex(mixer->wakeup_threads_m);
800 fluid_mixer_buffers_free(&mixer->buffers);
803 for(i = 0; i < mixer->fx_units; i++)
805 if(mixer->fx[i].reverb)
807 delete_fluid_revmodel(mixer->fx[i].reverb);
810 if(mixer->fx[i].chorus)
812 delete_fluid_chorus(mixer->fx[i].chorus);
816 FLUID_FREE(mixer->fx);
817 FLUID_FREE(mixer->rvoices);
824 * Set a LADSPS fx instance to be used by the mixer and assign the mixer buffers
825 * as LADSPA host buffers with sensible names */
826 void fluid_rvoice_mixer_set_ladspa(fluid_rvoice_mixer_t *mixer,
827 fluid_ladspa_fx_t *ladspa_fx, int audio_groups)
829 mixer->ladspa_fx = ladspa_fx;
831 if(ladspa_fx == NULL)
837 fluid_real_t *main_l = fluid_align_ptr(mixer->buffers.left_buf, FLUID_DEFAULT_ALIGNMENT);
838 fluid_real_t *main_r = fluid_align_ptr(mixer->buffers.right_buf, FLUID_DEFAULT_ALIGNMENT);
840 fluid_real_t *rev = fluid_align_ptr(mixer->buffers.fx_left_buf, FLUID_DEFAULT_ALIGNMENT);
841 fluid_real_t *chor = rev;
843 rev = &rev[SYNTH_REVERB_CHANNEL * FLUID_BUFSIZE * FLUID_MIXER_MAX_BUFFERS_DEFAULT];
844 chor = &chor[SYNTH_CHORUS_CHANNEL * FLUID_BUFSIZE * FLUID_MIXER_MAX_BUFFERS_DEFAULT];
846 fluid_ladspa_add_host_ports(ladspa_fx, "Main:L", audio_groups,
848 FLUID_BUFSIZE * FLUID_MIXER_MAX_BUFFERS_DEFAULT);
850 fluid_ladspa_add_host_ports(ladspa_fx, "Main:R", audio_groups,
852 FLUID_BUFSIZE * FLUID_MIXER_MAX_BUFFERS_DEFAULT);
854 fluid_ladspa_add_host_ports(ladspa_fx, "Reverb:Send", 1,
856 FLUID_BUFSIZE * FLUID_MIXER_MAX_BUFFERS_DEFAULT);
858 fluid_ladspa_add_host_ports(ladspa_fx, "Chorus:Send", 1,
860 FLUID_BUFSIZE * FLUID_MIXER_MAX_BUFFERS_DEFAULT);
865 DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_mixer_set_reverb_enabled)
867 fluid_rvoice_mixer_t *mixer = obj;
870 mixer->with_reverb = on;
873 DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_mixer_set_chorus_enabled)
875 fluid_rvoice_mixer_t *mixer = obj;
877 mixer->with_chorus = on;
880 void fluid_rvoice_mixer_set_mix_fx(fluid_rvoice_mixer_t *mixer, int on)
882 mixer->mix_fx_to_out = on;
885 DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_mixer_set_chorus_params)
887 fluid_rvoice_mixer_t *mixer = obj;
888 int set = param[0].i;
890 fluid_real_t level = param[2].real;
891 fluid_real_t speed = param[3].real;
892 fluid_real_t depth_ms = param[4].real;
893 int type = param[5].i;
896 for(i = 0; i < mixer->fx_units; i++)
898 fluid_chorus_set(mixer->fx[i].chorus, set, nr, level, speed, depth_ms, type);
902 DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_mixer_set_reverb_params)
904 fluid_rvoice_mixer_t *mixer = obj;
905 int set = param[0].i;
906 fluid_real_t roomsize = param[1].real;
907 fluid_real_t damping = param[2].real;
908 fluid_real_t width = param[3].real;
909 fluid_real_t level = param[4].real;
912 for(i = 0; i < mixer->fx_units; i++)
914 fluid_revmodel_set(mixer->fx[i].reverb, set, roomsize, damping, width, level);
918 DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_mixer_reset_reverb)
920 fluid_rvoice_mixer_t *mixer = obj;
922 for(i = 0; i < mixer->fx_units; i++)
924 fluid_revmodel_reset(mixer->fx[i].reverb);
928 DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_mixer_reset_chorus)
930 fluid_rvoice_mixer_t *mixer = obj;
932 for(i = 0; i < mixer->fx_units; i++)
934 fluid_chorus_reset(mixer->fx[i].chorus);
938 int fluid_rvoice_mixer_get_bufs(fluid_rvoice_mixer_t *mixer,
939 fluid_real_t **left, fluid_real_t **right)
941 *left = fluid_align_ptr(mixer->buffers.left_buf, FLUID_DEFAULT_ALIGNMENT);
942 *right = fluid_align_ptr(mixer->buffers.right_buf, FLUID_DEFAULT_ALIGNMENT);
943 return mixer->buffers.buf_count;
946 int fluid_rvoice_mixer_get_fx_bufs(fluid_rvoice_mixer_t *mixer,
947 fluid_real_t **fx_left, fluid_real_t **fx_right)
949 *fx_left = fluid_align_ptr(mixer->buffers.fx_left_buf, FLUID_DEFAULT_ALIGNMENT);
950 *fx_right = fluid_align_ptr(mixer->buffers.fx_right_buf, FLUID_DEFAULT_ALIGNMENT);
951 return mixer->buffers.fx_buf_count;
954 int fluid_rvoice_mixer_get_bufcount(fluid_rvoice_mixer_t *mixer)
956 return FLUID_MIXER_MAX_BUFFERS_DEFAULT;
960 int fluid_rvoice_mixer_get_active_voices(fluid_rvoice_mixer_t *mixer)
962 return mixer->active_voices;
966 #if ENABLE_MIXER_THREADS
968 static FLUID_INLINE fluid_rvoice_t *
969 fluid_mixer_get_mt_rvoice(fluid_rvoice_mixer_t *mixer)
971 int i = fluid_atomic_int_exchange_and_add(&mixer->current_rvoice, 1);
973 if(i >= mixer->active_voices)
978 return mixer->rvoices[i];
981 #define THREAD_BUF_PROCESSING 0
982 #define THREAD_BUF_VALID 1
983 #define THREAD_BUF_NODATA 2
984 #define THREAD_BUF_TERMINATE 3
986 /* Core thread function (processes voices in parallel to primary synthesis thread) */
987 static fluid_thread_return_t
988 fluid_mixer_thread_func(void *data)
990 fluid_mixer_buffers_t *buffers = data;
991 fluid_rvoice_mixer_t *mixer = buffers->mixer;
992 int hasValidData = 0;
993 FLUID_DECLARE_VLA(fluid_real_t *, bufs, buffers->buf_count * 2 + buffers->fx_buf_count * 2);
995 int current_blockcount = 0;
996 fluid_real_t *local_buf = fluid_align_ptr(buffers->local_buf, FLUID_DEFAULT_ALIGNMENT);
998 while(!fluid_atomic_int_get(&mixer->threads_should_terminate))
1000 fluid_rvoice_t *rvoice = fluid_mixer_get_mt_rvoice(mixer);
1004 // if no voices: signal rendered buffers, sleep
1005 fluid_atomic_int_set(&buffers->ready, hasValidData ? THREAD_BUF_VALID : THREAD_BUF_NODATA);
1006 fluid_cond_mutex_lock(mixer->thread_ready_m);
1007 fluid_cond_signal(mixer->thread_ready);
1008 fluid_cond_mutex_unlock(mixer->thread_ready_m);
1010 fluid_cond_mutex_lock(mixer->wakeup_threads_m);
1014 int j = fluid_atomic_int_get(&buffers->ready);
1016 if(j == THREAD_BUF_PROCESSING || j == THREAD_BUF_TERMINATE)
1021 fluid_cond_wait(mixer->wakeup_threads, mixer->wakeup_threads_m);
1024 fluid_cond_mutex_unlock(mixer->wakeup_threads_m);
1030 // else: if buffer is not zeroed, zero buffers
1033 // blockcount may have changed, since thread was put to sleep
1034 current_blockcount = mixer->current_blockcount;
1035 fluid_mixer_buffers_zero(buffers, current_blockcount);
1036 bufcount = fluid_mixer_buffers_prepare(buffers, bufs);
1040 // then render voice to buffers
1041 fluid_mixer_buffers_render_one(buffers, rvoice, bufs, bufcount, local_buf, current_blockcount);
1045 return FLUID_THREAD_RETURN_VALUE;
1049 fluid_mixer_buffers_mix(fluid_mixer_buffers_t *dst, fluid_mixer_buffers_t *src, int current_blockcount)
1052 int scount = current_blockcount * FLUID_BUFSIZE;
1054 fluid_real_t *FLUID_RESTRICT base_src;
1055 fluid_real_t *FLUID_RESTRICT base_dst;
1057 minbuf = dst->buf_count;
1059 if(minbuf > src->buf_count)
1061 minbuf = src->buf_count;
1064 base_src = fluid_align_ptr(src->left_buf, FLUID_DEFAULT_ALIGNMENT);
1065 base_dst = fluid_align_ptr(dst->left_buf, FLUID_DEFAULT_ALIGNMENT);
1067 for(i = 0; i < minbuf; i++)
1069 #pragma omp simd aligned(base_dst,base_src:FLUID_DEFAULT_ALIGNMENT)
1071 for(j = 0; j < scount; j++)
1073 int dsp_i = i * FLUID_MIXER_MAX_BUFFERS_DEFAULT * FLUID_BUFSIZE + j;
1074 base_dst[dsp_i] += base_src[dsp_i];
1078 base_src = fluid_align_ptr(src->right_buf, FLUID_DEFAULT_ALIGNMENT);
1079 base_dst = fluid_align_ptr(dst->right_buf, FLUID_DEFAULT_ALIGNMENT);
1081 for(i = 0; i < minbuf; i++)
1083 #pragma omp simd aligned(base_dst,base_src:FLUID_DEFAULT_ALIGNMENT)
1085 for(j = 0; j < scount; j++)
1087 int dsp_i = i * FLUID_MIXER_MAX_BUFFERS_DEFAULT * FLUID_BUFSIZE + j;
1088 base_dst[dsp_i] += base_src[dsp_i];
1092 minbuf = dst->fx_buf_count;
1094 if(minbuf > src->fx_buf_count)
1096 minbuf = src->fx_buf_count;
1099 base_src = fluid_align_ptr(src->fx_left_buf, FLUID_DEFAULT_ALIGNMENT);
1100 base_dst = fluid_align_ptr(dst->fx_left_buf, FLUID_DEFAULT_ALIGNMENT);
1102 for(i = 0; i < minbuf; i++)
1104 #pragma omp simd aligned(base_dst,base_src:FLUID_DEFAULT_ALIGNMENT)
1106 for(j = 0; j < scount; j++)
1108 int dsp_i = i * FLUID_MIXER_MAX_BUFFERS_DEFAULT * FLUID_BUFSIZE + j;
1109 base_dst[dsp_i] += base_src[dsp_i];
1113 base_src = fluid_align_ptr(src->fx_right_buf, FLUID_DEFAULT_ALIGNMENT);
1114 base_dst = fluid_align_ptr(dst->fx_right_buf, FLUID_DEFAULT_ALIGNMENT);
1116 for(i = 0; i < minbuf; i++)
1118 #pragma omp simd aligned(base_dst,base_src:FLUID_DEFAULT_ALIGNMENT)
1120 for(j = 0; j < scount; j++)
1122 int dsp_i = i * FLUID_MIXER_MAX_BUFFERS_DEFAULT * FLUID_BUFSIZE + j;
1123 base_dst[dsp_i] += base_src[dsp_i];
1130 * Go through all threads and see if someone is finished for mixing
1133 fluid_mixer_mix_in(fluid_rvoice_mixer_t *mixer, int extra_threads, int current_blockcount)
1135 int i, result, hasmixed;
1142 for(i = 0; i < extra_threads; i++)
1144 int j = fluid_atomic_int_get(&mixer->threads[i].ready);
1148 case THREAD_BUF_PROCESSING:
1152 case THREAD_BUF_VALID:
1153 fluid_atomic_int_set(&mixer->threads[i].ready, THREAD_BUF_NODATA);
1154 fluid_mixer_buffers_mix(&mixer->buffers, &mixer->threads[i], current_blockcount);
1166 fluid_render_loop_multithread(fluid_rvoice_mixer_t *mixer, int current_blockcount)
1169 fluid_real_t *local_buf = fluid_align_ptr(mixer->buffers.local_buf, FLUID_DEFAULT_ALIGNMENT);
1171 FLUID_DECLARE_VLA(fluid_real_t *, bufs,
1172 mixer->buffers.buf_count * 2 + mixer->buffers.fx_buf_count * 2);
1173 // How many threads should we start this time?
1174 int extra_threads = mixer->active_voices / VOICES_PER_THREAD;
1176 if(extra_threads > mixer->thread_count)
1178 extra_threads = mixer->thread_count;
1181 if(extra_threads == 0)
1183 // No extra threads? No thread overhead!
1184 fluid_render_loop_singlethread(mixer, current_blockcount);
1188 bufcount = fluid_mixer_buffers_prepare(&mixer->buffers, bufs);
1190 // Prepare voice list
1191 fluid_cond_mutex_lock(mixer->wakeup_threads_m);
1192 fluid_atomic_int_set(&mixer->current_rvoice, 0);
1194 for(i = 0; i < extra_threads; i++)
1196 fluid_atomic_int_set(&mixer->threads[i].ready, THREAD_BUF_PROCESSING);
1199 // Signal threads to wake up
1200 fluid_cond_broadcast(mixer->wakeup_threads);
1201 fluid_cond_mutex_unlock(mixer->wakeup_threads_m);
1203 // If thread is finished, mix it in
1204 while(fluid_mixer_mix_in(mixer, extra_threads, current_blockcount))
1206 // Otherwise get a voice and render it
1207 fluid_rvoice_t *rvoice = fluid_mixer_get_mt_rvoice(mixer);
1211 fluid_profile_ref_var(prof_ref);
1212 fluid_mixer_buffers_render_one(&mixer->buffers, rvoice, bufs, bufcount, local_buf, current_blockcount);
1213 fluid_profile(FLUID_PROF_ONE_BLOCK_VOICE, prof_ref, 1,
1214 current_blockcount * FLUID_BUFSIZE);
1219 // If no voices, wait for mixes. Make sure one is still processing to avoid deadlock
1220 int is_processing = 0;
1222 fluid_cond_mutex_lock(mixer->thread_ready_m);
1224 for(i = 0; i < extra_threads; i++)
1226 if(fluid_atomic_int_get(&mixer->threads[i].ready) ==
1227 THREAD_BUF_PROCESSING)
1235 fluid_cond_wait(mixer->thread_ready, mixer->thread_ready_m);
1238 fluid_cond_mutex_unlock(mixer->thread_ready_m);
1242 //FLUID_LOG(FLUID_DBG, "Blockcount: %d, mixed %d of %d voices myself, waits = %d",
1243 // current_blockcount, test, mixer->active_voices, waits);
1246 static void delete_rvoice_mixer_threads(fluid_rvoice_mixer_t *mixer)
1249 fluid_atomic_int_set(&mixer->threads_should_terminate, 1);
1250 // Signal threads to wake up
1251 fluid_cond_mutex_lock(mixer->wakeup_threads_m);
1253 for(i = 0; i < mixer->thread_count; i++)
1255 fluid_atomic_int_set(&mixer->threads[i].ready, THREAD_BUF_TERMINATE);
1258 fluid_cond_broadcast(mixer->wakeup_threads);
1259 fluid_cond_mutex_unlock(mixer->wakeup_threads_m);
1261 for(i = 0; i < mixer->thread_count; i++)
1263 if(mixer->threads[i].thread)
1265 fluid_thread_join(mixer->threads[i].thread);
1266 delete_fluid_thread(mixer->threads[i].thread);
1269 fluid_mixer_buffers_free(&mixer->threads[i]);
1272 FLUID_FREE(mixer->threads);
1273 mixer->thread_count = 0;
1274 mixer->threads = NULL;
1278 * Update amount of extra mixer threads.
1279 * @param thread_count Number of extra mixer threads for multi-core rendering
1280 * @param prio_level real-time prio level for the extra mixer threads
1282 static int fluid_rvoice_mixer_set_threads(fluid_rvoice_mixer_t *mixer, int thread_count, int prio_level)
1287 // Kill all existing threads first
1288 if(mixer->thread_count)
1290 delete_rvoice_mixer_threads(mixer);
1293 if(thread_count == 0)
1298 // Now prepare the new threads
1299 fluid_atomic_int_set(&mixer->threads_should_terminate, 0);
1300 mixer->threads = FLUID_ARRAY(fluid_mixer_buffers_t, thread_count);
1302 if(mixer->threads == NULL)
1304 FLUID_LOG(FLUID_ERR, "Out of memory");
1305 return FLUID_FAILED;
1308 FLUID_MEMSET(mixer->threads, 0, thread_count * sizeof(fluid_mixer_buffers_t));
1309 mixer->thread_count = thread_count;
1311 for(i = 0; i < thread_count; i++)
1313 fluid_mixer_buffers_t *b = &mixer->threads[i];
1315 if(!fluid_mixer_buffers_init(b, mixer))
1317 return FLUID_FAILED;
1320 fluid_atomic_int_set(&b->ready, THREAD_BUF_NODATA);
1321 FLUID_SNPRINTF(name, sizeof(name), "mixer%d", i);
1322 b->thread = new_fluid_thread(name, fluid_mixer_thread_func, b, prio_level, 0);
1326 return FLUID_FAILED;
1335 * Synthesize audio into buffers
1336 * @param blockcount number of blocks to render, each having FLUID_BUFSIZE samples
1337 * @return number of blocks rendered
1340 fluid_rvoice_mixer_render(fluid_rvoice_mixer_t *mixer, int blockcount)
1342 fluid_profile_ref_var(prof_ref);
1344 mixer->current_blockcount = blockcount;
1347 fluid_mixer_buffers_zero(&mixer->buffers, blockcount);
1348 fluid_profile(FLUID_PROF_ONE_BLOCK_CLEAR, prof_ref, mixer->active_voices,
1349 blockcount * FLUID_BUFSIZE);
1351 #if ENABLE_MIXER_THREADS
1353 if(mixer->thread_count > 0)
1355 fluid_render_loop_multithread(mixer, blockcount);
1360 fluid_render_loop_singlethread(mixer, blockcount);
1363 fluid_profile(FLUID_PROF_ONE_BLOCK_VOICES, prof_ref, mixer->active_voices,
1364 blockcount * FLUID_BUFSIZE);
1367 // Process reverb & chorus
1368 fluid_rvoice_mixer_process_fx(mixer, blockcount);
1370 // Call the callback and pack active voice array
1371 fluid_rvoice_mixer_process_finished_voices(mixer);