I am making a rockband game and I want to make an editor, so I need to have the waveform for the song displayed at the bottom. Is anyone familiar with a way to draw waveforms from a sound file? If you can understand why its important than great! otherwise, its important!!! please help me!
If you want to draw the audio currently playing, you can use the script below attached to a GUITexture which has the AudioSource where the sound is playing:
@script RequireComponent(GUITexture);
@script RequireComponent(AudioSource);
var width: int = 500; // texture width
var height: int = 100; // texture height
var backgroundColor: Color = Color.black;
var waveformColor: Color = Color.green;
var size = 2048; // size of sound segment displayed in texture
private var blank: Color[]; // blank image array
private var texture: Texture2D;
private var samples: float[]; // audio samples array
function Start(){
// create the samples array
samples = new float;
// create the texture and assign to the guiTexture:
texture = new Texture2D(width, height);
guiTexture.texture = texture;
// create a 'blank screen' image
blank = new Color[width * height];
for (var pixel in blank){
pixel = backgroundColor;
}
// refresh the display each 100mS
while (true){
GetCurWave();
yield WaitForSeconds(0.1);
}
}
function GetCurWave(){
// clear the texture
texture.SetPixels(blank, 0);
// get samples from channel 0 (left)
audio.GetOutputData(samples, 0);
// draw the waveform
for (i = 0; i < size; i++){
texture.SetPixel(width * i / size, height * (samples*+1f)/2, waveformColor);*
*}*
*// upload to the graphics card*
*texture.Apply();*
*}*
**
But if you want to draw the waveform of an audio clip, you should get the samples using AudioClip.GetData(), like in the function below. Unfortunately, I could not make this routine work: Unity displays two weird runtime error messages and refuses to get the samples, what lets the screen with only the zero line. I suppose this is a bug in GetData, which seems to be a new feature of 3.5 version. Anyway, I’m posting the code below: GetWaveForm should be called whenever you select a new audio clip to update the display.
function GetWaveform(clip: AudioClip){
var size = clip.samples * clip.channels;
var samples = new float;
clip.GetData(samples, 0);
// clear the texture
texture.SetPixels(blank, 0);
// draw the waveform
for (i = 0; i < size; i++){
texture.SetPixel(width * i / size, height * (samples*+1f)/2, waveformColor);*
}
// upload to the graphics card
texture.Apply();
}
Hi,
I converted this to C#. The OP format is a bit janky, so thought this might help others. This is the live audio visualizer, and the Audio file is marked for streaming.
Works fine in 4.6.1
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
[RequireComponent(typeof(GUITexture))]
public class AudioWaveFormVisualizer : MonoBehaviour
{
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;
private float[] samples; // audio samples array
IEnumerator Start ()
{
// create the samples array
samples = new float;
// create the texture and assign to the guiTexture:
texture = new Texture2D (width, height);
guiTexture.texture = texture;
// create a 'blank screen' image
blank = new Color[width * height];
for (int i = 0; i < blank.Length; i++) {
blank *= backgroundColor;*
-
}* -
// refresh the display each 100mS* -
while (true) {* -
GetCurWave ();* -
yield return new WaitForSeconds (0.1f);* -
}* -
}* -
void GetCurWave ()* -
{* -
// clear the texture* -
texture.SetPixels (blank, 0);* -
// get samples from channel 0 (left)* -
audio.GetOutputData (samples, 0);* -
// draw the waveform* -
for (int i = 0; i < size; i++) {*
_ texture.SetPixel ((int)(width * i / size), (int)(height * (samples + 1f) / 2f), waveformColor);_
* } // upload to the graphics card*
* texture.Apply ();*
* }*
}
hey Aldonaletto, your second method still appears to generate errors in Unity 4.2. is there perhaps a more updated way to draw a waveform preview of an AudioClip (not a live preview)? any help would be appreciated.
Edit - just found an answer, though it’s a workaround. the bug apparently applies to AudioClips that have the Compressed In Memory Import setting on them. setting this to Stream From Disc solved the issue! so the workaround for now is to change the import settings before running the script and change them back afterwards.
hope this helps some folks…