How to generate waveform from AudioClip?

Hi there,

How would I go about generating a static waveform preview like the one seen here? Not moving, not updating as the song plays. Just a preview of the audio.

alt text

I would like to make an identical function using 2D Rectangles, NOT a debug line or gui texture.

Not a visualizer, not a debug line. A waveform preview of the entire song as seen in the image above.
An example would be awesome if anyone has one to share :)!

I’ve had tons of trouble finding info on AudioClip.getData and how to use it, very little is out there about waveform preview generation. Tons of info however is available on visualizer generation :frowning: which is not what i want.

I do this, and it’s works perfectly :

	public static float[] GetWaveform (AudioClip audio, int size, float sat) {
		float[] samples = new float[audio.samples];
		float[] waveform = new float;
		audio.GetData(samples, 0);
		int packSize = audio.samples / size;

		float max = 0f;
	int c = 0;
	int s = 0;
	for (int i = 0; i < 2 * audio.samples; i++) {
		waveform 
 += Mathf.Abs (samples *);
			s++;
			if (s > packSize) {
				if (max < waveform [c])
					max = waveform [c];
				c++;
				s = 0;
			}
		}
    		for (int i = 0; i < size; i++) {
    			waveform * /= (max * sat);
    			if (waveform * > 1f)
    				waveform * = 1f;
    		}
    
    		return waveform;
    	}
    
    	public static Texture2D PaintWaveformSpectrum(float[] waveform, int height, Color c) {
    		Texture2D tex = new Texture2D (waveform.Length, height, TextureFormat.RGBA32, false);
    
    		for (int x = 0; x < waveform.Length; x++) {
    			for (int y = 0; y <= waveform [x] * (float)height / 2f; y++) {
    				tex.SetPixel (x, (height / 2) + y, c);
    				tex.SetPixel (x, (height / 2) - y, c);
    			}
    		}
    		tex.Apply ();
    
    		return tex;
    	}


and use it like this:

    Texture2D tex = AudioGetter.PaintWaveformSpectrum (AudioGetter.GetWaveform (clip, 500, 1f), 50, c);
    		GetComponent<Image> ().overrideSprite = Sprite.Create (tex, new Rect (0f, 0f, tex.width, tex.height), new Vector2 (0.5f, 0.5f));

I edited @Breakypower’s code in the previous answer to fix a couple bugs and optimize the generation:

public Texture2D PaintWaveformSpectrum(AudioClip audio, float saturation, int width, int height, Color col) {
    Texture2D tex = new Texture2D(width, height, TextureFormat.RGBA32, false);
    float[] samples = new float[audio.samples];
    float[] waveform = new float[width];
    audio.GetData(samples, 0);
    int packSize = ( audio.samples / width ) + 1;
    int s = 0;
    for (int i = 0; i < audio.samples; i += packSize) {
        waveform ~~= Mathf.Abs(samples*);*~~

s++;
}

for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
tex.SetPixel(x, y, Color.black);
}
}

for (int x = 0; x < waveform.Length; x++) {
for (int y = 0; y <= waveform[x] * ((float)height * .75f); y++) {
tex.SetPixel(x, ( height / 2 ) + y, col);
tex.SetPixel(x, ( height / 2 ) - y, col);
}
}
tex.Apply();

return tex;
}
It produces very lovely results for my purposes:
[132225-capture.png|132225]~~
~~