How can I play audio reverse? I searched in the forums and the documentation but couldn’t find an explanation. There are only a few topics about this in the forums with no real solutions, so maybe I missed something because this is a common task if you do audio stuff?
Thanks in advance
It’s actually surprisingly hard!
If you’re just loading a sample and reversing it, that’s easy, but to stream backwards is a real PITA. None of my streaming code would handle it, because it all expects to read ahead in a forwards direction. So you’d need to write some custom code to do it.
Thanks for your answer jules!
I had a few ideas, but I don’t know if any of them would work:
Buffer both directions in a custom BufferingAudioSource. Simply reverse the previous samples. Add a switch to change direction. So all the other AudioSources would still work??
As a variation of the previous idea: create two BufferingAudioSources, one custom BufferingAudioSource which buffers reverse and one which buffers forward. Then switch sources if needed. I’m really unsure if this would work.
Load the whole file in memory compressed. Add a switch to change direction. Decompress the samples when needed and reverse them if needed. Would this work and be fast enough??
Comments are welcome!
The only easy way is to load the whole thing, reverse it and play it. Writing a buffering source that runs both ways is really fiddley! And the worst of all is when you get compressed formats, e.g. OGG, which are designed to only read forwards - if every read involves seeking in the whole file, your cpu and disk use will go nuts!
In my application, I read blocks of 64k-samples and reverse them. It works flawlessly for MP3 and OGG. The only important thing is to have a Reader that is really sample-accurate.
Reading the entrie file first and then playing in reverse seems like overkill - it would not be a sustainable method for playing in reverse DJ-mixes of hours and hours.