gists/strings.py

173 lines
4.6 KiB
Python
Raw Normal View History

2014-09-29 21:15:51 -07:00
#!/usr/bin/python
2014-09-30 02:13:14 -07:00
from data import *
stock = []
stock += daddario_plain_steel
stock += daddario_nickle_wound
#stock += circle_k_plain
#stock += circle_k_hybrid_wound
2014-09-29 21:15:51 -07:00
mm_to_in = 0.03937
scale_length = 648*mm_to_in
string_sets = {
'regular light': (
# (desired note, tension, and type)
2014-09-30 02:13:14 -07:00
('E4' , 16, 'P'),
('B3' , 16, 'P'),
('G3' , 16, 'P'),
('D3' , 16, 'W'),
('A2' , 16, 'W'),
('E2' , 16, 'W'),
2014-09-29 21:15:51 -07:00
),
2014-09-29 21:47:58 -07:00
'jazz light': (
2014-09-30 02:13:14 -07:00
('E4' , 24, 'P'),
('B3' , 24, 'P'),
('G3' , 24, 'W'),
('D3' , 24, 'W'),
('A2' , 24, 'W'),
('E2' , 24, 'W'),
2014-09-29 21:15:51 -07:00
),
'regular light (DADGAD)': (
2014-09-30 02:13:14 -07:00
('D4' , 16, 'P'),
('A3' , 16, 'P'),
('G3' , 16, 'P'),
('D3' , 16, 'W'),
('A2' , 16, 'W'),
('D2' , 16, 'W'),
2014-09-29 21:15:51 -07:00
),
2014-09-29 21:47:58 -07:00
'regular light (open G, 6)': (
2014-09-30 02:13:14 -07:00
('D4' , 16, 'P'),
('B3' , 16, 'P'),
('G3' , 16, 'P'),
('D3' , 16, 'W'),
('B2' , 16, 'W'),
('D2' , 16, 'W'),
2014-09-29 21:15:51 -07:00
),
2014-09-29 21:47:58 -07:00
'regular light (open G, 7)': (
2014-09-30 02:13:14 -07:00
('D4' , 16, 'P'),
('B3' , 16, 'P'),
('G3' , 16, 'P'),
('D3' , 16, 'W'),
('B2' , 16, 'W'),
('G2' , 16, 'W'),
('D2' , 16, 'W'),
2014-09-29 21:15:51 -07:00
),
}
A = 440
notes = {
'C' : 0,
'D' : 2,
'E' : 4,
'F' : 5,
'G' : 7,
'A' : 9,
'B' : 11,
}
rel = (2**(1/12.))
def note2freq(name):
sharp = name[1] == '#'
flat = name[1] == 'b'
if sharp or flat:
note = name[0:1]
octave = int(name[2])
else:
note = name[0]
octave = int(name[1])
num = notes[note]
if sharp:
num += 1
if flat:
num -= 1
fullnum = num + 12*(octave - 5)
return A*rel**(fullnum + 3)
def test(name, expected):
print('{:3} gave {: 9.2f} Hz\nexpected {: 9.2f} Hz'.format(name, note2freq(name), expected))
if False:
test('C2' , 65.41)
test('Ab4', 415.30)
test('A4' , 440.00)
test('B4' , 493.88)
test('B#4', 523.25)
test('Cb5', 493.88)
test('C5' , 523.25)
print()
test('E4' , 329.63)
test('B3' , 246.94)
test('G3' , 196.00)
test('D3' , 146.83)
test('A2' , 110.00)
test('E2' , 82.41)
2014-09-29 21:47:58 -07:00
import sys
sys.exit(0)
2014-09-29 21:15:51 -07:00
uw_const = 386.4
def uw2tension(uw, freq):
return (uw*(2*scale_length*freq)**2)/uw_const
def tension2uw(t, freq):
return (t*uw_const)/(2*scale_length*freq)**2
2014-09-29 21:47:58 -07:00
if False:
2014-10-02 00:54:55 -07:00
# DAd's data is all screwy so we use change scale lengths for a best-fit
2014-09-29 21:47:58 -07:00
scale_length = 25.4825
D3 = note2freq('D3')
2014-10-02 00:54:55 -07:00
A2 = note2freq('A2')
E2 = note2freq('E2')
2014-09-29 21:47:58 -07:00
print('NW024 {:10.8f} == 0.00010857'.format(tension2uw(15.73, D3)))
print('NW025* {:10.8f} == ? '.format(tension2uw(17.21, D3)))
print('NW026 {:10.8f} == 0.00012671'.format(tension2uw(18.38, D3)))
2014-10-02 00:54:55 -07:00
print()
print('NW036 {:10.8f} == 0.00023964'.format(tension2uw(19.04, A2)))
print('NW037* {:10.8f} == ? '.format(tension2uw(20.23, A2)))
print('NW038 {:10.8f} == 0.00026471'.format(tension2uw(20.96, A2)))
print()
scale_length = 25.18
print('NW039 {:10.8f} == 0.00027932'.format(tension2uw(12.46, E2)))
print('NW040* {:10.8f} == ? '.format(tension2uw(13.18, E2)))
print('NW042 {:10.8f} == 0.00032279'.format(tension2uw(14.37, E2)))
print()
scale_length = 25.02
print('NW049 {:10.8f} == 0.00043014'.format(tension2uw(18.97, E2)))
print('NW050* {:10.8f} == ? '.format(tension2uw(19.68, E2)))
print('NW052 {:10.8f} == 0.00048109'.format(tension2uw(21.15, E2)))
2014-09-29 21:47:58 -07:00
import sys
sys.exit(0)
2014-10-02 00:54:55 -07:00
outfmt = '{:<8} at {:> 5.2f} lbs ({:>+4.2f})'
finalfmt = 'total: {:>7.2f} lbs ({:>+5.2f})'
2014-09-29 21:15:51 -07:00
def print_ideal_stock(strings):
total_tension = 0
total_desired_tension = 0
for note, tension, kind in strings:
freq = note2freq(note)
uw = tension2uw(tension, freq)
closest = ('n/a', 0)
for name, stock_uw in stock:
2014-09-30 02:13:14 -07:00
if kind == 'P' and name[2] != 'P':
continue
if kind == 'W' and name[3] != 'W':
2014-09-29 21:15:51 -07:00
continue
if abs(stock_uw - uw) < abs(closest[1] - uw):
closest = (name, stock_uw)
closest_tension = uw2tension(closest[1], freq)
diff = closest_tension - tension
print(outfmt.format(closest[0], closest_tension, diff))
total_tension += closest_tension
total_desired_tension += tension
error = total_tension - total_desired_tension
print(finalfmt.format(total_tension, error))
print('for a scale length of {:>5.2f} inches'.format(scale_length))
for name, strings in string_sets.items():
print()
print('"{}"'.format(name))
print_ideal_stock(strings)