![]() |
Mozzi
alpha 0.01.1t
sound synthesis library for Arduino
|
00001 ## generates a set of sin waves at different amplitudes 00002 ## tables go from 1 to numtables+1, as table 0 is all 0's 00003 00004 import array,os,textwrap,math 00005 00006 00007 00008 def generate(outfilename, tablename, tablelength, numtables): 00009 fout = open(os.path.expanduser(outfilename), "w") 00010 00011 00012 00013 fout.write('#ifndef ' + tablename + '_H_' + '\n') 00014 fout.write('#define ' + tablename + '_H_' + '\n \n') 00015 fout.write('#include "Arduino.h"'+'\n') 00016 fout.write('#include <avr/pgmspace.h>'+'\n \n') 00017 fout.write('#define ' + tablename + '_NUM_CELLS '+ str(tablelength) +'\n') 00018 fout.write('const char __attribute__((progmem)) ' + tablename + '_DATA [] = { \n') 00019 00020 try: 00021 for tablenum in range(numtables): 00022 try: 00023 outstring = '\n' 00024 for num in range(tablelength): 00025 ## range between 0 and 1 first 00026 x = float(num)/tablelength 00027 00028 t_x = math.sin(2*math.pi*x)*(float(tablenum+1)/numtables) 00029 00030 scaled = int(math.floor(t_x*127.999)) 00031 00032 outstring += str(scaled) + ', ' 00033 finally: 00034 outstring = textwrap.fill(outstring, 80) 00035 outstring += '\n' 00036 fout.write(outstring) 00037 finally: 00038 00039 fout.write('}; \n \n #endif /* ' + tablename + '_H_ */\n') 00040 fout.close() 00041 print "wrote " + outfilename 00042 00043 00044 generate("~/Desktop/sin_64level_int8.h", "SIN_64LEVEL", 256, 64)