1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-11-23 06:32:11 +00:00
8bitworkshop/tools/mknotes.py

49 lines
1.6 KiB
Python
Raw Normal View History

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")
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
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))
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
print '//', args
2017-03-31 17:25:18 +00:00
print '//', best_a440, best_error, test_notes
print "const int note_table[%d] = {" % final_notes
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
while period > maxval:
period /= 2
2017-03-31 17:25:18 +00:00
print '%d,' % period,
print "};"