Skip to content

File Common.h

File List > Amplitude > Core > Common.h

Go to the documentation of this file

// Copyright (c) 2021-present Sparky Studios. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#ifndef _AM_CORE_COMMON_H
#define _AM_CORE_COMMON_H

// Amplitude
// --------------------------------------------------------------

#include <SparkyStudios/Audio/Amplitude/Core/Common/Config.h>
#include <SparkyStudios/Audio/Amplitude/Core/Common/Types.h>

#include <SparkyStudios/Audio/Amplitude/Core/Common/Constants.h>

// Common defines
// --------------------------------------------------------------

#define AM_INVALID_HANDLE nullptr

#define AM_IS_VALID_HANDLE(handle) ((handle) != AM_INVALID_HANDLE)

#define AM_CLAMP(v, a, b) (((v) < (a)) ? (a) : ((v) > (b)) ? (b) : (v))

#define AM_BETWEEN(v, a, b) ((v) >= AM_MIN(a, b) && (v) <= AM_MAX(a, b))

#define AM_MIN(a, b) ((a) < (b) ? (a) : (b))

#define AM_MAX(a, b) ((a) > (b) ? (a) : (b))

#define AM_ABS(a) ((a) > 0 ? (a) : -(a))

#define AM_MOD(a, m) (((a) % (m)) >= 0 ? ((a) % (m)) : (((a) % (m)) + (m)))

#define AM_SQUARED(x) ((x) * (x))

#define AM_CUBED(x) ((x) * (x) * (x))

#define AM_CALLBACK(_type_, _name_) typedef _type_(AM_CALL_POLICY* _name_)

#define AM_AUDIO_SAMPLE_MIN (-1.0f)

#define AM_AUDIO_SAMPLE_MAX (+1.0f)

#define AM_UNUSED(x) ((void)(x))

#define AM_STRING_EXPAND(X) #X

#define AM_TO_STRING(X) AM_STRING_EXPAND(X)

namespace SparkyStudios::Audio::Amplitude
{
    class AM_API_PUBLIC AmAlignedReal32Buffer
    {
    public:
        AmAlignedReal32Buffer();

        ~AmAlignedReal32Buffer();

        AmResult Init(AmUInt32 size, bool clear = true);

        void Clear() const;

        void Release();

        [[nodiscard]] AM_INLINE AmUInt32 GetSize() const
        {
            return m_floats;
        }

        [[nodiscard]] AM_INLINE AmReal32* GetBuffer() const
        {
            return m_data;
        }

        [[nodiscard]] AM_INLINE AmUInt8Buffer GetPointer() const
        {
            return reinterpret_cast<AmUInt8*>(m_data);
        }

        void CopyFrom(const AmAlignedReal32Buffer& other) const;

        void Resize(AmUInt32 size, bool clear = true);

        static void Swap(AmAlignedReal32Buffer& a, AmAlignedReal32Buffer& b);

        [[nodiscard]] AM_INLINE const AmReal32* begin() const
        {
            return m_data;
        }

        [[nodiscard]] AM_INLINE const AmReal32* end() const
        {
            return m_data + m_floats;
        }

        [[nodiscard]] AM_INLINE AmReal32* begin()
        {
            return m_data;
        }

        [[nodiscard]] AM_INLINE AmReal32* end()
        {
            return m_data + m_floats;
        }

        AmReal32& operator[](const AmSize index)
        {
            AMPLITUDE_ASSERT(m_data != nullptr && index < m_floats);
            return m_data[index];
        }

        const AmReal32& operator[](const AmSize index) const
        {
            AMPLITUDE_ASSERT(m_data != nullptr && index < m_floats);
            return m_data[index];
        }

    private:
        AmReal32* m_data; // aligned pointer
        AmUInt32 m_floats; // size of buffer (w/out padding)
    };

    enum eErrorCode : AmUInt8
    {
        eErrorCode_Success = 0, 
        eErrorCode_InvalidParameter = 1, 
        eErrorCode_FileNotFound = 2, 
        eErrorCode_FileLoadFailed = 3, 
        eErrorCode_DllNotFound = 4, 
        eErrorCode_OutOfMemory = 5, 
        eErrorCode_NotImplemented = 6, 
        eErrorCode_Unknown = 7 
    };

    enum eAudioSampleFormat : AmUInt8
    {
        eAudioSampleFormat_Float32, 
        eAudioSampleFormat_Int16, //< 16-bit signed integer sample
        eAudioSampleFormat_Unknown, 
    };

    enum eSpatialization : AmUInt8
    {
        eSpatialization_None,

        eSpatialization_Position,

        eSpatialization_PositionOrientation,

        eSpatialization_HRTF
    };

    enum eScope : AmUInt8
    {
        eScope_World,

        eScope_Entity
    };

    enum ePanningMode : AmUInt8
    {
        ePanningMode_Stereo = 0,

        ePanningMode_BinauralLowQuality = 1,

        ePanningMode_BinauralMediumQuality = 2,

        ePanningMode_BinauralHighQuality = 3,
    };

    enum eHRIRSphereSamplingMode : AmUInt8
    {
        eHRIRSphereSamplingMode_Bilinear = 0,

        eHRIRSphereSamplingMode_NearestNeighbor = 1,
    };

    enum eParameterType
    {
        eParameterType_Float = 0,

        eParameterType_Int,

        eParameterType_Bool
    };

    struct AM_API_PUBLIC SoundFormat
    {
    public:
        void SetAll(
            AmUInt32 sampleRate,
            AmUInt16 numChannels,
            AmUInt32 bitsPerSample,
            AmUInt64 framesCount,
            AmUInt32 frameSize,
            eAudioSampleFormat sampleType);

        [[nodiscard]] AM_INLINE AmUInt32 GetSampleRate() const
        {
            return _sampleRate;
        }

        [[nodiscard]] AM_INLINE AmUInt16 GetNumChannels() const
        {
            return _numChannels;
        }

        [[nodiscard]] AM_INLINE AmUInt32 GetBitsPerSample() const
        {
            return _bitsPerSample;
        }

        [[nodiscard]] AM_INLINE AmUInt64 GetFramesCount() const
        {
            return _framesCount;
        }

        [[nodiscard]] AM_INLINE AmUInt32 GetFrameSize() const
        {
            return _frameSize;
        }

        [[nodiscard]] AM_INLINE eAudioSampleFormat GetSampleType() const
        {
            return _sampleType;
        }

    private:
        AmUInt32 _sampleRate = 0;
        AmUInt16 _numChannels = 0;
        AmUInt32 _bitsPerSample = 0;
        AmUInt64 _framesCount = 0;
        AmUInt32 _frameSize = 0;
        eAudioSampleFormat _sampleType = eAudioSampleFormat_Float32;
    };
} // namespace SparkyStudios::Audio::Amplitude

#endif // _AM_CORE_COMMON_H