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(
'#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 [] = {' 
   65         for i 
in range(len(values)):
 
   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]) + 
", " 
   72         outstring = textwrap.fill(outstring, 80)
 
   74         fout.write(
'\n\n#endif /* ' + tablename + 
'_H_ */\n')
 
   76         print (
"wrote " + outfile)
 
   78 char2mozzi(infile, outfile, tablename, samplerate)