35 lines
952 B
JavaScript
35 lines
952 B
JavaScript
function mix32(value) {
|
|
let mixed = value >>> 0;
|
|
mixed ^= mixed >>> 16;
|
|
mixed = Math.imul(mixed, 0x7feb352d);
|
|
mixed ^= mixed >>> 15;
|
|
mixed = Math.imul(mixed, 0x846ca68b);
|
|
mixed ^= mixed >>> 16;
|
|
return mixed >>> 0;
|
|
}
|
|
|
|
export function hashString(value) {
|
|
let hash = 2166136261;
|
|
for (let index = 0; index < value.length; index += 1) {
|
|
hash ^= value.charCodeAt(index);
|
|
hash = Math.imul(hash, 16777619);
|
|
}
|
|
return hash >>> 0;
|
|
}
|
|
|
|
export function whiteNoiseAt(seed, sampleIndex) {
|
|
const mixed = mix32((seed >>> 0) ^ Math.imul(sampleIndex + 1, 0x9e3779b1));
|
|
return (mixed / 0xffffffff) * 2 - 1;
|
|
}
|
|
|
|
export function pinkNoiseAt(seed, sampleIndex) {
|
|
let total = 0;
|
|
let weight = 0;
|
|
for (let octave = 0; octave < 8; octave += 1) {
|
|
const octaveWeight = 1 / (octave + 1);
|
|
total += whiteNoiseAt(seed + octave * 0x1f123bb5, sampleIndex >> octave) * octaveWeight;
|
|
weight += octaveWeight;
|
|
}
|
|
return total / weight;
|
|
}
|