|
| 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) 2021 MuseScore Limited |
| 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 "iex_lyricsexport.h" |
| 24 | + |
| 25 | +#include <QBuffer> |
| 26 | + |
| 27 | +#include "engraving/dom/masterscore.h" |
| 28 | +#include "engraving/dom/repeatlist.h" |
| 29 | +#include "engraving/dom/lyrics.h" |
| 30 | + |
| 31 | +using namespace mu::engraving; |
| 32 | + |
| 33 | +namespace mu::iex::lrcexport { |
| 34 | +// Interface implementation |
| 35 | +std::vector<project::INotationWriter::UnitType> LRCWriter::supportedUnitTypes() const |
| 36 | +{ |
| 37 | + return { UnitType::PER_PART }; |
| 38 | +} |
| 39 | + |
| 40 | +bool LRCWriter::supportsUnitType(UnitType ut) const { return ut == UnitType::PER_PART; } |
| 41 | + |
| 42 | +muse::Ret LRCWriter::write(notation::INotationPtr notation, muse::io::IODevice& device, const Options&) |
| 43 | +{ |
| 44 | + Score* score = notation->elements()->msScore(); |
| 45 | + QByteArray data; |
| 46 | + QBuffer buffer(&data); |
| 47 | + |
| 48 | + /*********** |
| 49 | + * |
| 50 | + * PENDING.... |
| 51 | + Is there any advantage to writing to a buffer first, and then writing that buffer to the device? It would seem more efficient to me to write to the device directly. |
| 52 | +
|
| 53 | + ******/ |
| 54 | + |
| 55 | + buffer.open(QIODevice::WriteOnly); |
| 56 | + |
| 57 | + writeMetadata(buffer, score); |
| 58 | + |
| 59 | + const auto lyrics = collectLyrics(score); |
| 60 | + |
| 61 | + // Write lyrics |
| 62 | + for (auto it = lyrics.constBegin(); it != lyrics.constEnd(); ++it) { |
| 63 | + buffer.write(QString("[%1]%2\n").arg(formatTimestamp(it.key()), it.value()).toUtf8()); |
| 64 | + } |
| 65 | + |
| 66 | + device.write(data); |
| 67 | + return muse::Ret(muse::Ret::Code::Ok); |
| 68 | +} |
| 69 | + |
| 70 | +muse::Ret LRCWriter::writeList(const notation::INotationPtrList&, muse::io::IODevice&, const Options&) |
| 71 | +{ |
| 72 | + return muse::Ret(muse::Ret::Code::NotSupported); |
| 73 | +} |
| 74 | + |
| 75 | +void LRCWriter::writeMetadata(QIODevice& device, const engraving::Score* score) const |
| 76 | +{ |
| 77 | + QString metadata; |
| 78 | + |
| 79 | + // Title |
| 80 | + QString title = QString::fromStdString(score->metaTag(muse::String("workTitle")).toStdString()); |
| 81 | + if (!title.isEmpty()) { |
| 82 | + metadata += QString("[ti:%1]\n").arg(title); |
| 83 | + } |
| 84 | + |
| 85 | + // Composer/Artist |
| 86 | + QString artist = QString::fromStdString(score->metaTag(muse::String("composer")).toStdString()); |
| 87 | + if (!artist.isEmpty()) { |
| 88 | + metadata += QString("[ar:%1]\n").arg(artist); |
| 89 | + } |
| 90 | + |
| 91 | + if (!metadata.isEmpty()) { |
| 92 | + device.write(metadata.toUtf8()); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +// Core lyric collection (simplified) |
| 97 | +QMap<qreal, QString> LRCWriter::collectLyrics(const Score* score) const |
| 98 | +{ |
| 99 | + QMap<qreal, QString> lyrics; |
| 100 | + const RepeatList& repeats = score->repeatList(); |
| 101 | + |
| 102 | + for (const RepeatSegment* rs : repeats) { |
| 103 | + const int tickOffset = rs->utick - rs->tick; |
| 104 | + |
| 105 | + for (const MeasureBase* mb = rs->firstMeasure(); mb; mb = mb->next()) { |
| 106 | + if (!mb->isMeasure()) { |
| 107 | + continue; |
| 108 | + } |
| 109 | + |
| 110 | + for (Segment* seg = toMeasure(mb)->first(); seg; seg = seg->next()) { |
| 111 | + if (!seg->isChordRestType()) { |
| 112 | + continue; |
| 113 | + } |
| 114 | + |
| 115 | + for (EngravingItem* e : seg->elist()) { |
| 116 | + if (!e || !e->isChordRest()) { |
| 117 | + continue; |
| 118 | + } |
| 119 | + |
| 120 | + for (Lyrics* l : toChordRest(e)->lyrics()) { |
| 121 | + // if (l->text().empty()) |
| 122 | + if (l->plainText().isEmpty()) { |
| 123 | + continue; |
| 124 | + } |
| 125 | + |
| 126 | + const qreal time = score->utick2utime(l->tick().ticks() + tickOffset) * 1000; |
| 127 | + lyrics.insert(time, l->plainText()); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + return lyrics; |
| 134 | +} |
| 135 | + |
| 136 | +QString LRCWriter::formatTimestamp(qreal ms) const |
| 137 | +{ |
| 138 | + const int totalSec = static_cast<int>(ms / 1000); |
| 139 | + return QString("%1:%2.%3") |
| 140 | + .arg(totalSec / 60, 2, 10, QLatin1Char('0')) |
| 141 | + .arg(totalSec % 60, 2, 10, QLatin1Char('0')) |
| 142 | + .arg(static_cast<int>(ms) % 1000 / 10, 2, 10, QLatin1Char('0')); |
| 143 | +} |
| 144 | +} // namespace mu::iex::lrcexport |
0 commit comments