Audio and Animation: Hand in Hand

February 2016

In this article, we are going to learn techniques to match audio pitch to animation speed. This is very crucial while editing videos and creating animated contents.

(For more resources related to this topic, see here.)

Matching the audio pitch to the animation speed

Many artifacts sound higher in pitch when accelerated and lower when slowed down. Car engines, fan coolers, Vinyl, a record player the list goes on. If you want to simulate this kind of sound effect in an animated object that can have its speed changed dynamically, follow this article.

Getting ready

For this, you'll need an animated 3D object and an audio clip. Please use the files animatedRocket.fbx and engineSound.wav, available in the 1362_09_01 folder, that you can find in code bundle of the book Unity 5.x Cookbook at https://www.packtpub.com/game-development/unity-5x-cookbook.

How to do it...

To change the pitch of an audio clip according to the speed of an animated object, please follow these steps:

  1. Import the animatedRocket.fbx file into your Project.
  2. Select the carousel.fbx file in the Project view. Then, from the Inspector view, check its Import Settings. Select Animations, then select the clip Take 001, and make sure to check the Loop Time option. Click on the Apply button, shown as follows to save the changes:

    The reason why we didn't need to check Loop Pose option is because our animation already loops in a seamless fashion. If it didn't, we could have checked that option to automatically create a seamless transition from the last to the first frame of the animation.

  3. Add the animatedRocket GameObject to the scene by dragging it from the Project view into the Hierarchy view.
  4. Import the engineSound.wav audio clip.
  5. Select the animatedRocket GameObject. Then, drag engineSound from the Project view into the Inspector view, adding it as an Audio Source for that object.
  6. In the Audio Source component of carousel, check the box for the Loop option, as shown in the following screenshot:

  7. We need to create a Controller for our object. In the Project view, click on the Create button and select Animator Controller. Name it as rocketlController.
  8. Double-click on rocketController object to open the Animator window, as shown. Then, right-click on the gridded area and select the Create State | Empty option, from the contextual menu.

  9. Name the new state spin and set Take 001 as its motion in the Motion field:

  10. From the Hierarchy view, select animatedRocket. Then, in the Animator component (in the Inspector view), set rocketController as its Controller and make sure that the Apply Root Motion option is unchecked as shown:

  11. In the Project view, create a new C# Script and rename it to ChangePitch.
  12. Open the script in your editor and replace everything with the following code:
    using UnityEngine;
    
    public class ChangePitch : MonoBehaviour{
    
      public float accel = 0.05f;
      public float minSpeed = 0.0f;
      public float maxSpeed = 2.0f;
      public float animationSoundRatio = 1.0f;
      private float speed = 0.0f;
      private Animator animator;
      private AudioSource audioSource;
    
      void Start(){
        animator = GetComponent<Animator>();
        audioSource = GetComponent<AudioSource>();
        speed = animator.speed;
        AccelRocket (0f);
      }
    
      void Update(){
        if (Input.GetKey (KeyCode.Alpha1))
        AccelRocket(accel);
    
        if (Input.GetKey (KeyCode.Alpha2))
        AccelRocket(-accel);
      }
    
      public void AccelRocket(float accel){
        speed += accel;
        speed = Mathf.Clamp(speed,minSpeed,maxSpeed);
        animator.speed = speed;
        float soundPitch = animator.speed * animationSoundRatio;
        audioSource.pitch = Mathf.Abs(soundPitch);
      }
    }
    
  13. Save your script and add it as a component to animatedRocket GameObject.
  14. Play the scene and change the animation speed by pressing key 1 (accelerate) and 2 (decelerate) on your alphanumeric keyboard. The audio pitch will change accordingly.

How it works...

At the Start() method, besides storing the Animator and Audio oururcecuSource components in variables, we'll get the initial speed from the Animator and, we'll call the AccelRocket() function by passing 0 as an argument, only for that function to calculate the resulting pitch for the Audio Source. During Update() function, the lines of the if(Input.GetKey (KeyCode.Alpha1)) and if(Input.GetKey (KeyCode.Alpha2)) code detect whenever the 1 or 2 keys are being pressed on the alphanumeric keyboard to call the AccelRocket() function, passing a accel float variable as an argument. The AccelRocket() function, in its turn, increments speed with the received argument (the accel float variable). However, it uses the Mathf.Clamp()command to limit the new speed value between the minimum and maximum speed as set by the user. Then, it changes the Animator speed and Audio Source pitch according to the new speed absolute value (the reason for making it an absolute value is keeping the pitch a positive number, even when the animation is reversed by a negative speed value). Also, please note that setting the animation speed and therefore, the sound pitch to 0 will cause the sound to stop, making it clear that stopping the object's animation also prevents the engine sound from playing.

There's more...

Here is some information on how to fine-tune and customize this recipe.

Changing the Animation/Sound Ratio

If you want the audio clip pitch to be more or less affected by the animation speed, change the value of the Animation/Sound Ratio parameter.

Accessing the function from other scripts

The AccelRocket()function was made public so that it can be accessed from other scripts. As an example, we have included the ExtChangePitch.cs script in 1362_09_01 folder. Try attaching this script to the Main Camera object and use it to control the speed by clicking on the left and right mouse buttons.

Summary

In this article we learned, how to match audio pitch to the animation speed, how to change Animation/Sound Ratio.

To learn more please refer to the following books:

  • Learning Unity 2D Game Development by Examplehttps://www.packtpub.com/game-development/learning-unity-2d-game-development-example.
  • Unity Game Development Blueprintshttps://www.packtpub.com/game-development/unity-game-development-blueprints.
  • Getting Started with Unityhttps://www.packtpub.com/game-development/getting-started-unity.

Resources for Article:

 


Further resources on this subject:


You've been reading an excerpt of:

Unity 5.x Cookbook

Explore Title