Jules can you confirm this is a bug on the JUCE side?
And about the other question (how to use readers): I have two possible ways of implementation:
1. Create a reader (as ScopedPointer member variable) when the sample is loaded (the reader is needed to prebuffer the sample and waits until the background thread requests more data). This is the current implementation and it means that the reader will keep the file handle open until the sample is unloaded.
2. Create a reader when the sample is loaded (as local variable), fill the prebuffer and delete it. If the sample is started, create a new reader and start fetching data in the background thread (and keep the reader alive until the sample is stopped so it doesn't have to create a new object every 8192 samples). There is enough time to create the reader because we have the prebuffer but it imposes a little bit of overhead at the sample start time (for the background thread, not the audio thread). On the other hand it reduces the open file handles to the smallest possible amount. So after all this is a tradeoff between having open file handles and less overhead at voice starts and this is where I would love to hear some pros / cons.
BTW Reaper also uses open file handles for their sample management.
Wouldn't call this a "bug", it's just how unix filesystems work..
However looking at otristan's code for setting the rlimit, it does look like a better plan than our current "try setting it to infinity but don't worry if it doesn't succeed" approach. I'll try out something like that.
The number that you need to keep open depends on many things... Certainly for short samples you'd be better off just loading them all into memory, and only keep really long ones as memory-maps.
You cannot open that many files on OSX AFAIK even when raising the limit.
There is the same kind of issue on Windows
from stackoverflow
Win32 API calls (CreateFile(), WriteFile(), and so forth) and the default maximum number of open files has been increased to 16384. The maximum can be increased further by using the --max-open-files=N option at server startup.
I am not sure if I do not undestand the purpose of this fix or it does not work as intended here.
As far as I understand, MaxNumFileHandlesInitialiser() should increase the maximum number of files to 8192 (or something below if it is not possible to set it to that value).
Here the initial call to getrlimit(RLIMIT_NOFILE, &lim) will always return the system default value lim.rlim_cur=256. So the first call setMaxNumberOfFileHandles(0) will just return true, because 256>0.
So the limit will remain on 256 and is never increased.