omni/audio/experimental/IAudioCapture.h

File members: omni/audio/experimental/IAudioCapture.h

// Copyright (c) 2021-2024, NVIDIA CORPORATION. All rights reserved.
//
// NVIDIA CORPORATION and its licensors retain all intellectual property
// and proprietary rights in and to this software, related documentation
// and any modifications thereto. Any use, reproduction, disclosure or
// distribution of this software and related documentation without an express
// license agreement from NVIDIA CORPORATION is strictly prohibited.
//
#pragma once

#include <omni/core/BuiltIn.h>
#include <omni/core/IObject.h>
#include <omni/core/Api.h>
#include <omni/log/ILog.h>

#include <carb/audio/AudioTypes.h>

namespace omni
{

namespace audio
{

namespace experimental
{
/******************************** renamed carb::audio types **************************************/
using AudioResult = carb::audio::AudioResult;

using SpeakerMode = carb::audio::SpeakerMode;

using SampleFormat = carb::audio::SampleFormat;

using SoundFormat = carb::audio::SoundFormat;

/******************************** typedefs, enums, & macros **************************************/

struct Format
{
    size_t frameRate = 0;

    size_t channels = 0;

    SpeakerMode channelMask = carb::audio::kSpeakerModeDefault;

    SampleFormat format = SampleFormat::eDefault;
};

using CaptureDeviceFlags = uint32_t;

struct CaptureDeviceDesc
{
    CaptureDeviceFlags flags = 0;

    size_t index = 0;

    Format format = {};

    size_t bufferLength = 0;

    size_t bufferFragments = 0;
};

class ICaptureStream;

using CaptureDataCallback = void (*)(ICaptureStream* stream, const void* buffer, size_t frames, void* context);

enum class CaptureInfoType
{
    eOverrun,

    eDeviceLost,
};

union CaptureInfoData
{
    struct
    {
    } overrun;

    struct
    {
    } deviceLost;

    struct
    {
        OMNI_ATTR("no_py") uint8_t x[256];
    } reserved;
};

using CaptureInfoCallback = void (*)(ICaptureStream* stream,
                                     CaptureInfoType type,
                                     const CaptureInfoData* data,
                                     void* context);

using CaptureStreamFlags = uint32_t;

constexpr CaptureStreamFlags fCaptureStreamFlagLowLatency = 0x1;

struct CaptureStreamDesc
{
    CaptureStreamFlags flags = 0;

    CaptureDataCallback dataCallback = nullptr;

    CaptureInfoCallback errorCallback = nullptr;

    void* callbackContext = nullptr;

    CaptureDeviceDesc device;
};

using CaptureStreamStartFlags = uint32_t;

constexpr CaptureStreamStartFlags fCaptureStreamStartFlagOneShot = 0x1;

using CaptureStreamStopFlags = uint32_t;

constexpr CaptureStreamStopFlags fCaptureStreamStopFlagSync = 0x1;

/********************************** IAudioCapture Interface **************************************/

class ICaptureStream_abi : public omni::core::Inherits<omni::core::IObject, OMNI_TYPE_ID("omni.audio.ICaptureStream")>
{
protected:
    virtual AudioResult start_abi(CaptureStreamStartFlags flags) noexcept = 0;

    virtual AudioResult stop_abi(CaptureStreamStopFlags flags) noexcept = 0;

    virtual bool isCapturing_abi() noexcept = 0;

    virtual AudioResult getAvailableFrames_abi(OMNI_ATTR("out") size_t* available) noexcept = 0;

    virtual AudioResult lock_abi(size_t request,
                                 OMNI_ATTR("*in, out") const void** region,
                                 OMNI_ATTR("out") size_t* received) noexcept = 0;

    virtual AudioResult unlock_abi(size_t consumed) noexcept = 0;

    virtual size_t getBufferSize_abi() noexcept = 0;

    virtual size_t getFragmentCount_abi() noexcept = 0;

    virtual void getSoundFormat_abi(OMNI_ATTR("out") SoundFormat* format) noexcept = 0;

    virtual AudioResult reset_abi() noexcept = 0;
};

class IAudioCapture
{
public:
    CARB_PLUGIN_INTERFACE("omni::audio::IAudioCapture", 0, 0)

    AudioResult (*internalCreateStream)(const CaptureStreamDesc* desc, ICaptureStream** stream);

    inline AudioResult createStream(const CaptureStreamDesc* desc, omni::core::ObjectPtr<ICaptureStream>* stream)
    {
        if (stream == nullptr)
        {
            OMNI_LOG_ERROR("stream was null");
            return carb::audio::AudioResult::eInvalidParameter;
        }

        ICaptureStream* raw = nullptr;
        AudioResult res = internalCreateStream(desc, &raw);
        *stream = omni::core::steal(raw);
        return res;
    }
};

} // namespace experimental
} // namespace audio
} // namespace omni

#define OMNI_BIND_INCLUDE_INTERFACE_DECL
#include "IAudioCapture.gen.h"

class omni::audio::experimental::ICaptureStream
    : public omni::core::Generated<omni::audio::experimental::ICaptureStream_abi>
{
};

#define OMNI_BIND_INCLUDE_INTERFACE_IMPL
#include "IAudioCapture.gen.h"