gpt4 book ai didi

c# - 如何使用 AudioGraph 获取音量读数 - UWP

转载 作者:行者123 更新时间:2023-12-02 22:54:46 28 4
gpt4 key购买 nike

我想使用带有音频帧输出节点的 AudioGraph 来获得音量。这篇文章,uwp AudioGraph audio processing ,有一些很好的信息;但我无法获得好的读数。

代码:

AudioGraph audioGraph;
AudioDeviceInputNode deviceInputNode;
AudioFrameOutputNode frameOutputNode;

private async Task InitAudioGraph()
{
AudioGraphSettings settings = new AudioGraphSettings(Windows.Media.Render.AudioRenderCategory.Media);

CreateAudioGraphResult result = await AudioGraph.CreateAsync(settings);
if (result.Status != AudioGraphCreationStatus.Success)
{
Debug.WriteLine("AudioGraph creation error: " + result.Status.ToString());
}
audioGraph = result.Graph;
CreateAudioDeviceInputNodeResult result1 = await audioGraph.CreateDeviceInputNodeAsync(Windows.Media.Capture.MediaCategory.Media);

if (result1.Status != AudioDeviceNodeCreationStatus.Success)
{
// Cannot create device output node
Debug.WriteLine(result.Status.ToString());
return;
}
deviceInputNode = result1.DeviceInputNode;
frameOutputNode = audioGraph.CreateFrameOutputNode();
frameOutputNode.Start();
audioGraph.QuantumProcessed += AudioGraph_QuantumProcessed;
}
private void AudioGraph_QuantumProcessed(AudioGraph sender, object args)
{
Debug.WriteLine("event called");
AudioFrame frame = frameOutputNode.GetFrame();
ProcessFrameOutput(frame);
}
unsafe private void ProcessFrameOutput(AudioFrame frame)
{
using (AudioBuffer buffer = frame.LockBuffer(AudioBufferAccessMode.Write))
using (IMemoryBufferReference reference = buffer.CreateReference())
{
byte* dataInBytes;
uint capacityInBytes;
float* dataInFloat;

// Get the buffer from the AudioFrame
((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacityInBytes);

dataInFloat = (float*)dataInBytes;
}

[ComImport]
[Guid("5B0D3235-4DBA-4D44-865E-8F1D0E4FD04D")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
unsafe interface IMemoryBufferByteAccess
{
void GetBuffer(out byte* buffer, out uint capacity);
}

上一篇文章解释了由于许多输入 channel 而导致的量子中的元素数量。但即使假设一个 channel ,如果我打印元素,它们仍然没有意义。大多数值为 0,有些值大于 1。
代码:
for (int i = 0; i < audioGraph.SamplesPerQuantum; i++)
Debug.WriteLine(dataInFloat[i]);

谢谢你。

最佳答案

But even assuming one channel, if I print the elements, they still don't make sense. Most values are 0 and some are greater than one



您需要使用 AudioDeviceInputNode.AddOutgoingConnection 在开始音频图之前将输入和输出节点链接在一起的方法:
deviceInputNode = result1.DeviceInputNode;
frameOutputNode = audioGraph.CreateFrameOutputNode();
deviceInputNode.AddOutgoingConnection(frameOutputNode);
audioGraph.Start();
audioGraph.QuantumProcessed += AudioGraph_QuantumProcessed;

enter image description here

frameOutputNode = audioGraph.CreateFrameOutputNode();
frameOutputNode.Start();



为什么要启动输出节点?请调用 AudioGraph.Start()方法启动音频图,否则 QuantumProcessed事件不会被调用。

关于c# - 如何使用 AudioGraph 获取音量读数 - UWP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39666277/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com