UWP - C# 实时绘制波形

  xmal:

<Grid MinHeight="600">
    <canvas:CanvasAnimatedControl x:Name="drawWaveformCanvas" Draw="DrawWaveformCanvas_Draw" ClearColor="LightGray"/>
</Grid>

  xmal.cs:

unsafe private void ProcessFrameOutput(AudioFrame frame)
{
	using (AudioBuffer buffer = frame.LockBuffer(AudioBufferAccessMode.Write))
	using (IMemoryBufferReference reference = buffer.CreateReference())
	{
		byte* dataInBytes;
		uint capacityInBytes;

		((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacityInBytes);

		float* dataInFloat = (float*)dataInBytes;
		int dataInFloatLength = (int)buffer.Length / sizeof(float);
		for (int i = 0; i < dataInFloatLength; i++)
		{
			try
			{
				waveformFloatData.Add(dataInFloat[i] * 200.0f + 300);
			}
			catch
			{

			}
		}
	}
}

private void DrawWaveformCanvas_Draw(Microsoft.Graphics.Canvas.UI.Xaml.ICanvasAnimatedControl sender, Microsoft.Graphics.Canvas.UI.Xaml.CanvasAnimatedDrawEventArgs args)
{
	float xAxis = 0.0f;
	for (int i = 0; i < waveformFloatData.Count; i++)
	{
		if (i == 0) continue;
		Vector2 point1 = new Vector2(xAxis, waveformFloatData[i - 1]);
		Vector2 point2 = new Vector2(xAxis, waveformFloatData[i]);
		args.DrawingSession.DrawLine(point1, point2, Colors.Red, 1f);
		xAxis += 0.3f;
	}
	waveformFloatData.Clear();
}

  截图:

UWP - C# 实时绘制波形

UWP - C# 实时绘制波形

上一篇:nested exception is com.sun.jersey.api.client.UniformInterfaceException: PUT


下一篇:三、几种视图实现源码剖析:django原生View、rest_framework.views.APIView、rest_framework.generics.GenericAPIView、ViewSetMixin