schellingb / TinySoundFont

SoundFont2 synthesizer library in a single C/C++ file
MIT License
623 stars 72 forks source link

voices active for slightly longer than they should be #73

Open magcius opened 2 years ago

magcius commented 2 years ago

https://github.com/schellingb/TinySoundFont/blob/e763bd93badbd390945a3d0ee25d75e54e4fb8eb/tsf.h#L970-L971

This "mystery slope" has the effect of scaling the slope by a factor of 9.226f, which means that it would hit the X axis sooner than it otherwise would. Why this specific constant was chosen, I'm not too sure, I suspect this is effectively replicating a bug in the original SF2 synths and measured by ear. Effectively, the specified release time duration is scaled down by 9.226f.

This same effect can also be calculated a different way, one which will allow the voice to cut off sooner:

e->samplesUntilNextSegment /= 9.226f;
e->slope = TSF_EXPF(1.0f / e->samplesUntilNextSegment);

This will allow the voice to end a bit earlier, meaning less to render. Whether you can do the same for decay, I'm not sure.