Example #1
0
def russianHMM(n_trials,characters,alphabet):
	model = hmm.hmmTrain(characters,2,tol=.1,maxiter=100)
	obs = hmm.transformObservations(alphabet,characters)
	logProb = hmm.score(model,obs)
	for i in xrange(n_trials-1):
		print(i)
		temp_model = hmm.hmmTrain(characters,2,tol=.1,maxiter=100)
		temp_logProb = hmm.score(temp_model,obs)
		if temp_logProb > logProb:
			model = temp_model
			logProb = temp_logProb
	return model
    for i in range(0, 3):
        midi_scratch_file.addTrackName(i, 0, "Pop Flute {0}".format(i))
        midi_scratch_file.addTempo(i, 0, 240)

        time = 0
        while time < song_length:
            duration = random.uniform(duration_min, duration_max)
            pitch = random.randint(pitch_lowbound, pitch_upbound)
            midi_scratch_file.addNote(i, i, pitch, time, duration, volume)
            # print "D: %d, p: %d, t: %d" % (duration, pitch, time)
            time += duration

    binfile = open(relative_file_path, 'w')
    midi_scratch_file.writeFile(binfile)
    binfile.close()

    # And write it to disk.
    # temp_file = tempfile.NamedTemporaryFile()
    # midi_scratch_file.writeFile(temp_file)
    # Next, score the file using a 2-depth HMM
    score = hmm.score(relative_file_path, hmm_depth=3, obs=song_length)
    new_best_score = max(best_score, score)
    if new_best_score > best_score:
        best_score = new_best_score
        shutil.copyfile(relative_file_path, "attempts/score_{0}.mid".format(best_score))

    print "Got Score: %d (tries %d, best score: %d)" % (score, tries, best_score)

shutil.copyfile(relative_file_path, "attempts/best_%d.mid" % (score,))
print "Finished!"
volume = 100

relative_file_path = "data/testing_songs/Random Songs"

for index in range(0, count_of_songs):
	song_length = random.randint(25, 80)
	file_name = str(index) + ".mid";
	time = 0;
	midi_scratch_file = MIDIFile(3)
	for i in range(0,3):
		midi_scratch_file.addTrackName(i, 0, "Pop Flute {0}".format(i))
		midi_scratch_file.addTempo(i, 0, 240)

		time = 0;
		while time < song_length:
			duration = random.uniform(duration_min, duration_max)
			pitch = random.randint(pitch_lowbound, pitch_upbound)
			midi_scratch_file.addNote(i, i, pitch, time, duration, volume)
			time += duration

	file_full_path = os.path.join(relative_file_path, file_name)
	binfile = open(file_full_path, 'w')
	midi_scratch_file.writeFile(binfile)
	binfile.close()

	score = hmm.score(file_full_path, hmm_depth=3, obs=song_length)
	print 'The socre of %s is %d' % (file_name, score)

print 'Finished!'