Threaded peak display


I’d like to have the following behaviour on my app :

when the user loads a song file, a thread is created to compute the peaks and show it to the user as fast as possible
and in the meantime, the user can start the playback, or seek inside the track

My question is : do I need to create a custom system for this or is AudioSource::getNextAudioBlock() already thread safe ?
I guess problems will appear if the two threads do setNextReadPosition() in the same time…

Or maybe I’m missing something obvious ?

No, that’s not thread-safe. Just create another audio-source in your background thread and keep them both separate. Even if it was thread-safe you’d just end up with the two threads fighting over which bit is cached, and it’d all go really slowly.

thanks for the fast answer,

here are some further details :
I want to be able to store the whole file in memory sometimes, and also compute peaks in a separate thread, here is what I intend to do :

  • write a class that inherits from AudioFormatReader, and that can store an other one in memory in a separate thread, and deal with any read() request, using memory if the chunk is already loaded or the owned AudioFormatReader otherwise.
  • create an AudioTransportSource for playback.
  • create another class that computes asynchronously the peaks using the custom AudioFormatReader.

do you think that’s a good strategy ?

by the way, I was looking at WavAudioFormat and AiffAudioFormat, and the ‘position’ variable are initialized but unused in both AiffAudioFormatReader and WavAudioFormatReader classes.
Just for my 2 cents : )

Sounds reasonable. Thanks for spotting those unused variables, too.