Skip to content

Conversation

@oxygen-dioxide
Copy link
Contributor

Midi files can contain lyrics. Many music creation softwares that deal with lyrics, especially sheet music editor and singing voice synthesizers, such as Musescore and OpenUtau, can create midi file with lyrics. After this PR, we can load lyrics from midi files.

#356 will be solved.

@coveralls
Copy link

coveralls commented Apr 17, 2025

Coverage Status

coverage: 93.013% (+0.001%) from 93.012%
when pulling 032c51f on oxygen-dioxide:midilyrics
into 41b6b6f on cuthbertLab:master.

@mscuthbert
Copy link
Member

hi @oxygen-dioxide -- can you add tests including doctests for getLyricsFromEvents. Thanks.

@oxygen-dioxide
Copy link
Contributor Author

oxygen-dioxide commented May 13, 2025

Added tests for lyrics importing. The song for testing is a small part of Haydn's String Quartet Op. 76 with Chinese and Korean lyrics.
Different softwares use different encoding when writing midi file, and some of them use your system encoding. Music21's parser uses UTF-8 encoding by default. To specify encoding, use converter.parse(fp, encoding_type = encoding). Tests for UTF-8, GBK and EUC-KR encodings are added.

@mscuthbert
Copy link
Member

Almost there -- can you fix the errors in flake8 run:

flake8 music21

fixing those should fix the pylint also.

Because note is a module name we don't use it as a variable name (same w/ pitch, interval, etc.) so use n or n1 etc.

@oxygen-dioxide
Copy link
Contributor Author

oxygen-dioxide commented May 15, 2025

I also want to implement "- to Slur". In singing synthesis softwares, we use - to make the current note and the previous note a slur, which means the syllable sings across two notes. So I think we should parse midi lyric - as Slurs in music21.
Example:
image
image

@mscuthbert
Copy link
Member

Closing and Reopening to get a clean slate.

@mscuthbert mscuthbert closed this May 15, 2025
@mscuthbert mscuthbert reopened this May 15, 2025
@mscuthbert
Copy link
Member

Hi @oxygen-dioxide -- Great PR -- there are still a few changes I wanted to make but they were easier for me to do because they mostly touched on parts of the system that were pre-existing.

As far as "-" getting translated to slurs automatically, I'm not going to put that into the music21 MIDI parsing routines unless it becomes a standard (or de-facto standard) across all of MIDI. I'd be glad to put example code of how to do this after the fact somewhere in the documentation system. Glad to know that automatic singing systems are also using music21 --I'd very much enjoy talking to you about what you're doing sometime!

@mscuthbert mscuthbert merged commit cbdd142 into cuthbertLab:master May 15, 2025
8 checks passed
mscuthbert added a commit that referenced this pull request May 16, 2025
midi.translate improvements

Followup from #1769 gave me an opportunity to find places to improve midi translate.  Pinging @oxygen-dioxide since some changes took place since then

* Change `getNotesFromEvents()` from potentially O(n^2) to O(n) while retaining sort order.

* Pass encoding to parsing of instrument names in addition to lyrics (renamed "encoding_type" to "encoding" -- missed in prior review)

* create TimingNoteEvent which is a typed NamedTuple that keeps track of when a MidiEvent NoteOn started, stopped, and what the NoteOn event was.  Replaced the old tuple of tuples.

* Faster gathering of Chords (remove potential O(n^2) search).

* Remove unused special case for single-note streams.

* opFrac many offsets and durations in MIDI for absolutely correct tuplets, etc.

* Misc: Update braille.translate SegmentKey to typing.NamedTuple

* Misc: Search code for places where we were still casting to float for Python 2 division!

* Misc: Add typing to text/Trigram and some layout things

This is part of the non-backwards compatible MIDI.translate guts rewrite discussed on the music21 list.

Coverage % is expected to drop because the amount of code has been reduced a lot.
@oxygen-dioxide
Copy link
Contributor Author

For the "'-' to slurs", I found that music21 doesn't allow "-" as note lyric. I understand that we don't make it builtin because it isn't a standard, but if a developer wants to convert "-" to slurs in their own python code, they can't, because all the infos about "-" lyrics are cleared when loading the midi file into music21.

@mscuthbert
Copy link
Member

Instead of setting ".lyric" on a note in MIDI output we should create a Lyric object and set the "rawText" to the value. That way it will preserve the hyphen.

Currently it should set the lyric to the Lyric continue type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants