如何使用AudioGraph - UWP获取音量

时间:2016-09-23 17:17:20

标签: 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);
    }

前一篇文章解释了由于许多输入通道而导致的量子元素数量。但即使假设一个频道,如果我打印元素,它们仍然没有意义。大多数值为0,有些值大于1。 代码:

for (int i = 0; i < audioGraph.SamplesPerQuantum; i++)
            Debug.WriteLine(dataInFloat[i]);

谢谢。

2 个答案:

答案 0 :(得分:2)

  

但即使假设一个频道,如果我打印元素,它们仍然没有意义。大多数值为0,有些值大于1

在启动音频图之前,您需要使用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事件。

答案 1 :(得分:0)

您可以使用.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);
        }
    }