1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
#include "importmidi_lyrics.h"
#include "importmidi_inner.h"
#include "importmidi_fraction.h"
#include "importmidi_chord.h"
#include "importmidi_operations.h"
#include "libmscore/box.h"
#include "libmscore/element.h"
#include "libmscore/measurebase.h"
#include "libmscore/score.h"
#include "libmscore/staff.h"
#include "libmscore/text.h"
#include "midi/midifile.h"
#include "mscore/preferences.h"
#include <set>
namespace Ms {
extern Preferences preferences;
namespace MidiLyrics {
const std::string META_PREFIX = "@";
const std::string TEXT_PREFIX = "@T";
bool isMetaText(const std::string &text)
{
return text.substr(0, META_PREFIX.size()) == META_PREFIX;
}
bool isLyricText(const std::string &text)
{
return !isMetaText(text) || text.substr(0, TEXT_PREFIX.size()) == TEXT_PREFIX;
}
bool isLyricEvent(const MidiEvent &e)
{
return e.type() == ME_META && (e.metaType() == META_TEXT
|| e.metaType() == META_LYRIC);
}
std::multimap<ReducedFraction, std::string>
extractLyricsFromTrack(const MidiTrack &track, int division, bool isDivisionInTps)
{
std::multimap<ReducedFraction, std::string> lyrics;
for (const auto &i: track.events()) {
const auto& e = i.second;
if (isLyricEvent(e)) {
const uchar* data = (uchar*)e.edata();
std::string text = MidiCharset::fromUchar(data);
if (isLyricText(text)) {
const auto tick = toMuseScoreTicks(i.first, division, isDivisionInTps);
// no charset handling here
lyrics.insert({tick, text});
}
}
}
return lyrics;
}
#ifdef IMPORTMIDI_DEBUG
bool areEqualIndexesSuccessive(const QList<MTrack> &tracks)
{
std::set<int> usedIndexes;
int prevIndex = -1;
for (const MTrack &track: tracks) {
if (track.indexOfOperation < 0)
return false;
if (track.indexOfOperation != prevIndex) {
const auto it = usedIndexes.find(track.indexOfOperation);
if (it == usedIndexes.end())
usedIndexes.insert(track.indexOfOperation);
else
return false;
prevIndex = track.indexOfOperation;
}
}
return true;
}
#endif
struct BestTrack
{
int index = -1;
// <orig time, current time - after quantization>
std::vector<std::pair<ReducedFraction, ReducedFraction>> matchedLyricTimes;
};
// find track to insert lyrics
// it will be the track with max onTime match cases, except drum tracks
BestTrack findBestTrack(
const QList<MTrack> &tracks,
const std::multimap<ReducedFraction, std::string> &lyricTrack,
const std::set<int> &usedTracks)
{
BestTrack bestTrack;
int maxMatches = 0;
Q_ASSERT_X(areEqualIndexesSuccessive(tracks),
"MidiLyrics::findBestTrack", "Equal indexes of operations are not successive");
for (int i = 0; i != tracks.size(); ++i) {
if (tracks[i].mtrack->drumTrack() || usedTracks.find(i) != usedTracks.end())
continue;
// due to staff split there can be equal indexes of operation
if (i > 0 && tracks[i].indexOfOperation == tracks[i - 1].indexOfOperation)
continue;
int matches = 0;
for (const auto &chord: tracks[i].chords) {
for (const auto ¬e: chord.second.notes) {
if (lyricTrack.find(note.origOnTime) != lyricTrack.end()) {
++matches;
bestTrack.matchedLyricTimes.push_back({chord.first,
note.origOnTime});
break;
}
}
}
if (matches > maxMatches) {
maxMatches = matches;
bestTrack.index = tracks[i].indexOfOperation;
}
}
return bestTrack;
}
bool isTitlePrefix(const QString &text)
{
return (text.left(TEXT_PREFIX.size()) == QString::fromStdString(TEXT_PREFIX));
}
void addTitleToScore(Score *score, const QString &string, int textCounter)
{
Text* text = new Text(score);
if (textCounter == 1)
text->setTextStyleType(TextStyleType::TITLE);
else if (textCounter == 2)
text->setTextStyleType(TextStyleType::COMPOSER);
text->setPlainText(string.right(string.size() - TEXT_PREFIX.size()));
MeasureBase* measure = score->first();
if (measure->type() != Element::Type::VBOX) {
measure = new VBox(score);
measure->setTick(0);
measure->setNext(score->first());
score->measures()->add(measure);
}
measure->add(text);
}
// remove slashes in kar format
QString removeSlashes(const QString &text)
{
QString newText = text;
newText = newText.replace("/", "");
newText = newText.replace("\\", "");
return newText;
}
std::string removeSlashes(const std::string &text)
{
std::string str = text;
char chars[] = "/\\";
for (unsigned int i = 0; i != strlen(chars); ++i)
str.erase(std::remove(str.begin(), str.end(), chars[i]), str.end());
return str;
}
void addTitleIfAny(const std::multimap<ReducedFraction, std::string> &lyricTrack, Score *score)
{
int textCounter = 0;
for (const auto &lyric: lyricTrack) {
if (lyric.first == ReducedFraction(0, 1)) {
QString text = MidiCharset::convertToCharset(lyric.second);
if (isTitlePrefix(text)) {
++textCounter;
addTitleToScore(score, text, textCounter);
}
}
else if (lyric.first > ReducedFraction(0, 1)) {
break;
}
}
}
void addLyricsToScore(
const std::multimap<ReducedFraction, std::string> &lyricTrack,
const std::vector<std::pair<ReducedFraction, ReducedFraction>> &matchedLyricTimes,
const Staff *staffAddTo)
{
Score *score = staffAddTo->score();
addTitleIfAny(lyricTrack, score);
for (const auto &timePair: matchedLyricTimes) {
const auto quantizedTime = timePair.first;
const auto originalTime = timePair.second;
const auto it = lyricTrack.find(originalTime);
Q_ASSERT_X(it != lyricTrack.end(),
"MidiLyrics::addLyricsToScore", "Lyric time not found");
QString text = MidiCharset::convertToCharset(it->second);
if (originalTime != ReducedFraction(0, 1) || !isTitlePrefix(text)) { // not title
score->addLyrics(quantizedTime.ticks(), staffAddTo->idx(),
removeSlashes(text).toHtmlEscaped());
}
}
}
void extractLyricsToMidiData(const MidiFile *mf)
{
for (const auto &t: mf->tracks()) {
const auto lyrics = extractLyricsFromTrack(t, mf->division(), mf->isDivisionInTps());
if (!lyrics.empty())
preferences.midiImportOperations.data()->lyricTracks.push_back(lyrics);
}
}
void setInitialLyricsFromMidiData(const QList<MTrack> &tracks)
{
std::set<int> usedTracks;
auto &data = *preferences.midiImportOperations.data();
const auto &lyricTracks = data.lyricTracks;
if (lyricTracks.isEmpty())
return;
for (int i = 0; i != lyricTracks.size(); ++i) {
const BestTrack bestTrack = findBestTrack(tracks, lyricTracks[i], usedTracks);
if (bestTrack.index >= 0) {
usedTracks.insert(bestTrack.index);
addLyricsToScore(lyricTracks[i],
bestTrack.matchedLyricTimes,
tracks[bestTrack.index].staff);
data.trackOpers.lyricTrackIndex.setValue(bestTrack.index, i);
}
}
}
std::vector<std::pair<ReducedFraction, ReducedFraction> > findMatchedLyricTimes(
const std::multimap<ReducedFraction, MidiChord> &chords,
const std::multimap<ReducedFraction, std::string> &lyricTrack)
{
// <chord quantized on time, chord original on time>
std::vector<std::pair<ReducedFraction, ReducedFraction> > matchedLyricTimes;
for (const auto &chord: chords) {
for (const auto ¬e: chord.second.notes) {
if (lyricTrack.find(note.origOnTime) != lyricTrack.end()) {
matchedLyricTimes.push_back({chord.first, note.origOnTime});
break;
}
}
}
return matchedLyricTimes;
}
void setLyricsFromOperations(const QList<MTrack> &tracks)
{
const auto &lyricTracks = preferences.midiImportOperations.data()->lyricTracks;
if (lyricTracks.isEmpty())
return;
for (const auto &track: tracks) {
const auto &opers = preferences.midiImportOperations.data()->trackOpers;
const int lyricTrackIndex = opers.lyricTrackIndex.value(track.indexOfOperation);
if (lyricTrackIndex >= 0 && lyricTrackIndex < lyricTracks.size()) {
const auto &lyricTrack = lyricTracks[lyricTrackIndex];
const auto matchedLyricTimes = findMatchedLyricTimes(track.chords, lyricTrack);
addLyricsToScore(lyricTrack, matchedLyricTimes, track.staff);
}
}
}
void setLyricsToScore(QList<MTrack> &tracks)
{
const auto *data = preferences.midiImportOperations.data();
if (data->processingsOfOpenedFile == 0) {
setInitialLyricsFromMidiData(tracks);
}
else {
setLyricsFromOperations(tracks);
}
}
QList<std::string> makeLyricsListForUI()
{
QList<std::string> list;
const auto &lyrics = preferences.midiImportOperations.data()->lyricTracks;
if (lyrics.isEmpty())
return list;
const unsigned int symbolLimit = 16;
for (const auto &trackLyric: lyrics) {
std::string lyricText;
for (const auto &lyric: trackLyric) {
const auto &text = removeSlashes(lyric.second);
if (isMetaText(text))
continue;
if (!lyricText.empty())
lyricText += " "; // visual text delimeter
if (lyricText.size() + text.size() > symbolLimit)
lyricText += text.substr(0, symbolLimit - lyricText.size());
else
lyricText += text;
if (lyricText.size() >= symbolLimit - 1) {
lyricText += "...";
break;
}
}
list.push_back(lyricText);
}
return list;
}
} // namespace MidiLyrics
} // namespace Ms
|