IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
LinearAudioPitch.cs
1 //======= Copyright (c) Valve Corporation, All rights reserved. ===============
2 //
3 // Purpose: Changes the pitch of this audio source based on a linear mapping
4 // and a curve
5 //
6 //=============================================================================
7 
8 using UnityEngine;
9 using System.Collections;
10 
11 namespace Valve.VR.InteractionSystem
12 {
13  //-------------------------------------------------------------------------
14  public class LinearAudioPitch : MonoBehaviour
15  {
16  public LinearMapping linearMapping;
17  public AnimationCurve pitchCurve;
18  public float minPitch;
19  public float maxPitch;
20  public bool applyContinuously = true;
21 
22  private AudioSource audioSource;
23 
24 
25  //-------------------------------------------------
26  void Awake()
27  {
28  if ( audioSource == null )
29  {
30  audioSource = GetComponent<AudioSource>();
31  }
32 
33  if ( linearMapping == null )
34  {
35  linearMapping = GetComponent<LinearMapping>();
36  }
37  }
38 
39 
40  //-------------------------------------------------
41  void Update()
42  {
43  if ( applyContinuously )
44  {
45  Apply();
46  }
47  }
48 
49 
50  //-------------------------------------------------
51  private void Apply()
52  {
53  float y = pitchCurve.Evaluate( linearMapping.value );
54 
55  audioSource.pitch = Mathf.Lerp( minPitch, maxPitch, y );
56  }
57  }
58 }