Mozzi  alpha 0.01.1t
sound synthesis library for Arduino
 All Classes Functions Typedefs
python/float2mozzi.py
00001 ## for converting 32 bit float raw files from Audacity, with values  -128 to 127 int8 Mozzi table
00002 
00003 import sys, array, os, textwrap, math
00004 
00005     if len(sys.argv) != 5:
00006         print 'Usage: float2mozzi.py <infilename outfilename tablename samplerate>'
00007         sys.exit(1)
00008 
00009 [infilename, outfilename, tablename, samplerate] = sys.argv[1:]
00010 
00011 def float2mozzi(infilename, outfilename, tablename,samplerate):
00012     fin = open(os.path.expanduser(infilename), "rb")
00013     print "opened " + infilename
00014     valuesetad = os.path.getsize(os.path.expanduser(infilename))/4 ## adjust for number format
00015 
00016     ##print valuesetad
00017     valuesfromfile = array.array('f')## array of floats
00018     try:
00019         valuesfromfile.fromfile(fin,valuesetad)
00020     finally:
00021         fin.close()
00022 
00023     values=valuesfromfile.tolist()
00024 ##    print values[0]
00025 ##    print values[len(values)-1]
00026 ##    print len(values)
00027     fout = open(os.path.expanduser(outfilename), "w")
00028     fout.write('#ifndef ' + tablename + '_H_' + '\n')
00029     fout.write('#define ' + tablename + '_H_' + '\n \n')
00030     fout.write('#include "Arduino.h"'+'\n')
00031     fout.write('#include <avr/pgmspace.h>'+'\n \n')
00032     fout.write('#define ' + tablename + '_NUM_CELLS '+ str(len(values))+'\n')
00033     fout.write('#define ' + tablename + '_SAMPLERATE '+ str(samplerate)+'\n \n')
00034     outstring = 'const char __attribute__((progmem)) ' + tablename + '_DATA [] = {'
00035     try:
00036         for num in values:
00037             outstring += str(math.trunc((num*256)+0.5)) + ", "
00038  ##           outstring += str(num) + ", "
00039         ##values.fromfile(fin, bytesetad)
00040     finally:
00041         outstring +=  "};"
00042         outstring = textwrap.fill(outstring, 80)
00043         fout.write(outstring)
00044         fout.write('\n \n #endif /* ' + tablename + '_H_ */\n')
00045         fout.close()
00046         print "wrote " + outfilename
00047 
00048 float2mozzi(infilename, outfilename, tablename, samplerate)