How can I have lyrics at regular intervals even if there is no corresponding element at that offset in the score?
I need to do:
myElement.lyric = 'bla bla'
Can I create invisible elements?
The answer is simple if you want to have lyrics at "empty" places in music21
. Just create a note.Lyric
object and put it in a base.ElementWrapper
. For instance:
s = stream.Measure()
n = note.Note()
n.lyric = "Hel-"
s.insert(0, n)
l = note.Lyric('-lo')
ew = base.ElementWrapper(l)
s.insert(1, ew)
Now your have a Stream with a Note with a lyric on beat 1 (offset 0) and a bare lyric on beat 2 (offset 1).
I can already guess your next question though, "Why doesn't it show up when I show it in [Finale/MuseScore/Sibelius/VexFlow/Lilypond]?"
There's a difference between what can be represented in music21
and what can be exported in any given format. Some formats, such as Lilypond or Humdrum, have the ability to put lyrics directly into a score. Some formats that this is possible have music21 support (I believe just Lilypond). Some formats support it, but music21 does not. And then there are formats that don't support it, such as MusicXML. However, there are some workarounds such as creating a rest at a certain location, attaching a lyric to it, and then hiding the rest. This is what most MusicXML clients do. And then there is the issue that music21
might support a feature, the format might support the feature, but the music notation client might not (ex., music21 supports ossias; MusicXML does also; Finale does also; but Finale does not support importing or exporting MusicXML ossias.)
The best way to see what is possible is to create what you want in your notation software, export it to musicxml. Import the musicxml into music21 and look at what types of music21 objects represent these lyrics. Then export back from music21 to musicxml, and reimport into your notation software.
Sorry it's not a simple answer. It's one of the more complex things to do.
As a work around -- many people choose to put a TextExpression() object at the location and hope that it can be made to look just like a lyric.