Search code examples
pythonmusic21

How do you color notes in Music21?


How do you color code notes in visual displays with the Music21 Python library?

I'm trying to create an animation where specific notes in sheet music are highlighted with a certain color at specific time intervals. So far I'm able to render sheet music with Music21 and code like:

import music21

music21.environment.set("musicxmlPath", "/usr/bin/musescore")
music21.environment.set("midiPath", "/usr/bin/timidity")

score = music21.converter.parse('score.abc')
score.show()

but it's only static and only black and white.

However, their documentation contains a few examples of sheet music containing color coded notes. Yet I can't see how these images are generated. Much less how to do it dynamically.

Is this supported by the library?


Solution

  • n = note.Note('C#4')
    n.style.color = 'red'
    n.show()
    

    On very old versions of music21 it'd be n.color = 'red'; n.noteheadColor = 'red'; etc.