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