-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`) | ||
}) | ||
}) |