carb/audio/AudioTypes.h

File members: carb/audio/AudioTypes.h

// Copyright (c) 2018-2023, 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 "../assets/AssetsTypes.h"
#include "carb/Types.h"
#include "carb/extras/Guid.h"

namespace carb
{
namespace audio
{

struct Context DOXYGEN_EMPTY_CLASS;

struct Voice DOXYGEN_EMPTY_CLASS;

constexpr size_t kMaxNameLength = 512;
constexpr size_t kMaxChannels = 64;
constexpr size_t kMinChannels = 1;
constexpr size_t kMaxFrameRate = 200000;
constexpr size_t kMinFrameRate = 1000;
enum class UnitType : uint32_t
{
    eBytes,
    eFrames,
    eMilliseconds,
    eMicroseconds,
};

enum class AudioResult
{
    eOk,
    eDeviceDisconnected,
    eDeviceLost,
    eDeviceNotOpen,
    eDeviceOpen,
    eOutOfRange,
    eTryAgain,
    eOutOfMemory,
    eInvalidParameter,
    eNotAllowed,
    eNotFound,
    eIoError,
    eInvalidFormat,
    eOverrun,
    eNotSupported,
};

enum class Speaker
{
    eFrontLeft,
    eFrontRight,
    eFrontCenter,
    eLowFrequencyEffect,
    eBackLeft,
    eBackRight,
    eBackCenter,
    eSideLeft,
    eSideRight,
    eTopFrontLeft,
    eTopFrontRight,
    eTopBackLeft,
    eTopBackRight,
    eFrontLeftWide,
    eFrontRightWide,
    eTopLeft,
    eTopRight,

    eCount,
};

typedef uint64_t SpeakerMode;

constexpr SpeakerMode makeSpeakerFlag(Speaker s)
{
    return 1ull << static_cast<SpeakerMode>(s);
}

constexpr SpeakerMode makeSpeakerFlag(size_t s)
{
    return makeSpeakerFlag(static_cast<Speaker>(s));
}

constexpr SpeakerMode fSpeakerFlagFrontLeft = makeSpeakerFlag(Speaker::eFrontLeft);

constexpr SpeakerMode fSpeakerFlagFrontRight = makeSpeakerFlag(Speaker::eFrontRight);

constexpr SpeakerMode fSpeakerFlagFrontCenter = makeSpeakerFlag(Speaker::eFrontCenter);

constexpr SpeakerMode fSpeakerFlagLowFrequencyEffect = makeSpeakerFlag(Speaker::eLowFrequencyEffect);

constexpr SpeakerMode fSpeakerFlagSideLeft = makeSpeakerFlag(Speaker::eSideLeft);

constexpr SpeakerMode fSpeakerFlagSideRight = makeSpeakerFlag(Speaker::eSideRight);

constexpr SpeakerMode fSpeakerFlagBackLeft = makeSpeakerFlag(Speaker::eBackLeft);

constexpr SpeakerMode fSpeakerFlagBackRight = makeSpeakerFlag(Speaker::eBackRight);

constexpr SpeakerMode fSpeakerFlagBackCenter = makeSpeakerFlag(Speaker::eBackCenter);

constexpr SpeakerMode fSpeakerFlagTopFrontLeft = makeSpeakerFlag(Speaker::eTopFrontLeft);

constexpr SpeakerMode fSpeakerFlagTopFrontRight = makeSpeakerFlag(Speaker::eTopFrontRight);

constexpr SpeakerMode fSpeakerFlagTopBackLeft = makeSpeakerFlag(Speaker::eTopBackLeft);

constexpr SpeakerMode fSpeakerFlagTopBackRight = makeSpeakerFlag(Speaker::eTopBackRight);

constexpr SpeakerMode fSpeakerFlagFrontLeftWide = makeSpeakerFlag(Speaker::eFrontLeftWide);

constexpr SpeakerMode fSpeakerFlagFrontRightWide = makeSpeakerFlag(Speaker::eFrontRightWide);

constexpr SpeakerMode fSpeakerFlagTopLeft = makeSpeakerFlag(Speaker::eTopLeft);

constexpr SpeakerMode fSpeakerFlagTopRight = makeSpeakerFlag(Speaker::eTopRight);
constexpr size_t kInvalidSpeakerName = ~0ull;

constexpr SpeakerMode kSpeakerModeDefault = 0;

constexpr SpeakerMode kSpeakerModeMono = fSpeakerFlagFrontLeft;

constexpr SpeakerMode kSpeakerModeStereo = fSpeakerFlagFrontLeft | fSpeakerFlagFrontRight;

constexpr SpeakerMode kSpeakerModeTwoPointOne =
    fSpeakerFlagFrontLeft | fSpeakerFlagFrontRight | fSpeakerFlagLowFrequencyEffect;

constexpr SpeakerMode kSpeakerModeQuad =
    fSpeakerFlagFrontLeft | fSpeakerFlagFrontRight | fSpeakerFlagBackLeft | fSpeakerFlagBackRight;

constexpr SpeakerMode kSpeakerModeFourPointOne = fSpeakerFlagFrontLeft | fSpeakerFlagFrontRight | fSpeakerFlagBackLeft |
                                                 fSpeakerFlagBackRight | fSpeakerFlagLowFrequencyEffect;

constexpr SpeakerMode kSpeakerModeFivePointOne = fSpeakerFlagFrontLeft | fSpeakerFlagFrontRight |
                                                 fSpeakerFlagFrontCenter | fSpeakerFlagLowFrequencyEffect |
                                                 fSpeakerFlagBackLeft | fSpeakerFlagBackRight;

constexpr SpeakerMode kSpeakerModeSixPointOne = fSpeakerFlagFrontLeft | fSpeakerFlagFrontRight |
                                                fSpeakerFlagFrontCenter | fSpeakerFlagLowFrequencyEffect |
                                                fSpeakerFlagBackCenter | fSpeakerFlagSideLeft | fSpeakerFlagSideRight;

constexpr SpeakerMode kSpeakerModeSevenPointOne =
    fSpeakerFlagFrontLeft | fSpeakerFlagFrontRight | fSpeakerFlagFrontCenter | fSpeakerFlagLowFrequencyEffect |
    fSpeakerFlagSideLeft | fSpeakerFlagSideRight | fSpeakerFlagBackLeft | fSpeakerFlagBackRight;

constexpr SpeakerMode kSpeakerModeNinePointOne =
    fSpeakerFlagFrontLeft | fSpeakerFlagFrontRight | fSpeakerFlagFrontCenter | fSpeakerFlagLowFrequencyEffect |
    fSpeakerFlagSideLeft | fSpeakerFlagSideRight | fSpeakerFlagBackLeft | fSpeakerFlagBackRight |
    fSpeakerFlagFrontLeftWide | fSpeakerFlagFrontRightWide;

constexpr SpeakerMode kSpeakerModeSevenPointOnePointFour =
    fSpeakerFlagFrontLeft | fSpeakerFlagFrontRight | fSpeakerFlagFrontCenter | fSpeakerFlagLowFrequencyEffect |
    fSpeakerFlagSideLeft | fSpeakerFlagSideRight | fSpeakerFlagBackLeft | fSpeakerFlagBackRight |
    fSpeakerFlagTopFrontLeft | fSpeakerFlagTopFrontRight | fSpeakerFlagTopBackLeft | fSpeakerFlagTopBackRight;

constexpr SpeakerMode kSpeakerModeNinePointOnePointFour =
    fSpeakerFlagFrontLeft | fSpeakerFlagFrontRight | fSpeakerFlagFrontCenter | fSpeakerFlagLowFrequencyEffect |
    fSpeakerFlagSideLeft | fSpeakerFlagSideRight | fSpeakerFlagBackLeft | fSpeakerFlagBackRight |
    fSpeakerFlagFrontLeftWide | fSpeakerFlagFrontRightWide | fSpeakerFlagTopFrontLeft | fSpeakerFlagTopFrontRight |
    fSpeakerFlagTopBackLeft | fSpeakerFlagTopBackRight;

constexpr SpeakerMode kSpeakerModeNinePointOnePointSix =
    fSpeakerFlagFrontLeft | fSpeakerFlagFrontRight | fSpeakerFlagFrontCenter | fSpeakerFlagLowFrequencyEffect |
    fSpeakerFlagSideLeft | fSpeakerFlagSideRight | fSpeakerFlagBackLeft | fSpeakerFlagBackRight |
    fSpeakerFlagFrontLeftWide | fSpeakerFlagFrontRightWide | fSpeakerFlagTopFrontLeft | fSpeakerFlagTopFrontRight |
    fSpeakerFlagTopBackLeft | fSpeakerFlagTopBackRight | fSpeakerFlagTopLeft | fSpeakerFlagTopRight;

constexpr SpeakerMode kSpeakerModeThreePointZero =
    fSpeakerFlagFrontLeft | fSpeakerFlagFrontRight | fSpeakerFlagFrontCenter;

constexpr SpeakerMode kSpeakerModeFivePointZero = fSpeakerFlagFrontLeft | fSpeakerFlagFrontRight |
                                                  fSpeakerFlagFrontCenter | fSpeakerFlagBackLeft | fSpeakerFlagBackRight;

constexpr size_t kSpeakerModeCount = 7;

constexpr SpeakerMode fSpeakerModeValidBits =
    fSpeakerFlagFrontLeft | fSpeakerFlagFrontRight | fSpeakerFlagFrontCenter | fSpeakerFlagLowFrequencyEffect |
    fSpeakerFlagSideLeft | fSpeakerFlagSideRight | fSpeakerFlagBackLeft | fSpeakerFlagBackRight |
    fSpeakerFlagFrontLeftWide | fSpeakerFlagFrontRightWide | fSpeakerFlagTopFrontLeft | fSpeakerFlagTopFrontRight |
    fSpeakerFlagTopBackLeft | fSpeakerFlagTopBackRight | fSpeakerFlagTopLeft | fSpeakerFlagTopRight;
typedef uint32_t DeviceFlags;

constexpr DeviceFlags fDeviceFlagNotOpen = 0x00000000;
constexpr DeviceFlags fDeviceFlagConnected = 0x00000001;
constexpr DeviceFlags fDeviceFlagDefault = 0x00000002;
constexpr DeviceFlags fDeviceFlagStreamer = 0x00000004;
typedef void(CARB_ABI* UserDataDestructor)(void* userData);

struct UserData
{
    void* data = nullptr;

    UserDataDestructor destructor = nullptr;
};

enum class SampleFormat : uint32_t
{
    ePcm8,

    ePcm16,

    ePcm24,

    ePcm32,

    ePcmFloat,

    ePcmCount,

    eVorbis,

    eFlac,

    eOpus,

    eMp3,

    eRaw,

    eDefault,

    eCount,
};

struct SoundFormat
{
    size_t channels;

    size_t bitsPerSample;

    size_t frameSize;

    size_t blockSize;

    size_t framesPerBlock;

    size_t frameRate;

    SpeakerMode channelMask;

    size_t validBitsPerSample;

    SampleFormat format = SampleFormat::eDefault;
};

constexpr size_t kInvalidDeviceIndex = ~0ull;

struct DeviceCaps
{
    size_t thisSize = sizeof(DeviceCaps);

    size_t index;

    DeviceFlags flags;

    char name[kMaxNameLength];

    carb::extras::Guid guid;

    size_t channels;

    size_t frameRate;

    SampleFormat format = SampleFormat::eDefault;
};

constexpr size_t kDefaultFrameRate = 48000;
constexpr size_t kDefaultChannelCount = 1;
constexpr SampleFormat kDefaultFormat = SampleFormat::ePcmFloat;
constexpr int64_t kImperceptibleDelay = 200000;

} // namespace audio
} // namespace carb