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

标签 c# audio uwp windows-10-universal

我想通过带有音频帧输出节点的 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/

相关文章:

c# - Windows Phone 8 上的 2D 游戏有哪些选项?

c# - 压缩 XML 的最佳方式

c# - 拼写检查器推荐

ios - 捕获 iPhone 游戏音频

javascript - 如何在博客中添加欢迎声音?

android - 在声音设置android中禁用音量选项

c# - ASP.Net MVC 应用程序看似忽略数据库连接字符串

c# - 如何防止 UserControl 的内容被覆盖?

uwp - 绘制虚线边框

c# - UWP XAML x :Bind inherited interfaces are not recognized