def do_POST(self): """All post requests are sent using AJAX from the web app. The requests should contain one waveform file to be parsed and added to the pattern dictionary. After the file is parsed, an output wavform will be sent.""" try: # get the form data form = cgi.FieldStorage( fp=self.rfile, headers=self.headers, environ={'REQUEST_METHOD':'POST', 'CONTENT_TYPE':self.headers['Content-Type'], }) # create a new generator object g = generator.Generator() # load pickle data into the generator if os.path.exists("pattern-data.pickle"): g.pattern_dictionary = pickle.load(open("pattern-data.pickle", "rb" )) # if the user submitted a file, add it to the generator's dictionary f = form['file'].file f.seek(0, 2) if f.tell() > 0: print "Success: A valid input file was received." f.seek(0,0) g.add_wave(f) else: print "Warning: No valid file was received." # dump the data into a pickle file pickle.dump(g.pattern_dictionary, open("pattern-data.pickle", "wb")) # build an output song print "Loaded Pattern Dictionary. Generating Song" output = StringIO.StringIO() waveform.to_file(output, waveform.from_string(str(g.run()))) print "Song Finished" # send header and body encoded in base64, which is what the web app # is using self.send_response(200) self.send_header("Content-type", "application/octet-stream") self.end_headers() self.wfile.write(base64.b64encode(output.getvalue())) except: print "Unexpected error:", sys.exc_info()[0] raise
import sys sys.path.append("../") # ****************************************************************************** import waveform if __name__=="__main__": if len(sys.argv) > 1: # LOAD A WAVEFORM FROM FILE wav = waveform.from_file(sys.argv[1]) originalsamplewidth = wav.getsamplewidth() originalchannelcount = wav.getchannelcount() originalsize = len(wav.getsamples()) # TEST ONE: LOADING AND WRITING # write the data to a file waveform.to_file('readertest.wav', wav) # check that the files are identical, ie. the data was read and processed properly import filecmp assert filecmp.cmp(sys.argv[1], 'readertest.wav') # delete the created test file from os import remove remove('readertest.wav') # TEST TWO: CHANGING THE CHANNEL COUNT # change the waveform sample width wav.setchannelcount(1) # check that the sample data reflects the change assert len(wav.getsamples()) == originalsize / originalchannelcount # TEST THREE: CHANGING SAMPLE WIDTH # change the waveform sample width