public void TriggerSampleEvent() => SampleEvent?.Invoke();
Exemple #2
0
     // Wrap the event in a protected virtual method
     // to enable derived classes to raise the event.
 protected virtual void OnRaiseSampleEvent()
 {
     // Raise the event by using the () operator.
     SampleEvent?.Invoke(this, new SampleEventArgs("Hello"));
 }
Exemple #3
0
 // Wrap the event in a protected virtual method
 // to enable derived classes to raise the event.
 protected virtual void RaiseSampleEvent()
 {
     // Raise the event in a thread-safe manner using the ?. operator.
     SampleEvent?.Invoke(this, new SampleEventArgs("Hello"));
 }
Exemple #4
0
 protected virtual void RaiseSampleEvent()
 {
     SampleEvent?.Invoke(this, new EventArgs());                                                 //In C# 6.0 you can use the ?. operator to only raise the event when the delegate is not null
 }
 public virtual void OnEventSubscribed()
 {
     SampleEvent?.Invoke(this, EventArgs.Empty);
 }
Exemple #6
0
        private void FrequencyBeatDetection()
        {
            var fftBuffer = new Complex[samples];
            var spectrum  = new float[samples / 2 + 1];

            var beats = new List <int>();

            for (var i = 0; i < samples; i++)
            {
                fftBuffer[i] = new Complex(samplesLeft[i], samplesRight[i]);
            }

            fft.Transform(fftBuffer);

            for (var i = 0; i < spectrum.Length; i++)
            {
                spectrum[i] = (float)Math.Sqrt(Math.Pow(fftBuffer[i].Real, 2) + Math.Pow(fftBuffer[i].Imaginary, 2));
            }

            if (bandType == BandType.Linear)
            {
                var bandWidth = spectrum.Length / bandSamples.Length;
                for (var band = 0; band < bandSamples.Length; band++)
                {
                    var bandAvg = 0f;
                    int freq;

                    for (freq = 0; freq < bandWidth; freq++)
                    {
                        var ix = freq + band * bandWidth;

                        if (ix > spectrum.Length)
                        {
                            break;
                        }

                        bandAvg += spectrum[ix];
                    }

                    bandAvg          /= freq + 1;
                    bandSamples[band] = bandAvg;

                    // TODO: Band freqs for linear
                }
            }

            if (bandType == BandType.Logarithmic)
            {
                // TODO: Write these equations here
                var a = (2 * spectrum.Length - 2 * bandSamples.Length * logStartBandWidth) / (float)(bandSamples.Length * (bandSamples.Length - 1));
                var b = logStartBandWidth - a;

                var offset = 0;
                for (var band = 0; band < bandSamples.Length; band++)
                {
                    bandFrequencies[band] = offset / SampleRatio;

                    var bandAvg   = 0f;
                    var bandWidth = (int)Mathf.Floor(a * (band + 1) + b);
                    int spectrumBand;

                    for (spectrumBand = 0; spectrumBand < bandWidth; spectrumBand++)
                    {
                        if (offset + spectrumBand > spectrum.Length)
                        {
                            break;
                        }

                        bandAvg += spectrum[offset + spectrumBand];
                    }

                    bandAvg          *= (spectrumBand + 1) / (float)spectrum.Length;
                    bandSamples[band] = bandAvg;
                    offset           += bandWidth;
                }
            }

            OnSample.Invoke(bandSamples);

            for (var i = 0; i < bandSamples.Length; i++)
            {
                var averageEnergy = 0.0f;
                for (var j = 0; j < energyHistory.GetLength(1); j++)
                {
                    averageEnergy += energyHistory[i, j];
                }

                averageEnergy /= energyHistory.GetLength(1);

                if (bandSamples[i] > averageEnergy * 1.4)
                {
                    beats.Add(i);
                }

                for (var j = energyHistory.GetLength(1) - 2; j >= 0; j--)
                {
                    energyHistory[i, j + 1] = energyHistory[i, j]; // TODO: Could make this faster without the array.
                }
                energyHistory[i, 0] = bandSamples[i];

                if (showDebug)
                {
                    var halfWidth  = debugLineWidth * 0.5f;
                    var lineHeight = debugHeight * 1.5f;
                    var x          = i * debugLineWidth - bandSamples.Length * halfWidth;

                    Debug.DrawLine(new Vector3(x - halfWidth, bandSamples[i] + debugHeight, 0),
                                   new Vector3(x + halfWidth, bandSamples[i] + debugHeight, 0),
                                   beats.Contains(i) ? Color.white : Color.red, SampleRatio);

                    Debug.DrawLine(new Vector3(x - halfWidth, averageEnergy + debugHeight, 0),
                                   new Vector3(x + halfWidth, averageEnergy + debugHeight, 0),
                                   Color.green, SampleRatio);

                    if (i > 0)
                    {
                        Debug.DrawLine(new Vector3(x - debugLineWidth, bandSamples[i - 1] + lineHeight, 0),
                                       new Vector3(x, bandSamples[i] + lineHeight, 0),
                                       Color.yellow, SampleRatio);

                        Debug.DrawLine(new Vector3(x - debugLineWidth, Mathf.Log(bandSamples[i - 1] + 1) + lineHeight, 0),
                                       new Vector3(x, Mathf.Log(bandSamples[i] + 1) + lineHeight, 0),
                                       Color.cyan, SampleRatio);
                    }
                }
            }


            if (beats.Count > 0)
            {
                OnBeat.Invoke(beats.ToArray(), bandSamples);
            }

            // Debug.Log(beats.Count > 0 ? "Beat" : "No Beat");
        }
Exemple #7
0
 // 将事件置入受保护的虚方法中,可以让派生类触发事件
 protected virtual void RaiseSampleEvent()
 {
     // 触发事件
     SampleEvent?.Invoke(this, new SampleEventArgs("Hello"));
 }