Piano Frequencies

Stephen Lukacs (2) iquanta.org/instruct/python
keynotef (Hz)f Tesla (Hz)
88C84186.009044109.89979
87B73951.066413879.22884
86A#7/B♭73729.310093661.50445
85A73520.03456.0
84G#7/A♭73322.437583262.02962
83G73135.963493078.94597
82F#7/G♭72959.955382906.13801
81F72793.825852743.02902
80E72637.020462589.07463
79D#7/E♭72489.015872443.76104
78D72349.318142306.60327
77C#7/D♭72217.461052177.14357
76C72093.004522054.94989
75B61975.533211939.61442
74A#6/B♭61864.655051830.75223
73A61760.01728.0
72G#6/A♭61661.218791631.01481
71G61567.981741539.47298
70F#6/G♭61479.977691453.06901
69F61396.912931371.51451
68E61318.510231294.53731
67D#6/E♭61244.507931221.88052
66D61174.659071153.30163
65C#6/D♭61108.730521088.57179
64C61046.502261027.47495
63B5987.7666969.80721
62A#5/B♭5932.32752915.37611
61A5880.0864.0
60G#5/A♭5830.6094815.50741
59G5783.99087769.73649
58F#5/G♭5739.98885726.5345
57F5698.45646685.75725
56E5659.25511647.26866
55D#5/E♭5622.25397610.94026
54D5587.32954576.65082
53C#5/D♭5554.36526544.28589
52C5523.25113513.73747
51B4493.8833484.9036
50A#4/B♭4466.16376457.68806
49A4440.0432.0
48G#4/A♭4415.3047407.7537
47G4391.99544384.86825
46F#4/G♭4369.99442363.26725
45F4349.22823342.87863
44E4329.62756323.63433
43D#4/E♭4311.12698305.47013
42D4293.66477288.32541
41C#4/D♭4277.18263272.14295
40C4261.62557256.86874
39B3246.94165242.4518
38A#3/B♭3233.08188228.84403
37A3220.0216.0
36G#3/A♭3207.65235203.87685
35G3195.99772192.43412
34F#3/G♭3184.99721181.63363
33F3174.61412171.43931
32E3164.81378161.81716
31D#3/E♭3155.56349152.73506
30D3146.83238144.1627
29C#3/D♭3138.59132136.07147
28C3130.81278128.43437
27B2123.47083121.2259
26A#2/B♭2116.54094114.42201
25A2110.0108.0
24G#2/A♭2103.82617101.93843
23G297.9988696.21706
22F#2/G♭292.4986190.81681
21F287.3070685.71966
20E282.4068980.90858
19D#2/E♭277.7817576.36753
18D273.4161972.08135
17C#2/D♭269.2956668.03574
16C265.4063964.21718
15B161.7354160.61295
14A#1/B♭158.2704757.21101
13A155.054.0
12G#1/A♭151.9130950.96921
11G148.9994348.10853
10F#1/G♭146.249345.40841
9F143.6535342.85983
8E141.2034440.45429
7D#1/E♭138.8908738.18377
6D136.708136.04068
5C#1/D♭134.6478334.01787
4C132.703232.10859
3B030.8677130.30648
2A#0/B♭029.1352428.6055
1A027.527.0

f(Hz) = 440 * 2(key-49)/12


"""
reference: https://iquanta.org/instruct/python ::: Piano: Frequencies of Notes ::: Stephen Lukacs, Ph.D. ©2023-03-22
"""
<page_scripts>
<style>
table.notes { width: 200px; }
table.notes td { padding: 2px 8px; }
table.notes tr:nth-child(12n+2) td { border: 2px solid gray; border-radius: 5px; }
table.notes tr:nth-child(12n+5) td { border: 2px solid pink; border-radius: 5px; }
</style>
</page_scripts>
from yatl.helpers import *
import plotly.graph_objects as go

BR = TAG['br/']
rtn = DIV()
if 1:
    html = { '':"&natural;", '-':"&natural;", '_':"&flat;", '^':"&sharp;", '#':"&sharp;" }
    octave = ['A', 'A#/B_', 'B', 'C', 'C#/D_', 'D', 'D#/E_', 'E', 'F', 'F#/G_', 'G', 'G#/A_']
    for i, o in enumerate(octave):
        xx = o.find('/')
        if (xx > -1):
            octave[i] = o[:xx]+"~"+'/<span class="flat">'+o[xx+1]+html["_"]+"</span>~"
        else:
            octave[i] = o+"~"
    Hz440 = lambda n: round(440.*(2**((n-49)/12.)), 5)
    Hz432 = lambda n: round(432.*(2**((n-49)/12.)), 5)
    nth_octave, ith_octave = 8, 3
    snth_octave = str(nth_octave)
    t = TABLE(TR(TH("key"), TH("note"), TH("f (Hz)"), TH("f Tesla (Hz)"),), _class="notes")
    for n in range(88, 0, -1):
        try:
            key = XML(octave[ith_octave].replace('~', snth_octave))
        except Exception as E:
            rtn.append(E)
        t.append(TR(TD(n), TD(key), TD(Hz440(n)), TD(Hz432(n))))
        ith_octave -= 1
        if (ith_octave < 0):
            ith_octave = 11
        if (ith_octave == 2):
            nth_octave -= 1
            snth_octave = str(nth_octave)
    rtn.append(DIV(t, _style="float:left;"))
    fig = go.Figure()
    fig.add_trace(go.Scatter(x=[ i for i in range(1, 89) ], y=[ Hz432(i) for i in range(1, 89) ], mode='markers', name="tesla", marker=go.scatter.Marker(color="DarkGray")))
    fig.add_trace(go.Scatter(x=[ i for i in range(1, 89) ], y=[ Hz440(i) for i in range(1, 89) ], mode='markers', name="notes", marker=go.scatter.Marker(color="Black")))
    fig.update_layout(xaxis=go.XAxis(title="key"), yaxis=go.YAxis(title="Frequency (Hz)", anchor='x', side='left'))
    fig.update_layout(title_text="Piano Frequency Response per Key", height=1050, margin=go.layout.Margin(l=25, r=25, b=60, t=60, pad=0), plot_bgcolor="#f5f5f5", paper_bgcolor="White")
    html = fig.to_html()
    rtn.append(CAT(DIV(H1(XML("f(Hz) = 440 * 2<sup>(key-49)/12</sup>")), BR(), XML(html[html.find('<div>'):html.rfind('</div>')+6].replace('<div>', '<div id="plotly">')), _style="float:left; margin: 0px 10px; width: 75%; border: 0px solid black; border-radius: 4px;"), DIV(_style="float:none; clear:both;")))