Skip to content

Commit

Permalink
Try roughness function
Browse files Browse the repository at this point in the history
  • Loading branch information
jonikorpi committed Feb 8, 2025
1 parent cb43d26 commit 9305bf0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
26 changes: 26 additions & 0 deletions example/Keyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,8 @@ const attackWithController = (
};

const updateConsonances = (midiNumber = 0, sign = 1.0) => {
// const frequency = midiToFrequency(midiNumber);

for (let index = 0; index < consonances.length; index++) {
for (const offset of consonances[index]) {
const key = document.querySelector(
Expand All @@ -506,6 +508,8 @@ const updateConsonances = (midiNumber = 0, sign = 1.0) => {

if (key) {
const consonance = ((index / (consonances.length - 1)) * 2.0 - 1.0) * sign;
// const consonance =
// (1 - getRoughness(frequency, midiToFrequency(midiNumber + offset)) / 0.091) * sign;

ongoingConsonances.set(
key,
Expand Down Expand Up @@ -539,6 +543,28 @@ const mix = (a = 0, b = 1, amount = 0) => a * (1.0 - amount) + b * amount;
const ongoingConsonances = new WeakMap();
const updatedConsonanceKeys = new Set<HTMLElement>();

// https://www.acousticslab.org/learnmoresra/moremodel.html
const getRoughness = (frequencyA = 440.0, frequencyB = 440.0, amplitudeA = 1, amplitudeB = 1) => {
const minFrequency = Math.min(frequencyA, frequencyB);
const maxFrequency = Math.max(frequencyA, frequencyB);
const minAmplitude = Math.min(amplitudeA, amplitudeB);
const maxAmplitude = Math.max(amplitudeA, amplitudeB);

const x = minAmplitude * maxAmplitude;
const y = (2 * minAmplitude) / (minAmplitude + maxAmplitude);

const b1 = 3.5;
const b2 = 5.75;
const s1 = 0.0207;
const s2 = 18.96;
const s = 0.24 / (s1 * minFrequency + s2);
const z =
Math.E ** (-b1 * s * (maxFrequency - minFrequency)) -
Math.E ** (-b2 * s * (maxFrequency - minFrequency));

return x ** 0.1 * (0.5 * y ** 3.11) * z;
};

// When rounded to closest just tones
// https://www.flickr.com/photos/omegatron/7524758406/in/album-72157629941546057
// 1 ~ 16/15 = very high
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vuoro/pelimanni",
"version": "0.16.1",
"version": "0.16.2",
"type": "module",
"main": "./instruments.js",
"exports": {
Expand Down

0 comments on commit 9305bf0

Please sign in to comment.