x


Rewind button works but not fast forward button?

Hello, everyone. I'm doing a project for school on the connection of music to the brian, and I'm using Unity because of it's gui system. I need to be able to fast forward and rewind audio clips, and I'm doing so with audio.pitch. However, the fast forward button has no effect, but the rewind button works fine. All help would be appreciated!!! :)

Here is my code:

@script RequireComponent(AudioSource)

private var INFOscrollVector : Vector2 = Vector2.zero;
private var AUDIOscrollVector : Vector2 = Vector2.zero;

var info : String;
var songs : AudioClip[];
var clipButtonSize : float;
var PlayIcon : Texture2D;
var PauseIcon : Texture2D;
var ffIcon : Texture2D;
var rwIcon : Texture2D;
var showAudioInterface : boolean = true;
private var aClip : AudioClip;
private var p : float;

function Awake()
{
    p = audio.pitch;
}
function OnGUI() 
{
    AudioInterface();
}
function Update()
{
    audio.clip = aClip;
}
function AudioInterface()
{
    //Background Boxes
    GUI.Box(Rect(10, 10, Screen.width/2-5, Screen.height - 60), "");
    GUI.Box(Rect(Screen.width/2+5, 10, Screen.width/2-10, Screen.height - 60), "");
    GUI.Box(Rect(10, Screen.height-50, Screen.width - 15, 40), "");
    //The Play/Pause and other options
    GUI.BeginGroup(Rect(10, Screen.height-50, Screen.width - 15, 40));
    if(GUI.Button(Rect(0, 0, 40, 40), PlayIcon))
    {
        audio.Play();
    }
    if(GUI.Button(Rect(40, 0, 40, 40), PauseIcon))
    {
        audio.Pause();
    }
    if(GUI.RepeatButton(Rect(80, 0, 40, 40), ffIcon))
    {
        audio.pitch = p * 2;
    } else
    {
        audio.pitch = p;
    }
    if(GUI.RepeatButton(Rect(120, 0, 40, 40), rwIcon))
    {
        audio.pitch = p/2;
    } else
    {
        audio.pitch = p;
    }
    GUI.EndGroup();

    //The Info Scroll View
    INFOscrollVector = GUI.BeginScrollView(Rect(10, 10, Screen.width/2-5, Screen.height - 60), INFOscrollVector, Rect(10, 10, Screen.width/2-25, 10000));

        GUI.Label(Rect(10, 10, Screen.width - 20, 10000), info);

    GUI.EndScrollView();

    //The Audio Scroll View
    AUDIOscrollVector = GUI.BeginScrollView(Rect(Screen.width/2+5, 10, Screen.width/2-10, Screen.height - 60), AUDIOscrollVector, Rect(10, 10, Screen.width/2-30, clipButtonSize*songs.length));
    for(i = 0; i < songs.length; i++)
    {
        if(GUI.Button(Rect(10, 10+(i*clipButtonSize), Screen.width/2-30, clipButtonSize), songs[i].name))
        {
            aClip = songs[i];
        }
    }

    GUI.EndScrollView();
}
more ▼

asked Apr 28 '11 at 08:10 PM

zmar0519 gravatar image

zmar0519
946 59 66 78

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

I don't have any experience with playing around with pitch, but I think your problem is here.

if(GUI.RepeatButton(Rect(80, 0, 40, 40), ffIcon))
    {
        audio.pitch = p * 2;
    } else
    {
        audio.pitch = p;
    }
    if(GUI.RepeatButton(Rect(120, 0, 40, 40), rwIcon))
    {
        audio.pitch = p/2;
    } else
    {
        audio.pitch = p;
    }

After you set the pitch faster for the ff button you then check the rewind button. However, since you aren't pressing the rewind button, it sets the pitch back to 'p.'

Something like this would be better.

audio.pitch = p;
if(GUI.RepeatButton(Rect(80, 0, 40, 40), ffIcon))
    {
        audio.pitch = p * 2;
    }
    if(GUI.RepeatButton(Rect(120, 0, 40, 40), rwIcon))
    {
        audio.pitch = p/2;
    }

NOTE: in my preview window it does not look like the code tags are going to work for this post: not sure why. If it does come out ugly, I will come back to it and edit it with a different browser later.

more ▼

answered Apr 29 '11 at 02:06 AM

Richard J. Hansen gravatar image

Richard J. Hansen
171 4 4 12

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3669
x1024
x43
x20
x5

asked: Apr 28 '11 at 08:10 PM

Seen: 761 times

Last Updated: Apr 28 '11 at 08:10 PM