Search Unity

Convert GUI image to Raw Image for Audio Waveform Visualizer

Discussion in 'Scripting' started by KnightRiderGuy, Mar 5, 2015.

  1. KnightRiderGuy

    KnightRiderGuy

    Joined:
    Nov 23, 2014
    Posts:
    515
    Hello,
    I'm looking to see if this C# script can be converted to use the new UI system of doing things. Basically I just want this script converted in a way that it will use either a UI image or UI Raw Image to render the spectrum data too. Reason being is that I find it WAY easier to do a proper GUI interface with the new UI system only I find it does not often play nice with other GUI elements from the old way of doing things. If someone could convert this script that would be just AWESOME :)

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. //[RequireComponent(typeof(AudioSource))]
    6. [RequireComponent(typeof(GUITexture))]
    7. public class AudioWaveFormVisualizer : MonoBehaviour
    8. {
    9.    
    10.     public AudioSource MusicPlayer;
    11.     public int width = 500; // texture width
    12.     public int height = 100; // texture height
    13.     public Color backgroundColor = Color.black;
    14.     public Color waveformColor = Color.green;
    15.     public int size = 2048; // size of sound segment displayed in texture
    16.    
    17.     private Color[] blank; // blank image array
    18.     private Texture2D texture;
    19.     private float[] samples; // audio samples array
    20.    
    21.     IEnumerator Start ()
    22.     {
    23.        
    24.         // create the samples array
    25.         samples = new float[size];
    26.        
    27.         // create the texture and assign to the guiTexture:
    28.         texture = new Texture2D (width, height);
    29.        
    30.         guiTexture.texture = texture;
    31.        
    32.         // create a 'blank screen' image
    33.         blank = new Color[width * height];
    34.        
    35.         for (int i = 0; i < blank.Length; i++) {
    36.             blank [i] = backgroundColor;
    37.         }
    38.        
    39.         // refresh the display each 100mS
    40.         while (true) {
    41.             GetCurWave ();
    42.             yield return new WaitForSeconds (0.1f);
    43.         }
    44.     }
    45.    
    46.     void GetCurWave ()
    47.     {
    48.         // clear the texture
    49.         texture.SetPixels (blank, 0);
    50.  
    51.        
    52.         // get samples from channel 0 (left)
    53.         MusicPlayer.GetOutputData (samples, 0);
    54.        
    55.         // draw the waveform
    56.         for (int i = 0; i < size; i++) {
    57.             texture.SetPixel ((int)(width * i / size), (int)(height * (samples [i] + 1f) / 2f), waveformColor);
    58.  
    59.         } // upload to the graphics card
    60.        
    61.         texture.Apply ();
    62.     }
    63. }