Mozzi  version v2.0
sound synthesis library for Arduino
float2mozzi_uint8.py
1 
2 
3 import sys, array, os, textwrap, math
4 
5 def float2mozzi_uint8(infile, outfile, tablename,samplerate):
6  fin = open(os.path.expanduser(infile), "rb")
7  print "opened " + infile
8  valuesetad = os.path.getsize(os.path.expanduser(infile))/4
9 
10 
11  valuesfromfile = array.array('f')
12  try:
13  valuesfromfile.fromfile(fin,valuesetad)
14  finally:
15  fin.close()
16 
17  values=valuesfromfile.tolist()
18 
21  fout = open(os.path.expanduser(outfile), "w")
22  fout.write('#ifndef ' + tablename + '_H_' + '\n')
23  fout.write('#define ' + tablename + '_H_' + '\n \n')
24  fout.write('#include <Arduino.h>'+'\n')
25  fout.write('#include "mozzi_pgmspace.h"'+'\n \n')
26  fout.write('#define ' + tablename + '_NUM_CELLS '+ str(len(values))+'\n')
27  fout.write('#define ' + tablename + '_SAMPLERATE '+ str(samplerate)+'\n \n')
28  outstring = 'CONSTTABLE_STORAGE(int8_t) ' + tablename + '_DATA [] = {'
29  try:
30  for num in values:
31  outstring += str(math.trunc((num*256)+0.5)) + ", "
32 
34  finally:
35  outstring += "};"
36  outstring = textwrap.fill(outstring, 80)
37  fout.write(outstring)
38  fout.write('\n \n #endif /* ' + tablename + '_H_ */\n')
39  fout.close()
40  print "wrote " + outfile
41 
42 float2mozzi_uint8(infile, outfile, tablename, samplerate)