Skip to content

Instantly share code, notes, and snippets.

@bluebrown
Last active June 18, 2021 05:51
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 bluebrown/ed803fc3ee29b4a910149e47a709da16 to your computer and use it in GitHub Desktop.
Save bluebrown/ed803fc3ee29b4a910149e47a709da16 to your computer and use it in GitHub Desktop.
Compute musical note from arbitrary scale
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