Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.05 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. [RequireComponent(typeof(AudioSource))]
  6. [RequireComponent(typeof(LineRenderer))]
  7. public class Visualiser : MonoBehaviour
  8. {
  9.     public int              _Samples = 100; // The number of samples we will sample the audiosource with. higher = lower performance but more points
  10.     public float            _Scale = 1.0f;  // The scale of the audio sample
  11.     public float            _Offset = 1.0f; // the offset from center for our circle
  12.     public float            _ColorMul = 1.0f;
  13.     public float            _LerpSpeed = 1.0f;
  14.  
  15.     public AudioClip        _AudioClip;     // Our audio clip
  16.  
  17.     public Color            _LowColor;
  18.     public Color            _HighColor;
  19.  
  20.     private AudioSource     _AudioSource;
  21.     private LineRenderer    _LineRenderer;
  22.  
  23.     // Start is called before the first frame update
  24.     void Start()
  25.     {
  26.         _AudioSource    = GetComponent<AudioSource>();
  27.         _LineRenderer   = GetComponent<LineRenderer>();
  28.  
  29.         _AudioSource.clip = _AudioClip;
  30.         _AudioSource.Play();
  31.     }
  32.  
  33.  
  34.     // Update is called once per frame
  35.     void Update()
  36.     {
  37.         float[] data = new float[_Samples]; // Create a basic array to store sample data
  38.         _AudioSource.clip.GetData(data, _AudioSource.timeSamples); // Sample the track at the current timestamp
  39.  
  40.         // We add 1 extra so we can loop to the start
  41.         _LineRenderer.positionCount = _Samples + 1;
  42.  
  43.         // For each sample...
  44.         float avg = 0.0f;
  45.        
  46.         for (int i = 0; i < _Samples; ++i)
  47.         {
  48.             // Calculate the angle in degrees and convert to radians
  49.             float a = ((360.0f / _Samples) * i) * Mathf.Deg2Rad;
  50.  
  51.             // Calculate the radius of the point, this will be our sample data
  52.             // multiplied by a scale added to an offset
  53.             float r = (data[i] * _Scale) + _Offset;
  54.  
  55.             // Calculate points around the circle
  56.             float x = r * Mathf.Sin(a);
  57.             float y = r * Mathf.Cos(a);
  58.  
  59.             // lerp line point
  60.             Vector3 currentPosition = _LineRenderer.GetPosition(i);
  61.             Vector3 newPosition = Vector3.Lerp(currentPosition, new Vector3(x, y, 0), _LerpSpeed * Time.deltaTime);
  62.  
  63.             _LineRenderer.SetPosition(i, newPosition);
  64.  
  65.             // Sometimes sample can be negative so abs it
  66.             avg += Mathf.Abs(data[i]);
  67.         }
  68.  
  69.         // Get avg
  70.         avg = avg / (float)_Samples;
  71.  
  72.         Color c = Color.Lerp(_LowColor, _HighColor, avg * _ColorMul);
  73.  
  74.         // Create color gradient
  75.         Gradient gradient = new Gradient();
  76.         gradient.SetKeys(
  77.             new GradientColorKey[] { new GradientColorKey(c, 0.0f), new GradientColorKey(c, 1.0f) },
  78.             new GradientAlphaKey[] { new GradientAlphaKey(1.0f, 0.0f), new GradientAlphaKey(1.0f, 1.0f) }
  79.         );
  80.         _LineRenderer.colorGradient = gradient;
  81.  
  82.         // Loop to the start
  83.         _LineRenderer.SetPosition(_Samples, _LineRenderer.GetPosition(0));
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement