Determine If Audio Was Recorded Backwards

I’m using this script to take an audio clip that was recorded in Unity and then play it back, by adjusting the pitch I can play the clip backwards. This is great but I’m wondering if there is a way in unity to determine if the audio was recorded backwards to begin with?

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class RevAudioPlayback : MonoBehaviour {

    public AudioRecorder RecorderObj;
    public Slider PitchSlider;

    public bool RevAudioIsOn = false;

    float val = 1;
    float Val{
        get{
            return val;
        }
        set{
            val = value;
            GetComponent<AudioSource>().pitch = val;
        }
    }
    void Start(){
        //GetComponent<AudioSource>().loop =true;
        //GetComponent<AudioSource>().Play();
    }
    void Update(){
        //if(Input.GetMouseButtonUp(0))
        //    Val = 1;
    }
    /*void OnGUI(){
        Val = GUI.HorizontalSlider(new Rect (100,40,200,200),Val,-1,2);
    }*/


    public void MoveSlider (float val){
        PitchSlider.value = val;
    }


    public void RevAudio(){
        RevAudioIsOn = ! RevAudioIsOn;
        if (RevAudioIsOn == false) {
            Val = 1;
            GetComponent<AudioSource> ().pitch = val;
        } else {
            RevAudioIsOn = true;
            Val = -1;
            GetComponent<AudioSource> ().pitch = val;
        }
    }

    public void LoopAudio(){
        RecorderObj.AudioLoop = !RecorderObj.AudioLoop;
            if (RecorderObj.AudioLoop == false){
            RecorderObj.AudioLoop = false;
            GetComponent<AudioSource>().loop =false;
            }else{
            GetComponent<AudioSource>().loop =true;
            RecorderObj.AudioLoop = true;
            }
    }
}

now the reason I would like to see if this could be done, is that in our app I would like to be able to ask KITT if the audio was "In Code?"I think it would be pretty awesome if there was a way in unity to determine if the Audio was recorded backwards so he could respond by stating that it was indeed recorded backwards and then play the audio in reverse like I demonstrate in my demo video above… I’m not even sure if it’s possible for Unity to be able to make that kind of a distinction with Audio???

To record audio backwards you will need a time machine :wink: The reason it plays backwards when the frequency is set to -1 is because the sampling increment is negative, which in turns means the buffer will be played in reverse.

But how can we record something in reverse? Do you mean start at the end of the record buffer and move the write pointer backwards? This might be possible if we know the length of the audio to begin with. But do we? And if so, it seems like a large limitation to impose. I really can’t think why this might be a good idea.

No recording backwards was not what I was interested in. For that I can just reverse the audio using Audacity.
I was wondering if there was a way somehow in unity to determine IF and audio clip was recorded backwards. But apparently this is something that is impossible for unity to do.
I thought maybe by somehow examining the RMS or Spectrum data it could be determined but nope :frowning:

How can an audio clip ever be recorded backwards?

Perhaps I’m wording it wrong, that’s semantics for ya :wink:
What I mean is an audio clip that was reversed so that when played the voice is backwards :wink:

Ah, Ok, so what you want to do is find out if it an audio clip is currently playing backwards? Ok, makes sense now. I think this would be pretty hard to do with most dedicated audio toolkits, let alone Unity. I guess you could extract the amplitude envelopes of each frequency bin and check their shape. The idea being that audio played in reverse would lack any hard attacks. But it all depends on the audio that is being played.

1 Like

Yes that’s sort of the right idea… but the audio would not be playing in reverse in Unity as it it would not be set at a pitch setting of -1
but that the audio clip would have already been a clip that was reversed in an application like Audacity.
Would that type of an audio clip be somehow possible to analyze in a method like the one you described?

I don’t really know to be honest, maybe, but it would be a whole lot of work. Do you provide the clips yourself? If so you could just save it with a name that indicates it has been reversed?

1 Like

Thanks @rorywalsh , well currently I have a default clip I use in the audio source clip slot, this clip usually gets replaced with a recorded clip from the microphone when I ask KITT to:
“Take a recording of this pal.”
The audio clip shows up in the Audio source slot as “microphone”
I can play that clip back, like you see in my demo video a few posts back.
It’s a bit odd though because I can voice command the default audio clip to play in reverse but the one from the microphone that was recorded only plays back one time, even when I have the “Loop Audio” toggled ON.
But to directly answer your question, the ideal scenario would be to have an audio clip recorded, Then somehow have KITT determine if the audio was actually recorded backwards.
(Right now I’m making a human judgment call on that, so I just have myself say:
“Hey KITT”
He’ll respond with:
“What Michael”
And then I can say:
“I think that was recorded backwards pal”
Which starts a coroutine that gives a voice response of either:
“Lets Find Out” Or “Not a very sophisticated code Michael”
There is a short pause and then he says:
“Yes, and when I reverse it…”
He then plays back the audio file backwards revealing the true nature of the audio’s hidden message.)

But like I say it works awesome on the default audio clip I just have dragged into the inspector but the recorded clip from the microphone just plays back the one time. Using the V.R. and the Coroutine, I can play around with the actually screen buttons and then get it to play backwards, but not as a loop?
I’m wondering if maybe the microphone recording needs to be saved to disk and then reopened? My thought is that might solve that one minor issue.

But yeah as for the actually being able to determine if the audio was actually reversed to begin with, I think you are right on track with somehow checking to see if it lacked any hard attacks at the beginning of each spoken word in the waveform, because most spoken words end to trail off so to speak… I’ll bet it could be done but I’m not that good of a programmer :wink: I’m a novice at best :wink: