I hacked on this a while ago, and the best libraries I found were one called Wad for playing notes and applying effects, and another called Teoria for building chords and intervals and so on.
I was hindered by not having any idea what I was doing but here's what I came up with: