protected int tWASAPI処理(IntPtr buffer, int length, IntPtr user)
        {
            // BASSミキサからの出力データをそのまま WASAPI buffer へ丸投げ。

            int num = Bass.BASS_ChannelGetData(this.hMixer_DeviceOut, buffer, length);                          // num = 実際に転送した長さ

            if (num == -1)
            {
                num = 0;
            }


            // 経過時間を更新。
            // データの転送差分ではなく累積転送バイト数から算出する。

            int n未再生バイト数 = BassWasapi.BASS_WASAPI_GetData(null, (int)BASSData.BASS_DATA_AVAILABLE);                     // 誤差削減のため、必要となるギリギリ直前に取得する。

            this.n経過時間ms            = (this.n累積転送バイト数 - n未再生バイト数) * 1000 / this.nミキサーの1秒あたりのバイト数;
            this.n経過時間を更新したシステム時刻ms = this.tmシステムタイマ.nシステム時刻ms;

            // 実出力遅延を更新。
            // 未再生バイト数の平均値。

            long n今回の遅延ms = n未再生バイト数 * 1000 / this.nミキサーの1秒あたりのバイト数;

            this.n実出力遅延ms    = (this.b最初の実出力遅延算出) ? n今回の遅延ms : (this.n実出力遅延ms + n今回の遅延ms) / 2;
            this.b最初の実出力遅延算出 = false;


            // 経過時間を更新後に、今回分の累積転送バイト数を反映。

            this.n累積転送バイト数 += num;
            return(num);
        }
Ejemplo n.º 2
0
        public bool GetFFTData(float[] fftDataBuffer)
        {
            float[] StereoBuffer = new float[(int)FFT * 2];
            if ((BassWasapi.BASS_WASAPI_GetData(StereoBuffer, (int)FFT * 2)) > 0)
            {
                //if all the values are 0, then don't bother
                if (StereoBuffer.Count(x => x != 0) != 0)
                {
                    float[] YStereoBuffer = new float[(int)FFT * 2];
                    float[] YBuffer       = new float[(int)FFT];
                    FastFourierTransform(StereoBuffer, YStereoBuffer);

                    //average the left and right channels
                    for (int i = 0; i < (int)FFT; i++)
                    {
                        fftDataBuffer[i] = (StereoBuffer[i] + StereoBuffer[(int)FFT * 2 - i - 1]) / 2;
                        YBuffer[i]       = (YStereoBuffer[i] + YStereoBuffer[(int)FFT * 2 - i - 1]) / 2;
                    }

                    for (int i = 0; i < fftDataBuffer.Length / 2; i++)
                    {
                        // Calculate actual intensities for the FFT results.
                        fftDataBuffer[i] = (float)Math.Sqrt(fftDataBuffer[i] * fftDataBuffer[i] + YStereoBuffer[i] * YStereoBuffer[i]) * 4;
                    }
                }

                return(true);
            }
            else
            {
                return(false);
            }
        }
Ejemplo n.º 3
0
        private decimal getData()
        {
            int ret = BassWasapi.BASS_WASAPI_GetData(this.fft, (int)BASSData.BASS_DATA_FFT2048); //get channel fft data

            if (ret < -1)
            {
                return(0m);
            }

            Int32 level = BassWasapi.BASS_WASAPI_GetLevel();

            if (level == this.lastLevel && level != 0)
            {
                this.hanctr++;
            }

            float left = Utils.LowWord32(level);

            //Required, because some programs hang the output. If the output hangs for a 75ms
            //this piece of code re initializes the output so it doesn't make a gliched sound for long.
            if (this.hanctr > 3)
            {
                this.hanctr = 0;
                Free();
                Bass.BASS_Init(0, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero);
                this.initialized = false;
                this.Init();
            }

            // convert to usable percentages
            decimal data = ((decimal)left / 10000 * 100);

            return(data);
        }
Ejemplo n.º 4
0
 public bool GetFFTData(float[] fftDataBuffer)
 {
     if (!BassWasapi.BASS_WASAPI_IsStarted())
     {
         return(false);
     }
     return(BassWasapi.BASS_WASAPI_GetData(fftDataBuffer, _maxFFT) > 0);
 }
Ejemplo n.º 5
0
 public bool GetWaveData32(int length, out float[] waveData32)
 {
     waveData32 = null;
     if (!BassWasapi.BASS_WASAPI_IsStarted())
     {
         return(false);
     }
     waveData32 = new float[length];
     return(BassWasapi.BASS_WASAPI_GetData(waveData32, length) == (int)BASSError.BASS_OK);
 }
Ejemplo n.º 6
0
        //timer
        public void _t_Tick(object sender, EventArgs e)
        {
            // get fft data. Return value is -1 on error
            int ret = BassWasapi.BASS_WASAPI_GetData(_fft, (int)BASSData.BASS_DATA_FFT2048);

            if (ret < 0)
            {
                return;
            }
            int x, y;
            int b0 = 0;

            //computes the spectrum data, the code is taken from a bass_wasapi sample.
            for (x = 0; x < _lines; x++)
            {
                float peak = 0;
                int   b1   = (int)Math.Pow(2, x * 10.0 / (_lines - 1));
                if (b1 > 1023)
                {
                    b1 = 1023;
                }
                if (b1 <= b0)
                {
                    b1 = b0 + 1;
                }
                for (; b0 < b1; b0++)
                {
                    if (peak < _fft[1 + b0])
                    {
                        peak = _fft[1 + b0];
                    }
                }
                y = (int)(Math.Sqrt(peak) * 3 * 255 - 4);
                if (y > 255)
                {
                    y = 255;
                }
                if (y < 0)
                {
                    y = 0;
                }
                _spectrumdata.Add((byte)y);
            }

            MainWindow.ActiveWindow.modeMusic.SetIntensity(_spectrumdata);
            _spectrumdata.Clear();

            int level = BassWasapi.BASS_WASAPI_GetLevel();

            if (level == _lastlevel && level != 0)
            {
                _hanctr++;
            }
            _lastlevel = level;
        }
Ejemplo n.º 7
0
        private void solidDiscoTimer_Tick(object sender, EventArgs e)
        {
            gfxSolidDisco.Clear(Color.FromArgb(255, 21, 32, 54));
            BassWasapi.BASS_WASAPI_GetData(_fft, (int)BASSData.BASS_DATA_FFT2048); //get channel fft data

            int y    = 0;
            int b0   = 0;
            int posX = 15;

            for (int x = 0; x < 16; x++)
            {
                float peak = 0;
                int   b1   = (int)Math.Pow(2, x * 10.0 / (16 - 1));

                if (b1 > 1023)
                {
                    b1 = 1023;
                }
                if (b1 <= b0)
                {
                    b1 = b0 + 1;
                }
                for (; b0 < b1; b0++)
                {
                    if (peak < _fft[1 + b0])
                    {
                        peak = _fft[1 + b0];
                    }
                }
                y = (int)(Math.Sqrt(peak) * 3 * 255 - 4);
                if (y > 255)
                {
                    y = 255;
                }
                if (y < 0)
                {
                    y = 0;
                }
                //draw lines
                if (x >= colorslider_simple.SelectedMin && x < colorslider_simple.SelectedMax)
                {
                    penSolidDisco.Color = colorslider_simple.color.Color;
                }
                else
                {
                    penSolidDisco.Color = Color.Black;
                }

                gfxSolidDisco.DrawLine(penSolidDisco, new Point(posX, bmpSolidDisco.Height), new Point(posX, bmpSolidDisco.Height - (y / 2)));
                posX += 25;
            }
            SolidDiscoVisualizer.Image = bmpSolidDisco;
        }
Ejemplo n.º 8
0
        //timer
        private void _t_Tick(object sender, EventArgs e)
        {
            int ret = BassWasapi.BASS_WASAPI_GetData(_fft, (int)BASSData.BASS_DATA_FFT8192);  //get channel fft data

            if (ret < -1)
            {
                return;
            }
            int x, y;
            int b0 = 0;

            //computes the spectrum data, the code is taken from a bass_wasapi sample.
            _spectrumdata.Clear();
            for (x = 0; x < _lines; x++)
            {
                float peak = 0;
                int   b1   = (int)Math.Pow(2, x * 10.0 / (_lines - 1));
                if (b1 > 1023)
                {
                    b1 = 1023;
                }
                if (b1 <= b0)
                {
                    b1 = b0 + 1;
                }
                for (; b0 < b1; b0++)
                {
                    if (peak < _fft[1 + b0])
                    {
                        peak = _fft[1 + b0];
                    }
                }
                y = (int)(Math.Sqrt(peak) * 3 * 255 - 4);
                if (y > 255)
                {
                    y = 255;
                }
                if (y < 0)
                {
                    y = 0;
                }
                _spectrumdata.Add((byte)y);
            }

            if (DisplayEnable)
            {
                foreach (var visualizer in _spectrumVisualizer)
                {
                    visualizer.Set(_spectrumdata.ToArray());
                }
            }
        }
        public List <float> GetSpectrum()
        {
            int ret = BassWasapi.BASS_WASAPI_GetData(_fft, (int)BASSData.BASS_DATA_FFT8192);

            if (ret < -1)
            {
                return(_spectrumdata);
            }
            else
            {
                int   x;
                float y;
                int   b0 = 0;

                for (x = 0; x < _lines; x++)
                {
                    float peak = 0;
                    int   b1   = (int)Math.Pow(2, x * 10.0 / (_lines - 1));
                    if (b1 > 1023)
                    {
                        b1 = 1023;
                    }
                    if (b1 <= b0)
                    {
                        b1 = b0 + 1;
                    }
                    for (; b0 < b1; b0++)
                    {
                        if (peak < _fft[1 + b0])
                        {
                            peak = _fft[1 + b0];
                        }
                    }

                    y = peak * multiplier;
                    if (y < 0)
                    {
                        y = 0;
                    }
                    _spectrumdata[x] = y;
                }

                return(new List <float>(_spectrumdata));
            }
        }
Ejemplo n.º 10
0
        //private void ResetCapture()
        //{
        //    _updateSpectrumDispatcherTimer.Stop();
        //    BassWasapi.BASS_WASAPI_Stop(true);
        //    BassWasapi.BASS_WASAPI_Free();
        //    Un4seen.Bass.Bass.BASS_Free();
        //}

        private void UpdateSpectrum(object sender, EventArgs e)
        {
            SpectrumData.Clear();

            var dataSet = BassWasapi.BASS_WASAPI_GetData(_fft, (int)BASSData.BASS_DATA_FFT8192);

            if (dataSet < -1)
            {
                return;
            }

            var bindex = 0; // sorry for this one

            for (var l = 0; l < Lines; l++)
            {
                float peak    = 0;
                var   binSize = (int)Math.Pow(2, l * 10.0 / (Lines - 1));
                if (binSize > 1023)
                {
                    binSize = 1023;                             // max binSize
                }
                if (binSize <= bindex)
                {
                    binSize = bindex + 1;                       // min binSize
                }
                for (; bindex < binSize; bindex++)              // select peak
                {
                    if (peak < _fft[1 + bindex])
                    {
                        peak = _fft[1 + bindex];
                    }
                }
                var value = (int)(Math.Sqrt(peak) * 3 * Normal - 4);
                if (value > Normal)
                {
                    value = Normal;
                }
                if (value < 0)
                {
                    value = 0;
                }
                SpectrumData.Add(value);
            }
        }
Ejemplo n.º 11
0
        // Performs FFT analysis in order to detect beat
        private void PerformAnalysis()
        {
            // Specifes on which result end which band (dividing it into 10 bands)
            // 19 - bass, 187 - mids, rest is highs
            int[]    BandRange = { 4, 8, 18, 38, 48, 94, 140, 186, 466, 1022, 22000 };
            double[] BandsTemp = new double[BANDS];
            int      n         = 0;
            int      level     = BassWasapi.BASS_WASAPI_GetLevel();

            // Get FFT
            int ret = BassWasapi.BASS_WASAPI_GetData(_FFTData, (int)BASSData.BASS_DATA_FFT1024 | (int)BASSData.BASS_DATA_FFT_COMPLEX); //get channel fft data

            if (ret < -1)
            {
                return;
            }

            // Calculate the energy of every result and divide it into subbands
            float sum = 0;

            for (int i = 2; i < 2048; i = i + 2)
            {
                float real    = _FFTData[i];
                float complex = _FFTData[i + 1];
                sum += (float)Math.Sqrt((double)(real * real + complex * complex));

                if (i == BandRange[n])
                {
                    BandsTemp[n++] = (BANDS * sum) / 1024;
                    sum            = 0;
                }
            }

            // Detect beat basing on FFT results
            DetectBeat(BandsTemp, level);

            // Shift the history register and save new values
            ShiftHistory(1);

            for (int i = 0; i < BANDS; i++)
            {
                _History[i, 0] = BandsTemp[i];
            }
        }
Ejemplo n.º 12
0
        private void OnDispatcherTimerTick(object sender, EventArgs e)
        {
            if (BassWasapi.BASS_WASAPI_GetData(inputBuffer, (int)BASSData.BASS_DATA_FFT8192) < -1)
            {
                return;
            }

            int b0 = 0;

            for (int channel = 0; channel < Channels; channel++)
            {
                float peak = 0;
                int   b1   = (int)Math.Pow(2, channel * 10.0 / (Channels - 1));

                if (b1 > 1023)
                {
                    b1 = 1023;
                }

                if (b1 <= b0)
                {
                    b1 = b0 + 1;
                }

                while (b0 < b1)
                {
                    if (peak < inputBuffer[1 + b0])
                    {
                        peak = inputBuffer[1 + b0];
                    }

                    b0++;
                }

                SpectrumData[channel] = (byte)Math.Min(255, Math.Max(0, (int)(Math.Sqrt(peak) * 3 * 255 - 4)));
            }

            Tick?.Invoke(this, EventArgs.Empty);
        }
Ejemplo n.º 13
0
        //timer
        private void _t_Tick(object sender, EventArgs e)
        {
            // get fft data. Return value is -1 on error
            int ret = BassWasapi.BASS_WASAPI_GetData(_fft, (int)BASSData.BASS_DATA_FFT2048);

            if (ret < 0)
            {
                return;
            }

            OnAudioAvailable(_fft);
            _spectrumdata.Clear();


            int level = BassWasapi.BASS_WASAPI_GetLevel();

            _l = Utils.LowWord32(level);
            _r = Utils.HighWord32(level);
            if (level == _lastlevel && level != 0)
            {
                _hanctr++;
            }
            _lastlevel = level;

            //Required, because some programs hang the output. If the output hangs for a 75ms
            //this piece of code re initializes the output
            //so it doesn't make a gliched sound for long.
            if (_hanctr > 3)
            {
                _hanctr = 0;
                _l      = 0;
                _r      = 0;
                Free();
                Bass.BASS_Init(0, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero);
                _initialized = false;
                Enable       = true;
            }
        }
Ejemplo n.º 14
0
        //timer
        private void OnTimerTick(object sender, EventArgs e)
        {
            // get fft data. Return value is -1 on error
            var ret = BassWasapi.BASS_WASAPI_GetData(_fft, (int)BASSData.BASS_DATA_FFT2048);

            if (ret < 0)
            {
                return;
            }

            OnAudioAvailable(_fft);
            _spectrumdata.Clear();


            var level = BassWasapi.BASS_WASAPI_GetLevel();

            leftChannelIntensity  = Utils.LowWord32(level);
            rightChannelIntensity = Utils.HighWord32(level);
            if (level == lastOutLevel && level != 0)
            {
                lastOutputLevelCounter++;
            }

            lastOutLevel = level;

            //Required, because some programs hang the output. If the output hangs for a 75ms
            //this piece of code re initializes the output
            //so it doesn't make a gliched sound for long.

            if (lastOutputLevelCounter > 3)
            {
                lastOutputLevelCounter = 0;
                leftChannelIntensity   = 0;
                rightChannelIntensity  = 0;
            }
        }
Ejemplo n.º 15
0
        private void _t_Tick(object sender, EventArgs e)
        {
            // get fft data. Return value is -1 on error
            int ret = BassWasapi.BASS_WASAPI_GetData(_fft, (int)BASSData.BASS_DATA_FFT2048);

            if (ret < 0)
            {
                return;
            }
            int x, y;
            int b0 = 0;

            //computes the spectrum data, the code is taken from a bass_wasapi sample.
            for (x = 0; x < _lines; x++)
            {
                float peak = 0;
                int   b1   = (int)Math.Pow(2, x * 10.0 / (_lines - 1));
                if (b1 > 1023)
                {
                    b1 = 1023;
                }
                if (b1 <= b0)
                {
                    b1 = b0 + 1;
                }
                for (; b0 < b1; b0++)
                {
                    if (peak < _fft[1 + b0])
                    {
                        peak = _fft[1 + b0];
                    }
                }
                y = (int)(Math.Sqrt(peak) * 3 * 255 - 4);
                if (y > 255)
                {
                    y = 255;
                }
                if (y < 0)
                {
                    y = 0;
                }
                spectrumdata.Add((byte)y);
            }

            // use (_spectrumdata)
            // it's an array of values 0-255
            spectrumdataHistory.Add(new List <byte>(spectrumdata));
            if (spectrumdataHistory.Count > spectrumdataHistoryLength)
            {
                // If the list gets bigger than set number, remove oldest entry
                spectrumdataHistory.RemoveAt(0);
            }
            HandleSpectrumDataHistory(spectrumdataHistory);
            spectrumdata.Clear();


            //int level = BassWasapi.BASS_WASAPI_GetLevel();


            //Required, because some programs hang the output. If the output hangs for a 75ms
            //this piece of code re initializes the output
            //so it doesn't make a gliched sound for long.
            if (_hanctr > 3)
            {
                _hanctr = 0;
                Free();
                Bass.BASS_Init(0, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero);
                _initialized = false;
                Enable(0);
            }
        }
Ejemplo n.º 16
0
        public List <byte> _getSpectrum()
        {
            _spectrumdata.Clear();

            int ret = BassWasapi.BASS_WASAPI_GetData(_fft, (int)BASSData.BASS_DATA_FFT2048);  //get channel fft data//default BASS_DATA_FFT2048

            if (ret < -1)
            {
                return(_spectrumdata);
            }
            int x, y;
            int b0 = 0;

            //computes the spectrum data, the code is taken from a bass_wasapi sample.
            for (x = 0; x < _lines; x++)
            {
                float peak = 0;
                int   b1   = (int)Math.Pow(2, x * 10.0 / (_lines - 1));
                if (b1 > 1023)
                {
                    b1 = 1023;
                }
                if (b1 <= b0)
                {
                    b1 = b0 + 1;
                }
                for (; b0 < b1; b0++)
                {
                    if (peak < _fft[1 + b0])
                    {
                        peak = _fft[1 + b0];
                    }
                }
                y = (int)(Math.Sqrt(peak) * 3 * 255 - 4);
                if (y > 255)
                {
                    y = 255;
                }
                if (y < 0)
                {
                    y = 0;
                }
                _spectrumdata.Add((byte)y);
            }
            _spectrum.Set(_spectrumdata);

            int level = BassWasapi.BASS_WASAPI_GetLevel();

            if (level == _lastlevel && level != 0)
            {
                _hanctr++;
            }
            _lastlevel = level;

            //Required, because some programs hang the output. If the output hangs for a 75ms
            //this piece of code re initializes the output so it doesn't make a gliched sound for long.
            if (_hanctr >= 3)
            {
                _hanctr = 0;
                Free();
                Bass.BASS_Init(0, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero);
                _initialized = false;
                Enable       = true;
            }

            return(_spectrumdata);
        }
Ejemplo n.º 17
0
        //timer
        private void _t_Tick(object sender, EventArgs e)
        {
            int ret = BassWasapi.BASS_WASAPI_GetData(_fft, (int)BASSData.BASS_DATA_FFT8192);  //get ch.annel fft data

            if (ret < -1)
            {
                return;
            }
            int x, y;
            int b0 = 0;

            //computes the spectrum data, the code is taken from a bass_wasapi sample.
            for (x = 0; x < _lines; x++)
            {
                float peak = 0;
                int   b1   = (int)Math.Pow(2, x * 10.0 / (_lines - 1));
                if (b1 > 1023)
                {
                    b1 = 1023;
                }
                if (b1 <= b0)
                {
                    b1 = b0 + 1;
                }
                for (; b0 < b1; b0++)
                {
                    if (peak < _fft[1 + b0])
                    {
                        peak = _fft[1 + b0];
                    }
                }
                y = (int)(Math.Sqrt(peak) * 3 * 255 - 4);
                if (y > 255)
                {
                    y = 255;
                }
                if (y < 0)
                {
                    y = 0;
                }
                _spectrumdata.Add((byte)y);
                //Console.Write("{0, 3} ", y);
            }

            if (DisplayEnable)
            {
                _spectrum.Set(_spectrumdata);
            }
            for (int i = 0; i < _spectrumdata.ToArray().Length; i++)
            {
                try
                {
                    _chart.Series["wave"].Points.Add(_spectrumdata[i]);
                }
                catch (Exception)
                {
                }
                try
                {
                    _chart.Series["wave"].Points.RemoveAt(0);
                }
                catch (Exception)
                {
                }
            }
            _spectrumdata.Clear();


            int level = BassWasapi.BASS_WASAPI_GetLevel();

            _l.Value = (Utils.LowWord32(level));
            _r.Value = (Utils.HighWord32(level));
            if (level == _lastlevel && level != 0)
            {
                _hanctr++;
            }
            _lastlevel = level;

            //Required, because some programs hang the output. If the output hangs for a 75ms
            //this piece of code re initializes the output so it doesn't make a gliched sound for long.
            if (_hanctr > 3)
            {
                _hanctr  = 0;
                _l.Value = (0);
                _r.Value = (0);
                Free();
                Bass.BASS_Init(0, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero);
                _initialized = false;
                Enable       = true;
            }
        }
Ejemplo n.º 18
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (BassWasapi.BASS_WASAPI_GetData(fft, (int)BASSData.BASS_DATA_FFT8192) < -1)
            {
                return;
            }
            int num1 = 0;

            if (isChanging)
            {
                int colorToChange = rnd.Next(0, 3);
                if (colorToChange == 0)
                {
                    if (r < 10)
                    {
                        r += diff;
                    }
                    else if (r > 245 || rnd.Next(0, 2) == 1)
                    {
                        r -= diff;
                    }
                    else
                    {
                        r += diff;
                    }
                }
                if (colorToChange == 1)
                {
                    if (g < 10)
                    {
                        g += diff;
                    }
                    else if (g > 245 || rnd.Next(0, 2) == 1)
                    {
                        g -= diff;
                    }
                    else
                    {
                        g += diff;
                    }
                }
                if (colorToChange == 2)
                {
                    if (b < 10)
                    {
                        b += diff;
                    }
                    else if (b > 245 || rnd.Next(0, 2) == 1)
                    {
                        b -= diff;
                    }
                    else
                    {
                        b += diff;
                    }
                }
            }
            else if (isRainbow)
            {
                if (rainb == 306)
                {
                    rainb = 0;
                }
                if (rainb >= 0 && rainb < 51)
                {
                    g += 5;
                }
                else if (rainb < 102)
                {
                    r -= 5;
                }
                else if (rainb < 153)
                {
                    b += 5;
                }
                else if (rainb < 204)
                {
                    g -= 5;
                }
                else if (rainb < 255)
                {
                    r += 5;
                }
                else if (rainb < 306)
                {
                    b -= 5;
                }
                rainb++;
            }
            for (int i = 0; i < _lines; i++)
            {
                float max_val = 0;
                int   value   = (int)Math.Pow(2, i * 10.0 / (_lines - 1));
                if (value > 1023)
                {
                    value = 1023;
                }
                if (value <= num1)
                {
                    value = num1 + 1;
                }
                for (; num1 < value; ++num1)
                {
                    if (max_val < fft[1 + num1])
                    {
                        max_val = fft[1 + num1];
                    }
                }
                int num4 = (int)(Math.Sqrt(max_val) * 3 * 255 - 4.0);
                if (num4 < 0)
                {
                    num4 = 0;
                }
                if (num4 > 315)
                {
                    num4 = 315;
                }
                byt1[i] = num4 * 6;
                byt1[_lines * 2 - i - 1] = num4 * 6;
            }
            int max;

            if (isReflected)
            {
                max = _lines * 2 - 1;
            }
            else
            {
                max = _lines;
            }
            for (int i = 0; i < max; i++)
            {
                try
                {
                    chart1.Series["wave"].Color = Color.FromArgb(a, r, g, b);
                    chart1.ChartAreas["ChartArea1"].BackColor = Color.FromArgb(ba, br, bg, bb);
                    chart1.Series["wave"].Points.Add(byt1[i]);
                    chart1.Series["wave"].Points.RemoveAt(0);
                }
                catch (Exception)
                {
                    if (a > 255 || a < 0)
                    {
                        a = 255;
                    }
                    if (r > 255 || r < 0)
                    {
                        r = 0;
                    }
                    if (g > 255 || g < 0)
                    {
                        g = 0;
                    }
                    if (b > 255 || b < 0)
                    {
                        b = 0;
                    }
                    if (ba > 255 || ba < 0)
                    {
                        ba = 255;
                    }
                    if (br > 255 || br < 0)
                    {
                        br = 255;
                    }
                    if (bg > 255 || bg < 0)
                    {
                        bg = 255;
                    }
                    if (bb > 255 || bb < 0)
                    {
                        bb = 255;
                    }
                }
            }
        }
        private void timerTick(object sender, EventArgs e)
        {
            int ret = BassWasapi.BASS_WASAPI_GetData(_fft, (int)BASSData.BASS_DATA_FFT2048);

            if (ret < -1)
            {
                return;
            }
            int x, y;
            int b0 = 0;

            //8 = Launchpad line count
            for (x = 0; x < 8; x++)
            {
                float peak = 0;
                int   b1   = (int)Math.Pow(2, x * 10.0 / (8 - 1));
                if (b1 > 1023)
                {
                    b1 = 1023;
                }
                if (b1 <= b0)
                {
                    b1 = b0 + 1;
                }
                for (; b0 < b1; b0++)
                {
                    if (peak < _fft[1 + b0])
                    {
                        peak = _fft[1 + b0];
                    }
                }
                y = (int)(Math.Sqrt(peak) * 3 * 255 - 4);
                if (y > 255)
                {
                    y = 255;
                }
                if (y < 0)
                {
                    y = 0;
                }
                _spectrumdata.Add((byte)y);
            }

            //Software Visualization
            if (useSoftwareVis)
            {
                _vis.SetData(_spectrumdata);
            }

            //Launchpad Serialization here
            if (_lInt != null)
            {
                for (int i = 0; i < 8; i++)
                {
                    _val  = ((float)_spectrumdata[i] / 255) * 8;
                    _ledY = _val >= 8 ? 7 : _val;
                    int v = GetVelocityForVolume(_spectrumdata[i]);

                    for (int tX = 1; tX <= 8; tX++)
                    {
                        for (int tY = 1; tY <= 8; tY++)
                        {
                            int veloAtThisPoint = leds[tX - 1, tY - 1];
                            if (tX == i + 1 && tY == 8 - (int)_ledY)
                            {
                                _lInt.fillLEDs(tX, 8 - (int)_ledY, tX, 8, v);     //Write Color Track
                                if (8 - (int)_ledY != 8 && 8 - (int)_ledY != 1)   //Write it only if it is not 8th or 1st LED (which causes flickering)
                                {
                                    _lInt.fillLEDs(tX, 1, tX, 8 - (int)_ledY, 0); //Write zero track (inverse of color track)
                                }
                                leds[tX - 1, tY - 1] = v;
                            }
                        }
                    }
                }
            }

            _spectrumdata.Clear();

            int level = BassWasapi.BASS_WASAPI_GetLevel();

            if (level == _lastlevel && level != 0)
            {
                _hanctr++;
            }
            _lastlevel = level;

            if (_hanctr > 3)
            {
                _hanctr = 0;
                Free();
                Bass.BASS_Init(0, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero);
                _initialized = false;
                Enable       = true;
            }
        }
Ejemplo n.º 20
0
        //timer
        private void _t_Tick(object sender, EventArgs e)
        {
            int ret = BassWasapi.BASS_WASAPI_GetData(_fft, (int)BASSData.BASS_DATA_FFT8192);      //get ch.annel fft data

            if (ret < -1)
            {
                return;
            }
            int x, y;
            int b0 = 0;

            //computes the spectrum data, the code is taken from a bass_wasapi sample.
            for (x = 0; x < 64; x++)
            {
                float peak = 0;
                int   b1   = (int)Math.Pow(2, x * 10.0 / (_lines - 1));
                if (b1 > 1023)
                {
                    b1 = 1023;
                }
                if (b1 <= b0)
                {
                    b1 = b0 + 1;
                }
                for (; b0 < b1; b0++)
                {
                    if (peak < _fft[1 + b0])
                    {
                        peak = _fft[1 + b0];
                    }
                }
                y = (int)(Math.Sqrt(peak) * 3 * 255 - 4);
                if (y > 255)
                {
                    y = 255;
                }
                if (y < 0)
                {
                    y = 0;
                }
                _spectrumdata.Add((byte)y);
                //Console.WriteLine("{0, 3} ", y);
            }



            if (DisplayEnable)
            {
                _spectrum.Set(_spectrumdata);
            }
            for (int i = 0; i < _spectrumdata.ToArray().Length; i++)
            {
                try
                {
                    _chart.Series["wave"].Points.Add(_spectrumdata[i]);
                }
                catch (Exception)
                {
                }
                try
                {
                    _chart.Series["wave"].Points.RemoveAt(0);
                }
                catch (Exception)
                {
                }
            }

            if (!port.IsOpen)
            {
                try
                {
                    int dataforduino = 0;
                    if (high_low == 1)
                    {
                        dataforduino = (_spectrumdata[0] + _spectrumdata[1] + _spectrumdata[2] + _spectrumdata[1]) * 2;
                    }
                    else if (high_low == 2)
                    {
                        dataforduino = (_spectrumdata[11] + _spectrumdata[11] + _spectrumdata[12] + _spectrumdata[12]) * 2;
                    }
                    else if (high_low == 3)
                    {
                        dataforduino = (_spectrumdata[30] + _spectrumdata[30] + _spectrumdata[32] + _spectrumdata[32]) * 2;
                    }
                    dataforduino = dataforduino / 4;

                    port.Open();

                    if (_red_btn.Checked == true)
                    {
                        port.Write(dataforduino + ",0,20,0\n");
                    }
                    else if (_green_btn.Checked == true)
                    {
                        port.Write("0," + dataforduino + ",20,0\n");
                    }
                    else if (_blue_btn.Checked == true)
                    {
                        port.Write("0,20," + dataforduino + ",0\n");
                    }
                    else if (_yellow_btn.Checked == true)
                    {
                        port.Write(dataforduino + "," + dataforduino + ",0,0\n");
                    }
                    else if (_cyan_btn.Checked == true)
                    {
                        port.Write("0," + dataforduino + "," + dataforduino + ",0\n");
                    }
                    else if (_magenta_btn.Checked == true)
                    {
                        port.Write(dataforduino + ",0," + dataforduino + ",0\n");
                    }
                    else if (_white_btn.Checked == true)
                    {
                        port.Write(dataforduino + "," + dataforduino + "," + dataforduino + ",0\n");
                    }
                    else if (_fade_btn.Checked == true)
                    {
                        decimal color_fade = dataforduino;
                        if (fade_durchlauf_ganz == 0)
                        {
                            fade_durchlauf++;
                            if (fade_durchlauf == 256)
                            {
                                fade_durchlauf      = 0;
                                fade_durchlauf_ganz = 1;
                            }
                            else
                            {
                                port.Write(Math.Round((color_fade / 255) * (255 - fade_durchlauf), 0) + "," + Math.Round((color_fade / 255) * fade_durchlauf, 0) + "," + "0" + ",0\n");
                            }
                        }
                        else if (fade_durchlauf_ganz == 1)
                        {
                            fade_durchlauf++;
                            if (fade_durchlauf == 256)
                            {
                                fade_durchlauf      = 0;
                                fade_durchlauf_ganz = 2;
                            }
                            else
                            {
                                port.Write("0" + "," + Math.Round((color_fade / 255) * (255 - fade_durchlauf), 0) + "," + Math.Round((color_fade / 255) * fade_durchlauf, 0) + ",0\n");
                            }
                        }
                        else if (fade_durchlauf_ganz == 2)
                        {
                            fade_durchlauf++;
                            if (fade_durchlauf == 256)
                            {
                                fade_durchlauf      = 0;
                                fade_durchlauf_ganz = 0;
                            }
                            else
                            {
                                port.Write(Math.Round((color_fade / 255) * fade_durchlauf, 0) + "," + "0" + "," + Math.Round((color_fade / 255) * (255 - fade_durchlauf), 0) + ",0\n");
                            }
                        }
                    }

                    port.Close();
                }
                catch
                {
                }
            }

            _spectrumdata.Clear();


            int level = BassWasapi.BASS_WASAPI_GetLevel();

            _l.Value = (Utils.LowWord32(level));
            _r.Value = (Utils.HighWord32(level));
            if (level == _lastlevel && level != 0)
            {
                _hanctr++;
            }
            _lastlevel = level;

            //Required, because some programs hang the output. If the output hangs for a 75ms
            //this piece of code re initializes the output so it doesn't make a gliched sound for long.
            if (_hanctr > 3)
            {
                _hanctr  = 0;
                _l.Value = (0);
                _r.Value = (0);
                Free();
                Bass.BASS_Init(0, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero);
                _initialized = false;
                Enable       = true;
            }
        }
Ejemplo n.º 21
0
        private void _t_Tick(object sender, EventArgs e)
        {
            int ret = BassWasapi.BASS_WASAPI_GetData(_fft, (int)BASSData.BASS_DATA_FFT2048); //get channel fft data

            if (ret < -1)
            {
                return;
            }
            int x, y;
            int b0 = 0;

            //computes the spectrum data, the code is taken from a bass_wasapi sample.
            for (x = 0; x < _lines; x++)
            {
                float peak = 0;
                int   b1   = (int)Math.Pow(2, x * 10.0 / (_lines - 1));
                if (b1 > 1023)
                {
                    b1 = 1023;
                }
                if (b1 <= b0)
                {
                    b1 = b0 + 1;
                }
                for (; b0 < b1; b0++)
                {
                    if (peak < _fft[1 + b0])
                    {
                        peak = _fft[1 + b0];
                    }
                }
                y = (int)(Math.Sqrt(peak) * 3 * 255 - 4);
                if (y > 255)
                {
                    y = 255;
                }
                if (y < 0)
                {
                    y = 0;
                }
                _spectrumdata.Add((byte)y);
            }

            if (DisplayEnable)
            {
                _spectrum.Set(_spectrumdata);
            }

            if (skip >= skipper)
            {
                if (activeAlgo != null && _rgbOutput != null && _rgbOutput.IsEnabled())
                {
                    activeAlgo.showRGB(_spectrumdata.ToArray(), (int)_mrs.RangeMin, (int)_mrs.RangeMax, minSliderValue, absNotRel, _rgbOutput);  // Now show Audio_RGB-Algorithm
                }
                skip = 0;
            }
            skip++;
            _spectrumdata.Clear();


            int level = BassWasapi.BASS_WASAPI_GetLevel();

            _l.Value = Utils.LowWord32(level);
            _r.Value = Utils.HighWord32(level);
            if (level == _lastlevel && level != 0)
            {
                _hanctr++;
            }
            _lastlevel = level;

            //Required, because some programs hang the output. If the output hangs for a 75ms
            //this piece of code re initializes the output so it doesn't make a gliched sound for long.
            if (_hanctr > 3)
            {
                _hanctr  = 0;
                _l.Value = 0;
                _r.Value = 0;
                Free();
                Bass.BASS_Init(0, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero);
                _initialized = false;
                Enable       = true;
            }
        }
Ejemplo n.º 22
0
        private void GetBarData()
        {
            //Get FFT data
            BassWasapi.BASS_WASAPI_GetData(_fftData, (int)_maxFft);
            int barIndex = 0;

            //Calculate bar values by squaring fftData from log(x) fft bin, and multiply by a few magic values to end up with a somewhat reasonable graphical representation of the sound
            for (barIndex = 0; barIndex < BAR_COUNT; barIndex++)
            {
                _barValues[barIndex] = ((float)Math.Sqrt(_fftData[_logIndex[barIndex]]) * 15f * VolumeScalar) * _freqVolScalar[barIndex];
            }
            barIndex = 0;

            //This chunk of code is supposed to do a rolling average to smooth out the lower values to look cleaner for another visualizer i'm working on
            float preScaled;

            float preScaled1 = _barValues[barIndex];

            preScaled1         += _barValues[barIndex + 1];
            preScaled1         /= 2f;
            BarValues[barIndex] = Utility.Clamp(preScaled1, 0f, 1f);

            barIndex++;

            preScaled1          = _barValues[barIndex - 1] * 0.75f;
            preScaled1         += _barValues[barIndex];
            preScaled1         += _barValues[barIndex + 1] * 0.75f;
            preScaled1         /= 2.5f;
            BarValues[barIndex] = Utility.Clamp(preScaled1, 0f, 1f);

            for (barIndex = 2; barIndex < 50; barIndex++)
            {
                preScaled           = _barValues[barIndex - 2] * 0.5f;
                preScaled          += _barValues[barIndex - 1] * 0.75f;
                preScaled          += _barValues[barIndex];
                preScaled          += _barValues[barIndex + 1] * 0.75f;
                preScaled          += _barValues[barIndex + 2] * 0.5f;
                preScaled          /= 3.5f;
                BarValues[barIndex] = Utility.Clamp(preScaled, 0f, 1f);
            }
            for (barIndex = 50; barIndex < 999; barIndex++)
            {
                preScaled           = _barValues[barIndex - 1] * 0.75f;
                preScaled          += _barValues[barIndex];
                preScaled          += _barValues[barIndex + 1] * 0.75f;
                preScaled          /= 2.5f;
                BarValues[barIndex] = Utility.Clamp(preScaled, 0f, 1f);
            }
            preScaled           = _barValues[barIndex - 1];
            preScaled          += _barValues[barIndex];
            preScaled          /= 2f;
            BarValues[barIndex] = Utility.Clamp(preScaled, 0f, 1f);

            //Calculate the song beat
            float sum = 0f;

            for (int i = 2; i < 28; i++)
            {
                sum += (float)Math.Sqrt(_barValues[i]); //Prettier scaling > Accurate scaling
            }
            SongBeat = (sum / 25f);
        }
        //timer
        private void _t_Tick(object sender, EventArgs e)
        {
            var ret = BassWasapi.BASS_WASAPI_GetData(_fft, (int)BASSData.BASS_DATA_FFT2048); //get channel fft data

            if (ret < -1)
            {
                return;
            }
            int line;
            var b0 = 0;

            //computes the spectrum data, the code is taken from a bass_wasapi sample.
            for (line = 0; line < _lines; line++)
            {
                float peak = 0;
                var   b1   = (int)Math.Pow(2, line * 10.0 / (_lines - 1));
                if (b1 > 1023)
                {
                    b1 = 1023;
                }
                if (b1 <= b0)
                {
                    b1 = b0 + 1;
                }
                for (; b0 < b1; b0++)
                {
                    if (peak < _fft[1 + b0])
                    {
                        peak = _fft[1 + b0];
                    }
                }
                var y = (int)(Math.Sqrt(peak) * 496);
                if (y > 255)
                {
                    y = 255;
                }
                if (y < 0)
                {
                    y = 0;
                }

                _spectrumdata.Add((byte)y);
            }

            // Pre-Process the spectrum data
            ProcessSpectrum(_spectrumdata, _oldSpectrumdata);

            if (NormalizeEnabled)
            {
                for (line = 0; line < _spectrumdata.Count; line++)
                {
                    if (_maxSpectrumValue[line] > _minSpectrumValue[line] + 20)
                    {
                        // ReSharper disable once UnusedVariable
                        var precentApart = Math.Abs(_maxSpectrumValue[line] - _minSpectrumValue[line]) / 255;
                        _maxSpectrumValue[line] -= NormalDecayVelocity;
                        //_minSpectrumValue[line] += NormalDecayVelocity;
                    }

                    if (_maxSpectrumValue[line] < _spectrumdata[line])
                    {
                        _maxSpectrumValue[line] = (byte)_spectrumdata[line];
                    }
                    //if (_minSpectrumValue[line] > _spectrumdata[line]) _minSpectrumValue[line] = (byte)_spectrumdata[line];

                    _spectrumdata[line] = (byte)ConvertRange(_minSpectrumValue[line], _maxSpectrumValue[line], 0, 255, _spectrumdata[line]);
                    //Console.WriteLine((byte)_minSpectrumValue[line]);
                }
            }

            //_mode.Display(_spectrumdata);

            _oldSpectrumdata.Clear();
            _oldSpectrumdata.AddRange(_spectrumdata.ToArray());

            // Post

            if (DisplayEnable)
            {
                _spectrum.Set(ByteSpectrumData);
            }
            Serial?.Write(ByteSpectrumData.ToArray(), 0, _spectrumdata.Count);

            _spectrumdata.Clear();


            var level = BassWasapi.BASS_WASAPI_GetLevel();

            if (level == _lastlevel && level != 0)
            {
                _hanctr++;
            }
            _lastlevel = level;

            if (SetModeFlag && Serial != null)
            {
                SetMode(Modes.ModeList[_mode]);
            }

            //Required, because some programs hang the output. If the output hangs for a 75ms
            //this piece of code re initializes the output so it doesn't make a gliched sound for long.
            if (_hanctr <= 3)
            {
                return;
            }
            _hanctr = 0;
            Free();
            Bass.BASS_Init(0, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero);
            _initialized = false;
            Enable       = true;
        }
Ejemplo n.º 24
0
        // timer
        private void _t_Tick(object sender, EventArgs e)
        {
            // get fft data. Return value is -1 on error
            int ret = BassWasapi.BASS_WASAPI_GetData(_fft, (int)BASSData.BASS_DATA_FFT4096);

            if (ret < 0)
            {
                return;
            }
            int x, y;
            int b0 = 0;

            // computes the spectrum data, the code is taken from a bass_wasapi sample.
            for (x = 0; x < _lines; x++)
            {
                float peak = 0;
                int   b1   = (int)Math.Pow(2, x * 10.0 / (_lines - 1));
                if (b1 > 1023)
                {
                    b1 = 1023;
                }
                if (b1 <= b0)
                {
                    b1 = b0 + 1;
                }
                for (; b0 < b1; b0++)
                {
                    if (peak < _fft[1 + b0])
                    {
                        peak = _fft[1 + b0];
                    }
                }
                y = (int)(Math.Sqrt(peak) * 3 * 255 - 4);
                if (y > 255)
                {
                    y = 255;
                }
                if (y < 0)
                {
                    y = 0;
                }
                _spectrumdata.Add((byte)y);
            }

            mainWindow.GetSpectrumData(_spectrumdata);
            _spectrumdata.Clear();


            int level = BassWasapi.BASS_WASAPI_GetLevel();

            if (level == _lastlevel && level != 0)
            {
                _hanctr++;
            }
            _lastlevel = level;

            // Required, because some programs hang the output. If the output hangs for a 75ms
            // this piece of code re initializes the output
            // so it doesn't make a gliched sound for long.
            if (_hanctr > 3)
            {
                _hanctr = 0;
                Free();
                Bass.BASS_Init(0, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero);
                _initialized = false;
                Enable       = true;
            }
        }
Ejemplo n.º 25
0
        private void _t_Tick(object sender, EventArgs e)
        {
            // get fft data. Return value is -1 on error
            int ret = BassWasapi.BASS_WASAPI_GetData(_fft, (int)BASSData.BASS_DATA_FFT2048);

            if (ret < 0)
            {
                return;
            }
            int    x;
            double y;
            int    b0 = 0;

            //computes the spectrum data, the code is taken from a bass_wasapi sample.
            for (x = 0; x < _lines; x++)
            {
                float peak = 0;
                int   b1   = (int)Math.Pow(2, x * 10.0 / (_lines - 1));
                if (b1 > 1023)
                {
                    b1 = 1023;
                }
                if (b1 <= b0)
                {
                    b1 = b0 + 1;
                }
                for (; b0 < b1; b0++)
                {
                    if (peak < _fft[1 + b0])
                    {
                        peak = _fft[1 + b0];
                    }
                }
                y = (Math.Sqrt(peak));

                Channels[x].Value = y;
            }



            int level = BassWasapi.BASS_WASAPI_GetLevel();

            //_l.Value = Utils.LowWord32(level);
            //_r.Value = Utils.HighWord32(level);
            //if (level == _lastlevel && level != 0) _hanctr++;
            _lastlevel = level;

            //Required, because some programs hang the output. If the output hangs for a 75ms
            //this piece of code re initializes the output
            //so it doesn't make a gliched sound for long.
            if (_hanctr > 3)
            {
                _hanctr = 0;
                //_l.Value = 0;
                //_r.Value = 0;
                Free();
                Bass.BASS_Init(0, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero);
                _initialized = false;
                //Enable = true;
            }
        }
Ejemplo n.º 26
0
        //timer
        private void _t_Tick(object sender, EventArgs e)
        {
            var ret = BassWasapi.BASS_WASAPI_GetData(_fft, (int)BASSData.BASS_DATA_FFT2048); //get channel fft data

            if (ret < -1)
            {
                return;
            }
            int x;
            var b0 = 0;

            //computes the spectrum data, the code is taken from a bass_wasapi sample.
            for (x = 0; x < Lines; x++)
            {
                float peak = 0;
                var   b1   = (int)Math.Pow(2, x * 10.0 / (Lines - 1));
                if (b1 > 1023)
                {
                    b1 = 1023;
                }
                if (b1 <= b0)
                {
                    b1 = b0 + 1;
                }
                for (; b0 < b1; b0++)
                {
                    if (peak < _fft[1 + b0])
                    {
                        peak = _fft[1 + b0];
                    }
                }
                var y = (int)(Math.Sqrt(peak) * 3 * 255 - 4);
                if (y > 255)
                {
                    y = 255;
                }
                if (y < 0)
                {
                    y = 0;
                }
                _spectrumdata.Add((byte)y);
            }

            AnalyerDataReady?.Invoke(_spectrumdata);
            _spectrumdata.Clear();


            var level = BassWasapi.BASS_WASAPI_GetLevel();

            if ((level == _lastlevel) && (level != 0))
            {
                _hanctr++;
            }
            _lastlevel = level;

            //Required, because some programs hang the output. If the output hangs for a 75ms
            //this piece of code re initializes the output so it doesn't make a gliched sound for long.
            if (_hanctr <= 3)
            {
                return;
            }

            _hanctr = 0;
            Free();
            Bass.BASS_Init(0, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero);
            _initialized = false;
            Enable(_enabledButton, _enabledComboBox);
        }
Ejemplo n.º 27
0
        private void AudioDataThread()
        {
            DateTime           VisualizerRPSCounter     = new DateTime();
            DateTime           CalibrateRefreshRate     = new DateTime();
            int                VisualizerUpdatesCounter = 0;
            List <List <int> > AudioDataPointStore      = new List <List <int> >();

            float[] AudioData = { };

            int VisualSamles  = 0;
            int Smoothness    = 0;
            int Sensitivity   = 0;
            int BASSDataRate  = 0;
            int BeatZoneFrom  = 0;
            int BeatZoneTo    = 0;
            int SelectedIndex = 0;
            int TriggerHeight = 0;
            int SpectrumSplit = 0;
            int RefreshTime   = 0;

            MainFormClass.VisualizerPanel.Invoke((MethodInvoker) delegate {
                VisualSamles  = (int)MainFormClass.VisualSamplesNumericUpDown.Value;
                Smoothness    = MainFormClass.SmoothnessTrackBar.Value;
                Sensitivity   = MainFormClass.SensitivityTrackBar.Value;
                BASSDataRate  = Int32.Parse(MainFormClass.AudioSampleRateComboBox.SelectedItem.ToString());
                BeatZoneFrom  = MainFormClass.BeatZoneFromTrackBar.Value;
                BeatZoneTo    = MainFormClass.BeatZoneToTrackBar.Value;
                AudioData     = new float[Int32.Parse(MainFormClass.AudioSampleRateComboBox.SelectedItem.ToString())];
                SelectedIndex = MainFormClass.VisualizationTypeComboBox.SelectedIndex;
                TriggerHeight = MainFormClass.BeatZoneTriggerHeight.Value;
                RefreshTime   = MainFormClass.SampleTimeTrackBar.Value;
                SpectrumSplit = (int)MainFormClass.FullSpectrumNumericUpDown.Value;
                for (int i = 0; i < MainFormClass.VisualSamplesNumericUpDown.Value; i++)
                {
                    AudioDataPointStore.Add(new List <int>(new int[Smoothness]));
                }
            });

            while (RunVisualizerThread)
            {
                CalibrateRefreshRate = DateTime.Now;

                MainFormClass.BeatZoneTriggerHeight.Invoke((MethodInvoker) delegate { TriggerHeight = MainFormClass.BeatZoneTriggerHeight.Value; });

                Series BeatZoneSeries = new Series
                {
                    IsVisibleInLegend = false,
                    IsXValueIndexed   = false,
                    ChartType         = SeriesChartType.Column,
                    Color             = Color.FromArgb(0, 122, 217)
                };

                int ReturnValue = BassWasapi.BASS_WASAPI_GetData(AudioData, (int)(BASSData)Enum.Parse(typeof(BASSData), "BASS_DATA_FFT" + BASSDataRate));
                if (ReturnValue < -1)
                {
                    return;
                }

                int X, Y;
                int B0 = 0;
                for (X = BeatZoneFrom; X < BeatZoneTo; X++)
                {
                    float Peak = 0;
                    int   B1   = (int)Math.Pow(2, X * 10.0 / ((int)VisualSamles - 1));
                    if (B1 > 1023)
                    {
                        B1 = 1023;
                    }
                    if (B1 <= B0)
                    {
                        B1 = B0 + 1;
                    }
                    for (; B0 < B1; B0++)
                    {
                        if (Peak < AudioData[1 + B0])
                        {
                            Peak = AudioData[1 + B0];
                        }
                    }
                    Y = (int)(Math.Sqrt(Peak) * Sensitivity * 255 - 4);
                    if (Y > 255)
                    {
                        Y = 255;
                    }
                    if (Y < 1)
                    {
                        Y = 1;
                    }

                    if (X >= BeatZoneFrom)
                    {
                        if (X <= BeatZoneTo)
                        {
                            AudioDataPointStore[X].Add((byte)Y);
                            while (AudioDataPointStore[X].Count > Smoothness)
                            {
                                AudioDataPointStore[X].RemoveAt(0);
                            }

                            int AverageValue = 0;
                            if (Smoothness > 1)
                            {
                                for (int s = 0; s < Smoothness; s++)
                                {
                                    AverageValue += AudioDataPointStore[X][s];
                                }
                                AverageValue = AverageValue / Smoothness;
                            }
                            else
                            {
                                AverageValue = AudioDataPointStore[X][0];
                            }
                            if (AverageValue > 255)
                            {
                                AverageValue = 255;
                            }
                            if (AverageValue < 0)
                            {
                                AverageValue = 0;
                            }

                            BeatZoneSeries.Points.AddXY(X, AverageValue);
                        }
                    }
                }

                if (SelectedIndex == 0)
                {
                    double Hit = 0;
                    for (int i = 0; i < BeatZoneSeries.Points.Count; i++)
                    {
                        if (BeatZoneSeries.Points[i].YValues[0] >= TriggerHeight)
                        {
                            Hit++;
                        }
                    }
                    double OutValue = Math.Round(Math.Round((Hit / ((double)BeatZoneTo - (double)BeatZoneFrom)), 2) * 99, 0);
                    AutoTrigger((OutValue / 99) * (255 * 3));
                    if (OutValue > 99)
                    {
                        OutValue = 99;
                    }
                    string SerialOut = "2;" + OutValue.ToString().Replace(',', '.');
                    MainFormClass.SendDataBySerial(SerialOut);
                }
                if (SelectedIndex == 1 | SelectedIndex == 2)
                {
                    double EndR   = 0;
                    double EndG   = 0;
                    double EndB   = 0;
                    int    CountR = 0;
                    int    CountG = 0;
                    int    CountB = 0;
                    int    Hit    = 0;
                    for (int i = 0; i < BeatZoneSeries.Points.Count; i++)
                    {
                        if (BeatZoneSeries.Points[i].YValues[0] >= TriggerHeight)
                        {
                            try
                            {
                                if (MainFormClass.SpectrumChart.Series[0].Points[i].YValues[0] <= 255)
                                {
                                    if (MainFormClass.SpectrumChart.Series[0].Points[i].YValues[0] >= 0)
                                    {
                                        EndR += MainFormClass.SpectrumChart.Series[0].Points[i].YValues[0];
                                        CountR++;
                                    }
                                }
                            }
                            catch
                            {
                                EndR += 0;
                                CountR++;
                            }
                            try
                            {
                                if (MainFormClass.SpectrumChart.Series[1].Points[i].YValues[0] <= 255)
                                {
                                    if (MainFormClass.SpectrumChart.Series[1].Points[i].YValues[0] >= 0)
                                    {
                                        EndG += MainFormClass.SpectrumChart.Series[1].Points[i].YValues[0];
                                        CountG++;
                                    }
                                }
                            }
                            catch
                            {
                                EndG += 0;
                                CountG++;
                            }
                            try
                            {
                                if (MainFormClass.SpectrumChart.Series[2].Points[i].YValues[0] <= 255)
                                {
                                    if (MainFormClass.SpectrumChart.Series[2].Points[i].YValues[0] >= 0)
                                    {
                                        EndB += MainFormClass.SpectrumChart.Series[2].Points[i].YValues[0];
                                        CountB++;
                                    }
                                }
                            }
                            catch
                            {
                                EndB += 0;
                                CountB++;
                            }
                            Hit++;
                        }
                    }

                    AutoTrigger(((float)Hit / ((float)BeatZoneTo - (float)BeatZoneFrom)) * (255 * 3));

                    if (CountR > 0)
                    {
                        EndR = EndR / CountR;
                    }
                    if (CountG > 0)
                    {
                        EndG = EndG / CountG;
                    }
                    if (CountB > 0)
                    {
                        EndB = EndB / CountB;
                    }

                    Color AfterShuffel = MainFormClass.ShuffleColors(Color.FromArgb((int)Math.Round(EndR, 0), (int)Math.Round(EndG, 0), (int)Math.Round(EndB, 0)));

                    string SerialOut = "";
                    if (SelectedIndex == 1)
                    {
                        SerialOut = "1;" + AfterShuffel.R + ";" + AfterShuffel.G + ";" + AfterShuffel.B + ";0;0";
                    }
                    if (SelectedIndex == 2)
                    {
                        SerialOut = "3;" + AfterShuffel.R + ";" + AfterShuffel.G + ";" + AfterShuffel.B;
                    }
                    MainFormClass.SendDataBySerial(SerialOut);
                }
                if (SelectedIndex == 3 | SelectedIndex == 4)
                {
                    int EndR = 0;
                    int EndG = 0;
                    int EndB = 0;
                    int Hit  = 0;

                    for (int i = 0; i < BeatZoneSeries.Points.Count; i++)
                    {
                        if (BeatZoneSeries.Points[i].YValues[0] >= TriggerHeight)
                        {
                            Hit++;
                        }
                    }

                    int EndValue = (int)(((float)255 * (float)3) * ((float)Hit / ((float)BeatZoneTo - (float)BeatZoneFrom)));
                    if (EndValue >= 765)
                    {
                        EndValue = 764;
                    }
                    if (EndValue < 0)
                    {
                        EndValue = 0;
                    }

                    MainFormClass.BeatWaveProgressBar.Invoke((MethodInvoker) delegate { MainFormClass.BeatWaveProgressBar.Value = EndValue; });
                    try
                    {
                        EndR = (int)MainFormClass.WaveChart.Series[0].Points[EndValue].YValues[0];
                        EndG = (int)MainFormClass.WaveChart.Series[1].Points[EndValue].YValues[0];
                        EndB = (int)MainFormClass.WaveChart.Series[2].Points[EndValue].YValues[0];
                    }
                    catch
                    {
                        EndR = 0;
                        EndG = 0;
                        EndB = 0;
                    }

                    AutoTrigger(((float)Hit / ((float)BeatZoneTo - (float)BeatZoneFrom)) * (255 * 3));

                    if (EndR > 255)
                    {
                        EndR = 0;
                    }

                    if (EndG > 255)
                    {
                        EndG = 0;
                    }

                    if (EndB > 255)
                    {
                        EndB = 0;
                    }

                    if (EndR < 0)
                    {
                        EndR = 0;
                    }

                    if (EndG < 0)
                    {
                        EndG = 0;
                    }

                    if (EndB < 0)
                    {
                        EndB = 0;
                    }

                    Color AfterShuffel = MainFormClass.ShuffleColors(Color.FromArgb(EndR, EndG, EndB));

                    string SerialOut = "";
                    if (SelectedIndex == 4)
                    {
                        SerialOut = "1;" + AfterShuffel.R + ";" + AfterShuffel.G + ";" + AfterShuffel.B + ";0;0";
                    }
                    if (SelectedIndex == 3)
                    {
                        SerialOut = "3;" + AfterShuffel.R + ";" + AfterShuffel.G + ";" + AfterShuffel.B + "";
                    }
                    MainFormClass.SendDataBySerial(SerialOut);
                }
                if (SelectedIndex == 5)
                {
                    int    Hit       = 0;
                    string SerialOut = "5;" + SpectrumSplit.ToString() + ";";
                    for (int i = 0; i < BeatZoneSeries.Points.Count; i++)
                    {
                        if (BeatZoneSeries.Points[i].YValues[0] >= TriggerHeight)
                        {
                            SerialOut += Math.Round((BeatZoneSeries.Points[i].YValues[0] / 255) * (double)SpectrumSplit, 0) + ";";
                            Hit++;
                        }
                        else
                        {
                            SerialOut += "0;";
                        }
                    }

                    AutoTrigger(((float)Hit / ((float)BeatZoneTo - (float)BeatZoneFrom)) * (255 * 3));

                    MainFormClass.SendDataBySerial(SerialOut);
                }

                VisualizerUpdatesCounter++;
                if ((DateTime.Now - VisualizerRPSCounter).TotalSeconds >= 1)
                {
                    MainFormClass.VisualizerRPSLabel.Invoke((MethodInvoker) delegate { MainFormClass.VisualizerRPSLabel.Text = "RPS: " + VisualizerUpdatesCounter; });
                    VisualizerUpdatesCounter = 0;
                    VisualizerRPSCounter     = DateTime.Now;
                }
                MainFormClass.BeatZoneChart.Invoke((MethodInvoker) delegate
                {
                    MainFormClass.BeatZoneChart.Series.Clear();
                    MainFormClass.BeatZoneChart.Series.Add(BeatZoneSeries);
                });

                int ExectuionTime      = (int)(DateTime.Now - CalibrateRefreshRate).TotalMilliseconds;
                int ActuralRefreshTime = RefreshTime - ExectuionTime;

                if (ActuralRefreshTime < 0)
                {
                    ActuralRefreshTime = 0;
                }

                Thread.Sleep(ActuralRefreshTime);
            }
        }
Ejemplo n.º 28
0
        //timer
        private void _t_Tick(object sender, EventArgs e)
        {
            int ret = BassWasapi.BASS_WASAPI_GetData(_fft, (int)BASSData.BASS_DATA_FFT2048); //get channel fft data

            if (ret < -1)
            {
                return;
            }
            int x, y;
            int b0 = 0;

            //computes the spectrum data, the code is taken from a bass_wasapi sample.
            for (x = 0; x < _lines; x++)
            {
                float peak = 0;
                int   b1   = (int)Math.Pow(2, x * 10.0 / (_lines - 1));
                if (b1 > 1023)
                {
                    b1 = 1023;
                }
                if (b1 <= b0)
                {
                    b1 = b0 + 1;
                }
                for (; b0 < b1; b0++)
                {
                    if (peak < _fft[1 + b0])
                    {
                        peak = _fft[1 + b0];
                    }
                }
                y = (int)(Math.Sqrt(peak) * 3 * 255 - 4);
                if (y > 255)
                {
                    y = 255;
                }
                if (y < 0)
                {
                    y = 0;
                }
                _spectrumdata.Add((byte)y);
            }

            if (DisplayEnable)
            {
                _chart.Series[0].Points.Clear();
            }

            _sb.Clear();

            for (int i = 0; i < _spectrumdata.Count; i++)
            {
                if (!_realMode)
                {
                    if (_spectrumdata[i] > max[i])
                    {
                        max[i] = _spectrumdata[i];                                          //if the new level is greater than original, then set to max
                    }
                    else if (max[i] >= 12 && _spectrumdata[i] < max[i] - 5)
                    {
                        max[i] -= 12;                                                         //if greater than 12 and difference is bigger than 5, decrease by 12
                    }
                    else if (max[i] > 0)
                    {
                        max[i]--;                                                           //else decrease by one
                    }
                }
                else
                {
                    max[i] = _spectrumdata[i];
                }
                if (DisplayEnable)
                {
                    _chart.Series[0].Points.AddXY(i + 1, max[i]);
                    if (_variableColor == 5)                                    //Dynamic pink
                    {
                        _chart.Series[0].Points[i].Color = System.Drawing.Color.FromArgb(max[i], 0, 150);
                    }
                    else if (_variableColor == 6)                               //Default dynamic
                    {
                        if (max[i] < 65)
                        {
                            _chart.Series[0].Points[i].Color = System.Drawing.Color.Lime;
                        }
                        else if (max[i] < 130)
                        {
                            _chart.Series[0].Points[i].Color = System.Drawing.Color.Yellow;
                        }
                        else if (max[i] < 189)
                        {
                            _chart.Series[0].Points[i].Color = System.Drawing.Color.Orange;
                        }
                        else
                        {
                            _chart.Series[0].Points[i].Color = System.Drawing.Color.Red;
                        }
                    }
                    else if (_variableColor == 7)                               //Dynamic cyan
                    {
                        _chart.Series[0].Points[i].Color = System.Drawing.Color.FromArgb(0, max[i], 150);
                    }
                    else if (_variableColor == 8)                               //Dynamic Fire
                    {
                        //_chart.Series[0].Color = System.Drawing.Color.Red;
                        //_chart.Series[0].BackSecondaryColor = System.Drawing.Color.Yellow;
                        _chart.Series[0].Points[i].Color = System.Drawing.Color.FromArgb(max[i], 0, 0);
                        _chart.Series[0].Points[i].BackSecondaryColor = System.Drawing.Color.FromArgb(250, max[i], 31);;
                    }
                    else if (_variableColor == 9)                               //Dynamic sth
                    {
                        _chart.Series[0].Points[i].Color = System.Drawing.Color.FromArgb(max[i], 0, 140);
                        _chart.Series[0].Points[i].BackSecondaryColor = System.Drawing.Color.FromArgb(0, max[i], 255);
                    }
                }
                char c = 'a';
                byte b = max[i];
                if (b < 5)
                {
                    c = 'a';
                }
                else if (b < 17)
                {
                    c = 'b';
                }
                else if (b < 39)
                {
                    c = 'c';
                }
                else if (b < 51)
                {
                    c = 'd';
                }
                else if (b < 63)
                {
                    c = 'e';
                }
                else if (b < 75)
                {
                    c = 'f';
                }
                else if (b < 87)
                {
                    c = 'g';
                }
                else if (b < 99)
                {
                    c = 'h';
                }
                else if (b < 111)
                {
                    c = 'i';
                }
                else if (b < 123)
                {
                    c = 'j';
                }
                else if (b < 135)
                {
                    c = 'k';
                }
                else if (b < 147)
                {
                    c = 'l';
                }
                else if (b < 159)
                {
                    c = 'm';
                }
                else if (b < 171)
                {
                    c = 'n';
                }
                else if (b < 183)
                {
                    c = 'o';
                }
                else if (b < 195)
                {
                    c = 'q';
                }
                else if (b < 207)
                {
                    c = 'r';
                }
                else if (b < 219)
                {
                    c = 's';
                }
                else if (b < 231)
                {
                    c = 't';
                }
                else if (b < 243)
                {
                    c = 'u';
                }
                else
                {
                    c = 'v';
                }
                int num = c - 'a';
                _sb.Append(c);
            }

            //Console.WriteLine(output);
            if (Serial != null)
            {
                Serial.Write(_sb.ToString());                 //Serial.Write(output);
            }
            //Console.WriteLine();

            _spectrumdata.Clear();


            int level = BassWasapi.BASS_WASAPI_GetLevel();

            if (level == _lastlevel && level != 0)
            {
                _hanctr++;
            }
            _lastlevel = level;

            //Required, because some programs hang the output. If the output hangs for a 75ms
            //this piece of code re initializes the output so it doesn't make a gliched sound for long.

            /*
             * if (_hanctr > 3)
             * {
             *  Console.WriteLine("error");
             *
             *  _hanctr = 0;
             *  _l.Value = 0;
             *  _r.Value = 0;
             *  Free();
             *  Bass.BASS_Init(0, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero);
             *  _initialized = false;
             *  Enable = true;
             * }
             */
        }
Ejemplo n.º 29
0
        //timer
        private void _t_Tick(object sender, EventArgs e)
        {
            int ret = BassWasapi.BASS_WASAPI_GetData(_fft, (int)BASSData.BASS_DATA_FFT2048); //get channel fft data

            if (ret < -1)
            {
                return;
            }
            int         x, y;
            int         b0         = 0;
            List <byte> serialdata = new List <byte>();

            //computes the spectrum data, the code is taken from a bass_wasapi sample.
            for (x = 0; x < _lines; x++)
            {
                float peak = 0;
                int   b1   = (int)Math.Pow(2, x * 10.0 / (_lines - 1));
                if (b1 > 1023)
                {
                    b1 = 1023;
                }
                if (b1 <= b0)
                {
                    b1 = b0 + 1;
                }
                for (; b0 < b1; b0++)
                {
                    if (peak < _fft[1 + b0])
                    {
                        peak = _fft[1 + b0];
                    }
                }
                y = (int)(Math.Sqrt(peak) * 3 * 255 - 4);
                if (y > 255)
                {
                    y = 255;
                }
                if (y < 0)
                {
                    y = 0;
                }
                _spectrumdata.Add((byte)y);
                if (x == _sendline)
                {
                    serialdata.Add((byte)y);
                }
                //Console.Write("{0, 3} ", y);
            }

            if (DisplayEnable)
            {
                _spectrum.Set(_spectrumdata);
            }
            if (Serial != null)
            {
                Serial.Write(serialdata.ToArray(), 0, serialdata.Count);
                Console.WriteLine(ConvertStringArrayToStringJoin(serialdata.ToArray()));
            }
            _spectrumdata.Clear();


            int level = BassWasapi.BASS_WASAPI_GetLevel();

            _l.Value = Utils.LowWord32(level);
            _r.Value = Utils.HighWord32(level);
            if (level == _lastlevel && level != 0)
            {
                _hanctr++;
            }
            _lastlevel = level;

            //Required, because some programs hang the output. If the output hangs for a 75ms
            //this piece of code re initializes the output so it doesn't make a gliched sound for long.
            if (_hanctr > 3)
            {
                _hanctr  = 0;
                _l.Value = 0;
                _r.Value = 0;
                Free();
                Bass.BASS_Init(0, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero);
                _initialized = false;
                Enable       = true;
            }
        }
Ejemplo n.º 30
0
        //timer
        private void _t_Tick(object sender, EventArgs e)
        {
            int ret = BassWasapi.BASS_WASAPI_GetData(_fft, (int)BASSData.BASS_DATA_FFT2048); //get channel fft data

            if (ret < -1)
            {
                return;
            }
            int x, y;
            int b0 = 0;
            int hz = 255;

            //computes the spectrum data, the code is taken from a bass_wasapi sample.
            for (x = 0; x < _lines; x++)
            {
                float peak = 0;
                int   b1   = (int)Math.Pow(2, x * 10.0 / (_lines - 1));
                if (b1 > 1023)
                {
                    b1 = 1023;
                }
                if (b1 <= b0)
                {
                    b1 = b0 + 1;
                }
                for (; b0 < b1; b0++)
                {
                    if (peak < _fft[1 + b0])
                    {
                        peak = _fft[1 + b0];
                    }
                }
                y = (int)(Math.Sqrt(peak) * 3 * hz - 4);
                if (y > hz)
                {
                    y = hz;
                }
                if (y < 0)
                {
                    y = 0;
                }
                _spectrumdata.Add((byte)y);
            }

            _spectrum.Set(_spectrumdata);

            _spectrumdata.Clear();


            int level = BassWasapi.BASS_WASAPI_GetLevel();

            _l.Value = Utils.LowWord32(level);
            _r.Value = Utils.HighWord32(level);
            if (level == _lastlevel && level != 0)
            {
                _hanctr++;
            }
            _lastlevel = level;

            //Required, because some programs hang the output. If the output hangs for a 75ms
            //this piece of code re initializes the output so it doesn't make a gliched sound for long.
            if (_hanctr > 3)
            {
                _hanctr      = 0;
                _l.Value     = 0;
                _r.Value     = 0;
                _initialized = false;
                Free();
                NewInit();
            }
        }