00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "fluidsynth_priv.h"
00022 #include "fluid_voice.h"
00023 #include "fluid_mod.h"
00024 #include "fluid_chan.h"
00025 #include "fluid_conv.h"
00026 #include "fluid_synth.h"
00027 #include "fluid_sys.h"
00028 #include "fluid_sfont.h"
00029
00030
00031
00032 #define FLUID_MAX_AUDIBLE_FILTER_FC 19000.0f
00033 #define FLUID_MIN_AUDIBLE_FILTER_Q 1.2f
00034
00035
00036
00037
00038
00039 #define FLUID_NOISE_FLOOR 0.00003
00040
00041
00042 #define FLUID_MIN_LOOP_SIZE 2
00043 #define FLUID_MIN_LOOP_PAD 0
00044
00045
00046 #define FLUID_MIN_VOLENVRELEASE -7200.0f
00047
00048
00049 static inline void fluid_voice_effects (fluid_voice_t *voice, int count,
00050 fluid_real_t* dsp_left_buf,
00051 fluid_real_t* dsp_right_buf,
00052 fluid_real_t* dsp_reverb_buf,
00053 fluid_real_t* dsp_chorus_buf);
00054
00055
00056
00057 fluid_voice_t*
00058 new_fluid_voice(fluid_real_t output_rate)
00059 {
00060 fluid_voice_t* voice;
00061 voice = FLUID_NEW(fluid_voice_t);
00062 if (voice == NULL) {
00063 FLUID_LOG(FLUID_ERR, "Out of memory");
00064 return NULL;
00065 }
00066 voice->status = FLUID_VOICE_CLEAN;
00067 voice->chan = NO_CHANNEL;
00068 voice->key = 0;
00069 voice->vel = 0;
00070 voice->channel = NULL;
00071 voice->sample = NULL;
00072 voice->output_rate = output_rate;
00073
00074
00075
00076
00077
00078
00079 voice->volenv_data[FLUID_VOICE_ENVSUSTAIN].count = 0xffffffff;
00080 voice->volenv_data[FLUID_VOICE_ENVSUSTAIN].coeff = 1.0f;
00081 voice->volenv_data[FLUID_VOICE_ENVSUSTAIN].incr = 0.0f;
00082 voice->volenv_data[FLUID_VOICE_ENVSUSTAIN].min = -1.0f;
00083 voice->volenv_data[FLUID_VOICE_ENVSUSTAIN].max = 2.0f;
00084
00085 voice->volenv_data[FLUID_VOICE_ENVFINISHED].count = 0xffffffff;
00086 voice->volenv_data[FLUID_VOICE_ENVFINISHED].coeff = 0.0f;
00087 voice->volenv_data[FLUID_VOICE_ENVFINISHED].incr = 0.0f;
00088 voice->volenv_data[FLUID_VOICE_ENVFINISHED].min = -1.0f;
00089 voice->volenv_data[FLUID_VOICE_ENVFINISHED].max = 1.0f;
00090
00091 voice->modenv_data[FLUID_VOICE_ENVSUSTAIN].count = 0xffffffff;
00092 voice->modenv_data[FLUID_VOICE_ENVSUSTAIN].coeff = 1.0f;
00093 voice->modenv_data[FLUID_VOICE_ENVSUSTAIN].incr = 0.0f;
00094 voice->modenv_data[FLUID_VOICE_ENVSUSTAIN].min = -1.0f;
00095 voice->modenv_data[FLUID_VOICE_ENVSUSTAIN].max = 2.0f;
00096
00097 voice->modenv_data[FLUID_VOICE_ENVFINISHED].count = 0xffffffff;
00098 voice->modenv_data[FLUID_VOICE_ENVFINISHED].coeff = 0.0f;
00099 voice->modenv_data[FLUID_VOICE_ENVFINISHED].incr = 0.0f;
00100 voice->modenv_data[FLUID_VOICE_ENVFINISHED].min = -1.0f;
00101 voice->modenv_data[FLUID_VOICE_ENVFINISHED].max = 1.0f;
00102
00103 return voice;
00104 }
00105
00106
00107
00108
00109 int
00110 delete_fluid_voice(fluid_voice_t* voice)
00111 {
00112 if (voice == NULL) {
00113 return FLUID_OK;
00114 }
00115 FLUID_FREE(voice);
00116 return FLUID_OK;
00117 }
00118
00119
00120
00121
00122
00123 int
00124 fluid_voice_init(fluid_voice_t* voice, fluid_sample_t* sample,
00125 fluid_channel_t* channel, int key, int vel, unsigned int id,
00126 unsigned int start_time, fluid_real_t gain)
00127 {
00128
00129
00130
00131
00132
00133
00134 voice->id = id;
00135 voice->chan = fluid_channel_get_num(channel);
00136 voice->key = (unsigned char) key;
00137 voice->vel = (unsigned char) vel;
00138 voice->channel = channel;
00139 voice->mod_count = 0;
00140 voice->sample = sample;
00141 voice->start_time = start_time;
00142 voice->ticks = 0;
00143 voice->debug = 0;
00144 voice->has_looped = 0;
00145 voice->last_fres = -1;
00146 voice->filter_startup = 1;
00147 voice->interp_method = fluid_channel_get_interp_method(voice->channel);
00148
00149
00150 voice->volenv_count = 0;
00151 voice->volenv_section = 0;
00152 voice->volenv_val = 0.0f;
00153 voice->amp = 0.0f;
00154
00155
00156
00157
00158 voice->modenv_count = 0;
00159 voice->modenv_section = 0;
00160 voice->modenv_val = 0.0f;
00161
00162
00163 voice->modlfo_val = 0.0;
00164
00165
00166
00167
00168 voice->viblfo_val = 0.0f;
00169
00170
00171 voice->hist1 = 0;
00172 voice->hist2 = 0;
00173
00174
00175
00176
00177
00178
00179
00180 fluid_gen_init(&voice->gen[0], channel);
00181
00182 voice->synth_gain = gain;
00183
00184 if (voice->synth_gain < 0.0000001){
00185 voice->synth_gain = 0.0000001;
00186 }
00187
00188
00189
00190
00191
00192
00193 voice->amplitude_that_reaches_noise_floor_nonloop = FLUID_NOISE_FLOOR / voice->synth_gain;
00194 voice->amplitude_that_reaches_noise_floor_loop = FLUID_NOISE_FLOOR / voice->synth_gain;
00195
00196
00197
00198 fluid_sample_incr_ref(voice->sample);
00199
00200 return FLUID_OK;
00201 }
00202
00203 void fluid_voice_gen_set(fluid_voice_t* voice, int i, float val)
00204 {
00205 voice->gen[i].val = val;
00206 voice->gen[i].flags = GEN_SET;
00207 }
00208
00209 void fluid_voice_gen_incr(fluid_voice_t* voice, int i, float val)
00210 {
00211 voice->gen[i].val += val;
00212 voice->gen[i].flags = GEN_SET;
00213 }
00214
00215 float fluid_voice_gen_get(fluid_voice_t* voice, int gen)
00216 {
00217 return voice->gen[gen].val;
00218 }
00219
00220 fluid_real_t fluid_voice_gen_value(fluid_voice_t* voice, int num)
00221 {
00222
00223
00224
00225 if (voice->gen[num].flags == GEN_ABS_NRPN) {
00226 return (fluid_real_t) voice->gen[num].nrpn;
00227 } else {
00228 return (fluid_real_t) (voice->gen[num].val + voice->gen[num].mod + voice->gen[num].nrpn);
00229 }
00230 }
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244 int
00245 fluid_voice_write(fluid_voice_t* voice,
00246 fluid_real_t* dsp_left_buf, fluid_real_t* dsp_right_buf,
00247 fluid_real_t* dsp_reverb_buf, fluid_real_t* dsp_chorus_buf)
00248 {
00249 unsigned int i;
00250 fluid_real_t incr;
00251 fluid_real_t fres;
00252 fluid_real_t target_amp;
00253 int count;
00254
00255 int dsp_interp_method = voice->interp_method;
00256
00257 fluid_real_t dsp_buf[FLUID_BUFSIZE];
00258 fluid_env_data_t* env_data;
00259 fluid_real_t x;
00260
00261
00262
00263 if (!_PLAYING(voice)) return FLUID_OK;
00264
00265
00266
00267 if (voice->sample == NULL)
00268 {
00269 fluid_voice_off(voice);
00270 return FLUID_OK;
00271 }
00272
00273 fluid_check_fpe ("voice_write startup");
00274
00275
00276
00277 fluid_voice_check_sample_sanity (voice);
00278
00279
00280
00281 env_data = &voice->volenv_data[voice->volenv_section];
00282
00283
00284 while (voice->volenv_count >= env_data->count)
00285 {
00286
00287 if (env_data && voice->volenv_section == FLUID_VOICE_ENVDECAY)
00288 voice->volenv_val = env_data->min * env_data->coeff;
00289
00290 env_data = &voice->volenv_data[++voice->volenv_section];
00291 voice->volenv_count = 0;
00292 }
00293
00294
00295 x = env_data->coeff * voice->volenv_val + env_data->incr;
00296 if (x < env_data->min)
00297 {
00298 x = env_data->min;
00299 voice->volenv_section++;
00300 voice->volenv_count = 0;
00301 }
00302 else if (x > env_data->max)
00303 {
00304 x = env_data->max;
00305 voice->volenv_section++;
00306 voice->volenv_count = 0;
00307 }
00308
00309 voice->volenv_val = x;
00310 voice->volenv_count++;
00311
00312 if (voice->volenv_section == FLUID_VOICE_ENVFINISHED)
00313 {
00314 fluid_profile (FLUID_PROF_VOICE_RELEASE, voice->ref);
00315 fluid_voice_off (voice);
00316 return FLUID_OK;
00317 }
00318
00319 fluid_check_fpe ("voice_write vol env");
00320
00321
00322
00323 env_data = &voice->modenv_data[voice->modenv_section];
00324
00325
00326 while (voice->modenv_count >= env_data->count)
00327 {
00328 env_data = &voice->modenv_data[++voice->modenv_section];
00329 voice->modenv_count = 0;
00330 }
00331
00332
00333 x = env_data->coeff * voice->modenv_val + env_data->incr;
00334
00335 if (x < env_data->min)
00336 {
00337 x = env_data->min;
00338 voice->modenv_section++;
00339 voice->modenv_count = 0;
00340 }
00341 else if (x > env_data->max)
00342 {
00343 x = env_data->max;
00344 voice->modenv_section++;
00345 voice->modenv_count = 0;
00346 }
00347
00348 voice->modenv_val = x;
00349 voice->modenv_count++;
00350 fluid_check_fpe ("voice_write mod env");
00351
00352
00353
00354 if (voice->ticks >= voice->modlfo_delay)
00355 {
00356 voice->modlfo_val += voice->modlfo_incr;
00357
00358 if (voice->modlfo_val > 1.0)
00359 {
00360 voice->modlfo_incr = -voice->modlfo_incr;
00361 voice->modlfo_val = (fluid_real_t) 2.0 - voice->modlfo_val;
00362 }
00363 else if (voice->modlfo_val < -1.0)
00364 {
00365 voice->modlfo_incr = -voice->modlfo_incr;
00366 voice->modlfo_val = (fluid_real_t) -2.0 - voice->modlfo_val;
00367 }
00368 }
00369
00370 fluid_check_fpe ("voice_write mod LFO");
00371
00372
00373
00374 if (voice->ticks >= voice->viblfo_delay)
00375 {
00376 voice->viblfo_val += voice->viblfo_incr;
00377
00378 if (voice->viblfo_val > (fluid_real_t) 1.0)
00379 {
00380 voice->viblfo_incr = -voice->viblfo_incr;
00381 voice->viblfo_val = (fluid_real_t) 2.0 - voice->viblfo_val;
00382 }
00383 else if (voice->viblfo_val < -1.0)
00384 {
00385 voice->viblfo_incr = -voice->viblfo_incr;
00386 voice->viblfo_val = (fluid_real_t) -2.0 - voice->viblfo_val;
00387 }
00388 }
00389
00390 fluid_check_fpe ("voice_write Vib LFO");
00391
00392
00393
00394
00395
00396
00397
00398
00399 if (voice->volenv_section == FLUID_VOICE_ENVDELAY)
00400 goto post_process;
00401
00402 if (voice->volenv_section == FLUID_VOICE_ENVATTACK)
00403 {
00404
00405
00406
00407 target_amp = fluid_atten2amp (voice->attenuation)
00408 * fluid_cb2amp (voice->modlfo_val * -voice->modlfo_to_vol)
00409 * voice->volenv_val;
00410 }
00411 else
00412 {
00413 fluid_real_t amplitude_that_reaches_noise_floor;
00414 fluid_real_t amp_max;
00415
00416 target_amp = fluid_atten2amp (voice->attenuation)
00417 * fluid_cb2amp (960.0f * (1.0f - voice->volenv_val)
00418 + voice->modlfo_val * -voice->modlfo_to_vol);
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432 if (voice->has_looped)
00433 amplitude_that_reaches_noise_floor = voice->amplitude_that_reaches_noise_floor_loop;
00434 else
00435 amplitude_that_reaches_noise_floor = voice->amplitude_that_reaches_noise_floor_nonloop;
00436
00437
00438
00439
00440
00441
00442
00443 amp_max = fluid_atten2amp (voice->min_attenuation_cB) * voice->volenv_val;
00444
00445
00446
00447
00448 if (amp_max < amplitude_that_reaches_noise_floor)
00449 {
00450 fluid_profile (FLUID_PROF_VOICE_RELEASE, voice->ref);
00451 fluid_voice_off (voice);
00452 goto post_process;
00453 }
00454 }
00455
00456
00457 voice->amp_incr = (target_amp - voice->amp) / FLUID_BUFSIZE;
00458
00459 fluid_check_fpe ("voice_write amplitude calculation");
00460
00461
00462 if ((voice->amp == 0.0f) && (voice->amp_incr == 0.0f))
00463 goto post_process;
00464
00465
00466
00467
00468
00469 voice->phase_incr = fluid_ct2hz_real
00470 (voice->pitch + voice->modlfo_val * voice->modlfo_to_pitch
00471 + voice->viblfo_val * voice->viblfo_to_pitch
00472 + voice->modenv_val * voice->modenv_to_pitch) / voice->root_pitch;
00473
00474 fluid_check_fpe ("voice_write phase calculation");
00475
00476
00477 if (voice->phase_incr == 0) voice->phase_incr = 1;
00478
00479
00480
00481
00482 fres = fluid_ct2hz(voice->fres
00483 + voice->modlfo_val * voice->modlfo_to_fc
00484 + voice->modenv_val * voice->modenv_to_fc);
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499 if (fres > 0.45f * voice->output_rate)
00500 fres = 0.45f * voice->output_rate;
00501 else if (fres < 5)
00502 fres = 5;
00503
00504
00505 if ((abs (fres - voice->last_fres) > 0.01))
00506 {
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522 fluid_real_t omega = (fluid_real_t) (2.0 * M_PI * (fres / ((float) voice->output_rate)));
00523 fluid_real_t sin_coeff = (fluid_real_t) sin(omega);
00524 fluid_real_t cos_coeff = (fluid_real_t) cos(omega);
00525 fluid_real_t alpha_coeff = sin_coeff / (2.0f * voice->q_lin);
00526 fluid_real_t a0_inv = 1.0f / (1.0f + alpha_coeff);
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537 fluid_real_t a1_temp = -2.0f * cos_coeff * a0_inv;
00538 fluid_real_t a2_temp = (1.0f - alpha_coeff) * a0_inv;
00539 fluid_real_t b1_temp = (1.0f - cos_coeff) * a0_inv * voice->filter_gain;
00540
00541 fluid_real_t b02_temp = b1_temp * 0.5f;
00542
00543 if (voice->filter_startup)
00544 {
00545
00546
00547
00548 voice->a1 = a1_temp;
00549 voice->a2 = a2_temp;
00550 voice->b02 = b02_temp;
00551 voice->b1 = b1_temp;
00552 voice->filter_coeff_incr_count = 0;
00553 voice->filter_startup = 0;
00554
00555 }
00556 else
00557 {
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568 #define FILTER_TRANSITION_SAMPLES (FLUID_BUFSIZE)
00569
00570 voice->a1_incr = (a1_temp - voice->a1) / FILTER_TRANSITION_SAMPLES;
00571 voice->a2_incr = (a2_temp - voice->a2) / FILTER_TRANSITION_SAMPLES;
00572 voice->b02_incr = (b02_temp - voice->b02) / FILTER_TRANSITION_SAMPLES;
00573 voice->b1_incr = (b1_temp - voice->b1) / FILTER_TRANSITION_SAMPLES;
00574
00575 voice->filter_coeff_incr_count = FILTER_TRANSITION_SAMPLES;
00576 }
00577 voice->last_fres = fres;
00578 fluid_check_fpe ("voice_write filter calculation");
00579 }
00580
00581
00582 fluid_check_fpe ("voice_write DSP coefficients");
00583
00584
00585
00586
00587
00588
00589
00590 voice->dsp_buf = dsp_buf;
00591
00592 switch (voice->interp_method)
00593 {
00594 case FLUID_INTERP_NONE:
00595 count = fluid_dsp_float_interpolate_none (voice);
00596 break;
00597 case FLUID_INTERP_LINEAR:
00598 count = fluid_dsp_float_interpolate_linear (voice);
00599 break;
00600 case FLUID_INTERP_4THORDER:
00601 default:
00602 count = fluid_dsp_float_interpolate_4th_order (voice);
00603 break;
00604 case FLUID_INTERP_7THORDER:
00605 count = fluid_dsp_float_interpolate_7th_order (voice);
00606 break;
00607 }
00608
00609 fluid_check_fpe ("voice_write interpolation");
00610
00611 if (count > 0)
00612 fluid_voice_effects (voice, count, dsp_left_buf, dsp_right_buf,
00613 dsp_reverb_buf, dsp_chorus_buf);
00614
00615
00616 if (count < FLUID_BUFSIZE)
00617 {
00618 fluid_profile(FLUID_PROF_VOICE_RELEASE, voice->ref);
00619 fluid_voice_off(voice);
00620 }
00621
00622 post_process:
00623 voice->ticks += FLUID_BUFSIZE;
00624 fluid_check_fpe ("voice_write postprocess");
00625 return FLUID_OK;
00626 }
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659 static inline void
00660 fluid_voice_effects (fluid_voice_t *voice, int count,
00661 fluid_real_t* dsp_left_buf, fluid_real_t* dsp_right_buf,
00662 fluid_real_t* dsp_reverb_buf, fluid_real_t* dsp_chorus_buf)
00663 {
00664
00665 fluid_real_t dsp_hist1 = voice->hist1;
00666 fluid_real_t dsp_hist2 = voice->hist2;
00667
00668
00669 fluid_real_t dsp_a1 = voice->a1;
00670 fluid_real_t dsp_a2 = voice->a2;
00671 fluid_real_t dsp_b02 = voice->b02;
00672 fluid_real_t dsp_b1 = voice->b1;
00673 fluid_real_t dsp_a1_incr = voice->a1_incr;
00674 fluid_real_t dsp_a2_incr = voice->a2_incr;
00675 fluid_real_t dsp_b02_incr = voice->b02_incr;
00676 fluid_real_t dsp_b1_incr = voice->b1_incr;
00677 int dsp_filter_coeff_incr_count = voice->filter_coeff_incr_count;
00678
00679 fluid_real_t *dsp_buf = voice->dsp_buf;
00680
00681 fluid_real_t dsp_centernode;
00682 int dsp_i;
00683 float v;
00684
00685
00686
00687
00688 if (fabs (dsp_hist1) < 1e-20) dsp_hist1 = 0.0f;
00689
00690
00691
00692
00693
00694
00695 if (dsp_filter_coeff_incr_count > 0)
00696 {
00697
00698 for (dsp_i = 0; dsp_i < count; dsp_i++)
00699 {
00700
00701 dsp_centernode = dsp_buf[dsp_i] - dsp_a1 * dsp_hist1 - dsp_a2 * dsp_hist2;
00702 dsp_buf[dsp_i] = dsp_b02 * (dsp_centernode + dsp_hist2) + dsp_b1 * dsp_hist1;
00703 dsp_hist2 = dsp_hist1;
00704 dsp_hist1 = dsp_centernode;
00705
00706 if (dsp_filter_coeff_incr_count-- > 0)
00707 {
00708 dsp_a1 += dsp_a1_incr;
00709 dsp_a2 += dsp_a2_incr;
00710 dsp_b02 += dsp_b02_incr;
00711 dsp_b1 += dsp_b1_incr;
00712 }
00713 }
00714 }
00715 else
00716 {
00717 for (dsp_i = 0; dsp_i < count; dsp_i++)
00718 {
00719 dsp_centernode = dsp_buf[dsp_i] - dsp_a1 * dsp_hist1 - dsp_a2 * dsp_hist2;
00720 dsp_buf[dsp_i] = dsp_b02 * (dsp_centernode + dsp_hist2) + dsp_b1 * dsp_hist1;
00721 dsp_hist2 = dsp_hist1;
00722 dsp_hist1 = dsp_centernode;
00723 }
00724 }
00725
00726
00727
00728
00729
00730
00731 if ((-0.5 < voice->pan) && (voice->pan < 0.5))
00732 {
00733
00734 for (dsp_i = 0; dsp_i < count; dsp_i++)
00735 {
00736 v = voice->amp_left * dsp_buf[dsp_i];
00737 dsp_left_buf[dsp_i] += v;
00738 dsp_right_buf[dsp_i] += v;
00739 }
00740 }
00741 else
00742 {
00743 if (voice->amp_left != 0.0)
00744 {
00745 for (dsp_i = 0; dsp_i < count; dsp_i++)
00746 dsp_left_buf[dsp_i] += voice->amp_left * dsp_buf[dsp_i];
00747 }
00748
00749 if (voice->amp_right != 0.0)
00750 {
00751 for (dsp_i = 0; dsp_i < count; dsp_i++)
00752 dsp_right_buf[dsp_i] += voice->amp_right * dsp_buf[dsp_i];
00753 }
00754 }
00755
00756
00757 if ((dsp_reverb_buf != NULL) && (voice->amp_reverb != 0.0))
00758 {
00759 for (dsp_i = 0; dsp_i < count; dsp_i++)
00760 dsp_reverb_buf[dsp_i] += voice->amp_reverb * dsp_buf[dsp_i];
00761 }
00762
00763
00764 if ((dsp_chorus_buf != NULL) && (voice->amp_chorus != 0))
00765 {
00766 for (dsp_i = 0; dsp_i < count; dsp_i++)
00767 dsp_chorus_buf[dsp_i] += voice->amp_chorus * dsp_buf[dsp_i];
00768 }
00769
00770 voice->hist1 = dsp_hist1;
00771 voice->hist2 = dsp_hist2;
00772 voice->a1 = dsp_a1;
00773 voice->a2 = dsp_a2;
00774 voice->b02 = dsp_b02;
00775 voice->b1 = dsp_b1;
00776 voice->filter_coeff_incr_count = dsp_filter_coeff_incr_count;
00777
00778 fluid_check_fpe ("voice_effects");
00779 }
00780
00781
00782
00783
00784 fluid_channel_t*
00785 fluid_voice_get_channel(fluid_voice_t* voice)
00786 {
00787 return voice->channel;
00788 }
00789
00790
00791
00792
00793 void fluid_voice_start(fluid_voice_t* voice)
00794 {
00795
00796
00797
00798
00799 fluid_voice_calculate_runtime_synthesis_parameters(voice);
00800
00801
00802
00803 voice->check_sample_sanity_flag=FLUID_SAMPLESANITY_STARTUP;
00804
00805 voice->ref = fluid_profile_ref();
00806
00807 voice->status = FLUID_VOICE_ON;
00808 }
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820 int
00821 fluid_voice_calculate_runtime_synthesis_parameters(fluid_voice_t* voice)
00822 {
00823 fluid_real_t x;
00824 fluid_real_t q_db;
00825 int i;
00826
00827 int list_of_generators_to_initialize[35] = {
00828 GEN_STARTADDROFS,
00829 GEN_ENDADDROFS,
00830 GEN_STARTLOOPADDROFS,
00831 GEN_ENDLOOPADDROFS,
00832
00833 GEN_MODLFOTOPITCH,
00834 GEN_VIBLFOTOPITCH,
00835 GEN_MODENVTOPITCH,
00836 GEN_FILTERFC,
00837 GEN_FILTERQ,
00838 GEN_MODLFOTOFILTERFC,
00839 GEN_MODENVTOFILTERFC,
00840
00841 GEN_MODLFOTOVOL,
00842
00843 GEN_CHORUSSEND,
00844 GEN_REVERBSEND,
00845 GEN_PAN,
00846
00847
00848
00849 GEN_MODLFODELAY,
00850 GEN_MODLFOFREQ,
00851 GEN_VIBLFODELAY,
00852 GEN_VIBLFOFREQ,
00853 GEN_MODENVDELAY,
00854 GEN_MODENVATTACK,
00855 GEN_MODENVHOLD,
00856 GEN_MODENVDECAY,
00857
00858 GEN_MODENVRELEASE,
00859
00860
00861 GEN_VOLENVDELAY,
00862 GEN_VOLENVATTACK,
00863 GEN_VOLENVHOLD,
00864 GEN_VOLENVDECAY,
00865
00866 GEN_VOLENVRELEASE,
00867
00868
00869
00870 GEN_KEYNUM,
00871 GEN_VELOCITY,
00872 GEN_ATTENUATION,
00873
00874
00875
00876 GEN_OVERRIDEROOTKEY,
00877 GEN_PITCH,
00878 -1};
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899 for (i = 0; i < voice->mod_count; i++) {
00900 fluid_mod_t* mod = &voice->mod[i];
00901 fluid_real_t modval = fluid_mod_get_value(mod, voice->channel, voice);
00902 int dest_gen_index = mod->dest;
00903 fluid_gen_t* dest_gen = &voice->gen[dest_gen_index];
00904 dest_gen->mod += modval;
00905
00906 }
00907
00908
00909
00910
00911
00912
00913
00914
00915 if (fluid_channel_has_tuning(voice->channel)) {
00916
00917 #define __pitch(_k) fluid_tuning_get_pitch(tuning, _k)
00918 fluid_tuning_t* tuning = fluid_channel_get_tuning(voice->channel);
00919 voice->gen[GEN_PITCH].val = (__pitch(60) + (voice->gen[GEN_SCALETUNE].val / 100.0f *
00920 (__pitch(voice->key) - __pitch(60))));
00921 } else {
00922 voice->gen[GEN_PITCH].val = (voice->gen[GEN_SCALETUNE].val * (voice->key - 60.0f)
00923 + 100.0f * 60.0f);
00924 }
00925
00926
00927
00928
00929
00930
00931
00932
00933
00934
00935
00936
00937
00938
00939
00940 for (i = 0; list_of_generators_to_initialize[i] != -1; i++) {
00941 fluid_voice_update_param(voice, list_of_generators_to_initialize[i]);
00942 }
00943
00944
00945 voice->min_attenuation_cB = fluid_voice_get_lower_boundary_for_attenuation(voice);
00946
00947 return FLUID_OK;
00948 }
00949
00950
00951
00952
00953 int calculate_hold_decay_buffers(fluid_voice_t* voice, int gen_base,
00954 int gen_key2base, int is_decay)
00955 {
00956
00957
00958
00959
00960
00961
00962
00963
00964
00965 fluid_real_t timecents;
00966 fluid_real_t seconds;
00967 int buffers;
00968
00969
00970
00971
00972
00973
00974
00975
00976 timecents = (_GEN(voice, gen_base) + _GEN(voice, gen_key2base) * (60.0 - voice->key));
00977
00978
00979 if (is_decay){
00980
00981 if (timecents > 8000.0) {
00982 timecents = 8000.0;
00983 }
00984 } else {
00985
00986 if (timecents > 5000) {
00987 timecents = 5000.0;
00988 }
00989
00990
00991
00992 if (timecents <= -32768.) {
00993 return 0;
00994 }
00995 }
00996
00997 if (timecents < -12000.0) {
00998 timecents = -12000.0;
00999 }
01000
01001 seconds = fluid_tc2sec(timecents);
01002
01003
01004
01005 buffers = (int)(((fluid_real_t)voice->output_rate * seconds)
01006 / (fluid_real_t)FLUID_BUFSIZE
01007 +0.5);
01008
01009 return buffers;
01010 }
01011
01012
01013
01014
01015
01016
01017
01018
01019
01020
01021
01022
01023
01024
01025
01026
01027
01028
01029
01030
01031 void
01032 fluid_voice_update_param(fluid_voice_t* voice, int gen)
01033 {
01034 double q_dB;
01035 fluid_real_t x;
01036 fluid_real_t y;
01037 unsigned int count;
01038
01039 static const float ALT_ATTENUATION_SCALE = 0.4;
01040
01041 switch (gen) {
01042
01043 case GEN_PAN:
01044
01045 voice->pan = _GEN(voice, GEN_PAN);
01046 voice->amp_left = fluid_pan(voice->pan, 1) * voice->synth_gain / 32768.0f;
01047 voice->amp_right = fluid_pan(voice->pan, 0) * voice->synth_gain / 32768.0f;
01048 break;
01049
01050 case GEN_ATTENUATION:
01051 voice->attenuation = ((fluid_real_t)(voice)->gen[GEN_ATTENUATION].val*ALT_ATTENUATION_SCALE) +
01052 (fluid_real_t)(voice)->gen[GEN_ATTENUATION].mod + (fluid_real_t)(voice)->gen[GEN_ATTENUATION].nrpn;
01053
01054
01055
01056
01057 fluid_clip(voice->attenuation, 0.0, 1440.0);
01058 break;
01059
01060
01061
01062
01063 case GEN_PITCH:
01064 case GEN_COARSETUNE:
01065 case GEN_FINETUNE:
01066
01067 voice->pitch = (_GEN(voice, GEN_PITCH)
01068 + 100.0f * _GEN(voice, GEN_COARSETUNE)
01069 + _GEN(voice, GEN_FINETUNE));
01070 break;
01071
01072 case GEN_REVERBSEND:
01073
01074 voice->reverb_send = _GEN(voice, GEN_REVERBSEND) / 1000.0f;
01075 fluid_clip(voice->reverb_send, 0.0, 1.0);
01076 voice->amp_reverb = voice->reverb_send * voice->synth_gain / 32768.0f;
01077 break;
01078
01079 case GEN_CHORUSSEND:
01080
01081 voice->chorus_send = _GEN(voice, GEN_CHORUSSEND) / 1000.0f;
01082 fluid_clip(voice->chorus_send, 0.0, 1.0);
01083 voice->amp_chorus = voice->chorus_send * voice->synth_gain / 32768.0f;
01084 break;
01085
01086 case GEN_OVERRIDEROOTKEY:
01087
01088
01089
01090
01091
01092
01093 if (voice->gen[GEN_OVERRIDEROOTKEY].val > -1) {
01094 voice->root_pitch = voice->gen[GEN_OVERRIDEROOTKEY].val * 100.0f
01095 - voice->sample->pitchadj;
01096 } else {
01097 voice->root_pitch = voice->sample->origpitch * 100.0f - voice->sample->pitchadj;
01098 }
01099 voice->root_pitch = fluid_ct2hz(voice->root_pitch);
01100 if (voice->sample != NULL) {
01101 voice->root_pitch *= (fluid_real_t) voice->output_rate / voice->sample->samplerate;
01102 }
01103 break;
01104
01105 case GEN_FILTERFC:
01106
01107
01108
01109
01110
01111 voice->fres = _GEN(voice, GEN_FILTERFC);
01112
01113
01114
01115 voice->last_fres = -1.0f;
01116 break;
01117
01118 case GEN_FILTERQ:
01119
01120
01121 q_dB = _GEN(voice, GEN_FILTERQ) / 10.0f;
01122
01123
01124 fluid_clip(q_dB, 0.0f, 96.0f);
01125
01126
01127
01128
01129
01130
01131
01132
01133
01134
01135
01136
01137
01138
01139
01140
01141 q_dB -= 3.01f;
01142
01143
01144
01145 voice->q_lin = (fluid_real_t) (pow(10.0f, q_dB / 20.0f));
01146
01147
01148
01149
01150
01151
01152
01153
01154
01155
01156
01157
01158 voice->filter_gain = (fluid_real_t) (1.0 / sqrt(voice->q_lin));
01159
01160
01161 voice->last_fres = -1.;
01162 break;
01163
01164 case GEN_MODLFOTOPITCH:
01165 voice->modlfo_to_pitch = _GEN(voice, GEN_MODLFOTOPITCH);
01166 fluid_clip(voice->modlfo_to_pitch, -12000.0, 12000.0);
01167 break;
01168
01169 case GEN_MODLFOTOVOL:
01170 voice->modlfo_to_vol = _GEN(voice, GEN_MODLFOTOVOL);
01171 fluid_clip(voice->modlfo_to_vol, -960.0, 960.0);
01172 break;
01173
01174 case GEN_MODLFOTOFILTERFC:
01175 voice->modlfo_to_fc = _GEN(voice, GEN_MODLFOTOFILTERFC);
01176 fluid_clip(voice->modlfo_to_fc, -12000, 12000);
01177 break;
01178
01179 case GEN_MODLFODELAY:
01180 x = _GEN(voice, GEN_MODLFODELAY);
01181 fluid_clip(x, -12000.0f, 5000.0f);
01182 voice->modlfo_delay = (unsigned int) (voice->output_rate * fluid_tc2sec_delay(x));
01183 break;
01184
01185 case GEN_MODLFOFREQ:
01186
01187
01188
01189 x = _GEN(voice, GEN_MODLFOFREQ);
01190 fluid_clip(x, -16000.0f, 4500.0f);
01191 voice->modlfo_incr = (4.0f * FLUID_BUFSIZE * fluid_act2hz(x) / voice->output_rate);
01192 break;
01193
01194 case GEN_VIBLFOFREQ:
01195
01196
01197
01198
01199
01200 x = _GEN(voice, GEN_VIBLFOFREQ);
01201 fluid_clip(x, -16000.0f, 4500.0f);
01202 voice->viblfo_incr = (4.0f * FLUID_BUFSIZE * fluid_act2hz(x) / voice->output_rate);
01203 break;
01204
01205 case GEN_VIBLFODELAY:
01206 x = _GEN(voice,GEN_VIBLFODELAY);
01207 fluid_clip(x, -12000.0f, 5000.0f);
01208 voice->viblfo_delay = (unsigned int) (voice->output_rate * fluid_tc2sec_delay(x));
01209 break;
01210
01211 case GEN_VIBLFOTOPITCH:
01212 voice->viblfo_to_pitch = _GEN(voice, GEN_VIBLFOTOPITCH);
01213 fluid_clip(voice->viblfo_to_pitch, -12000.0, 12000.0);
01214 break;
01215
01216 case GEN_KEYNUM:
01217
01218
01219
01220
01221
01222
01223
01224
01225 x = _GEN(voice, GEN_KEYNUM);
01226 if (x >= 0){
01227 voice->key = x;
01228 }
01229 break;
01230
01231 case GEN_VELOCITY:
01232
01233
01234
01235
01236
01237
01238
01239 x = _GEN(voice, GEN_VELOCITY);
01240 if (x > 0) {
01241 voice->vel = x;
01242 }
01243 break;
01244
01245 case GEN_MODENVTOPITCH:
01246 voice->modenv_to_pitch = _GEN(voice, GEN_MODENVTOPITCH);
01247 fluid_clip(voice->modenv_to_pitch, -12000.0, 12000.0);
01248 break;
01249
01250 case GEN_MODENVTOFILTERFC:
01251 voice->modenv_to_fc = _GEN(voice,GEN_MODENVTOFILTERFC);
01252
01253
01254
01255
01256
01257 fluid_clip(voice->modenv_to_fc, -12000.0, 12000.0);
01258 break;
01259
01260
01261
01262
01263
01264
01265
01266
01267
01268
01269
01270
01271
01272 case GEN_STARTADDROFS:
01273 case GEN_STARTADDRCOARSEOFS:
01274 if (voice->sample != NULL) {
01275 voice->start = (voice->sample->start
01276 + (int) _GEN(voice, GEN_STARTADDROFS)
01277 + 32768 * (int) _GEN(voice, GEN_STARTADDRCOARSEOFS));
01278 voice->check_sample_sanity_flag = FLUID_SAMPLESANITY_CHECK;
01279 }
01280 break;
01281 case GEN_ENDADDROFS:
01282 case GEN_ENDADDRCOARSEOFS:
01283 if (voice->sample != NULL) {
01284 voice->end = (voice->sample->end
01285 + (int) _GEN(voice, GEN_ENDADDROFS)
01286 + 32768 * (int) _GEN(voice, GEN_ENDADDRCOARSEOFS));
01287 voice->check_sample_sanity_flag = FLUID_SAMPLESANITY_CHECK;
01288 }
01289 break;
01290 case GEN_STARTLOOPADDROFS:
01291 case GEN_STARTLOOPADDRCOARSEOFS:
01292 if (voice->sample != NULL) {
01293 voice->loopstart = (voice->sample->loopstart
01294 + (int) _GEN(voice, GEN_STARTLOOPADDROFS)
01295 + 32768 * (int) _GEN(voice, GEN_STARTLOOPADDRCOARSEOFS));
01296 voice->check_sample_sanity_flag = FLUID_SAMPLESANITY_CHECK;
01297 }
01298 break;
01299
01300 case GEN_ENDLOOPADDROFS:
01301 case GEN_ENDLOOPADDRCOARSEOFS:
01302 if (voice->sample != NULL) {
01303 voice->loopend = (voice->sample->loopend
01304 + (int) _GEN(voice, GEN_ENDLOOPADDROFS)
01305 + 32768 * (int) _GEN(voice, GEN_ENDLOOPADDRCOARSEOFS));
01306 voice->check_sample_sanity_flag = FLUID_SAMPLESANITY_CHECK;
01307 }
01308 break;
01309
01310
01311 #define NUM_BUFFERS_DELAY(_v) (unsigned int) (voice->output_rate * fluid_tc2sec_delay(_v) / FLUID_BUFSIZE)
01312 #define NUM_BUFFERS_ATTACK(_v) (unsigned int) (voice->output_rate * fluid_tc2sec_attack(_v) / FLUID_BUFSIZE)
01313 #define NUM_BUFFERS_RELEASE(_v) (unsigned int) (voice->output_rate * fluid_tc2sec_release(_v) / FLUID_BUFSIZE)
01314
01315
01316
01317
01318
01319
01320
01321 case GEN_VOLENVDELAY:
01322 x = _GEN(voice, GEN_VOLENVDELAY);
01323 fluid_clip(x, -12000.0f, 5000.0f);
01324 count = NUM_BUFFERS_DELAY(x);
01325 voice->volenv_data[FLUID_VOICE_ENVDELAY].count = count;
01326 voice->volenv_data[FLUID_VOICE_ENVDELAY].coeff = 0.0f;
01327 voice->volenv_data[FLUID_VOICE_ENVDELAY].incr = 0.0f;
01328 voice->volenv_data[FLUID_VOICE_ENVDELAY].min = -1.0f;
01329 voice->volenv_data[FLUID_VOICE_ENVDELAY].max = 1.0f;
01330 break;
01331
01332 case GEN_VOLENVATTACK:
01333 x = _GEN(voice, GEN_VOLENVATTACK);
01334 fluid_clip(x, -12000.0f, 8000.0f);
01335 count = 1 + NUM_BUFFERS_ATTACK(x);
01336 voice->volenv_data[FLUID_VOICE_ENVATTACK].count = count;
01337 voice->volenv_data[FLUID_VOICE_ENVATTACK].coeff = 1.0f;
01338 voice->volenv_data[FLUID_VOICE_ENVATTACK].incr = count ? 1.0f / count : 0.0f;
01339 voice->volenv_data[FLUID_VOICE_ENVATTACK].min = -1.0f;
01340 voice->volenv_data[FLUID_VOICE_ENVATTACK].max = 1.0f;
01341 break;
01342
01343 case GEN_VOLENVHOLD:
01344 case GEN_KEYTOVOLENVHOLD:
01345 count = calculate_hold_decay_buffers(voice, GEN_VOLENVHOLD, GEN_KEYTOVOLENVHOLD, 0);
01346 voice->volenv_data[FLUID_VOICE_ENVHOLD].count = count;
01347 voice->volenv_data[FLUID_VOICE_ENVHOLD].coeff = 1.0f;
01348 voice->volenv_data[FLUID_VOICE_ENVHOLD].incr = 0.0f;
01349 voice->volenv_data[FLUID_VOICE_ENVHOLD].min = -1.0f;
01350 voice->volenv_data[FLUID_VOICE_ENVHOLD].max = 2.0f;
01351 break;
01352
01353 case GEN_VOLENVDECAY:
01354 case GEN_VOLENVSUSTAIN:
01355 case GEN_KEYTOVOLENVDECAY:
01356 y = 1.0f - 0.001f * _GEN(voice, GEN_VOLENVSUSTAIN);
01357 fluid_clip(y, 0.0f, 1.0f);
01358 count = calculate_hold_decay_buffers(voice, GEN_VOLENVDECAY, GEN_KEYTOVOLENVDECAY, 1);
01359 voice->volenv_data[FLUID_VOICE_ENVDECAY].count = count;
01360 voice->volenv_data[FLUID_VOICE_ENVDECAY].coeff = 1.0f;
01361 voice->volenv_data[FLUID_VOICE_ENVDECAY].incr = count ? -1.0f / count : 0.0f;
01362 voice->volenv_data[FLUID_VOICE_ENVDECAY].min = y;
01363 voice->volenv_data[FLUID_VOICE_ENVDECAY].max = 2.0f;
01364 break;
01365
01366 case GEN_VOLENVRELEASE:
01367 x = _GEN(voice, GEN_VOLENVRELEASE);
01368 fluid_clip(x, FLUID_MIN_VOLENVRELEASE, 8000.0f);
01369 count = 1 + NUM_BUFFERS_RELEASE(x);
01370 voice->volenv_data[FLUID_VOICE_ENVRELEASE].count = count;
01371 voice->volenv_data[FLUID_VOICE_ENVRELEASE].coeff = 1.0f;
01372 voice->volenv_data[FLUID_VOICE_ENVRELEASE].incr = count ? -1.0f / count : 0.0f;
01373 voice->volenv_data[FLUID_VOICE_ENVRELEASE].min = 0.0f;
01374 voice->volenv_data[FLUID_VOICE_ENVRELEASE].max = 1.0f;
01375 break;
01376
01377
01378 case GEN_MODENVDELAY:
01379 x = _GEN(voice, GEN_MODENVDELAY);
01380 fluid_clip(x, -12000.0f, 5000.0f);
01381 voice->modenv_data[FLUID_VOICE_ENVDELAY].count = NUM_BUFFERS_DELAY(x);
01382 voice->modenv_data[FLUID_VOICE_ENVDELAY].coeff = 0.0f;
01383 voice->modenv_data[FLUID_VOICE_ENVDELAY].incr = 0.0f;
01384 voice->modenv_data[FLUID_VOICE_ENVDELAY].min = -1.0f;
01385 voice->modenv_data[FLUID_VOICE_ENVDELAY].max = 1.0f;
01386 break;
01387
01388 case GEN_MODENVATTACK:
01389 x = _GEN(voice, GEN_MODENVATTACK);
01390 fluid_clip(x, -12000.0f, 8000.0f);
01391 count = 1 + NUM_BUFFERS_ATTACK(x);
01392 voice->modenv_data[FLUID_VOICE_ENVATTACK].count = count;
01393 voice->modenv_data[FLUID_VOICE_ENVATTACK].coeff = 1.0f;
01394 voice->modenv_data[FLUID_VOICE_ENVATTACK].incr = count ? 1.0f / count : 0.0f;
01395 voice->modenv_data[FLUID_VOICE_ENVATTACK].min = -1.0f;
01396 voice->modenv_data[FLUID_VOICE_ENVATTACK].max = 1.0f;
01397 break;
01398
01399 case GEN_MODENVHOLD:
01400 case GEN_KEYTOMODENVHOLD:
01401 count = calculate_hold_decay_buffers(voice, GEN_MODENVHOLD, GEN_KEYTOMODENVHOLD, 0);
01402 voice->modenv_data[FLUID_VOICE_ENVHOLD].count = count;
01403 voice->modenv_data[FLUID_VOICE_ENVHOLD].coeff = 1.0f;
01404 voice->modenv_data[FLUID_VOICE_ENVHOLD].incr = 0.0f;
01405 voice->modenv_data[FLUID_VOICE_ENVHOLD].min = -1.0f;
01406 voice->modenv_data[FLUID_VOICE_ENVHOLD].max = 2.0f;
01407 break;
01408
01409 case GEN_MODENVDECAY:
01410 case GEN_MODENVSUSTAIN:
01411 case GEN_KEYTOMODENVDECAY:
01412 count = calculate_hold_decay_buffers(voice, GEN_MODENVDECAY, GEN_KEYTOMODENVDECAY, 1);
01413 y = 1.0f - 0.001f * _GEN(voice, GEN_MODENVSUSTAIN);
01414 fluid_clip(y, 0.0f, 1.0f);
01415 voice->modenv_data[FLUID_VOICE_ENVDECAY].count = count;
01416 voice->modenv_data[FLUID_VOICE_ENVDECAY].coeff = 1.0f;
01417 voice->modenv_data[FLUID_VOICE_ENVDECAY].incr = count ? -1.0f / count : 0.0f;
01418 voice->modenv_data[FLUID_VOICE_ENVDECAY].min = y;
01419 voice->modenv_data[FLUID_VOICE_ENVDECAY].max = 2.0f;
01420 break;
01421
01422 case GEN_MODENVRELEASE:
01423 x = _GEN(voice, GEN_MODENVRELEASE);
01424 fluid_clip(x, -12000.0f, 8000.0f);
01425 count = 1 + NUM_BUFFERS_RELEASE(x);
01426 voice->modenv_data[FLUID_VOICE_ENVRELEASE].count = count;
01427 voice->modenv_data[FLUID_VOICE_ENVRELEASE].coeff = 1.0f;
01428 voice->modenv_data[FLUID_VOICE_ENVRELEASE].incr = count ? -1.0f / count : 0.0;
01429 voice->modenv_data[FLUID_VOICE_ENVRELEASE].min = 0.0f;
01430 voice->modenv_data[FLUID_VOICE_ENVRELEASE].max = 2.0f;
01431 break;
01432
01433 }
01434 }
01435
01463 int fluid_voice_modulate(fluid_voice_t* voice, int cc, int ctrl)
01464 {
01465 int i, k;
01466 fluid_mod_t* mod;
01467 int gen;
01468 fluid_real_t modval;
01469
01470
01471
01472 for (i = 0; i < voice->mod_count; i++) {
01473
01474 mod = &voice->mod[i];
01475
01476
01477
01478 if (fluid_mod_has_source(mod, cc, ctrl)) {
01479
01480 gen = fluid_mod_get_dest(mod);
01481 modval = 0.0;
01482
01483
01484
01485 for (k = 0; k < voice->mod_count; k++) {
01486 if (fluid_mod_has_dest(&voice->mod[k], gen)) {
01487 modval += fluid_mod_get_value(&voice->mod[k], voice->channel, voice);
01488 }
01489 }
01490
01491 fluid_gen_set_mod(&voice->gen[gen], modval);
01492
01493
01494
01495
01496 fluid_voice_update_param(voice, gen);
01497 }
01498 }
01499 return FLUID_OK;
01500 }
01501
01509 int fluid_voice_modulate_all(fluid_voice_t* voice)
01510 {
01511 fluid_mod_t* mod;
01512 int i, k, gen;
01513 fluid_real_t modval;
01514
01515
01516
01517
01518
01519
01520
01521
01522
01523
01524 for (i = 0; i < voice->mod_count; i++) {
01525
01526 mod = &voice->mod[i];
01527 gen = fluid_mod_get_dest(mod);
01528 modval = 0.0;
01529
01530
01531
01532 for (k = 0; k < voice->mod_count; k++) {
01533 if (fluid_mod_has_dest(&voice->mod[k], gen)) {
01534 modval += fluid_mod_get_value(&voice->mod[k], voice->channel, voice);
01535 }
01536 }
01537
01538 fluid_gen_set_mod(&voice->gen[gen], modval);
01539
01540
01541
01542 fluid_voice_update_param(voice, gen);
01543 }
01544
01545 return FLUID_OK;
01546 }
01547
01548
01549
01550
01551 int
01552 fluid_voice_noteoff(fluid_voice_t* voice)
01553 {
01554 fluid_profile(FLUID_PROF_VOICE_NOTE, voice->ref);
01555
01556 if (voice->channel && fluid_channel_sustained(voice->channel)) {
01557 voice->status = FLUID_VOICE_SUSTAINED;
01558 } else {
01559 if (voice->volenv_section == FLUID_VOICE_ENVATTACK) {
01560
01561
01562
01563
01564
01565
01566 if (voice->volenv_val > 0){
01567 fluid_real_t lfo = voice->modlfo_val * -voice->modlfo_to_vol;
01568 fluid_real_t amp = voice->volenv_val * pow (10.0, lfo / -200);
01569 fluid_real_t env_value = - ((-200 * log (amp) / log (10.0) - lfo) / 960.0 - 1);
01570 fluid_clip (env_value, 0.0, 1.0);
01571 voice->volenv_val = env_value;
01572 }
01573 }
01574 voice->volenv_section = FLUID_VOICE_ENVRELEASE;
01575 voice->volenv_count = 0;
01576 voice->modenv_section = FLUID_VOICE_ENVRELEASE;
01577 voice->modenv_count = 0;
01578 }
01579
01580 return FLUID_OK;
01581 }
01582
01583
01584
01585
01586
01587
01588
01589
01590
01591
01592
01593
01594 int
01595 fluid_voice_kill_excl(fluid_voice_t* voice){
01596
01597 if (!_PLAYING(voice)) {
01598 return FLUID_OK;
01599 }
01600
01601
01602
01603
01604 fluid_voice_gen_set(voice, GEN_EXCLUSIVECLASS, 0);
01605
01606
01607 if (voice->volenv_section != FLUID_VOICE_ENVRELEASE){
01608 voice->volenv_section = FLUID_VOICE_ENVRELEASE;
01609 voice->volenv_count = 0;
01610 voice->modenv_section = FLUID_VOICE_ENVRELEASE;
01611 voice->modenv_count = 0;
01612 }
01613
01614
01615
01616 fluid_voice_gen_set(voice, GEN_VOLENVRELEASE, -200);
01617 fluid_voice_update_param(voice, GEN_VOLENVRELEASE);
01618
01619
01620 fluid_voice_gen_set(voice, GEN_MODENVRELEASE, -200);
01621 fluid_voice_update_param(voice, GEN_MODENVRELEASE);
01622
01623 return FLUID_OK;
01624 }
01625
01626
01627
01628
01629
01630
01631
01632
01633 int
01634 fluid_voice_off(fluid_voice_t* voice)
01635 {
01636 fluid_profile(FLUID_PROF_VOICE_RELEASE, voice->ref);
01637
01638 voice->chan = NO_CHANNEL;
01639 voice->volenv_section = FLUID_VOICE_ENVFINISHED;
01640 voice->volenv_count = 0;
01641 voice->modenv_section = FLUID_VOICE_ENVFINISHED;
01642 voice->modenv_count = 0;
01643 voice->status = FLUID_VOICE_OFF;
01644
01645
01646 if (voice->sample) {
01647 fluid_sample_decr_ref(voice->sample);
01648 voice->sample = NULL;
01649 }
01650
01651 return FLUID_OK;
01652 }
01653
01654
01655
01656
01657
01658
01659
01660
01661
01662
01663
01664
01665 void
01666 fluid_voice_add_mod(fluid_voice_t* voice, fluid_mod_t* mod, int mode)
01667 {
01668 int i;
01669
01670
01671
01672
01673
01674
01675
01676 if (((mod->flags1 & FLUID_MOD_CC) == 0)
01677 && ((mod->src1 != 0)
01678 && (mod->src1 != 2)
01679 && (mod->src1 != 3)
01680 && (mod->src1 != 10)
01681 && (mod->src1 != 13)
01682 && (mod->src1 != 14)
01683 && (mod->src1 != 16))) {
01684 FLUID_LOG(FLUID_WARN, "Ignoring invalid controller, using non-CC source %i.", mod->src1);
01685 return;
01686 }
01687
01688 if (mode == FLUID_VOICE_ADD) {
01689
01690
01691 for (i = 0; i < voice->mod_count; i++) {
01692 if (fluid_mod_test_identity(&voice->mod[i], mod)) {
01693
01694 voice->mod[i].amount += mod->amount;
01695 return;
01696 }
01697 }
01698
01699 } else if (mode == FLUID_VOICE_OVERWRITE) {
01700
01701
01702 for (i = 0; i < voice->mod_count; i++) {
01703 if (fluid_mod_test_identity(&voice->mod[i], mod)) {
01704
01705 voice->mod[i].amount = mod->amount;
01706 return;
01707 }
01708 }
01709 }
01710
01711
01712
01713
01714 if (voice->mod_count < FLUID_NUM_MOD) {
01715 fluid_mod_clone(&voice->mod[voice->mod_count++], mod);
01716 }
01717 }
01718
01719 unsigned int fluid_voice_get_id(fluid_voice_t* voice)
01720 {
01721 return voice->id;
01722 }
01723
01724 int fluid_voice_is_playing(fluid_voice_t* voice)
01725 {
01726 return _PLAYING(voice);
01727 }
01728
01729
01730
01731
01732
01733
01734
01735
01736
01737
01738
01739
01740
01741 fluid_real_t fluid_voice_get_lower_boundary_for_attenuation(fluid_voice_t* voice)
01742 {
01743 int i;
01744 fluid_mod_t* mod;
01745 fluid_real_t possible_att_reduction_cB=0;
01746 fluid_real_t lower_bound;
01747
01748 for (i = 0; i < voice->mod_count; i++) {
01749 mod = &voice->mod[i];
01750
01751
01752 if ((mod->dest == GEN_ATTENUATION)
01753 && ((mod->flags1 & FLUID_MOD_CC) || (mod->flags2 & FLUID_MOD_CC))) {
01754
01755 fluid_real_t current_val = fluid_mod_get_value(mod, voice->channel, voice);
01756 fluid_real_t v = fabs(mod->amount);
01757
01758 if ((mod->src1 == FLUID_MOD_PITCHWHEEL)
01759 || (mod->flags1 & FLUID_MOD_BIPOLAR)
01760 || (mod->flags2 & FLUID_MOD_BIPOLAR)
01761 || (mod->amount < 0)) {
01762
01763 v *= -1.0;
01764 } else {
01765
01766 v = 0;
01767 }
01768
01769
01770
01771
01772
01773
01774 if (current_val > v){
01775 possible_att_reduction_cB += (current_val - v);
01776 }
01777 }
01778 }
01779
01780 lower_bound = voice->attenuation-possible_att_reduction_cB;
01781
01782
01783 if (lower_bound < 0) {
01784 lower_bound = 0;
01785 }
01786 return lower_bound;
01787 }
01788
01789
01790
01791
01792
01793
01794
01795 void fluid_voice_check_sample_sanity(fluid_voice_t* voice)
01796 {
01797 int min_index_nonloop=(int) voice->sample->start;
01798 int max_index_nonloop=(int) voice->sample->end;
01799
01800
01801 int min_index_loop=(int) voice->sample->start + FLUID_MIN_LOOP_PAD;
01802 int max_index_loop=(int) voice->sample->end - FLUID_MIN_LOOP_PAD + 1;
01803 fluid_check_fpe("voice_check_sample_sanity start");
01804
01805 if (!voice->check_sample_sanity_flag){
01806 return;
01807 }
01808
01809 #if 0
01810 printf("Sample from %i to %i\n",voice->sample->start, voice->sample->end);
01811 printf("Sample loop from %i %i\n",voice->sample->loopstart, voice->sample->loopend);
01812 printf("Playback from %i to %i\n", voice->start, voice->end);
01813 printf("Playback loop from %i to %i\n",voice->loopstart, voice->loopend);
01814 #endif
01815
01816
01817 if (voice->start < min_index_nonloop){
01818 voice->start = min_index_nonloop;
01819 } else if (voice->start > max_index_nonloop){
01820 voice->start = max_index_nonloop;
01821 }
01822
01823
01824 if (voice->end < min_index_nonloop){
01825 voice->end = min_index_nonloop;
01826 } else if (voice->end > max_index_nonloop){
01827 voice->end = max_index_nonloop;
01828 }
01829
01830
01831 if (voice->start > voice->end){
01832 int temp = voice->start;
01833 voice->start = voice->end;
01834 voice->end = temp;
01835
01836 }
01837
01838
01839 if (voice->start == voice->end){
01840 fluid_voice_off(voice);
01841 return;
01842 }
01843
01844 if ((_SAMPLEMODE(voice) == FLUID_LOOP_UNTIL_RELEASE)
01845 || (_SAMPLEMODE(voice) == FLUID_LOOP_DURING_RELEASE)) {
01846
01847 if (voice->loopstart < min_index_loop){
01848 voice->loopstart = min_index_loop;
01849 } else if (voice->loopstart > max_index_loop){
01850 voice->loopstart = max_index_loop;
01851 }
01852
01853
01854 if (voice->loopend < min_index_loop){
01855 voice->loopend = min_index_loop;
01856 } else if (voice->loopend > max_index_loop){
01857 voice->loopend = max_index_loop;
01858 }
01859
01860
01861 if (voice->loopstart > voice->loopend){
01862 int temp = voice->loopstart;
01863 voice->loopstart = voice->loopend;
01864 voice->loopend = temp;
01865
01866 }
01867
01868
01869 if (voice->loopend < voice->loopstart + FLUID_MIN_LOOP_SIZE){
01870 voice->gen[GEN_SAMPLEMODE].val = FLUID_UNLOOPED;
01871 }
01872
01873
01874
01875 if ((int)voice->loopstart >= (int)voice->sample->loopstart
01876 && (int)voice->loopend <= (int)voice->sample->loopend){
01877
01878 if (voice->sample->amplitude_that_reaches_noise_floor_is_valid){
01879 voice->amplitude_that_reaches_noise_floor_loop=voice->sample->amplitude_that_reaches_noise_floor / voice->synth_gain;
01880 } else {
01881
01882 voice->amplitude_that_reaches_noise_floor_loop=voice->amplitude_that_reaches_noise_floor_nonloop;
01883 };
01884 };
01885
01886 }
01887
01888
01889 if (voice->check_sample_sanity_flag & FLUID_SAMPLESANITY_STARTUP){
01890 if (max_index_loop - min_index_loop < FLUID_MIN_LOOP_SIZE){
01891 if ((_SAMPLEMODE(voice) == FLUID_LOOP_UNTIL_RELEASE)
01892 || (_SAMPLEMODE(voice) == FLUID_LOOP_DURING_RELEASE)){
01893 voice->gen[GEN_SAMPLEMODE].val = FLUID_UNLOOPED;
01894 }
01895 }
01896
01897
01898
01899 fluid_phase_set_int(voice->phase, voice->start);
01900 }
01901
01902
01903
01904 if (((_SAMPLEMODE(voice) == FLUID_LOOP_UNTIL_RELEASE) && (voice->volenv_section < FLUID_VOICE_ENVRELEASE))
01905 || (_SAMPLEMODE(voice) == FLUID_LOOP_DURING_RELEASE)) {
01906
01907
01908
01909
01910
01911
01912
01913
01914
01915
01916
01917 int index_in_sample = fluid_phase_index(voice->phase);
01918 if (index_in_sample >= voice->loopend){
01919
01920 fluid_phase_set_int(voice->phase, voice->loopstart);
01921 }
01922 }
01923
01924
01925
01926
01927 voice->check_sample_sanity_flag=0;
01928 #if 0
01929 printf("Sane? playback loop from %i to %i\n", voice->loopstart, voice->loopend);
01930 #endif
01931 fluid_check_fpe("voice_check_sample_sanity");
01932 }
01933
01934
01935 int fluid_voice_set_param(fluid_voice_t* voice, int gen, fluid_real_t nrpn_value, int abs)
01936 {
01937 voice->gen[gen].nrpn = nrpn_value;
01938 voice->gen[gen].flags = (abs)? GEN_ABS_NRPN : GEN_SET;
01939 fluid_voice_update_param(voice, gen);
01940 return FLUID_OK;
01941 }
01942
01943 int fluid_voice_set_gain(fluid_voice_t* voice, fluid_real_t gain)
01944 {
01945
01946 if (gain < 0.0000001){
01947 gain = 0.0000001;
01948 }
01949
01950 voice->synth_gain = gain;
01951 voice->amp_left = fluid_pan(voice->pan, 1) * gain / 32768.0f;
01952 voice->amp_right = fluid_pan(voice->pan, 0) * gain / 32768.0f;
01953 voice->amp_reverb = voice->reverb_send * gain / 32768.0f;
01954 voice->amp_chorus = voice->chorus_send * gain / 32768.0f;
01955
01956 return FLUID_OK;
01957 }
01958
01959
01960
01961
01962
01963
01964 int fluid_voice_optimize_sample(fluid_sample_t* s)
01965 {
01966 signed short peak_max = 0;
01967 signed short peak_min = 0;
01968 signed short peak;
01969 fluid_real_t normalized_amplitude_during_loop;
01970 double result;
01971 int i;
01972
01973
01974 if (!s->valid) return (FLUID_OK);
01975
01976 if (!s->amplitude_that_reaches_noise_floor_is_valid){
01977
01978 for (i = (int)s->loopstart; i < (int) s->loopend; i ++){
01979 signed short val = s->data[i];
01980 if (val > peak_max) {
01981 peak_max = val;
01982 } else if (val < peak_min) {
01983 peak_min = val;
01984 }
01985 }
01986
01987
01988 if (peak_max >- peak_min){
01989 peak = peak_max;
01990 } else {
01991 peak =- peak_min;
01992 };
01993 if (peak == 0){
01994
01995 peak = 1;
01996 };
01997
01998
01999
02000
02001
02002
02003
02004
02005
02006 normalized_amplitude_during_loop = ((fluid_real_t)peak)/32768.;
02007 result = FLUID_NOISE_FLOOR / normalized_amplitude_during_loop;
02008
02009
02010 s->amplitude_that_reaches_noise_floor = (double)result;
02011 s->amplitude_that_reaches_noise_floor_is_valid = 1;
02012 #if 0
02013 printf("Sample peak detection: factor %f\n", (double)result);
02014 #endif
02015 };
02016 return FLUID_OK;
02017 }