summaryrefslogtreecommitdiff
path: root/src/codecs/sample.cpp
blob: c99710f13e8bac0d4cd7dd732238a2e9ac9b3817 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
 * Copyright 2023 jacqueline <me@jacqueline.id.au>
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#include "sample.hpp"
#include <stdint.h>

#include <cstdint>

#include "komihash.h"

namespace sample {

static uint64_t sSeed1{0};
static uint64_t sSeed2{0};

auto shiftWithDither(int64_t src, uint_fast8_t bits) -> Sample {
  // Generate `bits` random bits
  uint64_t mask = 0xFFFFFFFF;
  mask >>= 32 - bits;
  int64_t noise = static_cast<int32_t>(komirand(&sSeed1, &sSeed2) & mask);
  // Apply to the sample, then shift to 16 bit.
  return (src + noise) >> bits;
}

}  // namespace sample