Deluge Firmware 1.3.0
Build date: 2026.10.14
Loading...
Searching...
No Matches
db_shift.h
1#pragma once
2#include <algorithm>
3#include <cstdint>
4#include <limits>
5
7constexpr float dbIntervals[] = {
8 24, // Not really real
9 12.1, 7, 5, 3.9, 3.2, 2.6, 2.4, 2, 1.8, 1.7, 1.5, 1.4, 1.3, 1.2, 1.1,
10};
11
14inline int32_t shiftVolumeByDB(int32_t oldValue, float offset) {
15 uint32_t oldValuePositive = static_cast<uint32_t>(oldValue) + 2147483648u;
16 uint32_t currentInterval = oldValuePositive >> 28;
17
18 // Interval 0 is not a real dB interval - nothing to shift within it.
19 if (currentInterval < 1) {
20 return oldValue;
21 }
22
23 float howFarUpInterval = static_cast<float>(oldValuePositive & 268435455u) / 268435456.0f;
24
25 while (true) {
26 float dbThisInterval = dbIntervals[currentInterval];
27
28 if (offset >= 0.0f) {
29 // How many more dB can we get before we reach the top end of this interval?
30 float dbLeftThisInterval = (1.0f - howFarUpInterval) * dbThisInterval;
31 if (dbLeftThisInterval > offset) {
32 howFarUpInterval += offset / dbThisInterval;
33 break;
34 }
35 if (currentInterval == 15) {
36 return std::numeric_limits<int32_t>::max();
37 }
38 offset -= dbLeftThisInterval;
39 currentInterval++;
40 howFarUpInterval = 0.0f;
41 }
42 else {
43 // How many dB can we shed before we reach the bottom end of this interval?
44 float dbLeftThisInterval = howFarUpInterval * dbThisInterval;
45 if (dbLeftThisInterval > -offset) {
46 howFarUpInterval += offset / dbThisInterval;
47 break;
48 }
49 if (currentInterval == 1) {
50 return std::numeric_limits<int32_t>::min();
51 }
52 offset += dbLeftThisInterval;
53 currentInterval--;
54 howFarUpInterval = 1.0f;
55 }
56 }
57
58 howFarUpInterval = std::clamp(howFarUpInterval, 0.0f, 0.99999994f);
59 uint32_t newValuePositive = (currentInterval << 28) + static_cast<uint32_t>(howFarUpInterval * 268435456.0f);
60 return static_cast<int32_t>(newValuePositive - 2147483648u);
61}