diff options
author | Daniil Cherednik <dan.cherednik@gmail.com> | 2016-03-13 09:49:33 +0300 |
---|---|---|
committer | Daniil Cherednik <dan.cherednik@gmail.com> | 2016-09-02 21:21:28 +0300 |
commit | cfaa2cd39b7256a868a4f5cd83aac207df6bd1b3 (patch) | |
tree | 75efff26584e046566d17cd308d45b6b0fd5abfc /src/util.h | |
parent | b4df8a7c2dd12eea27c8cc52bd52a1bb8c00943f (diff) | |
download | atracdenc-cfaa2cd39b7256a868a4f5cd83aac207df6bd1b3.tar.gz |
Dirty implementation of atrac3 encoder:
- no JS mode
- constant quantiser for tonal components
- gain controll implemented but produces some artifacts with real signals.
- etc...
Diffstat (limited to 'src/util.h')
-rw-r--r-- | src/util.h | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..f75c48e --- /dev/null +++ b/src/util.h @@ -0,0 +1,58 @@ +#pragma once +#include <cstdint> +#include <vector> +#include <algorithm> +#include <cmath> + +#include "config.h" +#include <cstring> + +template<class T> +inline void SwapArray(T* p, const size_t len) { + for (size_t i = 0, j = len - 1; i < len / 2; ++i, --j) { + T tmp = p[i]; + p[i] = p[j]; + p[j] = tmp; + } +} + +template<size_t N> +inline void InvertSpectrInPlase(TFloat* in) { + for (size_t i = 0; i < N; i+=2) + in[i] *= -1; +} + +template<size_t N> +inline std::vector<TFloat> InvertSpectr(const TFloat* in) { + std::vector<TFloat> buf(N); + std::memcpy(&buf[0], in, N * sizeof(TFloat)); + InvertSpectrInPlase<N>(&buf[0]); + return buf; +} + +inline uint16_t GetFirstSetBit(uint32_t x) { + static const uint16_t multiplyDeBruijnBitPosition[32] = { + 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, + 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 + }; + x |= x >> 1; + x |= x >> 2; + x |= x >> 4; + x |= x >> 8; + x |= x >> 16; + return multiplyDeBruijnBitPosition[(uint32_t)(x * 0x07C4ACDDU) >> 27]; +} + +template<class T> +inline uint16_t Log2FloatToIdx(T x, uint16_t shift) { + T t = x * shift; + return GetFirstSetBit(std::trunc(t)); +} + +template<class T> +inline T CalcMedian(T* in, uint32_t len) { + std::vector<T> tmp(in, in+len); + std::sort(tmp.begin(), tmp.end()); + uint32_t pos = (len - 1) / 2; + return tmp[pos]; +} |