Commit f128a4d1 authored by phcoder's avatar phcoder
Browse files

Replace Mersenne twister with LFSR in noise generator

This uses less memory and avoids allocation error on djgpp while
resulting in no difference
parent 825d28ae
......@@ -112,18 +112,37 @@ inline void DSP::organWave(uint32_t frequency, int16_t amplitude, int16_t offset
inline void DSP::noise(uint32_t frequency, int16_t amplitude, int32_t position, int16_t* dest, size_t samples)
{
static std::random_device rdevice;
static std::mt19937 mt(rdevice());
std::uniform_int_distribution<int16_t> dist(-amplitude/2, amplitude/2);
const size_t periodLength = float(rate) / frequency;
const size_t halfPeriod = periodLength / 2;
static uint32_t lfsr_state = 0x12345678;
static const uint8_t poly = 0x34; // second-smallest maximal-period polynomial.
static const int trailing_zeros = 1; // 1 trailing 0.
static const int significant_bits = 5; // 5 bits from highest set to lowest set
static const int batch = 4; // Batch to handle in a table
static const int table_size = 1 << (batch + significant_bits);
static const int table_mask = table_size - 1;
static uint8_t polytable[table_size];
if (polytable[1] == 0) {
for (size_t i = 1; i < table_size; ++i)
{
uint32_t cur = i << trailing_zeros;
for (int j = 0; j < batch; j++)
{
uint8_t parity = cur & poly;
parity ^= parity >> 4;
parity ^= parity >> 2;
parity ^= parity >> 1;
cur = (cur >> 1) | ((parity & 1) << 31);
}
polytable[i] = cur >> (32 - batch);
}
}
for (size_t i = 0; i < samples; ++i)
{
const auto sampleInPeriod = position % periodLength;
dest[i] += dist(mt);
dest[i] += lfsr_state % amplitude - (amplitude / 2);
lfsr_state = (lfsr_state >> batch) | (polytable[(lfsr_state >> significant_bits) & table_mask] << (32 - batch));
lfsr_state = (lfsr_state >> batch) | (polytable[(lfsr_state >> significant_bits) & table_mask] << (32 - batch));
lfsr_state = (lfsr_state >> batch) | (polytable[(lfsr_state >> significant_bits) & table_mask] << (32 - batch));
lfsr_state = (lfsr_state >> batch) | (polytable[(lfsr_state >> significant_bits) & table_mask] << (32 - batch));
++position;
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment