From 8a9639ec7e859b8f95a56b0e81977e80ad4a58ca Mon Sep 17 00:00:00 2001 From: Martin Piatka Date: Mon, 9 Oct 2023 14:11:12 +0200 Subject: [PATCH] Partial Revert "audio: simplified samples storing/loading" The loading of samples with negative value was not working properly because of sign expansion. The storing works fine, because samples are only ever written with the same or less BPS. This reverts commit 586c3fc585a807d174995747fb41edda39974dff. --- src/audio/utils.cpp | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/audio/utils.cpp b/src/audio/utils.cpp index 3ac1b9867..90eec92c3 100644 --- a/src/audio/utils.cpp +++ b/src/audio/utils.cpp @@ -76,23 +76,34 @@ static void format_to_out_bps(char *out, int bps, int32_t out_value); * Loads sample with BPS width and returns it cast to * int32_t. */ -template -static int32_t -load_sample(const char *data) -{ - int32_t ret = 0; -#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - memcpy(&ret, data, BPS); -#else - memcpy((char *) &ret + 4 - BPS, data, BPS); -#endif - return ret; +template static int32_t load_sample(const char *data); + +template<> int32_t load_sample<1>(const char *data) { + return *reinterpret_cast(data); +} + +template<> int32_t load_sample<2>(const char *data) { + return *reinterpret_cast(data); +} + +template<> int32_t load_sample<3>(const char *data) { + int32_t in_value = 0; + memcpy(&in_value, data, 3); + + if ((in_value & 1U<<23U) != 0U) { // negative + in_value |= 0xFF000000U; + } + + return in_value; +} + +template<> int32_t load_sample<4>(const char *data) { + return *reinterpret_cast(data); } /** * @brief Calculates mean and peak RMS from audio samples * - * * @param[in] frame audio frame * @param[in] channel channel index to calculate RMS to * @param[out] peak peak RMS