blob: 7c9cbbaed38f0dbf3663fb710a371a6bfb828406 (
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
#include "pch.h"
#include "DspDither.h"
namespace SaneAudioRenderer
{
void DspDither::Initialize(DspFormat outputFormat)
{
m_enabled = (outputFormat == DspFormat::Pcm16);
m_active = m_enabled;
for (size_t i = 0; i < 18; i++)
{
m_previous[i] = 0.0f;
m_generator[i].seed((uint32_t)(GetPerformanceCounter() + i));
m_distributor[i] = std::uniform_real_distribution<float>(0, 1.0f);
}
}
bool DspDither::Active()
{
return m_enabled && m_active;
}
void DspDither::Process(DspChunk& chunk)
{
if (!m_enabled || chunk.IsEmpty() || chunk.GetFormatSize() <= DspFormatSize(DspFormat::Pcm16))
{
m_active = false;
return;
}
m_active = true;
DspChunk::ToFloat(chunk);
DspChunk output(DspFormat::Pcm16, chunk.GetChannelCount(), chunk.GetFrameCount(), chunk.GetRate());
auto inputData = reinterpret_cast<const float*>(chunk.GetData());
auto outputData = reinterpret_cast<int16_t*>(output.GetData());
const size_t channels = chunk.GetChannelCount();
for (size_t frame = 0, frames = chunk.GetFrameCount(); frame < frames; frame++)
{
for (size_t channel = 0; channel < channels; channel++)
{
float inputSample = inputData[frame * channels + channel] * (INT16_MAX - 1);
// High-pass TPDF, 2 LSB amplitude.
float r = m_distributor[channel](m_generator[channel]);
float noise = r - m_previous[channel];
m_previous[channel] = r;
float outputSample = std::round(inputSample + noise);
assert(outputSample >= INT16_MIN && outputSample <= INT16_MAX);
outputData[frame * channels + channel] = (int16_t)outputSample;
}
}
chunk = std::move(output);
}
void DspDither::Finish(DspChunk& chunk)
{
Process(chunk);
}
}
|