Mozzi  version v1.1.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('#if ARDUINO >= 100'+'\n')
31  fout.write('#include "Arduino.h"'+'\n')
32  fout.write('#else'+'\n')
33  fout.write('#include "WProgram.h"'+'\n')
34  fout.write('#endif'+'\n')
35  fout.write('#include "mozzi_pgmspace.h"'+'\n \n')
36  fout.write('#define ' + tablename + '_NUM_CELLS '+ str(len(values))+'\n')
37  fout.write('#define ' + tablename + '_SAMPLERATE '+ str(samplerate)+'\n \n')
38  outstring = 'CONSTTABLE_STORAGE(int8_t) ' + tablename + '_DATA [] = {'
39  try:
40  for num in values:
41  outstring += str(math.trunc((num*256)+0.5)) + ", "
42 
44  finally:
45  outstring += "};"
46  outstring = textwrap.fill(outstring, 80)
47  fout.write(outstring)
48  fout.write('\n \n #endif /* ' + tablename + '_H_ */\n')
49  fout.close()
50  print "wrote " + outfile
51 
52 float2mozzi(infile, outfile, tablename, samplerate)