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();
}

  截图:

猜你喜欢

转载自www.cnblogs.com/darkchii/p/12549703.html
UWP
今日推荐