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

How to get volume reading with AudioGraph - UWP

我想通过带有音频帧输出节点的 AudioGraph 获得音量级别。这个 post, , 有一些很好的信息;但我无法获得好的读数。

代码:

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

上一篇文章解释了一个quantum中的元素个数,因为输入通道很多。但即使假设一个通道,如果我打印元素,它们仍然没有意义。大多数值为 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方法将输入和输出节点Link在一起:

deviceInputNode = result1.DeviceInputNode;
frameOutputNode = audioGraph.CreateFrameOutputNode();
deviceInputNode.AddOutgoingConnection(frameOutputNode);
audioGraph.Start();
audioGraph.QuantumProcessed += AudioGraph_QuantumProcessed;

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

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

您可以使用 .OutgoingGain 属性 获取和设置音量级别,如下所示。

private static async Task AddFileToSounds(string uri)
    {
        // Load and add resource sound file to memory dictionary for playing

        var soundFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(uri));
        var fileInputResult = await graph.CreateFileInputNodeAsync(soundFile);

        if (AudioFileNodeCreationStatus.Success == fileInputResult.Status)
        {
            fileInputs.Add(soundFile.Name, fileInputResult.FileInputNode);
            fileInputResult.FileInputNode.Stop();
            // set volume here using outgoing gain, values 0 - 1
            fileInputResult.FileInputNode.OutgoingGain = 0.1;
            // get volume using the same property
            Debug.WriteLine("fileInputResult.FileInputNode.OutgoingGain = "+ fileInputResult.FileInputNode.OutgoingGain);
            fileInputResult.FileInputNode.AddOutgoingConnection(deviceOutput);
        }
    }