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

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.

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";
     }
}
  int qSamples = 1024;  // array size
  float refValue = 0.1; // RMS value for 0 dB
  float threshold = 0.02;  // minimum amplitude to extract pitch
  float rmsValue;  // sound level - RMS
  float dbValue;  // sound level - dB
  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;

  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;
  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.5*(dR*dR - dL*dL);
  }
  pitchValue = freqN*(fSample/2)/qSamples; // convert index to frequency
  }

  GUIText 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";
  }
  }

just quickly converted it to c#. not sure if it works though

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

 int qSamples = 1024;  // array size
  float refValue = 0.1f; // RMS value for 0 dB
  float threshold = 0.02f;  // minimum amplitude to extract pitch
  float rmsValue;  // sound level - RMS
  float dbValue;  // sound level - dB
  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.5*(dR*dR - dL*dL);
  }
  pitchValue = freqN*(fSample/2)/qSamples; // convert index to frequency
  }

  GUIText 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";
  }
  }

oh yeah didnt even notice that. that should fix it.

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?2433284--166772--Screen Shot 2015-12-21 at 10.42.42 AM.png

   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.5*(dR*dR - dL*dL);
     }
     pitchValue = freqN*(fSample/2)/qSamples; // convert index to frequency
   }

   GUIText 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";
     }
   }

didnt realise you wanted them to be public variables, fixed now. thats mondays for you

1 Like

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 :wink:

just replace GUIText display; with public Text display; youll need to add the ui namespace at the top of the script too. using UnityEngine.UI;

Awesome, thanks mikeymike, now it’s perfect :smile:
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 :wink: