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_*samples*; // 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.().GetSpectrumData(spectrum, 0, FFTWindow.BlackmanHarris);
var maxV: float = 0;
var maxN: int = 0;
for (i=0; i < qSamples; i++){ // find max
if (spectrum > maxV && spectrum > threshold){
maxV = spectrum*;*
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*(dRdR - dLdL);
}
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.().Play();
}
AnalyzeSound();
if (display){
display.text = "RMS: "+rmsValue.ToString(“F2”)+
" (“+dbValue.ToString(“F1”)+” dB)
"+
“Pitch: “+pitchValue.ToString(“F0”)+” Hz”;
}
}
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_*samples*; // 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().GetSpectrumData(spectrum, 0, FFTWindow.BlackmanHarris);*
* float maxV = 0.0f;*
* int maxN = 0;*
* for (i=0; i < qSamples; i++){ // find max*
if (spectrum > maxV && spectrum > threshold){
_ maxV = spectrum*;
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(dRdR - dLdL);
* }
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().Play();*
* }*
* AnalyzeSound();*
* if (display){*
* display.text = "RMS: "+rmsValue.ToString(“F2”)+*
* " (“+dbValue.ToString(“F1”)+” dB)
"+*
* “Pitch: “+pitchValue.ToString(“F0”)+” Hz”;*
* }*
* }*
}