3 ms·
Here are two ways to get the keys' measurements, which I made/use. The tricky part from a naive approach is that white keys are on a grid of seven whereas black
by keymasta 4y ago
Here are two ways to get the keys' measurements, which I made/use. The tricky part from a naive approach is that white keys are on a grid of seven whereas black keys are on a grid of twelve. I copypasta'ed from my code so I hope it's still correct. This is Python but it should be trivial to use any language.
evenlySpacedWhites = True
octaveWidthProportion = 2 #Piano keys longer/shorter
blackKeyProportion = 0.61832 #Black keys longer/shorter
height = 256 #This can be any number and controls total size
if evenlySpacedWhites:
CtoEWhites = 35
FtoBWhites = 35
CtoEBlacks = 21
FtoBBlacks = 20
else:
CtoEWhites = 20
FtoBWhites = 21
CtoEBlacks = 12
FtoBBlacks = 12
#before normalisation
octaveNumberWidth = 3 * CtoEWhites + 4 * FtoBWhites
#using the proportion makes the output image have the same height, regardless if whether evenlySpacedWhites is True/False
octaveWidth = round(height * octaveWidthProportion)
proportion = octaveWidth / octaveNumberWidth
CtoEWhites = round(CtoEWhites * proportion)
FtoBWhites = round(FtoBWhites * proportion)
CtoEBlacks = round(CtoEBlacks * proportion)
FtoBBlacks = round(FtoBBlacks * proportion)
blackKeyLength = round(height * blackKeyProportion)