Skip to content

File AudioBuffer.h

File List > Amplitude > Core > AudioBuffer.h

Go to the documentation of this file

// Copyright (c) 2024-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_AUDIO_BUFFER_H
#define _AM_CORE_AUDIO_BUFFER_H

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

#include <vector>

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

        [[nodiscard]] AmSize size() const;

        AmReal32* begin();

        [[nodiscard]] const AmReal32* begin() const;

        AmReal32* end();

        [[nodiscard]] const AmReal32* end() const;

        void clear();

        [[nodiscard]] bool enabled() const;

        AmReal32& operator[](AmSize index);

        [[nodiscard]] const AmReal32& operator[](AmSize index) const;

        AudioBufferChannel& operator=(const std::vector<AmReal32>& data);

        AudioBufferChannel& operator=(const AudioBufferChannel& channel);

        AudioBufferChannel& operator+=(const AudioBufferChannel& channel);

        AudioBufferChannel& operator-=(const AudioBufferChannel& channel);

        AudioBufferChannel& operator*=(const AudioBufferChannel& channel);

        AudioBufferChannel& operator*=(AmReal32 scalar);

    private:
        friend class AudioBuffer;

        AudioBufferChannel(AmReal32* begin, AmSize numFrames);

        AmReal32* _begin;

        AmSize _frameCount;

        bool _isEnabled;
    };

    class AM_API_PUBLIC AudioBuffer
    {
    public:
        static void Copy(
            const AudioBuffer& source, AmSize sourceOffset, AudioBuffer& destination, AmSize destinationOffset, AmSize numFrames);

        AudioBuffer();

        AudioBuffer(AmSize numFrames, AmSize numChannels);

        AudioBuffer(const AudioBuffer& buffer) = delete;

        AudioBuffer(AudioBuffer&& buffer) noexcept;

        ~AudioBuffer();

        [[nodiscard]] bool IsEmpty() const;

        [[nodiscard]] AmSize GetFrameCount() const;

        [[nodiscard]] AmSize GetChannelCount() const;

        void Clear();

        [[nodiscard]] const AmAlignedReal32Buffer& GetData() const;

        [[nodiscard]] std::vector<AmAudioSample> GetFrame(AmSize index) const;

        AudioBufferChannel& GetChannel(AmSize index);

        [[nodiscard]] const AudioBufferChannel& GetChannel(AmSize index) const;

        AudioBuffer Clone() const;

        AudioBufferChannel& operator[](AmSize index);

        [[nodiscard]] const AudioBufferChannel& operator[](AmSize index) const;

        AudioBuffer& operator=(const AudioBuffer& buffer);

        AudioBuffer& operator+=(const AudioBuffer& buffer);

        AudioBuffer& operator-=(const AudioBuffer& buffer);

        AudioBuffer& operator*=(const AudioBuffer& buffer);

        AudioBuffer& operator*=(AmReal32 scalar);

    private:
        void Initialize(AmSize channelCount);

        AmSize _frameCount;

        std::vector<AudioBufferChannel> _channels;

        AmAlignedReal32Buffer _data;
    };
} // namespace SparkyStudios::Audio::Amplitude

#endif // _AM_CORE_AUDIO_BUFFER_H