Niels
Niels

Reputation: 2596

How to use default output device as a source to record audio with AudioGraph?

In my UWP app I have the below code, that works with an input device (DeviceInformation), to record audio and to process this. I want to extend this, by also using the default output device instead of the mic. This basically means the app will analyze the audio going over the audio card and the speakers.

This is my code:

 AudioGraphSettings settings = new AudioGraphSettings(AudioRenderCategory.Media)
            {
                QuantumSizeSelectionMode = QuantumSizeSelectionMode.LowestLatency
            };

            CreateAudioGraphResult result = await AudioGraph.CreateAsync(settings);

            if (result.Status != AudioGraphCreationStatus.Success)
            {
                // Cannot create graph
                System.Diagnostics.Debug.WriteLine(String.Format("AudioGraph Creation Error because {0}", result.Status.ToString()));
                return;
            }

            graph = result.Graph;

            // Create a device output node
            CreateAudioDeviceOutputNodeResult deviceOutputNodeResult = await graph.CreateDeviceOutputNodeAsync();
            if (deviceOutputNodeResult.Status != AudioDeviceNodeCreationStatus.Success)
            {
                return;
            }

            AudioDeviceOutputNode deviceOutputNode = deviceOutputNodeResult.DeviceOutputNode;
            System.Diagnostics.Debug.WriteLine("Device Output connection successfully created");


            // Create a device input node using the default audio input device
            CreateAudioDeviceInputNodeResult deviceInputNodeResult = await graph.CreateDeviceInputNodeAsync(MediaCategory.Other, graph.EncodingProperties, SelectedDevice);

            if (deviceInputNodeResult.Status != AudioDeviceNodeCreationStatus.Success)
            {
                // Cannot create device input node
                System.Diagnostics.Debug.WriteLine(String.Format("Audio Device Input unavailable because {0}", deviceInputNodeResult.Status.ToString()));

                return;
            }

            AudioDeviceInputNode deviceInputNode = deviceInputNodeResult.DeviceInputNode;
            System.Diagnostics.Debug.WriteLine("Device Input connection successfully created");


            frameOutputNode = graph.CreateFrameOutputNode();
            deviceInputNode.AddOutgoingConnection(frameOutputNode);

            AudioFrameInputNode frameInputNode = graph.CreateFrameInputNode();
            frameInputNode.AddOutgoingConnection(deviceOutputNode);
            //   frameInputNode.QuantumStarted += FrameInputNode_QuantumStarted;

            // Attach to QuantumStarted event in order to receive synchronous updates from audio graph (to capture incoming audio).
            graph.QuantumStarted += GraphOnQuantumProcessed;

How can I use the default output device in

CreateAudioDeviceInputNodeResult deviceInputNodeResult = await graph.CreateDeviceInputNodeAsync(MediaCategory.Other, graph.EncodingProperties, SelectedDevice);

Upvotes: 2

Views: 1012

Answers (1)

Arlo
Arlo

Reputation: 990

To get the current default audio output (Render) device in one line:

var defaultDevice = await DeviceInformation.CreateFromIdAsync(MediaDevice.GetDefaultAudioRenderId(AudioDeviceRole.Default));

You can also get the default Capture device and listen to changed events for each with the MediaDevice class.

It would seem that you can't use a Render device as an input node to record system audio (see here)

Upvotes: 0

Related Questions