Skip to content

Instantly share code, notes, and snippets.

@mrk-its
Created December 7, 2020 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrk-its/6a8e7b4180a653b754bb958f8bb17de6 to your computer and use it in GitHub Desktop.
Save mrk-its/6a8e7b4180a653b754bb958f8bb17de6 to your computer and use it in GitHub Desktop.
Pokey noise, 4-bit poly, joined channels (16bit divider)
poly_4 = [0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1]
poly_5 = [0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1]
CLOCK_177 = 1778400
MIN_SAMPLE_RATE = 8000
MAX_SAMPLE_RATE = 96000
function createNoise16Buffer(ctx, poly_data, divider) {
let position = 0;
sample_rate = Math.floor(CLOCK_177 / (divider + 1))
let multiplier = 1
if(sample_rate < MIN_SAMPLE_RATE) {
multiplier = Math.ceil(MIN_SAMPLE_RATE / sample_rate)
} else if(sample_rate > MAX_SAMPLE_RATE) {
return null;
}
let n_samples = sample_rate * multiplier;
console.log("sample rate:", sample_rate, "mult:", multiplier)
data = new Float32Array(n_samples)
let index = 0;
for(let n=0; n<n_samples; n++) {
let sample = poly_data[position % poly_data.length] * 2 - 0.5
position += 1
for(let i=0; i<multiplier; i++) {
data[index] = sample;
index += 1;
}
}
console.log(data);
let buffer = ctx.createBuffer(1, n_samples, n_samples)
buffer.copyToChannel(data, 0)
return buffer
}
let ctx = new AudioContext()
let buffer = createNoise16Buffer(ctx, poly_4, 0x5e6)
buffer_source = ctx.createBufferSource()
buffer_source.buffer = buffer
buffer_source.connect(ctx.destination)
buffer_source.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment