Mozzi  version v1.1.0
sound synthesis library for Arduino
sin_multi_levels_int8.py
1 
3 
4 import array,os,textwrap,math
5 
6 
7 
8 def generate(outfile, tablename, tablelength, numtables):
9  fout = open(os.path.expanduser(outfile), "w")
10 
11 
12 
13  fout.write('#ifndef ' + tablename + '_H_' + '\n')
14  fout.write('#define ' + tablename + '_H_' + '\n \n')
15  fout.write('#if ARDUINO >= 100'+'\n')
16  fout.write('#include "Arduino.h"'+'\n')
17  fout.write('#else'+'\n')
18  fout.write('#include "WProgram.h"'+'\n')
19  fout.write('#endif'+'\n')
20  fout.write('#include "mozzi_pgmspace.h"'+'\n \n')
21  fout.write('#define ' + tablename + '_NUM_CELLS '+ str(tablelength) +'\n')
22  fout.write('CONSTTABLE_STORAGE(int8_t) ' + tablename + '_DATA [] = { \n')
23 
24  try:
25  for tablenum in range(numtables):
26  try:
27  outstring = '\n'
28  for num in range(tablelength):
29 
30  x = float(num)/tablelength
31 
32  t_x = math.sin(2*math.pi*x)*(float(tablenum+1)/numtables)
33 
34  scaled = int16_t(math.floor(t_x*127.999))
35 
36  outstring += str(scaled) + ', '
37  finally:
38  outstring = textwrap.fill(outstring, 80)
39  outstring += '\n'
40  fout.write(outstring)
41  finally:
42 
43  fout.write('}; \n \n #endif /* ' + tablename + '_H_ */\n')
44  fout.close()
45  print "wrote " + outfile
46 
47 
48 generate("~/Desktop/sin_64level_int8.h", "SIN_64LEVEL", 256, 64)