gists/strings.py
2014-09-30 14:26:11 -07:00

155 lines
3.8 KiB
Python
Executable file

#!/usr/bin/python
from data import *
stock = []
stock += daddario_plain_steel
stock += daddario_nickle_wound
#stock += circle_k_plain
#stock += circle_k_hybrid_wound
mm_to_in = 0.03937
scale_length = 648*mm_to_in
string_sets = {
'regular light': (
# (desired note, tension, and type)
('E4' , 16, 'P'),
('B3' , 16, 'P'),
('G3' , 16, 'P'),
('D3' , 16, 'W'),
('A2' , 16, 'W'),
('E2' , 16, 'W'),
),
'jazz light': (
('E4' , 24, 'P'),
('B3' , 24, 'P'),
('G3' , 24, 'W'),
('D3' , 24, 'W'),
('A2' , 24, 'W'),
('E2' , 24, 'W'),
),
'regular light (DADGAD)': (
('D4' , 16, 'P'),
('A3' , 16, 'P'),
('G3' , 16, 'P'),
('D3' , 16, 'W'),
('A2' , 16, 'W'),
('D2' , 16, 'W'),
),
'regular light (open G, 6)': (
('D4' , 16, 'P'),
('B3' , 16, 'P'),
('G3' , 16, 'P'),
('D3' , 16, 'W'),
('B2' , 16, 'W'),
('D2' , 16, 'W'),
),
'regular light (open G, 7)': (
('D4' , 16, 'P'),
('B3' , 16, 'P'),
('G3' , 16, 'P'),
('D3' , 16, 'W'),
('B2' , 16, 'W'),
('G2' , 16, 'W'),
('D2' , 16, 'W'),
),
}
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)
import sys
sys.exit(0)
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
if False:
scale_length = 25.4825
D3 = note2freq('D3')
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)))
import sys
sys.exit(0)
outfmt = '{:<6} at {:> 5.2f} lbs ({:>+4.2f})'
finalfmt = 'total: {:>7.2f} lbs ({:>+5.2f})'
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:
if kind == 'P' and name[2] != 'P':
continue
if kind == 'W' and name[3] != 'W':
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)