![]() |
Mozzi
alpha 0.01.1t
sound synthesis library for Arduino
|
00001 ## char2mozzi.py 00002 ## Created 2010-12 by Tim Barrass 00003 ## 00004 ## char2mozzi.py converts raw 8 bit sound data to wavetables for Mozzi. 00005 ## 00006 ## Usage: python char2mozzi.py <infilename outfilename "tablename" "samplerate"> 00007 ## 00008 ## Using Audacity to prepare raw sound files for converting: 00009 ## 00010 ## Set your Audacity project sample rate: 00011 ## 00012 ## For generated waveforms like sine or sawtooth, set the project 00013 ## rate to the size of the wavetable you wish to create, which must 00014 ## be a power of two (eg. 8192), and set the selection format 00015 ## (beneath the editing window) to samples. Then you can generate 00016 ## and save 1 second of a waveform and it will fit your table 00017 ## length. 00018 ## 00019 ## For a recorded audio sample, set the project rate to the 00020 ## Mozzi AUDIO_RATE (16384 in the current version). Then edit 00021 ## your sounds to a power-of-two number of samples. 00022 ## 00023 ## Save by exporting with the format set to "Other uncompressed formats", 00024 ## options set to "RAW(headerless)" and "Encoding 8 bit signed PCM". 00025 00026 00027 import sys, array, os, textwrap, random 00028 00029 if len(sys.argv) != 5: 00030 print 'Usage: char2mozzi.py <infilename outfilename "tablename" "samplerate">' 00031 sys.exit(1) 00032 00033 [infilename, outfilename, tablename, samplerate] = sys.argv[1:] 00034 00035 def char2mozzi(infilename, outfilename, tablename, samplerate): 00036 fin = open(os.path.expanduser(infilename), "rb") 00037 print "opened " + infilename 00038 bytestoread = os.path.getsize(os.path.expanduser(infilename)) 00039 ##print bytestoread 00040 valuesfromfile = array.array('b') # array of signed char ints 00041 try: 00042 valuesfromfile.fromfile(fin, bytestoread) 00043 finally: 00044 fin.close() 00045 00046 values=valuesfromfile.tolist() 00047 ## print values[0] 00048 ## print values[len(values)-1] 00049 ## print len(values) 00050 fout = open(os.path.expanduser(outfilename), "w") 00051 fout.write('#ifndef ' + tablename + '_H_' + '\n') 00052 fout.write('#define ' + tablename + '_H_' + '\n \n') 00053 fout.write('#include "Arduino.h"'+'\n') 00054 fout.write('#include <avr/pgmspace.h>'+'\n \n') 00055 fout.write('#define ' + tablename + '_NUM_CELLS '+ str(len(values))+'\n') 00056 fout.write('#define ' + tablename + '_SAMPLERATE '+ str(samplerate)+'\n \n') 00057 outstring = 'const char __attribute__((progmem)) ' + tablename + '_DATA [] = {' 00058 try: 00059 for i in range(len(values)): 00060 ## mega2560 boards won't upload if there is 33, 33, 33 in the array, so dither the 3rd 33 if there is one 00061 if (values[i] == 33) and (values[i+1] == 33) and (values[i+2] == 33): 00062 values[i+2] = random.choice([32, 34]) 00063 outstring += str(values[i]) + ", " 00064 finally: 00065 outstring += "};" 00066 outstring = textwrap.fill(outstring, 80) 00067 fout.write(outstring) 00068 fout.write('\n \n #endif /* ' + tablename + '_H_ */\n') 00069 fout.close() 00070 print "wrote " + outfilename 00071 00072 char2mozzi(infilename, outfilename, tablename, samplerate)