Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by ElaxomoeR · Apr 03, 2019 at 07:08 AM · textureaudiosetpixel

How to generate full complete waveform on texture with texture.SetPixel?

Can anyone help me with this ? Actually, I am not good at English and I don't understand how this whole script work even if I read all the answers and guides. I just want to generate waveform on the texture and show it. The waveforms in the pic are what I want and what I get now.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 [RequireComponent(typeof(AudioSource))]
 public class WaveFormTest : MonoBehaviour
 {
     public new AudioSource audio;
 
     public RawImage image;
 
     int resolution = 60;
 
     float[] waveForm;
     float[] samples;
 
     Texture2D texture;
 
     private void Start()
     {
         texture = new Texture2D(900, 100, TextureFormat.RGBA32, false);
         CreateWaveForm();
     }
 
     // This two are from unity answer (I mixed up)
     public void CreateWaveForm()
     {
         resolution = audio.clip.frequency / resolution;
 
         samples = new float[audio.clip.samples * audio.clip.channels];
         audio.clip.GetData(samples, 0);
 
         int s = 0;
         while (s < samples.Length)
         {
             samples[s] = samples[s] * 0.5F;
             ++s;
         }
         audio.clip.SetData(samples, 0);
 
         waveForm = new float[(samples.Length / resolution)];
 
         for (int i = 0; i < waveForm.Length; i++)
         {
             waveForm[i] = 0;
 
             for (int ii = 0; ii < resolution; ii++)
             {
                 waveForm[i] += Mathf.Abs(samples[(i * resolution) + ii]);
             }
 
             waveForm[i] /= resolution;
         }
 
         MakeTexture(900, 100, waveForm, Color.yellow);
     }
 
     public void MakeTexture(int width, int height, float[] waveform, Color col)
     {
         texture = new Texture2D(width, height, TextureFormat.RGBA32, false);
 
         for (int x = 0; x < width; x++)
         {
             for (int y = 0; y < height; y++)
             {
                 texture.SetPixel(x, y, Color.black);
             }
         }
 
         for (int x = 0; x < waveform.Length; x++)
         {
             for (int y = 0; y <= waveform[x] * ((float)height * .75f); y++)
             {
                 texture.SetPixel(x, (height / 2) + y, col);
                 texture.SetPixel(x, (height / 2) - y, col);
             }
         }
 
         texture.Apply();
 
         image.texture = texture;
     }
 
     void Update()
     {
         //script from unity doc.
         float[] spectrum = new float[1024];
 
         AudioListener.GetSpectrumData(spectrum, 0, FFTWindow.Rectangular);
 
         for (int i = 1; i < spectrum.Length - 1; i++)
         {
             Debug.DrawLine(new Vector3(i - 1, spectrum[i] + 10, 0), new Vector3(i, spectrum[i + 1] + 10, 0), Color.red);
             Debug.DrawLine(new Vector3(i - 1, Mathf.Log(spectrum[i - 1]) + 10, 2), new Vector3(i, Mathf.Log(spectrum[i]) + 10, 2), Color.cyan);
             Debug.DrawLine(new Vector3(Mathf.Log(i - 1), spectrum[i - 1] - 10, 1), new Vector3(Mathf.Log(i), spectrum[i] - 10, 1), Color.green);
             Debug.DrawLine(new Vector3(Mathf.Log(i - 1), Mathf.Log(spectrum[i - 1]), 3), new Vector3(Mathf.Log(i), Mathf.Log(spectrum[i]), 3), Color.blue);
         }
 
         //script from unity answer
         for (int i = 0; i < waveForm.Length - 1; i++)
         {
             Vector3 sv = new Vector3(i * .01f, waveForm[i] * 10, 0);
             Vector3 ev = new Vector3(i * .01f, -waveForm[i] * 10, 0);
 
             Debug.DrawLine(sv, ev, Color.yellow);
         }
 
         int current = audio.timeSamples / resolution;
         current *= 2;
 
         Vector3 c = new Vector3(current * .01f, 0, 0);
 
         Debug.DrawLine(c, c + Vector3.up * 10, Color.white);
     }
 }

editor view

capture2.jpg (329.5 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

221 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there a way to get the audio form mobile mic and find it's music key ? Like in a karaoke game. 0 Answers

Listening to Sound Card Output 0 Answers

Help destroying a seperate game object on collision enter 1 Answer

Precise metronome problem 1 Answer

can not get DeviceCaps by Microphone. 0 Answers


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