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

Javascript to C# translate

Discussion in 'Scripting' started by lushao, Nov 16, 2015.

  1. lushao

    lushao

    Joined:
    Oct 17, 2015
    Posts:
    27
    Such is the code I got from a friend. Can somebody translate it to me for C # and possibly improve?


    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var qSamples : int = 2048; // QWindow
    4. var channels : int = 2;
    5. var bassMinHz : int = 30; //Min bass Frequency in Hz
    6. var bassMaxHz : int = 120; // Max bass Frequency in Hz
    7. private var bassMinSamples: int ; //Min bass in samples
    8. private var bassMaxSamples : int ; // Max bass in samples
    9. private var spectrumSamples : float[];
    10.  
    11. var moveMultiplier : float = 10;
    12. var audioListener : AudioListener;
    13. var audioisPlaying : boolean = false;
    14.  
    15. var prefab: Enemy_Prefab; // Object to Instantiate
    16.  
    17. function Start()
    18. {
    19.     spectrumSamples = new float[ qSamples];
    20.     bassMinSamples = (bassMinHz * qSamples)/(AudioSettings.outputSampleRate/channels) ;
    21.     bassMaxSamples = (bassMaxHz * qSamples)/(AudioSettings.outputSampleRate/channels) ;
    22. }
    23.  
    24. function Update ()
    25. {
    26.     if( audioListener != null && audioisPlaying == true)
    27.     AnalizeSpectrum() ;
    28. }
    29. function AnalizeSpectrum()
    30. {
    31.     audioListener.GetSpectrumData( spectrumSamples, 0, FFTWindow.Rectangular );
    32.    
    33.     for ( var smplN = 0; smplN < spectrumSamples.Length ; smplN ++ )
    34.     {
    35.         // If the current sample is withing the bass range move the object
    36.         if( smplN >= bassMinSamples && smplN <= bassMaxSamples )
    37.         {
    38.             Instantiate(prefab, new Vector3(Random.Range(-10.0, 10.0), 5, 0), Quaternion.identity) = spectrumSamples[smplN] * moveMultiplier;
    39.         }
    40.     }
    41. }
    42.  
     
  2. Patico

    Patico

    Joined:
    May 21, 2013
    Posts:
    886
    Hey, it's not too hard code, I believe you can do it. If someone makes it instead of you, how do you solve further issues? :)
     
  3. lushao

    lushao

    Joined:
    Oct 17, 2015
    Posts:
    27
    Im completly S*** in Javascript. Im trying, but im S*** xD @Patico
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    var name : type => type name;
    function name () {} => public void name () {}


    should pretty much do the conversion... interestingly I didn't think unityscript allowed "new Vector3(...)"
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    You'll need to add "f" after the floats (in the new Vector3 bit).
     
  6. lushao

    lushao

    Joined:
    Oct 17, 2015
    Posts:
    27
    I have errors with lines [36, 41]. Someone can help?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Engine : MonoBehaviour
    5. {
    6.  
    7.  
    8.     int qSamples = 2048; // QWindow
    9.     int channels = 2;
    10.     int bassMinHz = 30; //Min bass Frequency in Hz
    11.     int bassMaxHz = 120; // Max bass Frequency in Hz
    12.     private int bassMinSamples; //Min bass in samples
    13.     private int bassMaxSamples; // Max bass in samples
    14.     private float[] spectrumSamples;
    15.  
    16.     float moveMultiplier = 10;
    17.     AudioListener Music;
    18.     bool audioisPlaying = false;
    19.  
    20.     public GameObject prefab; // Object to Instantiate
    21.  
    22.     void Start()
    23.     {
    24.         spectrumSamples = new float[qSamples];
    25.         bassMinSamples = (bassMinHz * qSamples) / (AudioSettings.outputSampleRate / channels);
    26.         bassMaxSamples = (bassMaxHz * qSamples) / (AudioSettings.outputSampleRate / channels);
    27.     }
    28.  
    29.     void Update()
    30.     {
    31.         if (Music != null && audioisPlaying == true)
    32.             AnalizeSpectrum();
    33.     }
    34.     void AnalizeSpectrum()
    35.     {
    36.        Music.GetSpectrumData(spectrumSamples, 0, FFTWindow.Rectangular);
    37.  
    38.         for ( int smplN = 0; smplN < spectrumSamples.Length; smplN++)
    39.         {
    40.             // If the current sample is withing the bass range move the object
    41.             if (smplN >= bassMinSamples && smplN <= bassMaxSamples)
    42.             {
    43.                 Instantiate(prefab, new Vector3(Random.Range(-10.0f, 10.0f), 5, 0), Quaternion.identity) = spectrumSamples[smplN] * moveMultiplier;
    44.             }
    45.         }
    46.     }
    47.  
    48. }
    49.  
     
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    The errors may look like gibberish to you, but they really do contain useful information. Please post the error itself.
     
  8. lushao

    lushao

    Joined:
    Oct 17, 2015
    Posts:
    27
    An errors v




     
  9. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    (Protip: You can copy and paste out of the box at the bottom of that)

    36: Change 'Music.GetSpectrumData' to 'AudioListener.GetSpectrumData'. GetSpectrumData is static, which means it's a part of the AudioListener class itself, not of any one AudioListener object. (It's possible that JS is more forgiving and allows you to access static members through an instance?)

    43: I have no idea what the fix is because I have no idea what the intention of that line is, but you definitely can't say "Instantiate(stuff) = anything". You probably want something that looks more like "GameObject spawned = Instantiate(stuff); spawned.GetComponent<Something>().something = anything;" FWIW, that would break in Javascript just as much - that's not a code translation issue, that's a "whoever coded this is clueless" issue.

    Possibly just remove everything from the = sign onwards. On the off chance that JS allows that line to compile, it would simply do nothing.
     
  10. lushao

    lushao

    Joined:
    Oct 17, 2015
    Posts:
    27
    I want spawn object when script detect a low frequency in music.
     
  11. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    I see the if, and the instantiate, which is explained by that intention, but nothing explains anything after the = sign.

    Just delete that.
     
  12. lushao

    lushao

    Joined:
    Oct 17, 2015
    Posts:
    27
    The script I wrote from scratch and work
    But it creates too many objects to one beat
    It should create one object to a single beat
    How can I do that?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Enginev2 : MonoBehaviour
    5. {
    6.     //ZMIENNE
    7.     //Obiekt
    8.     public GameObject objectPrefab;
    9.     //treshold
    10.     public float spawnThreshold = 0.05f;
    11.     //Frequency
    12.     public int frequency = 0;
    13.     //Window
    14.     public FFTWindow fftWindow;
    15.  
    16.    
    17.     //ZMIENNA ILOSC SAMPLI
    18.     private float[] samples = new float[4096]; //MUST BE A POWER OF TWO
    19.    
    20.     void Start()
    21.     {
    22.  
    23.     }
    24.    
    25.     // Update is called once per frame
    26.     void Update ()
    27.     {
    28.         //GETSPECTRUMDATA
    29.         AudioListener.GetSpectrumData(samples, 0, fftWindow);
    30.  
    31.         //Tworzenie Obiektu
    32.         if(samples[frequency] > spawnThreshold)
    33.         {
    34.             Instantiate(objectPrefab, new Vector3(Random.Range(-10.0f, 10.0f), 5, 0), Quaternion.identity);
    35.         }
    36.     }
    37. }
    38.  
    @StarManta
     
  13. Crazy_Man32

    Crazy_Man32

    Joined:
    Nov 19, 2015
    Posts:
    9
    I converted the code to c#. I had to change the for loop to a while and a couple other things but everything should be exactly the same. Try it out and see if it works.

    I also changed the name of the audioListener variable to just Listener, its not good to have a variable that is named so close to a Type method.

    Code (CSharp):
    1. public class Enginev2 : MonoBehaviour
    2. {
    3. public int qSamples= 2048;
    4.  
    5.     public int channels  = 2;
    6.     public int bassMinHz = 30;
    7.     public int bassMaxHz = 120;
    8.     private int  bassMinSamples;
    9.     private  int bassMaxSamples;
    10.     private float[] spectrumSamples;
    11.  
    12.         public float moveMultiplier = 10;
    13.     public AudioListener Listener;
    14.     public bool audioisPlaying;
    15.  
    16.  
    17.     void Start()
    18.     {
    19.         spectrumSamples = new float[ qSamples];
    20.         bassMinSamples = (bassMinHz * qSamples)/(AudioSettings.outputSampleRate/channels) ;
    21.         bassMaxSamples = (bassMaxHz * qSamples)/(AudioSettings.outputSampleRate/channels) ;
    22.     }
    23.  
    24.  
    25.  
    26.     void Update ()
    27.     {
    28.         if( Listener != null && audioisPlaying == true){
    29.             AnalizeSpectrum() ;
    30.         }
    31.     }
    32.  
    33.     void AnalizeSpectrum()
    34.     {
    35.         AudioListener.GetSpectrumData( spectrumSamples, 0, FFTWindow.Rectangular );
    36.  
    37.         int smplN = 0;
    38.  
    39.         while (smplN < spectrumSamples.Length   )
    40.         {
    41.             smplN++;
    42.             // If the current sample is withing the bass range move the object
    43.             if( smplN >= bassMinSamples && smplN <= bassMaxSamples )
    44.             {
    45.  
    46.                 Instantiate(prefab, new Vector3(Random.Range(-10.0, 10.0), 5, 0), Quaternion.identity) = spectrumSamples[smplN] * moveMultiplier;
    47.             }
    48.         }
    49.     }
    50.  
    51.  
    52.  
    53. }
     
    lushao likes this.
  14. lushao

    lushao

    Joined:
    Oct 17, 2015
    Posts:
    27
    @Crazy_Man32 thanks for help, but currently a wrote new script, and dont use this one which you translate