schellingb / TinySoundFont

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

Dynamic Voice Allocation and Immediate Cutoff Mechanism Proposal for Memory Optimization in SoundFont Library #83

Closed kazukazu123123 closed 3 months ago

kazukazu123123 commented 8 months ago

In the current implementation, setting very large values such as tsf_set_max_voice(5000) or tsf_set_max_voices(10000) results in a significant increase in RAM usage and a slowdown in rendering time, possibly due to voices being pre-allocated. This becomes a particularly problematic issue in high-load scenarios, such as rendering/playing MIDI files with a large number of events, like those seen in Black MIDI, where frequent note_on/note_off calls occur. To address this issue, this proposal suggests implementing a system that dynamically allocates voices as needed and releases them when they are no longer necessary. This approach can optimize memory usage efficiency.

Furthermore, it is recommended to consider a mechanism that immediately stops the playback of the oldest voice when the note_on function exceeds the limit of max_voice. This prevents the problem of new sounds not starting when the simultaneous voice count exceeds max_voice. (Apologies if this is already implemented or if there are other existing methods).

I truly appreciate this excellent library, which allows easy rendering of audio to the buffer and can be handled with a simple API such as tsf_note_on() and tsf_note_off. If possible, please consider this proposal.

I apologize for any shortcomings in the structure of the text. I am Japanese and not very fluent in English.

ghost commented 6 months ago

Sounds nice. To me the only "problem" with libtsf is indeed the optimization, otherwise as a barebones synth its actually quite nice. Maybe vectorization of the mixer/synthesizer with SSE/AVX/NEON may also help?