Mozzi  version v2.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('#include <Arduino.h>'+'\n')
11  fout.write('#include "mozzi_pgmspace.h"'+'\n \n')
12  fout.write('#define ' + tablename + '_NUM_CELLS '+ str(tablelength)+'\n')
13  fout.write('#define ' + tablename + '_SAMPLERATE '+ str(samplerate)+'\n \n')
14  outstring = 'CONSTTABLE_STORAGE(int8_t) ' + tablename + '_DATA [] = {'
15  try:
16  for num in range(tablelength):
17 
18  x = float(num)/tablelength
19  if x<0.5:
20  t_x = 2 * x
21  else:
22  t_x = 2 - 2 * x
23 
24  scaled = int(math.floor(t_x*255.999))
25  outstring += str(scaled) + ', '
26  finally:
27  outstring += "};"
28  outstring = textwrap.fill(outstring, 80)
29  fout.write(outstring)
30  fout.write('\n \n #endif /* ' + tablename + '_H_ */\n')
31  fout.close()
32  print "wrote " + outfile
33 
34 generate("~/Desktop/triangle512_uint8.h", "triange", 512, "512")