Skip to content

Commit

Permalink
Partial Revert "audio: simplified samples storing/loading"
Browse files Browse the repository at this point in the history
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 586c3fc.
  • Loading branch information
mpiatka committed Oct 9, 2023
1 parent cb269cf commit 8a9639e
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions src/audio/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <int BPS>
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<int BPS> static int32_t load_sample(const char *data);

template<> int32_t load_sample<1>(const char *data) {
return *reinterpret_cast<const int8_t *>(data);
}

template<> int32_t load_sample<2>(const char *data) {
return *reinterpret_cast<const int16_t *>(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<const int32_t *>(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
Expand Down

0 comments on commit 8a9639e

Please sign in to comment.