IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
SteamVR_Ears.cs
1 //======= Copyright (c) Valve Corporation, All rights reserved. ===============
2 //
3 // Purpose: Handles aligning audio listener when using speakers.
4 //
5 //=============================================================================
6 
7 using UnityEngine;
8 using Valve.VR;
9 
10 [RequireComponent(typeof(AudioListener))]
11 public class SteamVR_Ears : MonoBehaviour
12 {
13  public SteamVR_Camera vrcam;
14 
15  bool usingSpeakers;
16  Quaternion offset;
17 
18  private void OnNewPosesApplied()
19  {
20  var origin = vrcam.origin;
21  var baseRotation = origin != null ? origin.rotation : Quaternion.identity;
22  transform.rotation = baseRotation * offset;
23  }
24 
25  void OnEnable()
26  {
27  usingSpeakers = false;
28 
29  var settings = OpenVR.Settings;
30  if (settings != null)
31  {
32  var error = EVRSettingsError.None;
33  if (settings.GetBool(OpenVR.k_pch_SteamVR_Section, OpenVR.k_pch_SteamVR_UsingSpeakers_Bool, ref error))
34  {
35  usingSpeakers = true;
36 
37  var yawOffset = settings.GetFloat(OpenVR.k_pch_SteamVR_Section, OpenVR.k_pch_SteamVR_SpeakersForwardYawOffsetDegrees_Float, ref error);
38  offset = Quaternion.Euler(0.0f, yawOffset, 0.0f);
39  }
40  }
41 
42  if (usingSpeakers)
43  SteamVR_Events.NewPosesApplied.Listen(OnNewPosesApplied);
44  }
45 
46  void OnDisable()
47  {
48  if (usingSpeakers)
49  SteamVR_Events.NewPosesApplied.Remove(OnNewPosesApplied);
50  }
51 }
52