swesterfeld / liquidsfz

SFZ Sampler
Mozilla Public License 2.0
79 stars 12 forks source link

Multiple instruments #8

Closed iurienistor closed 4 years ago

iurienistor commented 4 years ago

Hi! I am thinking to develop a plugin based on liquidsfz. I am looking how I can change the instrument without loading every time from the file. Probably the solution would be to create several api instances. When the api instance is created in terms of resources are there new threads created or it makes use only of the user thread?

iurienistor commented 4 years ago

It seams that, yes, only the user thread is used.

swesterfeld commented 4 years ago

Probably the solution would be to create several api instances.

That sounds like a good way to do it. Synth instances are really tied to managing one sfz. One thing that you might need is a reset() function. Why? Suppose instrument 1 is trumpet and you are playing some notes. While they are playing you switch to instrument 2 (drums). Now if you switch back, the trumpet notes you have been playing are back. Feel free to either create a PR or issue for this if you need it.

It seams that, yes, only the user thread is used.

Correct. Currently liquidsfz never creates threads. It might be necessary in some future version where samples were loaded a background loading thread, but as of today I cannot predict how this would be mapped in the API.

iurienistor commented 4 years ago

I have tested to load files and the speed in enough to create the illusion of changing the instrument. Thus, a single instance for now will work even if loading is not very efficient, but as memory consumption is even better.

When changing the file during playing in order to avoid sharp cut of the sound (barely noticeable) there is a need probably to stop accepting key events, set decay for all (or maybe just key OFF for all keys if decay is short for this case), continue to process samples until to decay, then schedule loading next file. For LV2 this can be done only in LV2 run function probably.

swesterfeld commented 4 years ago

Yes this should work. You could simply fade out the whole output of your Synth instance over 50ms or so, then switch. It doesn't really matter if notes are playing as load() will reset all voices anyway.