示例#1
0
    /// <summary>
    /// Obtiene el valor medio cuadrático de 1024 muestras obtenidas en el instante.
    /// Con ese valor, calcula el valor en decibelios de la muestra.
    /// </summary>
    /// <returns>Valor en decibelios del micrófono.</returns>
    float getLoudness()
    {
        float[] waveData    = new float[_sampleWindow];
        int     micPosition = MicrophoneManager.GetMicrophonePosition() - (_sampleWindow + 1); // null means the first microphone

        if (micPosition < 0)
        {
            return(0);
        }
        MicrophoneManager.AudioClip.GetData(waveData, micPosition);

        //Normalize(waveData);

        //Root Mean Square value calculation
        float rmsvalue = 0.0f;

        for (int i = 0; i < _sampleWindow; i++)
        {
            rmsvalue += waveData[i] * waveData[i];
        }
        rmsvalue = Mathf.Sqrt(rmsvalue / _sampleWindow);

        float decibels = 20 * Mathf.Log10(rmsvalue / 0.01f);

        return(rmsvalue);
    }
    // Update is called once per frame
    void Update()
    {
        if (!collect)
        {
            return;
        }
        if ((Time.realtimeSinceStartup - actTime) > ClipLength)
        {
            //Get the data from the microphone
            float[] data = new float[window];

            int  position = MicrophoneManager.GetMicrophonePosition() - (window + 1);
            bool success  = MicrophoneManager.AudioClip.GetData(data, position);

            float z = data[0];
            //Parse it to double so python can use it
            double[] doubleArray = Array.ConvertAll(data, x => (double)x);

            if (success)
            {
                //  Debug.Log("Data copied");
                dynamic result = vokaWrapper.vokalculate(doubleArray, MicrophoneManager.SAMPLERATE);

                if (result["Success"])
                {
                    //Debug.Log("Neutrality: "+result["Neutral"]);
                    //Debug.Log("Happiness: " + result["Happy"]);
                    //Debug.Log("Sadness: " + result["Sad"]);
                    //Debug.Log("Anger: " + result["Angry"]);
                    //Debug.Log("Fear: " + result["Fear"]);
                    //Debug.Log("Error msg: "+ result["Error"]);

                    float neutral = (float)result["Neutral"];
                    float happy   = (float)result["Happy"];
                    float sad     = (float)result["Sad"];
                    float angry   = (float)result["Angry"];
                    float fear    = (float)result["Fear"];


                    results.Add(neutral);
                    results.Add(happy);
                    results.Add(sad);
                    results.Add(angry);
                    results.Add(fear);

                    DataManager.instance.AddVokaturi(
                        neutral,
                        happy,
                        sad,
                        angry,
                        fear);
                }
                else
                {
                    results.Add(-1);
                    results.Add(-1);
                    results.Add(-1);
                    results.Add(-1);
                    results.Add(-1);
                    DataManager.instance.AddVokaturi(-1, -1, -1, -1, -1);
                    // Debug.Log(result["Error"]);
                }
            }
            else
            {
                //Debug.Log("Something went wrong while copying the data ");
                results.Add(-1);
                results.Add(-1);
                results.Add(-1);
                results.Add(-1);
                results.Add(-1);
                DataManager.instance.AddVokaturi(-1, -1, -1, -1, -1);
            }
            actTime = Time.realtimeSinceStartup;
        }
    }