Mozzi  version v1.1.0
sound synthesis library for Arduino
char2mozzi.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 
35 
36 import sys, array, os, textwrap, random
37 
38 if len(sys.argv) != 5:
39  print ('Usage: char2mozzi.py <infile outfile tablename samplerate>')
40  sys.exit(1)
41 
42 [infile, outfile, tablename, samplerate] = sys.argv[1:]
43 
44 def char2mozzi(infile, outfile, tablename, samplerate):
45  fin = open(os.path.expanduser(infile), "rb")
46  print ("opened " + infile)
47  uint8_tstoread = os.path.getsize(os.path.expanduser(infile))
48 
49  valuesfromfile = array.array('b') # array of signed int8_t ints
50  try:
51  valuesfromfile.fromfile(fin, uint8_tstoread)
52  finally:
53  fin.close()
54 
55  values=valuesfromfile.tolist()
56  fout = open(os.path.expanduser(outfile), "w")
57  fout.write('#ifndef ' + tablename + '_H_' + '\n')
58  fout.write('#define ' + tablename + '_H_' + '\n \n')
59  fout.write('#if ARDUINO >= 100'+'\n')
60  fout.write('#include "Arduino.h"'+'\n')
61  fout.write('#else'+'\n')
62  fout.write('#include "WProgram.h"'+'\n')
63  fout.write('#endif'+'\n')
64  fout.write('#include "mozzi_pgmspace.h"'+'\n \n')
65  fout.write('#define ' + tablename + '_NUM_CELLS '+ str(len(values))+'\n')
66  fout.write('#define ' + tablename + '_SAMPLERATE '+ str(samplerate)+'\n \n')
67  outstring = 'CONSTTABLE_STORAGE(int8_t) ' + tablename + '_DATA [] = {'
68  try:
69  for i in range(len(values)):
70 
71  if (values[i] == 33) and (values[i+1] == 33) and (values[i+2] == 33):
72  values[i+2] = random.choice([32, 34])
73  outstring += str(values[i]) + ", "
74  finally:
75  outstring += "};"
76  outstring = textwrap.fill(outstring, 80)
77  fout.write(outstring)
78  fout.write('\n\n#endif /* ' + tablename + '_H_ */\n')
79  fout.close()
80  print ("wrote " + outfile)
81 
82 char2mozzi(infile, outfile, tablename, samplerate)