- Home /
Play audio in reverse (needed to "scratch")
Hi all
I'm trying to play an .mp3 backwards (not just sent back audio back and play it straight).
What I'm currently trying to do is:
First I start playing the audioSource. Then I set the audioSource.pitch to >0 and it speeds up. Then I set the audioSource.pitch to <=0 and nothing happens ever.
Reading on forum its a know-never solved issue.
A solution could be having 2 samples of the same audio source playing at the same time. When a scratching back is needed the first sample volume turns to 0 and the second one (after some time calculations) get to 1.
Any ideas?Plugins that do this?
I want to realize a "scratching" effect.
I'm not sure what you're asking...but I'm pretty sure adjusting the pitch will work just fine
that would definitely not achieve the desired result.
you would need to spend a lot of time building your samples. one way possibly of doing it (see my other comment for negatives) is to record the "songs" one sample at a time, then you can reverse each sample in time and have it play in order, this would require a LOT of work, so you would basically build the track up out of many samples and have them all running in sequence (then when you reverse, its the correct sample that is being reversed), I think that a rewrite of the audio system would be required to realize this, its a cool idea though:)
Answer by gregzo · Aug 02, 2013 at 08:16 PM
Hi 1911_Dev,
No problem changing playback speed as the clip is playing. Just make sure your file is in an uncompressed format ( wav ), and that in import settings it's set to load into memory.
You can then tweak playback speed as if you were scratching : invert, slow down, speed up, all in real time. If the clip is set to loop, it will loop both ways.
The question is how reactive is 60 fps, and how close to real scratching can you get this way? I'd say pretty close.
Jamora's suggestion is, sorry Jamora, inappopriate : inverting an array is pretty easy, resampling it to alter playback speed is not. And FMOD ( Unity's audio backend ) does it for you already, via precisely the pitch field.
Anyways, you're on the right track, what you need is a proper controller for this pitch parameter, and an uncompressed, load into memory, looped sample.
Thank you,works perfectly with .waw files.But idk if converting the .mp3s to waw or using 2 .mp3 tracks.
Answer by Jamora · Aug 02, 2013 at 06:30 PM
Scratching is like playing audio backwards right? I think this would be best done with two audioclips, one that has the audio normally, the other one has it backwards.
AudioClip has Get/SetData that you can use to reverse the audio; first get the data, then invert the array and Set it to a new audioclip. When you need to scratch, you need to calculate the new playing position for the other clip ( clip.length-current Position) and start playing from the other clip at that position.
I suppose this can be done with just one audioclip, but is more computationally expensive, as you'd need to invert the audioclip twice every time you want to scratch.
this might sound sucky. theres more involved than just playing it backwards. the main problem you will get is that if you merely reverse a clip, you will end up with a time mismatch.
say you have a track that is 1 $$anonymous$$ute in length, you reverse it.
then you play 15 seconds in and decide that this is time to "scratch", so you flip it to the reversed sample. the problem is that you are now playing 45 seconds in on the reversed track. this will definitely require a more elaborate solution imo.
Or.. use a free audio editing program to edit the song to include the scratching. There are some that will let you reverse a clip.
Answer by fafase · Aug 02, 2013 at 07:38 PM
Here is what I have so far kinda working:
float val = 1;
float Val{
get{
return val;
}
set{
val = value;
audio.pitch = val;
}
}
void Start(){
audio.loop =true;
audio.Play();
}
void Update(){
if(Input.GetMouseButtonUp(0))
Val = 1;
}
void OnGUI(){
Val = GUI.HorizontalSlider(new Rect (100,100,100,100),Val,-1,2);
}
It is not perfect but could be a base for you. Note that using an mp3 would not reverse it but using a wav worked fine.
Your answer
Follow this Question
Related Questions
Function Update play sound once 1 Answer
Audio Distortion effects? 0 Answers
Add Audio Source to a Prefab 2 Answers
2nd Audio Clip Not Playing 0 Answers
Need help with audio files causing unity to freeze 0 Answers