Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #15925: Include lyrics in MIDI export #21541

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bakajikara
Copy link
Contributor

Resolves: #15925

  • I signed the CLA
  • The title of the PR describes the problem it addresses
  • Each commit's message describes its purpose and effects, and references the issue it resolves
  • If changes are extensive, there is a sequence of easily reviewable commits
  • The code in the PR follows the coding rules
  • There are no unnecessary changes
  • The code compiles and runs on my machine, preferably after each commit individually
  • I created a unit test or vtest to verify the changes I made (if applicable)

@Jojo-Schmitz
Copy link
Contributor

Could this one get reviewed please?

for (const auto& lyric : cr->lyrics()) {
ByteArray lyricText = lyric->plainText().toUtf8();
size_t len = lyricText.size() + 1;
unsigned char* data = new unsigned char[len];
Copy link
Contributor

@mikekirin mikekirin Mar 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like memory leak here. According to the fact that this code used in the loop there is huge memory leak possible =)
Isn't there a method in mu::String which returns char*?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or just delete[] data; at the end of that loop

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this method is full of similar memory leaks, therefore I ignored it while reviewing.

Isn't there a method in mu::String which returns char*?

No, because mu::String uses utf16 so if you want char* you need to convert.

Or just delete[] data; at the end of that loop

Hell no 😄 because we're storing a pointer to it in the resulting MidiEvent.

I think the conclusion is that we should refactor to avoid the need for char*, and store it differently in MidiEvent.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

size_t len = lyricText.size(); // Not sure + 1 is necessary. If so it should be data[len] = '\0', otherwise it's undefined

However why not to use easier approach without unnecessary memory allocation?

std::string lyricText = lyric->plainText().toStdString();
size_t len = lyricText.size();
ev.setEData(lyricText.c_str());
ev.setLen(static_cast<int>(len));

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hell no 😄 because we're storing a pointer to it in the resulting MidiEvent.

Good point

Copy link
Contributor

@Jojo-Schmitz Jojo-Schmitz Mar 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ev.setEData(lyricText.c_str());

c_str() is not an unsigned char * like setEData() expects, seems some casting is needed

Copy link
Contributor

@Jojo-Schmitz Jojo-Schmitz Mar 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But even with that it just doesn't work, on importing such a MIDI the lyrics are completly broken
Actually always the same syllable, but even that sometimes trunccated

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

size_t len = lyricText.size(); // Not sure + 1 is necessary. If so it should be data[len] = '\0', otherwise it's undefined

However why not to use easier approach without unnecessary memory allocation?

std::string lyricText = lyric->plainText().toStdString();
size_t len = lyricText.size();
ev.setEData(lyricText.c_str());
ev.setLen(static_cast<int>(len));

That's basically equivalent to using delete: as soon as lyricText goes out of scope, the underlying c string is deleted but the MidiEvent still contains a pointer to it. That's probably why the result is "completely broken" on Windows and would cause Address Sanitizer complaints on macOS/Linux.

Jojo-Schmitz pushed a commit to Jojo-Schmitz/MuseScore that referenced this pull request Mar 12, 2024
Jojo-Schmitz pushed a commit to Jojo-Schmitz/MuseScore that referenced this pull request Mar 12, 2024
Jojo-Schmitz pushed a commit to Jojo-Schmitz/MuseScore that referenced this pull request Mar 12, 2024
Jojo-Schmitz pushed a commit to Jojo-Schmitz/MuseScore that referenced this pull request Mar 12, 2024
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.

Include lyrics when exporting MIDI files
5 participants