-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathnote-utils.js
More file actions
34 lines (28 loc) · 777 Bytes
/
note-utils.js
File metadata and controls
34 lines (28 loc) · 777 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
* Copyright (c) 2020 Alexander Pankratov, <ap@swapped.ch>
* https://swapped.ch/note-detector
*/
/*
* Distributed under the terms of the 2-clause BSD license.
* https://www.opensource.org/licenses/bsd-license.php
*/
function hzToNote(freq)
{
var note = 12 * ( Math.log(freq / 440) / Math.log(2) );
return Math.round(note) + 49;
}
function noteString(note)
{
const notes = [ "A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#" ];
const letter = notes[ (note + 11) % notes.length ];
const octave = Math.floor( (note - 49) / notes.length ) + 4;
return letter + (letter.length < 2 ? '.' : '') + octave;
}
function hzToNoteString(freq)
{
return noteString( hzToNote(freq) );
}
function noteToHz(note)
{
return 440 * Math.pow(2, (note-49)/12 );
}