Skip to content

File Codec.h

File List > Amplitude > Core > Codec.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_CODEC_H
#define _AM_CORE_CODEC_H

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

#include <map>

namespace SparkyStudios::Audio::Amplitude
{
    class AM_API_PUBLIC Codec
    {
    public:
        class AM_API_PUBLIC Decoder
        {
        public:
            explicit Decoder(const Codec* codec);

            virtual ~Decoder() = default;

            virtual bool Open(std::shared_ptr<File> file) = 0;

            virtual bool Close() = 0;

            [[nodiscard]] const SoundFormat& GetFormat() const;

            virtual AmUInt64 Load(AudioBuffer* out) = 0;

            virtual AmUInt64 Stream(AudioBuffer* out, AmUInt64 bufferOffset, AmUInt64 seekOffset, AmUInt64 length) = 0;

            virtual bool Seek(AmUInt64 offset) = 0;

        protected:
            SoundFormat m_format;

            const Codec* m_codec;
        };

        class AM_API_PUBLIC Encoder
        {
        public:
            explicit Encoder(const Codec* codec);

            virtual ~Encoder() = default;

            virtual bool Open(std::shared_ptr<File> file) = 0;

            virtual bool Close() = 0;

            virtual void SetFormat(const SoundFormat& format);

            virtual AmUInt64 Write(AudioBuffer* in, AmUInt64 offset, AmUInt64 length) = 0;

        protected:
            SoundFormat m_format;

            const Codec* m_codec;
        };

        explicit Codec(AmString name);

        virtual ~Codec();

        [[nodiscard]] virtual std::shared_ptr<Decoder> CreateDecoder() = 0;

        [[nodiscard]] virtual std::shared_ptr<Encoder> CreateEncoder() = 0;

        [[nodiscard]] virtual bool CanHandleFile(std::shared_ptr<File> file) const = 0;

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

        static void Register(std::shared_ptr<Codec> codec);

        static void Unregister(std::shared_ptr<const Codec> codec);

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

        static std::shared_ptr<Codec> FindForFile(std::shared_ptr<File> file);

        static void LockRegistry();

        static void UnlockRegistry();

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

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

#endif // _AM_CORE_CODEC_H