Mozzi  version v2.0
sound synthesis library for Arduino
chebyshev_int8.py
1 
2 
3 import array,os,textwrap,math
4 
5 
6 def chebyTable(outfile, tablename, tablelength, curvenum):
7 
8  """
9  Generates chebyshev polynomial curve tables for WaveShaper.
10 
11  Args:
12  outfile: The file to save as output.
13  tablename: The name to give the table of converted data in the new file.
14  tablelength: Use a power of two.
15  curvenum: The chebyshev polynomial curve number to chebyTable.
16 
17  Examples:
18  chebyTable("~/Desktop/waveshaper_chebyshev_3rd_256_int8.h", "CHEBYSHEV_3RD_256", 256, 3)
19  chebyTable("~/Desktop/waveshaper_chebyshev_4th_256_int8.h", "CHEBYSHEV_4TH_256", 256, 4)
20  chebyTable("~/Desktop/waveshaper_chebyshev_5th_256_int8.h", "CHEBYSHEV_5TH_256", 256, 5)
21  chebyTable("~/Desktop/waveshaper_chebyshev_6th_256_int8.h", "CHEBYSHEV_6TH_256", 256, 6)
22 
23  Resources:
24  http://www.obiwannabe.co.uk/html/music/6SS/six-waveshaper.html
25  http://mathworld.wolfram.com/ChebyshevPolynomialoftheFirstKind.html
26  The first few Chebyshev polynomials of the first kind are
27  T_0(x) = 1
28  T_1(x) = x
29  T_2(x) = 2x^2-1
30  T_3(x) = 4x^3-3x
31  T_4(x) = 8x^4-8x^2+1
32  T_5(x) = 16x^5-20x^3+5x
33  T_6(x) = 32x^6-48x^4+18x^2-1
34 
35  """
36 
37  fout = open(os.path.expanduser(outfile), "w")
38  fout.write('#ifndef ' + tablename + '_H_' + '\n')
39  fout.write('#define ' + tablename + '_H_' + '\n \n')
40  fout.write('#include <Arduino.h>'+'\n')
41  fout.write('#include "mozzi_pgmspace.h"'+'\n \n')
42  fout.write('#define ' + tablename + '_NUM_CELLS '+ str(tablelength) +'\n')
43  outstring = 'CONSTTABLE_STORAGE(int8_t) ' + tablename + '_DATA [] = {'
44  try:
45  for num in range(tablelength):
46 
47  x = 2*(float(num-(tablelength/2)))/tablelength
48 
49  if curvenum == 3:
50  t_x = 4*pow(x,3)-3*x
51  elif curvenum == 4:
52  t_x = 8*pow(x,4)-8*pow(x,2)+1
53  elif curvenum == 5:
54  t_x = 16*pow(x,5)-20*pow(x,3)+5*x
55  elif curvenum == 6:
56  t_x = 32*pow(x,6)-48*pow(x,4)+18*pow(x,2)-1
57 
58  scaled = int16_t(math.floor(t_x*127.999))
59 
60  outstring += str(scaled) + ", "
61  finally:
62  outstring += "};"
63  outstring = textwrap.fill(outstring, 80)
64  fout.write(outstring)
65  fout.write('\n \n #endif /* ' + tablename + '_H_ */\n')
66  fout.close()
67  print "wrote " + outfile
68