【问题标题】:How to get volume reading with AudioGraph - UWP如何使用 AudioGraph 获取音量读数 - UWP
【发布时间】:2017-02-01 15:20:21
【问题描述】:

我想使用带有音频帧输出节点的 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]);

谢谢。

【问题讨论】:

  • 我正在处理这个

标签: c# audio uwp windows-10-universal


【解决方案1】:

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

在启动音频图之前,您需要使用AudioDeviceInputNode.AddOutgoingConnection 方法将输入和输出节点链接在一起:

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

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

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

【讨论】:

  • 谢谢! AudioDeviceInputNode.AddOutgoingConnection 工作。
【解决方案2】:

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

【讨论】:

  • 双声道/立体声音频怎么样?每个声道的音量如何分别设置?
猜你喜欢
  • 2016-04-14
  • 1970-01-01
  • 2020-08-13
  • 2017-07-18
  • 1970-01-01
  • 2016-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多