A Markov Chain Based Music Generator

Mohana Ragam

Pranesh Srinivasan
License : Open BSD


A simple music generator that generates small pieces in carnatic music using Markov chains and random walks. Currently, it only generates pieces in the mohanam raga, but is written in an easily extendable manner. Any symmetric, linear raga should be easily pluggable given given the aarohanam / avarohanam and sample pieces.

I chose the Mohanam Ragam as it is one of the "smaller" (in terms of the number of notes) ragams in carnatic music. Further, it is supposed to sound good even without gamakams (slides+vibratos). It also has the same notes during its ascent and descent, thus making it even easier to deal with.

	Ascent  : sa ri ga pa dha Sa
	Descent : Sa dha pa ga ri sa

This python tool uses some small pieces in the mohanam ragam, to learn the raga by building a transition graph. It then performs a random walk on it, keeping in mind several factors like the size of transitions, small patterns and some rhythm to generate the resulting score.

It was written in two days, and obviously does not generate very good music :). Still, I think it has has produced some good pieces.

This idea struck me when I was doing Markov Chains during the "Performance Evaluation of Computer Systems" course I took in college.


During half a day of playing around with this program, it generated some pieces I liked. You can find them here. In particular I like out7.wav, out8.wav and out2.wav.

The input is in (nearly) standard Carnatic notation, thereby allowing easier import of music in the future. You can find the pieces it currently uses as reference here.

The Small Stuff

mcmg, is written in python, and uses the pysynth library to generate the audio file.

The "music theory" that is often an issue with such software has been handled in a couple of python files by me. Carnatic music structure and notation, allow for very easy manipulation. It is only in the final step that the music is converted into western notation for pysynth to handle.

pysynth can output the resultant wav file modelling

This program also supports all three kinds.


You can find the project on github here. You can clone the project by running:

  $ git clone https://github.com/spranesh/mcmg.git
. It requires Python 2.x and the modelling of the cached piano or a stringed instrument will require numpy/scipy.


Running the program is simple. Download and extract the tar file above. cd into the program root, and run

	$ python main.py pieces/*
for its simplest invocation.

The program takes many command line parameters, that control

(See python main.py --help for all the command line options.)

The last parameter, the memory width (-w) controls the amount of similarity with the original pieces in some sense.

A more complicated run would be

	$ python main.py -p s -w 5 -t 2 -n 60 -q 200 -o sample.wav -s sample_notes.txt pieces/*
This would make the program use