首页 > Windows开发 > 详细

UWP - C# 实时绘制波形

时间:2020-03-23 09:05:53      阅读:186      评论:0      收藏:0      [点我收藏+]

  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# 实时绘制波形

原文:https://www.cnblogs.com/darkchii/p/12549703.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!