#!/usr/bin/env python # version 0.0.1 - http://snw.lonningdal.no/soxvolume.php # Use constants to create list of volume coefficients with known bit length from decimal import Decimal # Number of decimals to create initial coeffs for the first 6dB of atteunation # 4 decimals 7 steps for each 6dB - step approx 1.00dB - max length 20-bit # 5 decimals 16 steps for each 6dB - step approx 0.30dB - max length 21-bit # 6 decimals 33 steps for each 6dB - step approx 0.14dB - max length 22-bit # 7 decimals 63 steps for each 6dB - step approx 0.07dB - max length 23-bit # 8 decimals 129 steps for each 6dB - step approx 0.035dB - max length 24-bit total of 2049 steps with max atteunation nrofdecimals=8 # input as bits, 3 equals -18dB, 9 -54dB, 16 -96dB maxattenuaton=16 # coefficients list are build in three parts, first from constant, then each # value are divided by a power of 2 and last we append 1.0 for zero atteunation coeffs=list() def build_coeffs_list(nrofdecimals, maxattenuaton): COEFFSCONST={4: 625, 5: 3125, 6: 15625, 7: 78125, 8: 390625} START={4: 10000, 5: 100000, 6: 1000000, 7: 10000000, 8: 100000000} MAXATTENUATION=([1, 0.5, 0.25, 0.125, 0.0625, 0.03125, 0.015625, 0.0078125, 0.00390625, 0.001953125, 0.0009765625, 0.00048828125, 0.000244140625, 0.0001220703125, 0.00006103515625, 0.000030517578125, 0.0000152587890625]) # from constant create first coefficients down to -6dB ~ 1-bit x=START[nrofdecimals]-COEFFSCONST[nrofdecimals] while x >= START[nrofdecimals]/2: # build valid Decimal number from string and integer coeffs.append(Decimal('0.'+str(x))) x=x-COEFFSCONST[nrofdecimals] # now build complete list from initial coeffs nrofcoeffs = len(coeffs) readposition = 0 newcoeff = 1 # must be more than 0.5 while newcoeff >= Decimal(MAXATTENUATION[maxattenuaton-1]): for i in range(nrofcoeffs): newcoeff=Decimal(str(coeffs[readposition])) / 2 coeffs.append(newcoeff) readposition += 1 readposition = len(coeffs) - nrofcoeffs # complete the list with value for zero atteunation coeffs.insert(0, 1.0) # END buildcoeffslist def write_coefficients_list_to_file(): f = open("coeffs.txt", "a") for coeff in coeffs: print (Decimal(coeff), file=f) f.close() build_coeffs_list(nrofdecimals, maxattenuaton) for coeff in coeffs: print (coeff)