-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNote.tsx
49 lines (44 loc) · 1.2 KB
/
Note.tsx
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import React from "react";
import { getNoteFrequency, getNoteName } from "utils/notes";
interface Props {
detailed?: boolean;
detune?: number;
octave: number;
twelfth: number;
}
const modifierStyles: React.CSSProperties = {
display: "inline-block",
textAlign: "center",
width: "0.7em",
};
const octaveStyles: React.CSSProperties = {
...modifierStyles,
verticalAlign: "sub",
};
const accidentalStyles: React.CSSProperties = {
...modifierStyles,
transform: "translateX(-100%)",
verticalAlign: "super",
};
function Note({ detailed = false, detune = 0, octave, twelfth }: Props) {
const [name, accidental] = getNoteName(twelfth).split("");
const frequency = getNoteFrequency(octave, twelfth);
const frequencyDetuned = frequency * Math.pow(2, detune / 1200);
return (
<span>
{name}
<small style={octaveStyles}>{octave}</small>
{accidental && <small style={accidentalStyles}>{accidental}</small>}
{detailed && (
<>
<small>
({frequency.toFixed(2)} Hz
{detune !== 0 && <> → {frequencyDetuned.toFixed(2)} Hz</>})
</small>
</>
)}
</span>
);
}
export default React.memo(Note);