- Home /
This question was
closed Mar 16, 2015 at 07:14 PM by
KnightRiderGuy for the following reason:
Duplicate Question
Question by
KnightRiderGuy · Mar 06, 2015 at 01:15 AM ·
audiorenderingui imagedatarender texture
Render Audio Waveform to UI Image
Is it possible to render an Audio waveform to a UI image in the same way that this script does? I tried making changes to this one to get it to work with a UI image but I am not having much luck.
UPDATE: I still have had no luck in searching for how to get this to work on either a UI image or UI Raw image, I suspect it is the Raw image that it needs to work on in much the same way as the webcam works with a Raw image.... but so far no luck with figuring out this waveform to display.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
//[RequireComponent(typeof(AudioSource))]
//[RequireComponent(typeof(GUITexture))]
public class AudioWaveFormVisualizerXXX : MonoBehaviour
{
public RawImage rawimage;
public AudioSource MusicPlayer;
public int width = 500; // texture width
public int height = 100; // texture height
public Color backgroundColor = Color.black;
public Color waveformColor = Color.green;
public int size = 2048; // size of sound segment displayed in texture
private Color[] blank; // blank image array
//private Texture2D texture;
public Texture2D texture;
private float[] samples; // audio samples array
IEnumerator Start ()
{
// create the samples array
samples = new float[size];
// create the texture and assign to the guiTexture:
texture = new Texture2D (width, height);
//rawimage = new RawImage (width, height);
guiTexture.texture = texture;
//RawImage = rawimage;
// create a 'blank screen' image
blank = new Color[width * height];
for (int i = 0; i < blank.Length; i++) {
blank [i] = backgroundColor;
}
// refresh the display each 100mS
while (true) {
GetCurWave ();
yield return new WaitForSeconds (0.1f);
}
}
void GetCurWave ()
{
// clear the texture
texture.SetPixels (blank, 0);
//RawImage.SetPixels (blank, 0);
// get samples from channel 0 (left)
MusicPlayer.GetOutputData (samples, 0);
// draw the waveform
for (int i = 0; i < size; i++) {
texture.SetPixel ((int)(width * i / size), (int)(height * (samples [i] + 1f) / 2f), waveformColor);
//RawImage.SetPixel ((int)(width * i / size), (int)(height * (samples [i] + 1f) / 2f), waveformColor);
} // upload to the graphics card
texture.Apply ();
//RawImage.Apply ();
}
}
Comment