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