diff options
| author | jacqueline <me@jacqueline.id.au> | 2023-12-19 22:45:29 +1100 |
|---|---|---|
| committer | jacqueline <me@jacqueline.id.au> | 2023-12-19 23:41:52 +1100 |
| commit | 2ccaaf5724fe08e63e06b677a42326d4f8e0550e (patch) | |
| tree | 73cf0da023b017852c7e394975b31719b8970d89 /src/codecs/sample.cpp | |
| parent | 8a260dad05f068727e538092ef9c56e714a7edb4 (diff) | |
| download | tangara-fw-2ccaaf5724fe08e63e06b677a42326d4f8e0550e.tar.gz | |
Add dither when requantising >16 bit samples
Diffstat (limited to 'src/codecs/sample.cpp')
| -rw-r--r-- | src/codecs/sample.cpp | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/codecs/sample.cpp b/src/codecs/sample.cpp new file mode 100644 index 00000000..c5c96fb9 --- /dev/null +++ b/src/codecs/sample.cpp @@ -0,0 +1,25 @@ +/* + * Copyright 2023 jacqueline <me@jacqueline.id.au> + * + * SPDX-License-Identifier: GPL-3.0-only + */ + +#include "sample.hpp" + +#include <cstdint> + +#include "komihash.h" + +namespace sample { + +static uint64_t sSeed1{0}; +static uint64_t sSeed2{0}; + +auto applyDither(int64_t src, uint_fast8_t bits) -> int32_t { + uint64_t mask = 0xFFFFFFFF; // 32 ones + mask >>= 32 - bits; // `bits` ones + uint64_t noise = komirand(&sSeed1, &sSeed2) & mask; // `bits` random noise + return std::clamp<int64_t>(src + noise, INT32_MIN, INT32_MAX); +} + +} // namespace sample |
