36 import sys, array, os, textwrap, random
38 if len(sys.argv) != 5:
39 print (
'Usage: char2mozzi.py <infile outfile tablename samplerate>')
42 [infile, outfile, tablename, samplerate] = sys.argv[1:]
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))
49 valuesfromfile = array.array(
'b')
51 valuesfromfile.fromfile(fin, uint8_tstoread)
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 [] = {' 69 for i
in range(len(values)):
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]) +
", " 76 outstring = textwrap.fill(outstring, 80)
78 fout.write(
'\n\n#endif /* ' + tablename +
'_H_ */\n')
80 print (
"wrote " + outfile)
82 char2mozzi(infile, outfile, tablename, samplerate)