Mozzi  version v2.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('#include <Arduino.h>'+'\n')
16  fout.write('#include "mozzi_pgmspace.h"'+'\n \n')
17  fout.write('#define ' + tablename + '_NUM_CELLS '+ str(tablelength) +'\n')
18  fout.write('CONSTTABLE_STORAGE(int8_t) ' + tablename + '_DATA [] = { \n')
19 
20  try:
21  for tablenum in range(numtables):
22  try:
23  outstring = '\n'
24  for num in range(tablelength):
25 
26  x = float(num)/tablelength
27 
28  t_x = math.sin(2*math.pi*x)*(float(tablenum+1)/numtables)
29 
30  scaled = int16_t(math.floor(t_x*127.999))
31 
32  outstring += str(scaled) + ', '
33  finally:
34  outstring = textwrap.fill(outstring, 80)
35  outstring += '\n'
36  fout.write(outstring)
37  finally:
38 
39  fout.write('}; \n \n #endif /* ' + tablename + '_H_ */\n')
40  fout.close()
41  print "wrote " + outfile
42 
43 
44 generate("~/Desktop/sin_64level_int8.h", "SIN_64LEVEL", 256, 64)