Ejemplo n.º 1
0
        /// <summary>
        /// 数量级
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static int Magnitude(this double value)
        {
            if (value.Equals(0.0))
            {
                return(0);
            }
            double magnitude = Math.Log10(Math.Abs(value));
            var    truncated = (int)Math.Truncate(magnitude);

            return(magnitude < 0d && truncated != magnitude
                ? truncated - 1
                : truncated);
        }
Ejemplo n.º 2
0
        public void Refresh()
        {
            AudioSourceVisualizer audioSourceVisualizer;

            try {
                audioSourceVisualizer = target as AudioSourceVisualizer;
            }
            catch (System.Exception e) {
                Debug.Log("Not ready");
                return;
            }
            AudioSource audioSource = audioSourceVisualizer.source;

            //First get the RMS of the audio channel as a whole

            /*float[] intensityBins = new float[AudioSourceVisualizerEditor.samples];
             * audioSource.GetOutputData(intensityBins, 0);
             * float sum = 0;
             * foreach(float sample in intensityBins) {
             *  sum += sample * sample; // sum squared samples
             * }
             * float rms = Mathf.Sqrt(sum / AudioSourceVisualizerEditor.samples); // rms = square root of average*/


            float[] spectrumBins = new float[AudioSourceVisualizerEditor.samples];
            audioSource.GetSpectrumData(spectrumBins, 0, FFTWindow.Rectangular);

            int idx = 0;

            foreach (VisualElement bin in bins)
            {
                double binRange = (double)idx / bins.Length;
                int    binIdx   = (int)Math.Truncate(Math.Pow(10.0, binRange * 3.6 + 1.0) / Math.Pow(10.0, 4.6) * samples);

                float db     = 20 * Mathf.Log10(spectrumBins[binIdx] / 1.0e-6f); // calculate dB
                float linear = Mathf.Clamp(db / 80.0f, 0.0f, 1.0f);

                bin.style.height          = linear * (AudioSourceVisualizerEditor.height - 2) + 2;
                bin.style.backgroundColor = new Color(linear, 68.0f / 255.0f, 136.0f / 255.0f, 1.0f);

                idx++;
            }
        }
Ejemplo n.º 3
0
        public void Refresh()
        {
            BPMSource bpmSource;

            try {
                bpmSource = target as BPMSource;
            }
            catch (System.Exception e) {
                Debug.Log("Not ready");
                return;
            }

            double blinkValue = 1.0 - (bpmSource.beat - Math.Truncate(bpmSource.beat));

            bpmIndicator.style.backgroundColor = new Color(0, 0, (float)blinkValue, 1);

            bpmLabel.text = "BPM " + bpmSource.bpm;

            double beat2         = Math.Truncate(bpmSource.beat * 100.0) / 100.0;
            string beatFormatted = beat2.ToString("n2");

            beatLabel.text = "B# " + beatFormatted;
        }
        public void Refresh()
        {
            AudioSourceVisualizer audioSourceVisualizer;

            try {
                audioSourceVisualizer = target as AudioSourceVisualizer;
            }
            catch (System.Exception e) {
                Debug.Log("Not ready");
                return;
            }
            AudioSource audioSource = audioSourceVisualizer.source;
            AudioClip   clip        = audioSource.clip;

            if (clip == null)
            {
                return; //Not ready
            }

            //First get the RMS of the audio channel as a whole

            /*float[] intensityBins = new float[AudioSourceVisualizerEditor.samples];
             * audioSource.GetOutputData(intensityBins, 0);
             * float sum = 0;
             * foreach(float sample in intensityBins) {
             *  sum += sample * sample; // sum squared samples
             * }
             * float rms = Mathf.Sqrt(sum / AudioSourceVisualizerEditor.samples); // rms = square root of average*/


            float[] spectrumBins = new float[samples];
            audioSource.GetSpectrumData(spectrumBins, 0, FFTWindow.Hanning);

            System.Func <double, float> frequencyIntensity = (double freq) => {
                if (freq > clip.frequency || freq < 0)
                {
                    return(0); //No data for this frequency
                }

                //spectrumBins is in the range 0 - clip.frequency
                int binIdx = (int)Math.Truncate(freq / clip.frequency * samples);
                return(spectrumBins[binIdx]);
            };

            int idx = 0;

            foreach (VisualElement bin in bins)
            {
                double binRange = (double)idx / bins.Length;
                double sampleFreq;
                if (audioSourceVisualizer.useLogScale)
                {
                    double fMin10 = Math.Log10(frequencyMin);
                    double fMax10 = Math.Log10(clip.frequency);
                    //Map from linear space with bins.Length to log space with samples as length
                    sampleFreq = Math.Pow(10.0, binRange * (fMax10 - fMin10) + fMin10);
                }
                else
                {
                    //Linear from frequencyMin to frequencyMax
                    sampleFreq = binRange * (clip.frequency -
                                             frequencyMin) + frequencyMin;
                }

                float intensity = frequencyIntensity(sampleFreq);
                float db        = 20 * Mathf.Log10(intensity / 1.0e-6f); // calculate dB from linear getSpectrumData
                float linear    = Mathf.Clamp(db / 80.0f, 0.0f, 1.0f);

                bin.style.height          = linear * (AudioSourceVisualizerEditor.height - 2) + 2;
                bin.style.backgroundColor = new Color(linear, 68.0f / 255.0f, 136.0f / 255.0f, 1.0f);

                idx++;
            }

            //Update xAxis
            xAxis.useLogScale  = audioSourceVisualizer.useLogScale;
            xAxis.frequencyMin = frequencyMin;
            xAxis.frequencyMax = clip.frequency;
            if (GetVisualizerWidth() != lastWidth)
            {
                xAxis.Redraw(); //Let it know it needs to redraw bc the width changed
            }
            lastWidth = GetVisualizerWidth();
        }