Mozzi  version v1.1.0
sound synthesis library for Arduino
triangle.py
1 import array
2 import os
3 import textwrap
4 import math
5 
6 def generate(outfile, tablename, tablelength, samplerate):
7  fout = open(os.path.expanduser(outfile), "w")
8  fout.write('#ifndef ' + tablename + '_H_' + '\n')
9  fout.write('#define ' + tablename + '_H_' + '\n \n')
10  fout.write('#if ARDUINO >= 100'+'\n')
11  fout.write('#include "Arduino.h"'+'\n')
12  fout.write('#else'+'\n')
13  fout.write('#include "WProgram.h"'+'\n')
14  fout.write('#endif'+'\n')
15  fout.write('#include "mozzi_pgmspace.h"'+'\n \n')
16  fout.write('#define ' + tablename + '_NUM_CELLS '+ str(tablelength)+'\n')
17  fout.write('#define ' + tablename + '_SAMPLERATE '+ str(samplerate)+'\n \n')
18  outstring = 'CONSTTABLE_STORAGE(int8_t) ' + tablename + '_DATA [] = {'
19  try:
20  for num in range(tablelength):
21 
22  x = float(num)/tablelength
23  if x<0.5:
24  t_x = 2 * x
25  else:
26  t_x = 2 - 2 * x
27 
28  scaled = int(math.floor(t_x*255.999))
29  outstring += str(scaled) + ', '
30  finally:
31  outstring += "};"
32  outstring = textwrap.fill(outstring, 80)
33  fout.write(outstring)
34  fout.write('\n \n #endif /* ' + tablename + '_H_ */\n')
35  fout.close()
36  print "wrote " + outfile
37 
38 generate("~/Desktop/triangle512_uint8.h", "triange", 512, "512")