x


Changing Audio Pitch Dynamically

Hi guys,

I want to have an object that can be manipulated to change the pitch of a sound that is playing as it plays, and stay that way until the object is Transformed again.

I really just need to know if I'm generally on the right track with my logic here, and if my planned scripts and objects they will be attached to is correct, or if I need to learn other principles. I'd like to avoid getting into bad habits early on!

I'm still learning the different sorts of variables and functions in the scripting etc, but what I was thinking:

I think I'd need three types of objects with their own script each, to achieve what I want...? 1. When clicked, plays specified sound at specified pitch 2. Selects the sound 3. Sets the pitch

Really rough pseudo-scripting (if it can even be called that!) below:

Object 1. When clicked, plays sound at pitch

   play{ 
   the sound declared by Object 2 at the pitch declared in Object 3
   }

I'm guessing this object would also need an Update function to pull the variables from the other objects on every frame so it always knows what the pitch should be?

Object 2. When clicked, selects which sound to play I'm not sure on the types of variables to use here..

    var object
    var sound

if gameObject(1){
   the sound gets changed to 1
else if gameObject(2)
   the sound gets changed to 2
etc, etc, etc
}

Object 3. When clicked and dragged, changes the pitch of the sound being played

   var pitch float = 1.0

    When the user clicks and drags the object {
      Transform.Rotate around single axis and set the "pitch" variable to a 
      number relative to its original rotation. So, as it Rotates one way,
      it would increment, the other way, it would decrease, and store variable
    }

Thanks guys, I'd really appreciate your feedback/advice.

(Edit: I just realised a simplification of what I want to do. You know how on some keyboards (musical) you have that little pitch bend wheel? My Object2 would be one of many keys, Object3 would be the key on the keyboard choosing your note, and imagine the sound wont play until Object1 is clicked, rather than when Object2 is.)

more ▼

asked Apr 10 '10 at 12:23 PM

MooseTrap gravatar image

MooseTrap
89 6 7 16

That is a long question I'd shorten it a bit to get sharp answers.

Apr 13 '10 at 08:14 AM Nicolaj Schweitz ♦♦

Hmm... I thought I'd get less answers if I was too vague. I don't want to be one of those "I wanna do this! Give me code to Copy/Paste!" guys.

Apr 16 '10 at 03:42 AM MooseTrap
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Start by reading this overview of how audio works in Unity.

Then, go check the docs on audio.pitch.

Basically you can use any floating point to set the pitch of an audio source, so I guess you could take a specific axis of your Object 3, and convert that axis' angle to a reasonable float that can be used with audio.pitch. E.g. using Quaternion.eulerAngles.y

Another thing you would want to take a look at first, is how you can drag the rotation of Object 3. First have the mouse's screen position converted to a 3d world coordinate (e.g. Camera.ScreenPointToRay

Then set the object's rotation based on the mouse movement while the mouse button is down. You can fin several handy scripts for this on the UnifyCommunity wiki's script sections.

Good luck.

more ▼

answered Apr 13 '10 at 08:37 AM

Nicolaj Schweitz gravatar image

Nicolaj Schweitz ♦♦
1.7k 19 26 57

Cheers! I'm away from my machine for a few days but I'll try as soon as I can. Thanks for the help! :-)

Apr 16 '10 at 03:43 AM MooseTrap
(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:

x5089
x3463
x1281
x1029
x43

asked: Apr 10 '10 at 12:23 PM

Seen: 4662 times

Last Updated: Apr 10 '10 at 12:23 PM