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