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 /
This question was closed Dec 21, 2015 at 05:54 PM by KnightRiderGuy for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by KnightRiderGuy · Dec 21, 2015 at 02:58 PM · c#javascriptguitextuifontconvertc#

render out the audio spectrum data to a UI text

I have this Unity script that I would like converted into C# but also if someone could change it so that the Old On Gui Text uses the new UI text on canvas system that would be awesome.

 var qSamples: int = 1024;  // array size
  var refValue: float = 0.1; // RMS value for 0 dB
  var threshold = 0.02;      // minimum amplitude to extract pitch
  var rmsValue: float;   // sound level - RMS
  var dbValue: float;    // sound level - dB
  var pitchValue: float; // sound pitch - Hz
  
  private var samples: float[]; // audio samples
  private var spectrum: float[]; // audio spectrum
  private var fSample: float;
  
  function Start () {
      
      samples = new float[qSamples];
      spectrum = new float[qSamples];
      fSample = AudioSettings.outputSampleRate;
  }
  
  function AnalyzeSound(){
      GetComponent.<AudioSource>().GetOutputData(samples, 0); // fill array with samples
      var i: int;
      var sum: float = 0;
      for (i=0; i < qSamples; i++){
          sum += samples[i]*samples[i]; // sum squared samples
      }
      rmsValue = Mathf.Sqrt(sum/qSamples); // rms = square root of average
      dbValue = 20*Mathf.Log10(rmsValue/refValue); // calculate dB
      if (dbValue < -160) dbValue = -160; // clamp it to -160dB min
      // get sound spectrum
      GetComponent.<AudioSource>().GetSpectrumData(spectrum, 0, FFTWindow.BlackmanHarris);
      var maxV: float = 0;
      var maxN: int = 0;
      for (i=0; i < qSamples; i++){ // find max 
          if (spectrum[i] > maxV && spectrum[i] > threshold){
              maxV = spectrum[i];
              maxN = i; // maxN is the index of max
          }
      }
      var freqN: float = maxN; // pass the index to a float variable
      if (maxN > 0 && maxN < qSamples-1){ // interpolate index using neighbours
          var dL = spectrum[maxN-1]/spectrum[maxN];
          var dR = spectrum[maxN+1]/spectrum[maxN];
          freqN += 0.5*(dR*dR - dL*dL);
      }
      pitchValue = freqN*(fSample/2)/qSamples; // convert index to frequency
  }
  
  var display: GUIText; // drag a GUIText here to show results
  
  function Update () {
      if (Input.GetKeyDown("p")){
          GetComponent.<AudioSource>().Play();
      }
      AnalyzeSound();
      if (display){ 
          display.text = "RMS: "+rmsValue.ToString("F2")+
          " ("+dbValue.ToString("F1")+" dB)\n"+
          "Pitch: "+pitchValue.ToString("F0")+" Hz";
      }
  }
  
Comment
Add comment · Show 4
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 KdRWaylander · Dec 21, 2015 at 03:39 PM 0
Share

Hey, people here on UA won't do the thing for you ... we're here to help you if you're struggling with a particular point but DEFINITLY NOT to take your order and do all the job ...

Look for hints on how to translate from JS to C# that's quite easy in fact, you just have to change a few things in variable declaration and functions' names ...

GUI Text and 1.6 UI Text are very different, one is "writing" on your screen while the other uses objects in Canvas ... even if we'd translate it for you, since you wouldn't understand what object to add and where to add them, it wouldn't be a solution either.

avatar image KnightRiderGuy KdRWaylander · Dec 21, 2015 at 04:09 PM 0
Share

@$$anonymous$$dRWaylander, That's nice and all but I have had a few kind souls that were willing to help, those I am eternally thankful for, for those that do not want to help for whatever reasons I have no interest in. I come here for such help, not grief ;)

avatar image KdRWaylander KnightRiderGuy · Dec 22, 2015 at 08:59 AM 1
Share

That's not that i don't want to help, see that i do help you on your other question. But UA is not meant for this kind a question (you want your script to be translated for your use, UA is more of a place where questions benefit to everyone and to people that will have the same question later on. Unity Forums is the correct place to deal with your problem).

Anyway i'm glad you could find an answer to your problem, try to change the title of your question to something like "render out the audio spectrum data to a UI text" so it will be useful to others :)

Show more comments

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by KnightRiderGuy · Dec 21, 2015 at 05:30 PM

Ok Thanks to one of the very helpful souls on the forum I was able to get this converted so I'll post it here just incase there is anyone else who needs to render out the audio spectrum data to a UI text and not the old very difficult to use onGUI text.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class SpectrumDataText : MonoBehaviour {
 
     public int qSamples = 1024;  // array size
     public float refValue = 0.1f; // RMS value for 0 dB
     public float threshold = 0.02f;  // minimum amplitude to extract pitch
     public float rmsValue;  // sound level - RMS
     public float dbValue;  // sound level - dB
     public float pitchValue; // sound pitch - Hz
     private float[] samples; // audio samples
     private float[] spectrum; // audio spectrum
     private float fSample;
 
     void Start () {
 
         samples = new float[qSamples];
         spectrum = new float[qSamples];
         fSample = AudioSettings.outputSampleRate;
     }
 
     void AnalyzeSound(){
         GetComponent<AudioSource>().GetOutputData(samples, 0); // fill array with samples
         int i;
         float sum = 0.0f;
 
         for (i=0; i < qSamples; i++){
             sum += samples[i]*samples[i]; // sum squared samples
         }
         rmsValue = Mathf.Sqrt(sum/qSamples); // rms = square root of average
         dbValue = 20*Mathf.Log10(rmsValue/refValue); // calculate dB
 
         if (dbValue < -160) dbValue = -160; // clamp it to -160dB min
         // get sound spectrum
 
         GetComponent<AudioSource>().GetSpectrumData(spectrum, 0, FFTWindow.BlackmanHarris);
         float maxV = 0.0f;
         int maxN = 0;
 
         for (i=0; i < qSamples; i++){ // find max
             if (spectrum[i] > maxV && spectrum[i] > threshold){
                 maxV = spectrum[i];
                 maxN = i; // maxN is the index of max
             }
         }
         float freqN = maxN; // pass the index to a float variable
         if (maxN > 0 && maxN < qSamples-1){ // interpolate index using neighbours
             var dL = spectrum[maxN-1]/spectrum[maxN];
             var dR = spectrum[maxN+1]/spectrum[maxN];
             freqN += 0.5f*(dR*dR - dL*dL);
         }
         pitchValue = freqN*(fSample/2)/qSamples; // convert index to frequency
     }
 
     public Text display; // drag a GUIText here to show results
 
     void Update () {
         if (Input.GetKeyDown("p")){
             GetComponent<AudioSource>().Play();
         }
         AnalyzeSound();
         if (display){
             display.text = "RMS: "+rmsValue.ToString("F2")+
                 " ("+dbValue.ToString("F1")+" dB)\n"+
                 "Pitch: "+pitchValue.ToString("F0")+" Hz";
         }
     }
 }
 
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

Follow this Question

Answers Answers and Comments

48 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

Related Questions

UnityScript to C# Conversion 1 Answer

convert java to c# 1 Answer

Loading saved location from a .cs file in my main menu which is a .js file. 0 Answers

Having trouble with UI text please help 1 Answer

Convert Js yo C# 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