AmbisonicAudioDemo.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. //-----------------------------------------------------------------------------
  5. // Copyright 2012-2022 RenderHeads Ltd. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. namespace RenderHeads.Media.AVProMovieCapture.Demos
  8. {
  9. public class AmbisonicAudioDemo : MonoBehaviour
  10. {
  11. [SerializeField] Transform[] _audioObjects = null;
  12. [SerializeField] AudioSource[] _audioSources = null;
  13. struct Instance
  14. {
  15. float x, y, z;
  16. float radius;
  17. }
  18. private int index;
  19. //private List<Instance> _instances = new List<Instance>(4);
  20. /*void Start()
  21. {
  22. foreach (Transform t in _audioObjects)
  23. {
  24. Instance instance = new Instance();
  25. instance.radius = Random.Range(0.5f, 2f);
  26. instance.x = Random.Range(0f, 100f);
  27. instance.y = Random.Range(0f, 100f);
  28. instance.z = Random.Range(0f, 100f);
  29. _instances.Add(instance);
  30. }
  31. }*/
  32. void Update()
  33. {
  34. float[] samples = new float[4];
  35. foreach (AudioSource audio in _audioSources)
  36. {
  37. audio.GetOutputData(samples, 0);
  38. float sample = Mathf.Abs(samples[2]);
  39. sample = Mathf.Sqrt(sample);
  40. float scale = audio.gameObject.transform.localScale.x;
  41. //audio.GetSpectrumData(samples, 0, FFTWindow.Hanning);
  42. scale = 0.15f + Mathf.Lerp(scale, sample, Time.deltaTime * 20f) * 0.85f;
  43. audio.gameObject.transform.localScale = Vector3.one * scale;
  44. }
  45. int index = 0;
  46. foreach (Transform t in _audioObjects)
  47. {
  48. //Vector3 v = axes[index % axes.Length];
  49. //t.RotateAround(Vector3.zero, Vector3.up, 40 * Time.deltaTime);
  50. //Quaternion q = Quaternion.Euler(0f, Time.timeSinceLevelLoad * 50f, 0f);
  51. //Matrix4x4.TRS(Vector3)
  52. float time = Time.timeSinceLevelLoad + index * 1.321f;
  53. float tt = Mathf.PingPong(Mathf.Sin(time * 2.23f) + 1f, 2f) / 2f;
  54. float r = Mathf.Lerp(0.5f, 3f, tt);
  55. float x = Mathf.Sin(time * 1f) * r;
  56. float z = Mathf.Cos(time * 1.13f) * r;
  57. float y = Mathf.Sin(time * 1.23f) * 1f;
  58. //Vector3 v = t.position.normalized * r;
  59. t.position = new Vector3(x, y, z);
  60. //t.position = new Vector3(t.position.x, y, t.position.z);
  61. index++;
  62. }
  63. }
  64. }
  65. }