Ejemplo n.º 1
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;
            }
        }
        //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.º 3
0
        void AnimateCenterRightLeft()
        {
            int x, y;
            int b0          = 0;
            int chanelCount = 1;

            _spectrumdata.Clear();
            //computes the spectrum data, the code is taken from a bass_wasapi sample.
            for (x = 0; x < chanelCount; x++)
            {
                float peak = 0;
                int   b1   = (int)Math.Pow(2, x * 10.0 / (chanelCount - 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);
            }
            //if (Serial != null)
            //{
            //    Serial.Write(_spectrumdata.ToArray(), 0, _spectrumdata.Count);
            //}


            int level = BassWasapi.BASS_WASAPI_GetLevel();


            var left  = (float)Utils.LowWord32(level) / 32768;;
            var right = (float)Utils.HighWord32(level) / 32768;

            if ((DateTime.Now - lastBeat).TotalSeconds > 1)
            {
                if (left > Percentile(powerHistory.Select(_ => _.Item1), .8f))
                {
                    lastBeat   = DateTime.Now;
                    lastindex += 125;
                    System.Diagnostics.Debug.WriteLine("beat " + DateTime.Now.Second + " " + DateTime.Now.Millisecond);
                }
            }

            if (client != null)
            {
                Array.Clear(_ledArray, 0, _ledArray.Length);

                lastindex += colorStep;
                if (lastindex > 256)
                {
                    lastindex = lastindex % 256;
                }



                for (int i = 0; i < _ledArray.Length; i++)
                {
                    _ledArray[i] = colorwheel((lastindex + i * colorStep) % 256);
                }
                var specLength = (_leftIdx - _rightIdx) / chanelCount;


                var rem = (_leftIdx - _rightIdx) % 3 + specLength % 2;
                specLength = specLength / 2;

                //Newtonsoft.Json.Linq.JArray arr = new Newtonsoft.Json.Linq.JArray();
                for (int i = 0; i < chanelCount; i++)
                {
                    var colorStart = (int)(i * ((float)256 / (float)chanelCount));
                    for (int j = 0; j < specLength; j++)
                    {
                        var powerLevel   = (float)_spectrumdata[i] / (float)255;
                        var percentLevel = (float)(j + 1) / (float)specLength;
                        // var color = 0;
                        var on = powerLevel > percentLevel;


                        var idxa = _rightIdx + i * (specLength * 2) + specLength - (j + 1);
                        var idxb = _rightIdx + i * (specLength * 2) + specLength + j;
                        _ledArray[idxa] = on ? _ledArray[idxa] : 0;
                        _ledArray[idxb] = on ? _ledArray[idxb] : 0;
                    }
                }
                //  var percentLoud = 1.1*  (float)Utils.LowWord32(level)/ (float)65553;
                for (int i = 0; i < _rightIdx; i++)
                {
                    var percentIdx = (float)(i + 1) / (float)_rightIdx;
                    var lon        = (1.1 * Math.Pow(left, .4)) > percentIdx;
                    var ron        = (1.1 * Math.Pow(right, .4)) > percentIdx;
                    _ledArray[i] = ron ? _ledArray[0] : 0;
                    _ledArray[_ledArray.Length - i - 1] = lon ? _ledArray[0] : 0;
                }


                Render();
            }
        }