# # dac.py # # 1. Record each dac_xxx.nes/nsf test, save as 16-bit mono WAV. # 2. Each loop of a test begins with a sawtooth buzz. # Find the sample position of the end of that buzz (DMC jumps back to 0), # one marking the start of the test, and another marking the end of it. # 3. Run dac_xxx( filename, start, end ) to analyze the test. # # tnd2 also has a "phase" parameter for the triangle # observe the start of the first triangle burst after reset, # you should find 15 steps down, with a short plateau after the 15th step, # this is considered starting at phase 1 of 32. # # (Each triangle burst advances the phase by 1 step, at the end of the first # step it has advanced from phase 0 to 1, so the starting phase is 1.) # # If reset was not used, the triangle may start at some random phase # you will have to manually determine. # import wave import struct import math import matplotlib.pyplot as pyplot import numpy import os.path BASE_FREQ = 1789772 / 4064 # approximated 440Hz ADJUST = True # some graphs become relative rather than absolute, easier to evaluate NOISE_DMC = False # show DMC noise vs noise on graph # # utilities # def loadwave(filename): w = wave.open(filename,"rb") global sr sr = w.getframerate() assert(w.getnchannels() == 1) # "Mono only." assert(w.getsampwidth() == 2) # "16-bit only." wb = w.readframes(w.getnframes()) ws = [] for i in range(0,len(wb),2): ws.append(struct.unpack_from("> 4 results[i0].append(rms) #print("%2d,%2d = %8.4f (%d,%d)" % (i0,i1,rms,int(test_pos),int(swindow))) # cache results and return numpy.savetxt(cache_filename,results) return results def dac_square_linear(): return [[y+x for x in range(16)] for y in range(16)] def squ_blargg(s0,s1): if s0 == 0 and s1 == 0: return 0 return 95.88 / ((8128 / (s0 + s1)) + 100) def squ_model(s0,s1,model): if s0 == 0 and s1 == 0: return 0 return 1 / ((model / (so + s1)) + 1) def dac_square_blargg(): return [[squ_blargg(y,x) for x in range(16)] for y in range(16)] def dec_square_model(model): return [[squ_model(y,x,model) for x in range(16)] for y in range(16)] def dac_square_normalize(results): # normalize based on average of single square at full volume m = (results[0][15] + results[15][0]) / 2 return [[v/m for v in row] for row in results] def dac_square_plot_a(results, colour): # combined overlapping graph of square curves, all combinations mixed graph = dac_square_normalize(results) # plot results for i in range(len(graph)): gy = graph[i] gx = [r+i for r in range(len(gy))] if ADJUST: gy = [graph[i][r]*(15/max(r+i,1)) for r in range(16)] if i == 0: # skip zeroes gy = gy[1:] gx = gx[1:] plot(gx,gy,colour) def dac_square_plot_b(results, colour): # simple graph of square curves, isolating both channels graph = dac_square_normalize(results) gy = graph[0] gx = range(len(gy)) if ADJUST: gy = [gy[r] * (15/max(r,1)) for r in range(16)] gy = gy[1:] gx = gx[1:] plot(gx,gy,colour) gy = [graph[i][0] for i in range(len(graph))] gx = [r+16 for r in range(len(gy))] if ADJUST: gy = [gy[r] * (15/max(r,1)) for r in range(16)] gy = gy[1:] gx = gx[1:] plot(gx,gy,colour) # graph of how symmetrical the output is (i.e. are the two squares identical) # each element in the table is compared to the average of its diagonal def dac_square_symmetry(name, results): print("symmetry_dac_square('%s')" % (name)) graph = dac_square_normalize(results) assert(len(graph)==len(graph[0])) axis = [] for i in range((len(graph)*2)-1): accum = 0 count = 0 for j in range(len(graph)): if ((i-j) < 0) or ((i-j) >= len(graph)): continue accum += graph[i-j][j] count += 1 axis.append(accum/count) graph = [[graph[i][j]-axis[i+j] for j in range(len(graph[i]))] for i in range(len(graph))] table_2D(graph, "Square 0", "Square 1") # # dac_tnd_common # def tnd_blargg(t, n, d): if t == 7.5: # special case "7.5" is ultrasonic triangle, take average of all values accum = 0 for i in range(0,16): accum += tnd_blargg(i,n,d) return accum/16 if t == 0 and n == 0 and d == 0: return 0 return 159.79 / ((1 / ((t/8227) + (n/12241) + (d/22638))) + 100) def tnd_model(t, n, d, model): if t == 7.5: accum = 0 for i in range(0,16): accum += tnd_model(i,n,d,model) return accum/16 if t == 0 and n == 0 and d == 0: return 0 return 1 / ((1 / ((t/model[0]) + (n/model[1]) + (d/model[2]))) + 1) # # dac_tnd0 # def dac_tnd0(filename, start, end): print("dac_tnd0('%s',%d,%d)" % (filename, start, end)) # reference [ start, end, 1s of silence, # ref triangle, 1st sim triangle, 4th sim triangle, # noise 1, noise 15, sim noise 1, sim noise 127, # ref apu, sim 440 1, sim 440 127 reference = [ 16420, 33418324, 93220, 208013, 327850, 687581, 879208, 2556569, 2676516, 17765293, 17956832, 18076855, 33263128 ] reference = remap_reference(reference, start, end) # use cached results if they exist cache_filename = filename+"_%d_%d.npy" % (start,end) if os.path.exists(cache_filename) and (os.path.getmtime(filename) < os.path.getmtime(cache_filename)): return numpy.load(cache_filename,allow_pickle=True) # load waveform and calculate test positions (w,sr) = loadwave(filename) silence = reference[2] reftri = reference[3] dmctri1 = reference[4] dmctri4 = reference[5] noise1 = reference[6] noise15 = reference[7] dmcnoise1 = reference[8] dmcnoise127 = reference[9] refsquare = reference[10] dmcsquare1 = reference[11] dmcsquare127 = reference[12] swindow = sr # 1 second swindow = int ( (swindow / (sr/BASE_FREQ)) * (sr/BASE_FREQ) ) # snap to nearest period of waveform # read tests baseline = rms_test(w, silence, sr) refs = [ rms_test(w, reftri, swindow), rms_test(w, refsquare, swindow) ] results = [[],[],[],[],refs] for i in range(4): stest = (dmctri4-dmctri1)/3 test_pos = dmctri1 + (i * stest) rms = rms_test(w,test_pos,swindow) results[0].append(rms) for i in range(15): stest = (noise15-noise1)/14 test_pos = noise1 + (i * stest) rms = rms_test(w,test_pos,swindow) results[1].append(rms) for i in range(127): stest = (dmcnoise127-dmcnoise1)/126 test_pos = dmcnoise1 + (i * stest) rms = rms_test(w,test_pos,swindow) results[2].append(rms) stest = (dmcsquare127-dmcsquare1)/126 test_pos = dmcsquare1 + (i * stest) rms = rms_test(w,test_pos,swindow) results[3].append(rms) # cache results and return numpy.save(cache_filename,results) return results def dac_tnd0_blargg(): refs = [ tnd_blargg(15,0,0)-tnd_blargg(0,0,0), squ_blargg(15,0)-squ_blargg(0,0) ] results = [[],[],[],[],refs] for i in range(4): results[0].append(tnd_blargg(7.5,0,15<