Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-Studio-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2025 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 :
23 : #include "lrcwriter.h"
24 :
25 : #include <QBuffer>
26 : #include <QRegularExpression>
27 :
28 : #include "global/io/buffer.h"
29 : #include "global/io/file.h"
30 : #include "global/types/ret.h"
31 :
32 : #include "engraving/dom/lyrics.h"
33 : #include "engraving/dom/masterscore.h"
34 : #include "engraving/dom/repeatlist.h"
35 :
36 : #include "notation/inotation.h"
37 : #include "notation/inotationelements.h" // IWYU pragma: keep
38 :
39 : using namespace Qt::Literals;
40 :
41 : using namespace muse;
42 : using namespace muse::io;
43 : using namespace mu::engraving;
44 : using namespace mu::project;
45 : using namespace mu::iex::lrcexport;
46 :
47 0 : std::vector<INotationWriter::UnitType> LRCWriter::supportedUnitTypes() const
48 : {
49 0 : return { UnitType::PER_PART };
50 : }
51 :
52 0 : bool LRCWriter::supportsUnitType(UnitType ut) const { return ut == UnitType::PER_PART; }
53 :
54 0 : muse::Ret LRCWriter::write(notation::INotationPtr notation, muse::io::IODevice& device, const Options&)
55 : {
56 0 : Score* score = notation->elements()->msScore();
57 0 : bool enhancedLrc = configuration()->lrcUseEnhancedFormat();
58 :
59 0 : return doWrite(score, &device, enhancedLrc);
60 : }
61 :
62 12 : bool LRCWriter::writeScore(mu::engraving::Score* score, const muse::io::path_t& path, bool enhancedLrc)
63 : {
64 12 : auto outBuf = Buffer::opened(IODevice::WriteOnly);
65 12 : bool res = doWrite(score, &outBuf, enhancedLrc) && !outBuf.hasError();
66 12 : if (!res) {
67 0 : return res;
68 : }
69 12 : outBuf.close();
70 :
71 12 : return File::writeFile(path, outBuf.data());
72 12 : }
73 :
74 0 : muse::Ret LRCWriter::writeList(const notation::INotationPtrList&, muse::io::IODevice&, const Options&)
75 : {
76 0 : return make_ret(Ret::Code::NotSupported);
77 : }
78 :
79 12 : void LRCWriter::writeMetadata(muse::io::IODevice* device, const engraving::Score* score) const
80 : {
81 12 : QString metadata;
82 :
83 : // Title
84 12 : const QString title = score->metaTag(u"workTitle").toQString();
85 12 : if (!title.isEmpty()) {
86 24 : metadata += u"[ti:%1]\n"_s.arg(title);
87 : }
88 :
89 : // Composer/Artist
90 12 : const QString artist = score->metaTag(u"composer").toQString();
91 12 : if (!artist.isEmpty()) {
92 24 : metadata += u"[ar:%1]\n"_s.arg(artist);
93 : }
94 :
95 12 : if (!metadata.isEmpty()) {
96 12 : device->write(metadata.toUtf8());
97 : }
98 12 : }
99 :
100 12 : muse::Ret LRCWriter::doWrite(mu::engraving::Score* score, muse::io::IODevice* device, bool enhancedLrc)
101 : {
102 12 : writeMetadata(device, score);
103 :
104 12 : const auto lyrics = collectLyrics(score);
105 :
106 : // Write lyrics
107 836 : for (const auto& [timestamp, text] : lyrics) {
108 824 : if (enhancedLrc) {
109 : // As there should only be words we replace spaces by "-"
110 412 : QString lyricsText = text;
111 824 : lyricsText.replace(QRegularExpression("\\s"), "-");
112 412 : lyricsText.replace(u'\u00A0', u'-');
113 :
114 412 : device->write(QString("[%1] <%1> %2\n").arg(formatTimestamp(timestamp), lyricsText).toUtf8());
115 412 : } else {
116 412 : device->write(QString("[%1]%2\n").arg(formatTimestamp(timestamp), text).toUtf8());
117 : }
118 : }
119 :
120 24 : return make_ok();
121 12 : }
122 :
123 12 : std::map<double, QString> LRCWriter::collectLyrics(const mu::engraving::Score* score)
124 : {
125 12 : std::map<double, QString> lyrics;
126 12 : const RepeatList& repeats = score->repeatList();
127 :
128 : track_idx_t trackNumber;
129 : int lyricNumber;
130 :
131 12 : findTrackAndLyricToExport(score, trackNumber, lyricNumber);
132 :
133 72 : for (const RepeatSegment* rs : repeats) {
134 60 : const int tickOffset = rs->utick - rs->tick;
135 :
136 396 : for (const MeasureBase* mb : rs->measureList()) {
137 336 : if (!mb->isMeasure()) {
138 0 : continue;
139 : }
140 :
141 2838 : for (const Segment* seg = toMeasure(mb)->first(); seg; seg = seg->next()) {
142 2502 : if (!seg->isChordRestType()) {
143 1158 : continue;
144 : }
145 :
146 1344 : EngravingItem* e = seg->element(trackNumber);
147 1344 : if (!e || !e->isChordRest()) {
148 212 : continue;
149 : }
150 :
151 1972 : for (const Lyrics* l : toChordRest(e)->lyrics()) {
152 840 : if (l->plainText().isEmpty()) {
153 16 : continue;
154 : }
155 :
156 824 : if (lyricNumber == l->subtype()) {
157 824 : const double time = score->utick2utime(l->tick().ticks() + tickOffset) * 1000;
158 824 : lyrics.insert_or_assign(time, l->plainText());
159 : }
160 : }
161 : }
162 : }
163 : }
164 12 : return lyrics;
165 0 : }
166 :
167 824 : QString LRCWriter::formatTimestamp(double ms) const
168 : {
169 824 : const int totalSec = static_cast<int>(ms / 1000);
170 824 : return u"%1:%2.%3"_s
171 1648 : .arg(totalSec / 60, 2, 10, QLatin1Char('0'))
172 2472 : .arg(totalSec % 60, 2, 10, QLatin1Char('0'))
173 1648 : .arg(static_cast<int>(ms) % 1000 / 10, 2, 10, QLatin1Char('0'));
174 : }
175 :
176 12 : void LRCWriter::findTrackAndLyricToExport(const engraving::Score* score, mu::engraving::track_idx_t& trackNumber, int& lyricNumber)
177 : {
178 12 : bool lyricsFound = false;
179 12 : trackNumber = 0;
180 12 : lyricNumber = 0;
181 :
182 12 : const RepeatList& repeats = score->repeatList();
183 :
184 52 : for (const RepeatSegment* rs : repeats) {
185 276 : for (const MeasureBase* mb : rs->measureList()) {
186 236 : if (!mb->isMeasure()) {
187 0 : continue;
188 : }
189 :
190 1904 : for (const Segment* seg = toMeasure(mb)->first(); seg; seg = seg->next()) {
191 1672 : if (!seg->isChordRestType()) {
192 740 : continue;
193 : }
194 :
195 12068 : for (const EngravingItem* e : seg->elist()) {
196 11140 : if (!e || !e->isChordRest()) {
197 8190 : continue;
198 : }
199 :
200 3734 : for (const Lyrics* l : toChordRest(e)->lyrics()) {
201 : // if (l->text().empty())
202 784 : if (l->plainText().isEmpty()) {
203 12 : continue;
204 : }
205 :
206 772 : if (!lyricsFound) {
207 10 : lyricsFound = true;
208 10 : trackNumber = e->track();
209 10 : lyricNumber = l->subtype();
210 10 : continue;
211 : }
212 :
213 : // We check if we have a better option
214 762 : if (trackNumber > e->track()) {
215 6 : trackNumber = e->track();
216 6 : lyricNumber = l->subtype();
217 756 : } else if (trackNumber == e->track()) {
218 466 : lyricNumber = std::min(lyricNumber, l->subtype());
219 : }
220 : }
221 : // If we have already chosen the lowest/prioritized option we can return (no better option available)
222 2950 : if (lyricsFound && (trackNumber == 0) && (lyricNumber == 0)) {
223 4 : return;
224 : }
225 : }
226 : }
227 : }
228 : }
229 : }
|