fix: lyrics encoding and better lrc support

This commit is contained in:
Gary Wang 2024-09-22 14:27:53 +08:00
parent 2a92f4ea7f
commit b88ee1d0f1
No known key found for this signature in database
GPG Key ID: 5D30A4F15EA78760
5 changed files with 73 additions and 11 deletions

View File

@ -33,6 +33,12 @@ jobs:
:: ------ dep ------ :: ------ dep ------
set CMAKE_PREFIX_PATH=%PWD%/dependencies_bin set CMAKE_PREFIX_PATH=%PWD%/dependencies_bin
mkdir dependencies_src mkdir dependencies_src
:: ===== uchardet =====
echo "::group::build uchardet"
git clone -q https://gitlab.freedesktop.org/BLumia/uchardet.git --branch msvc dependencies_src/uchardet
cmake .\dependencies_src\uchardet -Bbuild_dependencies/uchardet -DBUILD_BINARY=OFF -DCMAKE_INSTALL_PREFIX="dependencies_bin" || goto :error
cmake --build build_dependencies/uchardet --config Release --target=install -j || goto :error
echo "::endgroup::"
:: ===== pkg-config ===== :: ===== pkg-config =====
choco install pkgconfiglite choco install pkgconfiglite
set PKG_CONFIG_PATH=%PWD%/dependencies_bin/lib/pkgconfig set PKG_CONFIG_PATH=%PWD%/dependencies_bin/lib/pkgconfig

View File

@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.12)
project(pineapple-music LANGUAGES CXX) project(pineapple-music LANGUAGES CXX)
include (GNUInstallDirs) include (GNUInstallDirs)
include (FeatureSummary)
set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_INCLUDE_CURRENT_DIR ON)
@ -13,7 +14,8 @@ set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Qt6 6.5.1 COMPONENTS Widgets Multimedia Network LinguistTools REQUIRED) find_package(Qt6 6.6 COMPONENTS Widgets Multimedia Network LinguistTools REQUIRED)
find_package(uchardet)
find_package(PkgConfig) find_package(PkgConfig)
if (PKG_CONFIG_FOUND) if (PKG_CONFIG_FOUND)
@ -74,6 +76,15 @@ else ()
target_link_libraries(${EXE_NAME} PRIVATE PkgConfig::TagLib) target_link_libraries(${EXE_NAME} PRIVATE PkgConfig::TagLib)
endif () endif ()
if (NOT uchardet_FOUND)
message (WARNING "uchardet not found!")
target_compile_definitions(${EXE_NAME} PRIVATE
NO_UCHARDET=1
)
else ()
target_link_libraries (${EXE_NAME} PRIVATE uchardet::libuchardet)
endif ()
target_link_libraries(${EXE_NAME} PRIVATE Qt::Widgets Qt::Multimedia Qt::Network) target_link_libraries(${EXE_NAME} PRIVATE Qt::Widgets Qt::Multimedia Qt::Network)
# Install settings # Install settings
@ -109,3 +120,5 @@ install (
TARGETS ${EXE_NAME} TARGETS ${EXE_NAME}
${INSTALL_TARGETS_DEFAULT_ARGS} ${INSTALL_TARGETS_DEFAULT_ARGS}
) )
feature_summary(WHAT ALL INCLUDE_QUIET_PACKAGES FATAL_ON_MISSING_REQUIRED_PACKAGES)

View File

@ -23,19 +23,24 @@ build_script:
- mkdir 3rdparty - mkdir 3rdparty
- choco install ninja - choco install ninja
- choco install pkgconfiglite - choco install pkgconfiglite
# build taglib
- cd 3rdparty - cd 3rdparty
# build uchardet
- git clone -q https://gitlab.freedesktop.org/uchardet/uchardet.git
- cd uchardet
- cmake -G "Ninja" . -DCMAKE_INSTALL_PREFIX=%PACKAGE_INSTALL_ROOT% -DBUILD_BINARY=OFF
- cmake --build . --target install
- cd %APPVEYOR_BUILD_FOLDER%
# build taglib
- git clone --recurse-submodules -q https://github.com/taglib/taglib.git - git clone --recurse-submodules -q https://github.com/taglib/taglib.git
- cd taglib - cd taglib
- cmake -G "Ninja" . -DCMAKE_INSTALL_PREFIX=%PACKAGE_INSTALL_ROOT% -DBUILD_SHARED_LIBS=ON - cmake -G "Ninja" . -DCMAKE_INSTALL_PREFIX=%PACKAGE_INSTALL_ROOT% -DBUILD_SHARED_LIBS=ON
- cmake --build .
- cmake --build . --target install - cmake --build . --target install
- cd %APPVEYOR_BUILD_FOLDER% - cd %APPVEYOR_BUILD_FOLDER%
- tree %PACKAGE_INSTALL_ROOT% /f - tree %PACKAGE_INSTALL_ROOT% /f
# finally... # finally...
- mkdir build - mkdir build
- cd build - cd build
- cmake .. -G "Ninja" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX='%cd%' - cmake .. -G "Ninja" -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=%CMAKE_INSTALL_ROOT% -DCMAKE_INSTALL_PREFIX='%cd%'
- cmake --build . - cmake --build .
- cmake --build . --target install - cmake --build . --target install
# fixme: I don't know how to NOT make the binary installed to the ./bin/ folder... # fixme: I don't know how to NOT make the binary installed to the ./bin/ folder...

View File

@ -7,6 +7,14 @@
#include <QDir> #include <QDir>
#include <QFileInfo> #include <QFileInfo>
#include <QRegularExpression> #include <QRegularExpression>
#include <QStringConverter>
#ifndef NO_UCHARDET
#include <uchardet/uchardet.h>
#endif
Q_LOGGING_CATEGORY(lcLyrics, "pmusic.lyrics")
Q_LOGGING_CATEGORY(lcLyricsParser, "pmusic.lyrics.parser")
LyricsManager::LyricsManager(QObject *parent) LyricsManager::LyricsManager(QObject *parent)
: QObject(parent) : QObject(parent)
@ -35,8 +43,25 @@ bool LyricsManager::loadLyrics(QString filepath)
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
return false; return false;
} }
QTextStream stream(&file); QByteArray fileContent(file.readAll());
QStringList lines = QString(stream.readAll()).split('\n'); #ifndef NO_UCHARDET
uchardet_t handle = uchardet_new();
uchardet_handle_data(handle, fileContent.data(), fileContent.length());
uchardet_data_end(handle);
const char* encoding = uchardet_get_charset(handle);
qCDebug(lcLyrics) << "Detected encoding:" << (encoding == NULL ? "unknown" : encoding);
QStringList lines;
if (QStringConverter::availableCodecs().contains(QString(encoding))) {
auto toUtf16 = QStringDecoder(encoding);
QString decodedResult = toUtf16(fileContent);
lines = decodedResult.split('\n');
} else {
lines = QString(fileContent).split('\n');
}
uchardet_delete(handle);
#else
QStringList lines = QString(fileContent).split('\n');
#endif
file.close(); file.close();
// parse lyrics timestamp // parse lyrics timestamp
@ -55,19 +80,28 @@ bool LyricsManager::loadLyrics(QString filepath)
if (tag == QLatin1String("offset")) { if (tag == QLatin1String("offset")) {
// The value is prefixed with either + or -, with + causing lyrics to appear sooner // The value is prefixed with either + or -, with + causing lyrics to appear sooner
m_timeOffset = -tagMatch.captured(2).toInt(); m_timeOffset = -tagMatch.captured(2).toInt();
qDebug() << m_timeOffset; qCDebug(lcLyricsParser) << m_timeOffset;
} }
qDebug() << "[tag]" << tagMatch.captured(1) << tagMatch.captured(2); qCDebug(lcLyricsParser) << "[tag]" << tagMatch.captured(1) << tagMatch.captured(2);
continue; continue;
} }
} }
QList<int> timestamps;
QString currentLrc;
QRegularExpressionMatch match = lrcRegex.match(line); QRegularExpressionMatch match = lrcRegex.match(line);
if (match.hasMatch()) { while (match.hasMatch()) {
tagSectionPassed = true; tagSectionPassed = true;
QTime timestamp(QTime::fromString(match.captured(1), "m:s.zz")); QTime timestamp(QTime::fromString(match.captured(1), "m:s.zz"));
m_lyricsMap.insert(timestamp.msecsSinceStartOfDay(), match.captured(2)); timestamps.append(timestamp.msecsSinceStartOfDay());
qDebug() << "[lrc]" << match.captured(1) << match.captured(2); currentLrc = match.captured(2);
match = lrcRegex.match(currentLrc);
}
if (!timestamps.isEmpty()) {
for (int timestamp : std::as_const(timestamps)) {
m_lyricsMap.insert(timestamp, currentLrc);
qCDebug(lcLyricsParser) << "[lrc]" << timestamp << currentLrc;
}
} }
} }
if (!m_lyricsMap.isEmpty()) { if (!m_lyricsMap.isEmpty()) {

View File

@ -6,8 +6,12 @@
#include <QHash> #include <QHash>
#include <QList> #include <QList>
#include <QLoggingCategory>
#include <QObject> #include <QObject>
Q_DECLARE_LOGGING_CATEGORY(lcLyrics)
Q_DECLARE_LOGGING_CATEGORY(lcLyricsParser)
class LyricsManager : public QObject class LyricsManager : public QObject
{ {
Q_OBJECT Q_OBJECT