That’s a niftey trick. Nice.
Thanks ! I was wondering about buffering and that’s what was needed. I’m not that familiar with Python which is sometimes a bit confusing but I seem to be picking it up as I go along. So are you writing that code from a Python perspective, or things unique to Audaspace ? Unless I’m missing something the documentation on Audaspace is very poor. For example I don’t think it mentions anything about HAVING to buffer for reverse() which seems to be the case.
I’ve never reversed audio with the python API, and it c omes down to a mix of experience, reading the API, and knowing how things work at a level slightly lower than blender.
First off, if you are confused about the API, I wrote a resource on it several years back. It didn’t mention the aud module, but it may help. WIth the AUD documentation, the classes (Eg handle and factory) are on the same page. What you’ll find is that all API’s seem pretty poor until you are fluent with the system being documented (in my experience).
So how did I write that code snippet. I recalled seeing that factories could be reversed, but that a handle could not be, so I decided the best approach was to have one playing forwards and another playing backwards. I then had to figure out how to reverse it. The API says:
Plays a factory reversed.
[TABLE=“class: docutils field-list”]
[TR=“class: field-odd field”]
The created Factory object.
[TR=“class: field-even field”]
[TH=“class: field-name”]Return type:[/TH]
The factory has to have a finite length and has to be seekable. It’s recommended to use this only with factories with fast and accurate seeking, which is not true for encoded audio files, such ones should be buffered using buffer() before being played reversed.
If seeking is not accurate in the underlying factory you’ll likely hear skips/jumps/cracks.
You’ll notice that it says that facroties have to be finite length and seekable, and even suggests buffering it.
So then it was simply a case of taking the factory, buffering it and then reversing it.
Now, I’ve found that unless you buffer audio, the first time you play it it will lag slightly, so I also buffered the reversed factory. Technically this shouldn’t be necessary, but for a smooth experience it probably is.
Then it was a case of looking for the seek method (handle.position, which I’ve also never used), and then playing/stopping (which I have used) it when required .
So there you go, that is how I wrote it.
Please note it takes a while to buffer the track so wait a few seconds after starting.
It may be worth creating a loading screen if this takes more than a few seconds, however this is often quite an involved process.
If you add some sort of music selection, this would also be nice. You’ve got me somewhat enthusiastic about this project, and I’ve always wanted to make a file selection screen, so I may do this this weekend…
Spining right with right mouse button just gives a “cannot set pitch” error as that part is not setup yet.
There are several issues I can see:
- You are’t using “handle.keep = true” so when the track reaches it’s end, the handle becomes invalid
- A pitch of zero is invalid (and also wouldn’t be playing anything), so maybe run a check against this and make all pitches of less than, say, 0.001 simply pause the handle.
- You’re also tarting the reverse track twice. (line 23 and line 35). The reason you start it twice is because you run handle.stop() rather than handle.pause(). handle.stop() completely invalidates the handle.
But yes, it is playing backwards.