Skip to content

Instantly share code, notes, and snippets.

@Ceasar
Created April 18, 2012 23:05
Show Gist options
  • Save Ceasar/2417260 to your computer and use it in GitHub Desktop.
Save Ceasar/2417260 to your computer and use it in GitHub Desktop.
Tool to generate music automatically using Markov Chains. Simply pass a Melopy mp file to the script and it will generate a song. Requires Nonsense and Melopy.
import sys
from itertools import izip
from melopy import Melopy
from nonsense import StationarySource
if __name__ == "__main__":
try:
sample = sys.argv[1] # must not have comments
except IndexError:
sample = sys.stdin.read()
degree = 2
output = "mysong"
else:
try:
degree = sys.argv[2]
except IndexError:
degree = 2
try:
output = sys.argv[3]
except IndexError:
output = "mysong"
ss = StationarySource(["".join(sample.split())], int(degree))
m = Melopy(output)
print "Generating song..."
song = "".join([c for c, _ in izip(ss.generate_sequence(), xrange(500))])
print "Parsng song..."
m.parse(song)
m.render()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment