![]() |
Mozzi
alpha 0.01.1t
sound synthesis library for Arduino
|
00001 ## generates chebyshev polynomial curve tables for WaveShaper 00002 00003 import array,os,textwrap,math 00004 00005 ##resources: 00006 ##http://www.obiwannabe.co.uk/html/music/6SS/six-waveshaper.html 00007 ##http://mathworld.wolfram.com/ChebyshevPolynomialoftheFirstKind.html 00008 ##The first few Chebyshev polynomials of the first kind are 00009 ##T_0(x) = 1 00010 ##T_1(x) = x 00011 ##T_2(x) = 2x^2-1 00012 ##T_3(x) = 4x^3-3x 00013 ##T_4(x) = 8x^4-8x^2+1 00014 ##T_5(x) = 16x^5-20x^3+5x 00015 ##T_6(x) = 32x^6-48x^4+18x^2-1 00016 00017 00018 def generate(outfilename, tablename, tablelength, curvenum): 00019 fout = open(os.path.expanduser(outfilename), "w") 00020 fout.write('#ifndef ' + tablename + '_H_' + '\n') 00021 fout.write('#define ' + tablename + '_H_' + '\n \n') 00022 fout.write('#include "Arduino.h"'+'\n') 00023 fout.write('#include <avr/pgmspace.h>'+'\n \n') 00024 fout.write('#define ' + tablename + '_NUM_CELLS '+ str(tablelength) +'\n') 00025 outstring = 'const char __attribute__((progmem)) ' + tablename + '_DATA [] = {' 00026 try: 00027 for num in range(tablelength): 00028 ## range between -1 and 1 first 00029 x = 2*(float(num-(tablelength/2)))/tablelength 00030 00031 if curvenum == 3: 00032 t_x = 4*pow(x,3)-3*x 00033 elif curvenum == 4: 00034 t_x = 8*pow(x,4)-8*pow(x,2)+1 00035 elif curvenum == 5: 00036 t_x = 16*pow(x,5)-20*pow(x,3)+5*x 00037 elif curvenum == 6: 00038 t_x = 32*pow(x,6)-48*pow(x,4)+18*pow(x,2)-1 00039 00040 scaled = int(math.floor(t_x*127.999)) 00041 00042 outstring += str(scaled) + ", " 00043 finally: 00044 outstring += "};" 00045 outstring = textwrap.fill(outstring, 80) 00046 fout.write(outstring) 00047 fout.write('\n \n #endif /* ' + tablename + '_H_ */\n') 00048 fout.close() 00049 print "wrote " + outfilename 00050 00051 00052 generate("~/Desktop/waveshaper_chebyshev_3rd_256_int8.h", "CHEBYSHEV_3RD_256", 256, 3) 00053 generate("~/Desktop/waveshaper_chebyshev_4th_256_int8.h", "CHEBYSHEV_4TH_256", 256, 4) 00054 generate("~/Desktop/waveshaper_chebyshev_5th_256_int8.h", "CHEBYSHEV_5TH_256", 256, 5) 00055 generate("~/Desktop/waveshaper_chebyshev_6th_256_int8.h", "CHEBYSHEV_6TH_256", 256, 6)