Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Unity Script convert to C# And On GUI Text Convert to UI TEXt

Discussion in 'Scripting' started by KnightRiderGuy, Dec 21, 2015.

  1. KnightRiderGuy

    KnightRiderGuy

    Joined:
    Nov 23, 2014
    Posts:
    514
    I have this Unity script that I would like converted to C# if someone much better at scripting than I would be so kind as too.

    Also I would like it to use the New UI text and not the On Gui text which I find very difficult to get to line up properly on canvases with multiple camera setups.

    Code (JavaScript):
    1. var qSamples: int = 1024;  // array size
    2. var refValue: float = 0.1; // RMS value for 0 dB
    3. var threshold = 0.02;      // minimum amplitude to extract pitch
    4. var rmsValue: float;   // sound level - RMS
    5. var dbValue: float;    // sound level - dB
    6. var pitchValue: float; // sound pitch - Hz
    7. private var samples: float[]; // audio samples
    8. private var spectrum: float[]; // audio spectrum
    9. private var fSample: float;
    10. function Start () {
    11.    
    12.      samples = new float[qSamples];
    13.      spectrum = new float[qSamples];
    14.      fSample = AudioSettings.outputSampleRate;
    15. }
    16. function AnalyzeSound(){
    17.      GetComponent.<AudioSource>().GetOutputData(samples, 0); // fill array with samples
    18.      var i: int;
    19.      var sum: float = 0;
    20.      for (i=0; i < qSamples; i++){
    21.          sum += samples[i]*samples[i]; // sum squared samples
    22.      }
    23.      rmsValue = Mathf.Sqrt(sum/qSamples); // rms = square root of average
    24.      dbValue = 20*Mathf.Log10(rmsValue/refValue); // calculate dB
    25.      if (dbValue < -160) dbValue = -160; // clamp it to -160dB min
    26.      // get sound spectrum
    27.      GetComponent.<AudioSource>().GetSpectrumData(spectrum, 0, FFTWindow.BlackmanHarris);
    28.      var maxV: float = 0;
    29.      var maxN: int = 0;
    30.      for (i=0; i < qSamples; i++){ // find max
    31.          if (spectrum[i] > maxV && spectrum[i] > threshold){
    32.              maxV = spectrum[i];
    33.              maxN = i; // maxN is the index of max
    34.          }
    35.      }
    36.      var freqN: float = maxN; // pass the index to a float variable
    37.      if (maxN > 0 && maxN < qSamples-1){ // interpolate index using neighbours
    38.          var dL = spectrum[maxN-1]/spectrum[maxN];
    39.          var dR = spectrum[maxN+1]/spectrum[maxN];
    40.          freqN += 0.5*(dR*dR - dL*dL);
    41.      }
    42.      pitchValue = freqN*(fSample/2)/qSamples; // convert index to frequency
    43. }
    44. var display: GUIText; // drag a GUIText here to show results
    45. function Update () {
    46.      if (Input.GetKeyDown("p")){
    47.          GetComponent.<AudioSource>().Play();
    48.      }
    49.      AnalyzeSound();
    50.      if (display){
    51.          display.text = "RMS: "+rmsValue.ToString("F2")+
    52.          " ("+dbValue.ToString("F1")+" dB)\n"+
    53.          "Pitch: "+pitchValue.ToString("F0")+" Hz";
    54.      }
    55. }
    56.  
     
  2. mikeymike

    mikeymike

    Joined:
    Oct 21, 2014
    Posts:
    35
    Code (csharp):
    1.  
    2.   int qSamples = 1024;  // array size
    3.   float refValue = 0.1; // RMS value for 0 dB
    4.   float threshold = 0.02;  // minimum amplitude to extract pitch
    5.   float rmsValue;  // sound level - RMS
    6.   float dbValue;  // sound level - dB
    7.   float pitchValue; // sound pitch - Hz
    8.   private float[] samples; // audio samples
    9.   private float[] spectrum; // audio spectrum
    10.   private float fSample;
    11.  
    12.   void Start () {
    13.    
    14.   samples = new float[qSamples];
    15.   spectrum = new float[qSamples];
    16.   fSample = AudioSettings.outputSampleRate;
    17.   }
    18.  
    19.   void AnalyzeSound(){
    20.   GetComponent<AudioSource>().GetOutputData(samples, 0); // fill array with samples
    21.   int i;
    22.   float sum = 0;
    23.  
    24.   for (i=0; i < qSamples; i++){
    25.   sum += samples[i]*samples[i]; // sum squared samples
    26.   }
    27.   rmsValue = Mathf.Sqrt(sum/qSamples); // rms = square root of average
    28.   dbValue = 20*Mathf.Log10(rmsValue/refValue); // calculate dB
    29.  
    30.   if (dbValue < -160) dbValue = -160; // clamp it to -160dB min
    31.   // get sound spectrum
    32.  
    33.   GetComponent<AudioSource>().GetSpectrumData(spectrum, 0, FFTWindow.BlackmanHarris);
    34.   float maxV = 0;
    35.   int maxN = 0;
    36.  
    37.   for (i=0; i < qSamples; i++){ // find max
    38.   if (spectrum[i] > maxV && spectrum[i] > threshold){
    39.   maxV = spectrum[i];
    40.   maxN = i; // maxN is the index of max
    41.   }
    42.   }
    43.   float freqN = maxN; // pass the index to a float variable
    44.   if (maxN > 0 && maxN < qSamples-1){ // interpolate index using neighbours
    45.   var dL = spectrum[maxN-1]/spectrum[maxN];
    46.   var dR = spectrum[maxN+1]/spectrum[maxN];
    47.   freqN += 0.5*(dR*dR - dL*dL);
    48.   }
    49.   pitchValue = freqN*(fSample/2)/qSamples; // convert index to frequency
    50.   }
    51.  
    52.   GUIText display; // drag a GUIText here to show results
    53.  
    54.   void Update () {
    55.   if (Input.GetKeyDown("p")){
    56.   GetComponent<AudioSource>().Play();
    57.   }
    58.   AnalyzeSound();
    59.   if (display){
    60.   display.text = "RMS: "+rmsValue.ToString("F2")+
    61.   " ("+dbValue.ToString("F1")+" dB)\n"+
    62.   "Pitch: "+pitchValue.ToString("F0")+" Hz";
    63.   }
    64.   }
    65.  
    66.  
    just quickly converted it to c#. not sure if it works though
     
  3. KnightRiderGuy

    KnightRiderGuy

    Joined:
    Nov 23, 2014
    Posts:
    514
    Thanks mikeymike,
    I really appreciate that, it gives me a couple of error:

    Assets/Scripts/SpectrumDataText.cs(8,28): error CS0664: Literal of type double cannot be implicitly converted to type `float'. Add suffix `f' to create a literal of this type

    And

    Assets/Scripts/SpectrumDataText.cs(9,30): error CS0664: Literal of type double cannot be implicitly converted to type `float'. Add suffix `f' to create a literal of this type
     
  4. mikeymike

    mikeymike

    Joined:
    Oct 21, 2014
    Posts:
    35
    Code (csharp):
    1.  int qSamples = 1024;  // array size
    2.   float refValue = 0.1f; // RMS value for 0 dB
    3.   float threshold = 0.02f;  // minimum amplitude to extract pitch
    4.   float rmsValue;  // sound level - RMS
    5.   float dbValue;  // sound level - dB
    6.   float pitchValue; // sound pitch - Hz
    7.   private float[] samples; // audio samples
    8.   private float[] spectrum; // audio spectrum
    9.   private float fSample;
    10.  
    11.   void Start () {
    12.    
    13.   samples = new float[qSamples];
    14.   spectrum = new float[qSamples];
    15.   fSample = AudioSettings.outputSampleRate;
    16.   }
    17.  
    18.   void AnalyzeSound(){
    19.   GetComponent<AudioSource>().GetOutputData(samples, 0); // fill array with samples
    20.   int i;
    21.   float sum = 0.0f;
    22.  
    23.   for (i=0; i < qSamples; i++){
    24.   sum += samples[i]*samples[i]; // sum squared samples
    25.   }
    26.   rmsValue = Mathf.Sqrt(sum/qSamples); // rms = square root of average
    27.   dbValue = 20*Mathf.Log10(rmsValue/refValue); // calculate dB
    28.  
    29.   if (dbValue < -160) dbValue = -160; // clamp it to -160dB min
    30.   // get sound spectrum
    31.  
    32.   GetComponent<AudioSource>().GetSpectrumData(spectrum, 0, FFTWindow.BlackmanHarris);
    33.   float maxV = 0.0f;
    34.   int maxN = 0;
    35.  
    36.   for (i=0; i < qSamples; i++){ // find max
    37.   if (spectrum[i] > maxV && spectrum[i] > threshold){
    38.   maxV = spectrum[i];
    39.   maxN = i; // maxN is the index of max
    40.   }
    41.   }
    42.   float freqN = maxN; // pass the index to a float variable
    43.   if (maxN > 0 && maxN < qSamples-1){ // interpolate index using neighbours
    44.   var dL = spectrum[maxN-1]/spectrum[maxN];
    45.   var dR = spectrum[maxN+1]/spectrum[maxN];
    46.   freqN += 0.5*(dR*dR - dL*dL);
    47.   }
    48.   pitchValue = freqN*(fSample/2)/qSamples; // convert index to frequency
    49.   }
    50.  
    51.   GUIText display; // drag a GUIText here to show results
    52.  
    53.   void Update () {
    54.   if (Input.GetKeyDown("p")){
    55.   GetComponent<AudioSource>().Play();
    56.   }
    57.   AnalyzeSound();
    58.   if (display){
    59.   display.text = "RMS: "+rmsValue.ToString("F2")+
    60.   " ("+dbValue.ToString("F1")+" dB)\n"+
    61.   "Pitch: "+pitchValue.ToString("F0")+" Hz";
    62.   }
    63.   }
    64.  
    oh yeah didnt even notice that. that should fix it.
     
  5. KnightRiderGuy

    KnightRiderGuy

    Joined:
    Nov 23, 2014
    Posts:
    514
    That fixed the errors but it's not showing the same stuff in the inspector as the unity script one. So how do I add a UI text to display the Spectrum data? Screen Shot 2015-12-21 at 10.42.42 AM.png
     
  6. mikeymike

    mikeymike

    Joined:
    Oct 21, 2014
    Posts:
    35
    Code (csharp):
    1.  
    2.  
    3.    public int qSamples = 1024;  // array size
    4.    public float refValue = 0.1f; // RMS value for 0 dB
    5.    public float threshold = 0.02f;  // minimum amplitude to extract pitch
    6.    public float rmsValue;  // sound level - RMS
    7.    public float dbValue;  // sound level - dB
    8.    public float pitchValue; // sound pitch - Hz
    9.    private float[] samples; // audio samples
    10.    private float[] spectrum; // audio spectrum
    11.    private float fSample;
    12.  
    13.    void Start () {
    14.  
    15.      samples = new float[qSamples];
    16.      spectrum = new float[qSamples];
    17.      fSample = AudioSettings.outputSampleRate;
    18.    }
    19.  
    20.    void AnalyzeSound(){
    21.      GetComponent<AudioSource>().GetOutputData(samples, 0); // fill array with samples
    22.      int i;
    23.      float sum = 0.0f;
    24.      for (i=0; i < qSamples; i++){
    25.        sum += samples[i]*samples[i]; // sum squared samples
    26.      }
    27.      rmsValue = Mathf.Sqrt(sum/qSamples); // rms = square root of average
    28.      dbValue = 20*Mathf.Log10(rmsValue/refValue); // calculate dB
    29.      if (dbValue < -160) dbValue = -160; // clamp it to -160dB min
    30.      // get sound spectrum
    31.      GetComponent<AudioSource>().GetSpectrumData(spectrum, 0, FFTWindow.BlackmanHarris);
    32.      float maxV = 0.0f;
    33.      int maxN = 0;
    34.      for (i=0; i < qSamples; i++){ // find max
    35.        if (spectrum[i] > maxV && spectrum[i] > threshold){
    36.          maxV = spectrum[i];
    37.          maxN = i; // maxN is the index of max
    38.        }
    39.      }
    40.      float freqN = maxN; // pass the index to a float variable
    41.      if (maxN > 0 && maxN < qSamples-1){ // interpolate index using neighbours
    42.        var dL = spectrum[maxN-1]/spectrum[maxN];
    43.        var dR = spectrum[maxN+1]/spectrum[maxN];
    44.        freqN += 0.5*(dR*dR - dL*dL);
    45.      }
    46.      pitchValue = freqN*(fSample/2)/qSamples; // convert index to frequency
    47.    }
    48.  
    49.    GUIText display; // drag a GUIText here to show results
    50.  
    51.    void Update () {
    52.      if (Input.GetKeyDown("p")){
    53.        GetComponent<AudioSource>().Play();
    54.      }
    55.      AnalyzeSound();
    56.      if (display){
    57.        display.text = "RMS: "+rmsValue.ToString("F2")+
    58.          " ("+dbValue.ToString("F1")+" dB)\n"+
    59.          "Pitch: "+pitchValue.ToString("F0")+" Hz";
    60.      }
    61.    }
    62.  
    63.  
    didnt realise you wanted them to be public variables, fixed now. thats mondays for you
     
    KnightRiderGuy likes this.
  7. KnightRiderGuy

    KnightRiderGuy

    Joined:
    Nov 23, 2014
    Posts:
    514

    Awesome, that fixed that How do I make it so that I can use a UI text to display the spectrum data...? One of these days that is going to sink into my thick skull lol I know what you mean about Monday's ;)
     
  8. mikeymike

    mikeymike

    Joined:
    Oct 21, 2014
    Posts:
    35
    just replace GUIText display; with
    Code (csharp):
    1. public Text display;
    youll need to add the ui namespace at the top of the script too.
    Code (csharp):
    1. using UnityEngine.UI;
     
  9. KnightRiderGuy

    KnightRiderGuy

    Joined:
    Nov 23, 2014
    Posts:
    514
    Awesome, thanks mikeymike, now it's perfect :D
    I really appreciate that man. Code is so not my thing, graphics on the other hand, so if you ever need any graphic help you just let me know ;)