Skip to content

File Curve.h

File List > Amplitude > Math > Curve.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_MATH_CURVE_H
#define _AM_MATH_CURVE_H

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

#include <SparkyStudios/Audio/Amplitude/Sound/Fader.h>

namespace SparkyStudios::Audio::Amplitude
{
    class CurveDefinition;
    class CurvePartDefinition;

    struct AM_API_PUBLIC CurvePoint
    {
        AmReal64 x;

        AmReal32 y;

        bool operator==(const CurvePoint& rhs) const;
        bool operator!=(const CurvePoint& rhs) const;
    };

    class AM_API_PUBLIC CurvePart
    {
    public:
        CurvePart();

        CurvePart(const CurvePart& other);

        ~CurvePart();

        void Initialize(const CurvePartDefinition* definition);

        [[nodiscard]] const CurvePoint& GetStart() const;

        void SetStart(const CurvePoint& start);

        [[nodiscard]] const CurvePoint& GetEnd() const;

        void SetEnd(const CurvePoint& end);

        [[nodiscard]] std::shared_ptr<FaderInstance> GetFader() const;

        void SetFader(const AmString& fader);

        [[nodiscard]] AmReal32 Get(AmReal64 x) const;

        CurvePart& operator=(const CurvePart& other);

    private:
        friend class Curve;

        CurvePoint _start;
        CurvePoint _end;

        AmString _faderName;
        std::shared_ptr<FaderInstance> _fader;
    };

    class AM_API_PUBLIC Curve
    {
    public:
        Curve();

        void Initialize(const CurveDefinition* definition);

        void Initialize(const std::vector<CurvePart>& parts);

        [[nodiscard]] AmReal32 Get(AmReal64 x) const;

    private:
        // Finds the curve part corresponding to the given X value.
        [[nodiscard]] const CurvePart* _findCurvePart(AmReal64 x) const;

        // The parts of the curve.
        std::vector<CurvePart> _parts;
    };
} // namespace SparkyStudios::Audio::Amplitude
#endif // _AM_MATH_CURVE_H