Skip to content

Commit ede06e3

Browse files
hpfmnlasconic
authored andcommitted
fix #119446: calculate envelope for first point of new envelope right, fix divison by zero in amp increment calculation
1 parent 34a8eaf commit ede06e3

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

fluid/dsp.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ namespace FluidS {
4646
* - dsp_buf: Output buffer of floating point values (FLUID_BUFSIZE in length)
4747
*/
4848

49-
void updateAmpInc(unsigned int &nextNewAmpInc,std::map<int, struct VolEnvValSection>::iterator &curSample2AmpInc, qreal &dsp_amp_incr, unsigned int dsp_i)
49+
inline void updateAmpInc(unsigned int &nextNewAmpInc,std::map<int, qreal>::iterator &curSample2AmpInc, qreal &dsp_amp_incr, unsigned int dsp_i)
5050
{
5151
if (dsp_i >= nextNewAmpInc) {
5252
curSample2AmpInc++;
5353
nextNewAmpInc = curSample2AmpInc->first;
54-
dsp_amp_incr = curSample2AmpInc->second.val;
54+
dsp_amp_incr = curSample2AmpInc->second;
5555
}
5656
}
5757

@@ -133,7 +133,7 @@ int Voice::dsp_float_interpolate_none(unsigned n)
133133
float *dsp_buf = voice->dsp_buf;
134134
float dsp_amp = voice->amp;
135135
auto curSample2AmpInc = Sample2AmpInc.begin();
136-
qreal dsp_amp_incr = curSample2AmpInc->second.val;
136+
qreal dsp_amp_incr = curSample2AmpInc->second;
137137
unsigned int nextNewAmpInc = curSample2AmpInc->first;
138138
unsigned int dsp_i = 0;
139139
unsigned int dsp_phase_index;
@@ -200,7 +200,7 @@ int Voice::dsp_float_interpolate_linear(unsigned n)
200200
float *dsp_buf = voice->dsp_buf;
201201
float dsp_amp = voice->amp;
202202
auto curSample2AmpInc = Sample2AmpInc.begin();
203-
qreal dsp_amp_incr = curSample2AmpInc->second.val;
203+
qreal dsp_amp_incr = curSample2AmpInc->second;
204204
unsigned int nextNewAmpInc = curSample2AmpInc->first;
205205
unsigned int dsp_i = 0;
206206
unsigned int dsp_phase_index;
@@ -293,7 +293,7 @@ int Voice::dsp_float_interpolate_4th_order(unsigned n)
293293
Phase dsp_phase_incr; // end_phase;
294294
short int* dsp_data = sample->data;
295295
auto curSample2AmpInc = Sample2AmpInc.begin();
296-
qreal dsp_amp_incr = curSample2AmpInc->second.val;
296+
qreal dsp_amp_incr = curSample2AmpInc->second;
297297
unsigned int nextNewAmpInc = curSample2AmpInc->first;
298298
unsigned int dsp_i = 0;
299299
unsigned int dsp_phase_index;
@@ -439,7 +439,7 @@ int Voice::dsp_float_interpolate_7th_order(unsigned n)
439439
float *dsp_buf = voice->dsp_buf;
440440
float dsp_amp = voice->amp;
441441
auto curSample2AmpInc = Sample2AmpInc.begin();
442-
qreal dsp_amp_incr = curSample2AmpInc->second.val;
442+
qreal dsp_amp_incr = curSample2AmpInc->second;
443443
unsigned int nextNewAmpInc = curSample2AmpInc->first;
444444
unsigned int dsp_i = 0;
445445
unsigned int dsp_phase_index;

fluid/voice.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ float Voice::gen_get(int g)
220220
return gen[g].val;
221221
}
222222

223-
void Voice::calcVolEnv(int n, fluid_env_data_t *env_data)
223+
inline void Voice::calcVolEnv(int n, fluid_env_data_t *env_data)
224224
{
225225
float x;
226226
/* calculate the envelope value and check for valid range */
@@ -273,7 +273,7 @@ void Voice::write(unsigned n, float* out, float* reverb, float* chorus)
273273

274274
int restN = n;
275275

276-
if (volenv_section == FLUID_VOICE_ENVFINISHED) {
276+
if (volenv_section >= FLUID_VOICE_ENVFINISHED) {
277277
off();
278278
return;
279279
}
@@ -294,7 +294,7 @@ void Voice::write(unsigned n, float* out, float* reverb, float* chorus)
294294
}
295295

296296
sample2VolEnvSection.insert(std::pair<int, int>(n, volenv_section));
297-
volumeChanges.insert(n-1);
297+
volumeChanges.insert(n);
298298

299299
fluid_check_fpe ("voice_write vol env");
300300

@@ -388,7 +388,20 @@ void Voice::write(unsigned n, float* out, float* reverb, float* chorus)
388388
else
389389
modlfo_val = 0;
390390

391-
if (curPos >= curVolEnvSection->first)
391+
// never calculate anything for the very first sample
392+
// everything should have been calculated in the last
393+
// cycle - it would also cause a divion by zero later
394+
if (curPos == 0) {
395+
curPos = 1;
396+
397+
// if we should calulate for position 1 already make sure we don't do it twice
398+
// could lead to curPos==lastPos which causes devision by zero
399+
if (volumeChanges.find(1) != volumeChanges.end())
400+
volumeChanges.erase(volumeChanges.find(1));
401+
}
402+
403+
// just go to the next volume section if we're below last volume point
404+
if (curPos >= curVolEnvSection->first && (unsigned int) curVolEnvSection->first < n)
392405
curVolEnvSection++;
393406

394407
volenv_count += curPos-lastPos;
@@ -462,7 +475,7 @@ void Voice::write(unsigned n, float* out, float* reverb, float* chorus)
462475
/* Volume increment to go from voice->amp to target_amp in FLUID_BUFSIZE steps */
463476
amp_incr = (target_amp - oldTargetAmp) / (curPos - lastPos);
464477
lastPos = curPos;
465-
Sample2AmpInc.insert(std::pair<int, struct VolEnvValSection>(curPos, {amp_incr, volenv_section}));
478+
Sample2AmpInc.insert(std::pair<int, qreal>(curPos, amp_incr));
466479
oldTargetAmp = target_amp;
467480
}
468481

fluid/voice.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,6 @@ enum fluid_voice_envelope_index_t {
5757
FLUID_VOICE_ENVLAST
5858
};
5959

60-
struct VolEnvValSection {
61-
qreal val;
62-
int volenv_section;
63-
};
64-
6560
//---------------------------------------------------------
6661
// Voice
6762
//---------------------------------------------------------
@@ -121,7 +116,7 @@ class Voice
121116
fluid_env_data_t volenv_data[FLUID_VOICE_ENVLAST];
122117
unsigned int volenv_count;
123118
int volenv_section;
124-
std::map<int, struct VolEnvValSection> Sample2AmpInc;
119+
std::map<int, qreal> Sample2AmpInc;
125120
float volenv_val;
126121
float amplitude_that_reaches_noise_floor_nonloop;
127122
float amplitude_that_reaches_noise_floor_loop;

0 commit comments

Comments
 (0)