Adding metronome ticks to a piece of music

If you need to produce a piece of music with metronome ticks, optionally only in the midi, you can use this boilerplate code. Note that it sounds nicer than it looks :) .