// SONIC ROBO BLAST 2 //----------------------------------------------------------------------------- // Copyright (C) 2022-2023 by Ronald "Eidolon" Kinard // // This program is free software distributed under the // terms of the GNU General Public License, version 2. // See the 'LICENSE' file for more details. //----------------------------------------------------------------------------- #ifndef __SRB2_AUDIO_SAMPLE_HPP__ #define __SRB2_AUDIO_SAMPLE_HPP__ #include namespace srb2::audio { template struct Sample { std::array amplitudes; constexpr Sample& operator+=(const Sample& rhs) noexcept { for (std::size_t i = 0; i < C; i++) { amplitudes[i] += rhs.amplitudes[i]; } return *this; } constexpr Sample& operator*=(float rhs) noexcept { for (std::size_t i = 0; i < C; i++) { amplitudes[i] *= rhs; } return *this; } }; template constexpr Sample operator+(const Sample& lhs, const Sample& rhs) noexcept { Sample out; for (std::size_t i = 0; i < C; i++) { out.amplitudes[i] = lhs.amplitudes[i] + rhs.amplitudes[i]; } return out; } template constexpr Sample operator-(const Sample& lhs, const Sample& rhs) noexcept { Sample out; for (std::size_t i = 0; i < C; i++) { out.amplitudes[i] = lhs.amplitudes[i] - rhs.amplitudes[i]; } return out; } template constexpr Sample operator*(const Sample& lhs, float rhs) noexcept { Sample out; for (std::size_t i = 0; i < C; i++) { out.amplitudes[i] = lhs.amplitudes[i] * rhs; } return out; } template static constexpr float sample_to_float(T sample) noexcept; template <> constexpr float sample_to_float(uint8_t sample) noexcept { return (sample / 128.f) - 1.f; } template <> constexpr float sample_to_float(int16_t sample) noexcept { return sample / 32768.f; } } // namespace srb2::audio #endif // __SRB2_AUDIO_SAMPLE_HPP__