Last active
June 18, 2021 05:51
-
-
Save bluebrown/ed803fc3ee29b4a910149e47a709da16 to your computer and use it in GitHub Desktop.
Compute musical note from arbitrary scale
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
export function computeNote( | |
// how far from the base note to go in either direction | |
distance = 0, | |
scale = { | |
// use this frequency as basis | |
// standard A4 is 440hz | |
base: 440.0, | |
// claim that its in the 4 repetition of made up sequence, | |
// on a normal piano it is saying this is A4 | |
position: 4, | |
// the scale will be divided by the number of notes | |
// which also have some arbitrary name like a b c | |
// on a normal piano these are 12 semi tones | |
notes: ["A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#"], | |
// the power tells what the value of the frequency should | |
// be after 1 repletion of the sequence. | |
// on a normal piano the frequency doubles (power of 2) | |
// I.E. A3 = 220hz, A4 = 440hz, A5 = 880hz | |
powerOf: 2 | |
} | |
) { | |
const scalar = scale.notes.length; | |
const loc = distance / scalar; | |
const offset = Math.floor(loc); | |
const freq = scale.base * Math.pow(scale.powerOf, loc); | |
const note = scale.notes[distance - offset * scalar]; | |
const position = offset + scale.position; | |
return { position, note, freq }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment