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