public void OnAudioDataAvailable(object sender, WaveInEventArgs e) { if (e.BytesRecorded < 2000) { return; } int bytesPerSample = Zybo.Fft.WaveIn.WaveFormat.BitsPerSample / 8; int samplesRecorded = e.BytesRecorded / bytesPerSample; Int16[] lastBuffer = new Int16[samplesRecorded]; for (int i = 0; i < samplesRecorded; i++) { int index = i * bytesPerSample; //if(e.Buffer[index] != 0xFF ) lastBuffer[i] = BitConverter.ToInt16(e.Buffer, index); } int lastBufferAmplitude = lastBuffer.Max() - lastBuffer.Min(); lastBufferAmplitude = lastBufferAmplitude > 10000 ? 0 : lastBufferAmplitude; int max = lastBuffer.Max(); int min = lastBuffer.Min(); double t = this.watch.ElapsedMilliseconds * 0.001; if ((AudioSignalModel.Series.First() as LineSeries).Points.Count > maxAudioPoints) { (AudioSignalModel.Series.First() as LineSeries).Points.RemoveAt(0); } (AudioSignalModel.Series.First() as LineSeries).Points.Add(new DataPoint(t, lastBufferAmplitude)); AudioSignalModel.InvalidatePlot(true); }
private void OnDataAvailable(object sender, NAudio.Wave.WaveInEventArgs args) { int bytesPerSample = wvin.WaveFormat.BitsPerSample / 8; int samplesRecorded = args.BytesRecorded / bytesPerSample; Int16[] lastBuffer = new Int16[samplesRecorded]; for (int i = 0; i < samplesRecorded; i++) { lastBuffer[i] = BitConverter.ToInt16(args.Buffer, i * bytesPerSample); } int lastBufferAmplitude = lastBuffer.Max() - lastBuffer.Min(); double amplitude = (double)lastBufferAmplitude / Math.Pow(2, wvin.WaveFormat.BitsPerSample); if (amplitude > peakAmplitudeSeen) { peakAmplitudeSeen = amplitude; } amplitude = amplitude / peakAmplitudeSeen * 100; buffersRead += 1; // TODO: make this sane ScottPlot.PlottableAxLine axLine = (ScottPlot.PlottableAxLine)scottPlotUC1.plt.GetPlottables()[1]; axLine.position = (buffersRead % amplitudes.Length) * 20.0 / 1000.0; Console.WriteLine(string.Format("Buffer {0:000} amplitude: {1:00.00}%", buffersRead, amplitude)); PlotAddPoint(amplitude); }
/// <summary> /// 根据指定集合的指定值属性计算颜色分布: /// start,end:起始颜色,建议颜色:Red-DarkKhaki /// property:需要计算的属性名称 /// attach:附加到Dynamic上的动态属性名称 /// attachColor,true:附加Color,false:附加SolidColorBrush /// </summary> public static void ColorDistributeAnalysis <TItem>(List <TItem> items, Color start, Color end, string property, string attach, bool attachColor = true) where TItem : IDynamicEnable { if (items == null || items.Count <= 0) { return; } //起始颜色 Int16[] s = new Int16[] { start.R.ToString().ToInt16(), start.G.ToString().ToInt16(), start.B.ToString().ToInt16() }; Int16[] e = new Int16[] { end.R.ToString().ToInt16(), end.G.ToString().ToInt16(), end.B.ToString().ToInt16(), }; //最大段数 var maxStep = Math.Abs(s.Max() - e.Min()) / 2; //数值 List <double> values = new List <double>(); items.ForEach(item => { if (item is DynamicX) { values.Add((item as DynamicX).Get(property).ToSafeString().ToSafeDouble()); } else { var v = System.Utility.Helper.Reflection.GetObjectValue <TItem>(item, property).ToSafeString().ToSafeDouble(); values.Add(v); } }); var max = values.Max(); var min = values.Min(); var sub = max - min; //设置颜色 SetStepColor(items, ref start, attach, attachColor, s, e, maxStep, values, min, sub); }
private void SetDataAmplitude(WaveInEventArgs args) { int bytesPerSample = _wi.WaveFormat.BitsPerSample / 8; int samplesRecorded = args.BytesRecorded / bytesPerSample; Int16[] lastBuffer = new Int16[samplesRecorded]; for (int i = 0; i < samplesRecorded; i++) { lastBuffer[i] = BitConverter.ToInt16(args.Buffer, i * bytesPerSample); } int lastBufferAmplitude = lastBuffer.Max() - lastBuffer.Min(); double amplitude = (double)lastBufferAmplitude / Math.Pow(2, _wi.WaveFormat.BitsPerSample); if (amplitude > peakAmplitudeSeen) { peakAmplitudeSeen = amplitude; } amplitude = amplitude / peakAmplitudeSeen * 100; //_spectrumdata.Clear(); if (_spectrumdata.Count >= NumberOfLines) { _spectrumdata.RemoveAt(0); } double regleDe3 = amplitude * 255 / 100; int y = Convert.ToInt32(regleDe3); if (y > 255) { y = 255; } if (y < 0) { y = 0; } _spectrumdata.Add((byte)y); SpectrumVisualizer.Set(_spectrumdata.ToArray()); }