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