Best practice to load samples


#1

Hi together

I currently working on a plugin that can load samples. Not very big samples, but samples :) There are two ways to load a sample:

1. The host loads the plugin and a preset.
2. The user changes the preset or loads a new sample.

What is the best way to do this in a plugin. Just load it synchronous or create a thread for this?
What thread type would you choose if a thread is the solution? 
What's the best way to notify the UI that the loading thread is finished?

Any help is welcome.


#2

If you're using file formats that support it, the smart thing to do would probably be to use memory-mapped audio file readers, and let the OS worry about loading the data.


#3

Thanks for the input. The MemoryMappedAudioFormatReader class seems to be a solution for this. I didn't know that, but i need some pre-processing and want to support for other audio formats too.

The files are not big and the loading time is short, maybe around 300 milliseconds. Is the UI thread the wrong place to load audio files if the user changes a preset?


#4

The UI is probably the wrong place to do it.  If the user doesn't open the GUI when loading a session back up into the DAW how would the sample get loaded?

I spawned a new thread from the processor for file loads ... but I hadn't spotted the MemoryMappedAudioFormatReader :)


#5

The UI is probably the wrong place to do it.  If the user doesn't open the GUI when loading a session back up into the DAW how would the sample get loaded?

I pointed to the UI thread and not the user interface itself. The use-case is following:

> The user loads a sample by clicking on a UI button.

I think the best solution would be a new thread on the top of the UI thread to load the sample to avoid a blocking GUI... what is the best way to setup such a thread and how does the thread notify the UI about the loading state? I think a normal thread can't do this out of the box.

 


#6

I’m curious which solution you end up with Patrick?


#7

I load the samples parallel in different threads now. If the host loads a project with the plugin, then i wait until all threads are finished. It’s synchronous for the host in this case. It’s a bit hacky, because i have to check all threads to be finished in a loop with some sleep time. Not sure if it is required to wait…

When the user loads a sample or preset i use a normal juce thread (not sure how the name is) with a callback when the thread is finished. Currently done with a interface for the callback. The callback sets some plugin state and calls notify program changed. You can use SuspendProcessing while loading. The UI maybe use a timer to check if something has changed. For example to show the Loading Screen…
Hope C++ will have something like tasks in the future. It’s a pain to use the threads. You can’t pass any parameters to the threads run() method.