Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by sharsnik · Nov 28, 2011 at 04:54 PM · editoraudiodisplay

Displaying an audio waveform in the editor

Unity's UI includes a display of audio waveforms in it's preview. Is there a simple way to display this, without parsing out the audio file myself?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by CleverToucan · Jan 30, 2019 at 01:32 PM

I made a simpler script to create a waveform texture for GUI elements:

 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[s] = Mathf.Abs(samples[i]);
         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's a modified version of @Breakypower's code in this thread: https://answers.unity.com/questions/699595/how-to-generate-waveform-from-audioclip.html

It produces very lovely results for my purposes:


capture.png (36.3 kB)
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image neoRiley · Mar 04, 2020 at 03:27 PM 0
Share

@CleverToucan - great solution, works perfectly! BTW, you never use the saturation param, so that can be removed

avatar image Alond · Dec 31, 2020 at 05:49 PM 0
Share

Great solution! one comment - the audio.samples property should be multiplied by the audio.channels property anywhere it appears. Otherwise, the waveform might be only partial.

avatar image
2

Answer by Unity-Paradox · Jul 01, 2016 at 03:47 PM

What I did was use AudioListener.GetOutputData, and used the output array in a GL drawing function to draw the waveform to the screen. Here's the script:

 using UnityEngine;
 using System.Collections;

 public class ScopeViewer : MonoBehaviour {
     float[][] audioData = new float[2][];
     Material glMat;
     
     void Start () {
         if (!glMat) {
             audioData[0] = new float[2048];
             audioData[1] = new float[2048];
             Shader glSh = Shader.Find("Hidden/Internal-Colored");
             glMat = new Material(glSh);
             glMat.hideFlags = HideFlags.HideAndDontSave;
             glMat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
             glMat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
             glMat.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
             glMat.SetInt("_ZWrite", 0);
         }
     }

     void OnRenderObject () {
         AudioListener.GetOutputData(audioData[0], 0);
         AudioListener.GetOutputData(audioData[1], 1);
         float[] highestOutput = new float[2];
         highestOutput[0] = 0;
         highestOutput[1] = 0;
         for (int i=0; i<audioData[0].Length; i++) {
             if (Mathf.Abs(audioData[0][i]) > highestOutput[0]) highestOutput[0] = Mathf.Abs(audioData[0][i]);
             if (Mathf.Abs(audioData[1][i]) > highestOutput[1]) highestOutput[1] = Mathf.Abs(audioData[1][i]);
         }
         if (highestOutput[0] == 0) highestOutput[0] = 1;
         if (highestOutput[1] == 0) highestOutput[1] = 1;

         glMat.SetPass(0);

         GL.PushMatrix();
         GL.MultMatrix(transform.localToWorldMatrix);

         GL.Begin(GL.QUADS);
         GL.Color(Color.white);
         GL.Vertex(new Vector3(-0.525f, 0.55f, 0));
         GL.Vertex(new Vector3(0.525f, 0.55f, 0));
         GL.Vertex(new Vector3(0.525f, -0.55f, 0));
         GL.Vertex(new Vector3(-0.525f, -0.55f, 0));
         GL.Color(Color.black);
         GL.Vertex(new Vector3(-0.5f, 0.5f, 0));
         GL.Vertex(new Vector3(0.5f, 0.5f, 0));
         GL.Vertex(new Vector3(0.5f, -0.5f, 0));
         GL.Vertex(new Vector3(-0.5f, -0.5f, 0));
         GL.End();

         GL.Begin(GL.LINES);
         GL.Color(Color.white);
         for (int i=0; i<audioData[0].Length/5-1; i++) {
             GL.Vertex(new Vector3((float)i/(audioData[0].Length/5)-0.5f, 0.5f*audioData[0][i]/highestOutput[0], 0));
             GL.Vertex(new Vector3((float)(i+1)/(audioData[0].Length/5)-0.5f, 0.5f*audioData[0][i+1]/highestOutput[0], 0));
         }
         for (int i=0; i<audioData[1].Length/5-1; i++) {
             GL.Vertex(new Vector3((float)i/(audioData[1].Length/5)-0.5f, 0.5f*audioData[1][i]/highestOutput[1], 0));
             GL.Vertex(new Vector3((float)(i+1)/(audioData[1].Length/5)-0.5f, 0.5f*audioData[1][i+1]/highestOutput[1], 0));
         }
         GL.End();

         GL.PopMatrix();
     }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Unity-Paradox · Jul 01, 2016 at 11:04 AM 0
Share

Please note, this code IS somewhat complex and a bit specialized to what I was using it for, but if you know your stuff, it shouldn't be too much of an issue to change the code to suit yourself :)

avatar image UnityCoach · Apr 20, 2017 at 11:06 PM 0
Share

Awesome work! Thanks for sharing this.

avatar image
2

Answer by Bunny83 · Jul 01, 2016 at 01:01 PM

Since the question got bumped here are the two ways the default inspector draws the wave preview:

  • AssetPreview.GetAssetPreview(audioClip);

  • AudioUtil.GetWaveFormFast(audioClip, 1, 0, audioClip.samples, r.width, r.height);

Both methods will return a Texture2D. The first one is used when the inspector width is smaller than 100 pixels, the second is used when it's greater or equal to 100.

However the AudioUtil class is an internal class of the editor so you can't use it directly without reflection.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AxonGenesis · Dec 02, 2016 at 11:52 PM 0
Share

For anyone wondering how to use AudioUtil, I found this very helpful thread including a wrapper class: https://forum.unity3d.com/threads/reflected-audioutil-class-for-making-audio-based-editor-extensions.308133/

avatar image AxonGenesis AxonGenesis · Dec 03, 2016 at 12:35 AM 0
Share

Unfortunately I am unable to get this to work. Trying to get the method GetWaveFormFast returns null. I've used Get$$anonymous$$ethods to see a full list of methods for AudioUtil, and GetWaveFormFast is not present. Has this been moved elsewhere, or is there a better way to draw audio waveforms in the inspector? Or am I doing something wrong here?

     $$anonymous$$ethodInfo[] methods = audioUtilClass.Get$$anonymous$$ethods(BindingFlags.Static | BindingFlags.Public);
     string log = "$$anonymous$$ethods:"+methods.Length+"\n";
     foreach($$anonymous$$ethodInfo mi in methods) {
         log += mi+"\n";
     }
     Debug.Log(log);
    
     $$anonymous$$ethodInfo method = audioUtilClass.Get$$anonymous$$ethod("GetWaveFormFast", BindingFlags.Static | BindingFlags.Public);
     if(method == null) {
         Debug.LogWarning("Failed getting method GetWaveFormFast");
     }
     else {
         texture = (Texture2D)method.Invoke(null, new object[] {clip, channel, fromSample, toSample, width, height});
     }
avatar image Bunny83 AxonGenesis · Dec 03, 2016 at 02:20 PM 1
Share

Ugh, don't use reflection to poke around classes. Download ILSpy and simply open the UnityEditor.dll inside your Unity installation folder. `\Unity\Editor\Data\$$anonymous$$anaged`. Visual Studio as wellas $$anonymous$$onoDevelop also have an assembly browser integrated, but they don't show the actual code, just the signature of the methods. Also i'm not sure if they actually display internal / private classes / members.

Show more comments
avatar image
0

Answer by Rijicho_nl · Jan 28, 2020 at 02:00 PM

My solution (Japanese page):

AudioUtil's wrapper: https://qiita.com/Rijicho_nl/items/c7e6a5cb9cf56e52588a

How to render the waveform: https://qiita.com/Rijicho_nl/items/3c51befd7bbe63c00878

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Audio popping on pause and volume change 1 Answer

Listen to audio when stepping frame by frame in editor 1 Answer

When accessing the Add Component drop-down menu, the following display issue occurs. How can I resolve this? 0 Answers

Unity mainmenu audio problem. 1 Answer

How to display a message while editor is blocked? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges