Mozzi  version v2.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('#include <Arduino.h>'+'\n')
60  fout.write('#include "mozzi_pgmspace.h"'+'\n \n')
61  fout.write('#define ' + tablename + '_NUM_CELLS '+ str(len(values))+'\n')
62  fout.write('#define ' + tablename + '_SAMPLERATE '+ str(samplerate)+'\n \n')
63  outstring = 'CONSTTABLE_STORAGE(int8_t) ' + tablename + '_DATA [] = {'
64  try:
65  for i in range(len(values)):
66 
67  if (values[i] == 33) and (values[i+1] == 33) and (values[i+2] == 33):
68  values[i+2] = random.choice([32, 34])
69  outstring += str(values[i]) + ", "
70  finally:
71  outstring += "};"
72  outstring = textwrap.fill(outstring, 80)
73  fout.write(outstring)
74  fout.write('\n\n#endif /* ' + tablename + '_H_ */\n')
75  fout.close()
76  print ("wrote " + outfile)
77 
78 char2mozzi(infile, outfile, tablename, samplerate)