2017-03-31 17:25:18 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
2017-04-15 04:12:21 +00:00
|
|
|
import sys, string, math, argparse
|
2017-03-31 17:25:18 +00:00
|
|
|
|
2017-04-15 04:12:21 +00:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('-l', '--length', type=int, default=64, help="length of note table")
|
|
|
|
parser.add_argument('-u', '--upper', type=int, default=49, help="upper note # to test")
|
|
|
|
parser.add_argument('-f', '--freq', type=float, default=3579545/32.0, help="base frequency (Hz)")
|
2017-04-19 18:26:46 +00:00
|
|
|
parser.add_argument('-b', '--bias', type=float, default=0, help="divisor bias")
|
2017-05-25 19:49:30 +00:00
|
|
|
parser.add_argument('-m', '--maxbits', type=float, default=12, help="max. # of bits")
|
2017-04-15 04:12:21 +00:00
|
|
|
args = parser.parse_args()
|
2017-03-31 17:25:18 +00:00
|
|
|
|
2017-04-15 04:12:21 +00:00
|
|
|
test_notes = args.upper
|
|
|
|
final_notes = args.length
|
|
|
|
basehz = args.freq
|
2017-04-20 13:45:35 +00:00
|
|
|
bias = args.bias
|
2017-05-25 19:49:30 +00:00
|
|
|
maxval = (1<<int(args.maxbits))-1
|
2017-03-31 17:25:18 +00:00
|
|
|
|
|
|
|
results = []
|
|
|
|
|
|
|
|
for a440 in range(4300,4500):
|
|
|
|
error = 0
|
|
|
|
for note in range(1,test_notes):
|
|
|
|
notehz = a440 / 10.0 * math.pow(2.0, (note - 49) / 12.0);
|
2017-04-20 13:45:35 +00:00
|
|
|
period = int(round(basehz / notehz))
|
2017-05-25 19:49:30 +00:00
|
|
|
while period > maxval:
|
|
|
|
period /= 2
|
2017-03-31 17:25:18 +00:00
|
|
|
tonehz = basehz / period
|
|
|
|
error += abs(notehz-tonehz)
|
|
|
|
#print a440,note,notehz,notehz-tonehz,period
|
|
|
|
#if a440 == 4405:
|
|
|
|
# print '%d,%f,%d' % (note,tonehz-notehz,period)
|
|
|
|
results.append((error, a440))
|
|
|
|
|
|
|
|
results.sort()
|
|
|
|
best_error, best_a440 = results[0]
|
|
|
|
best_a440 /= 10.0
|
2018-11-22 19:11:56 +00:00
|
|
|
print('//', args)
|
|
|
|
print('//', best_a440, best_error, test_notes)
|
2017-03-31 17:25:18 +00:00
|
|
|
|
2018-11-22 19:11:56 +00:00
|
|
|
print("const int note_table[%d] = {" % final_notes)
|
2017-03-31 17:25:18 +00:00
|
|
|
for note in range(0,final_notes):
|
|
|
|
notehz = best_a440 * math.pow(2.0, (note - 49) / 12.0);
|
2017-04-20 13:45:35 +00:00
|
|
|
period = int(round(basehz / notehz)) - bias
|
2017-05-25 19:49:30 +00:00
|
|
|
while period > maxval:
|
|
|
|
period /= 2
|
2018-11-22 19:11:56 +00:00
|
|
|
print('%d,' % period, end='')
|
|
|
|
print("};")
|