Skip to content

Commit

Permalink
Oscillator Noise WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ericyd committed Jan 9, 2024
1 parent 65be859 commit 3b5c7f8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export * from './components/index.js'
export * from './data-structures/grid.js'
export * from './data-structures/fractalized-line.js'
export * from './noise/oscillator.js'
export * from './noise/oscillator-noise.js'
export * from './render.js'
24 changes: 24 additions & 0 deletions lib/noise/oscillator-noise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { PI } from "../constants.js"
import { createRng, random } from "../random.js"
import { Oscillator } from "./oscillator.js"

/**
* @callback OscillatorNoiseFn
* @param {number} x
* @param {number} [y=0]
* @returns {number} ideally in range [-1, 1]
*/

/**
* Creates a noise function based on oscillators
* @param {string} seed
* @returns {OscillatorNoiseFn}
*/
export function createOscNoise(seed) {
const rng = createRng(seed)
// hmm... currently not working
const period = random(PI, 4 * PI, rng)
const phase = random(-PI, PI, rng)
const osc = new Oscillator({ amplitude: 1, period, phase, compress: false })
return (x, y = 0) => osc.output(x, y)
}
17 changes: 17 additions & 0 deletions lib/noise/oscillator-noise.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { describe, it } from "node:test";
import { createOscNoise } from './oscillator-noise.js'
import { randomSeed } from "../random.js";
import assert from "node:assert";

describe("OscillatorNoise", () => {
it('returns consistent values for the same seed/x/y inputs', () => {
const seed = randomSeed()
const noiseFn1 = createOscNoise(seed)
const actual1 = noiseFn1(100, 100)

const noiseFn2 = createOscNoise(seed)
const actual2 = noiseFn2(100, 100)

assert.strictEqual(actual1, actual2, `Seed ${seed} gave 2 different values: ${actual1} and ${actual2}`)
})
})

0 comments on commit 3b5c7f8

Please sign in to comment.