我想获得带有音频帧输出节点的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);
}
上一篇文章解释了由于许多输入通道而导致的量子元素数量。但是即使假设一个通道,如果我打印这些元素,它们仍然没有任何意义。多数值为0,有些大于1。
码:
for (int i = 0; i < audioGraph.SamplesPerQuantum; i++)
Debug.WriteLine(dataInFloat[i]);
谢谢。
最佳答案
在启动音频图之前,需要使用 AudioDeviceInputNode.AddOutgoingConnection
方法将输入和输出节点链接在一起:
deviceInputNode = result1.DeviceInputNode;
frameOutputNode = audioGraph.CreateFrameOutputNode();
deviceInputNode.AddOutgoingConnection(frameOutputNode);
audioGraph.Start();
audioGraph.QuantumProcessed += AudioGraph_QuantumProcessed;
为什么启动输出节点?请调用
AudioGraph.Start()
方法启动音频图,否则将不调用QuantumProcessed
事件。