Skip to content

File Fader.h

File List > Amplitude > Sound > Fader.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_SOUND_FADER_H
#define _AM_SOUND_FADER_H

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

#include <SparkyStudios/Audio/Amplitude/Math/Utils.h>

#include <map>
#include <memory>

namespace SparkyStudios::Audio::Amplitude
{
    enum eFaderState : AmInt8
    {
        eFaderState_Stopped = -1,

        eFaderState_Disabled = 0,

        eFaderState_Active = 1,
    };

    class AM_API_PUBLIC FaderInstance
    {
    public:
        struct Transition
        {
        public:
            Transition(AmReal32 x1, AmReal32 y1, AmReal32 x2, AmReal32 y2);

            explicit Transition(const BezierCurveControlPoints& controlPoints);

            [[nodiscard]] AmTime Ease(AmTime t) const;

            BezierCurveControlPoints m_controlPoints;

        private:
            [[nodiscard]] AmTime GetTFromX(AmReal64 x) const;

            AmReal64 _samples[11];
        };

        FaderInstance();

        virtual ~FaderInstance() = default;

        void Set(AmReal64 from, AmReal64 to, AmTime duration);

        void Set(AmReal64 from, AmReal64 to);

        void SetDuration(AmTime duration);

        virtual AmReal64 GetFromTime(AmTime time);

        virtual AmReal64 GetFromPercentage(AmReal64 percentage);

        [[nodiscard]] AM_INLINE eFaderState GetState() const
        {
            return m_state;
        }

        AM_INLINE void SetState(eFaderState state)
        {
            m_state = state;
        }

        void Start(AmTime time = 0.0);

    protected:
        AmReal64 m_from;

        AmReal64 m_to;

        AmReal64 m_delta;

        AmTime m_time;

        AmTime m_startTime;

        AmTime m_endTime;

        eFaderState m_state;

        Transition m_curve;
    };

    class AM_API_PUBLIC Fader
    {
    public:
        explicit Fader(AmString name);

        Fader();

        virtual ~Fader();

        virtual std::shared_ptr<FaderInstance> CreateInstance() = 0;

        [[nodiscard]] const AmString& GetName() const;

        [[nodiscard]] virtual BezierCurveControlPoints GetControlPoints() const = 0;

        static void Register(std::shared_ptr<Fader> fader);

        static void Unregister(std::shared_ptr<const Fader> fader);

        static std::shared_ptr<Fader> Find(const AmString& name);

        static std::shared_ptr<FaderInstance> Construct(const AmString& name);

        static void LockRegistry();

        static void UnlockRegistry();

        static const std::map<AmString, std::shared_ptr<Fader>>& GetRegistry();

    protected:
        AmString m_name;
    };
} // namespace SparkyStudios::Audio::Amplitude

#endif // _AM_SOUND_FADER_H