Examples#

Enumerating Playback Devices#

Collecting information about the connected audio devices is relatively straightforward. The only information that is needed is the device direction (playback or capture), and the index of the device to colelct the information for. The index of the device is just an integer ranging from 0 to one less than the total device count for the requested direction.

Python#

The example below collects the information about all of the playback devices that are currently connected to the system. To enumerate the capture devices instead, the only change needed is to change line 5 to set direction to the following:

direction = omni.kit.audiodeviceenum.Direction.CAPTURE

Enumerating playback devices:

import omni.kit.audiodeviceenum

device_enum = omni.kit.audiodeviceenum.get_audio_device_enum_interface()

direction = omni.kit.audiodeviceenum.Direction.PLAYBACK

count = device_enum.get_device_count(direction)

name = device_enum.get_device_name(direction, 0)
id = device_enum.get_device_id(direction, 0)
channels = device_enum.get_device_channel_count(direction, 0)
frame_rate = device_enum.get_device_frame_rate(direction, 0)
sample_size = device_enum.get_device_sample_size(direction, 0)
format = device_enum.get_device_sample_type(direction, 0)

print("Default playback device:")
print(f"    name = '{name}'")
print(f"    id = '{id}'")
print(f"    channels = '{channels}'")
print(f"    frame_rate = '{frame_rate}'")
print(f"    sample_size = '{sample_size}'")
print(f"    format = '{format}'")

if count > 1:
    print("Other playback devices:")

    for i in range(1, count):
        name = device_enum.get_device_name(direction, i)
        id = device_enum.get_device_id(direction, i)
        channels = device_enum.get_device_channel_count(direction, i)
        frame_rate = device_enum.get_device_frame_rate(direction, i)
        sample_size = device_enum.get_device_sample_size(direction, i)
        format = device_enum.get_device_sample_type(direction, i)

        print(f"    {i}: name = '{name}'")
        print(f"         id = '{id}'")
        print(f"         channels = '{channels}'")
        print(f"         frame_rate = '{frame_rate}'")
        print(f"         sample_size = '{sample_size}'")
        print(f"         format = '{format}'")

C++#

Using the IAudioDeviceEnum interface on the C++ side is largely the same. The same set of interface functions are still supported and behave in the same manner. The following is an example of using the C++ interface to perform the same task:

First, include the IAudioDeviceEnum interface’s main header and a couple of other helpful headers:

#include <omni/audio/IAudioDeviceEnum.h>

#include <carb/InterfaceUtils.h>
#include <carb/logging/Log.h>

Then make a helper function that writes the connected devices’ information to the app log:

bool logConnectedAudioDevices()
{
    size_t count;
    char name[256];
    char id[256];
    size_t channels;
    size_t frameRate;
    size_t sampleSize;
    omni::audio::SampleType sampleFormat;
    omni::audio::Direction direction = omni::audio::Direction::ePlayback;

    auto deviceEnum = carb::getCachedInterface<omni::audio::IAudioDeviceEnum>();
    auto getSampleTypeName = [](omni::audio::SampleType type) -> const char* {
        switch (type)
        {
            case omni::audio::SampleType::eUnknown:
                return "Unknown";

            case omni::audio::SampleType::ePcmSignedInteger:
                return "PCM Signed Integer";

            case omni::audio::SampleType::ePcmUnsignedInteger:
                return "PCM Unsigned Integer";

            case omni::audio::SampleType::ePcmFloat:
                return "PCM Float";

            case omni::audio::SampleType::eCompressed:
                return "Compressed";

            default:
                return "<unknown>";
        }
    };


    if (deviceEnum == nullptr)
        return false;

    count = deviceEnum->getDeviceCount(direction);

    deviceEnum->getDeviceName(direction, 0, name, CARB_COUNTOF(name));
    deviceEnum->getDeviceId(direction, 0, id, CARB_COUNTOF(id));
    channels = deviceEnum->getDeviceChannelCount(direction, 0);
    frameRate = deviceEnum->getDeviceFrameRate(direction, 0);
    sampleSize = deviceEnum->getDeviceSampleSize(direction, 0);
    sampleFormat = deviceEnum->getDeviceSampleType(direction, 0);

    CARB_LOG_INFO("Default playback device:");
    CARB_LOG_INFO("    name = '%s'", name);
    CARB_LOG_INFO("    id = '%s'", id);
    CARB_LOG_INFO("    channels = %zu", channels);
    CARB_LOG_INFO("    frameRate = %zu", frameRate);
    CARB_LOG_INFO("    sampleSize = %zu", sampleSize);
    CARB_LOG_INFO("    sampleFormat = '%s'", getSampleTypeName(sampleFormat));

    if (count > 1)
    {
        CARB_LOG_INFO("Other playback devices:");

        for (size_t i = 1; i < count; i++)
        {
            deviceEnum->getDeviceName(direction, i, name, CARB_COUNTOF(name));
            deviceEnum->getDeviceId(direction, i, id, CARB_COUNTOF(id));
            channels = deviceEnum->getDeviceChannelCount(direction, i);
            frameRate = deviceEnum->getDeviceFrameRate(direction, i);
            sampleSize = deviceEnum->getDeviceSampleSize(direction, i);
            sampleFormat = deviceEnum->getDeviceSampleType(direction, i);

            CARB_LOG_INFO("    %zu: name = '%s'", i, name);
            CARB_LOG_INFO("       id = '%s'", id);
            CARB_LOG_INFO("       channels = %zu", channels);
            CARB_LOG_INFO("       frameRate = %zu", frameRate);
            CARB_LOG_INFO("       sampleSize = %zu", sampleSize);
            CARB_LOG_INFO("       sampleFormat = '%s'", getSampleTypeName(sampleFormat));
        }
    }

    return true;
}