Mozzi  version v1.1.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('#if ARDUINO >= 100'+'\n')
41  fout.write('#include "Arduino.h"'+'\n')
42  fout.write('#else'+'\n')
43  fout.write('#include "WProgram.h"'+'\n')
44  fout.write('#endif'+'\n')
45  fout.write('#include "mozzi_pgmspace.h"'+'\n \n')
46  fout.write('#define ' + tablename + '_NUM_CELLS '+ str(tablelength) +'\n')
47  outstring = 'CONSTTABLE_STORAGE(int8_t) ' + tablename + '_DATA [] = {'
48  try:
49  for num in range(tablelength):
50
51  x = 2*(float(num-(tablelength/2)))/tablelength
52
53  if curvenum == 3:
54  t_x = 4*pow(x,3)-3*x
55  elif curvenum == 4:
56  t_x = 8*pow(x,4)-8*pow(x,2)+1
57  elif curvenum == 5:
58  t_x = 16*pow(x,5)-20*pow(x,3)+5*x
59  elif curvenum == 6:
60  t_x = 32*pow(x,6)-48*pow(x,4)+18*pow(x,2)-1
61
62  scaled = int16_t(math.floor(t_x*127.999))
63
64  outstring += str(scaled) + ", "
65  finally:
66  outstring += "};"
67  outstring = textwrap.fill(outstring, 80)
68  fout.write(outstring)
69  fout.write('\n \n #endif /* ' + tablename + '_H_ */\n')
70  fout.close()
71  print "wrote " + outfile
72