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