diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index 3666e5e..a284f67 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -33,6 +33,12 @@ jobs: :: ------ dep ------ set CMAKE_PREFIX_PATH=%PWD%/dependencies_bin 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 ===== choco install pkgconfiglite set PKG_CONFIG_PATH=%PWD%/dependencies_bin/lib/pkgconfig diff --git a/CMakeLists.txt b/CMakeLists.txt index ad5f83e..8e17ebd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.12) project(pineapple-music LANGUAGES CXX) include (GNUInstallDirs) +include (FeatureSummary) set(CMAKE_INCLUDE_CURRENT_DIR ON) @@ -13,7 +14,8 @@ set(CMAKE_AUTORCC ON) set(CMAKE_CXX_STANDARD 17) 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) if (PKG_CONFIG_FOUND) @@ -74,6 +76,15 @@ else () target_link_libraries(${EXE_NAME} PRIVATE PkgConfig::TagLib) 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) # Install settings @@ -109,3 +120,5 @@ install ( TARGETS ${EXE_NAME} ${INSTALL_TARGETS_DEFAULT_ARGS} ) + +feature_summary(WHAT ALL INCLUDE_QUIET_PACKAGES FATAL_ON_MISSING_REQUIRED_PACKAGES) diff --git a/appveyor.yml b/appveyor.yml index 0a67136..c2d0531 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -23,19 +23,24 @@ build_script: - mkdir 3rdparty - choco install ninja - choco install pkgconfiglite -# build taglib - 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 - cd taglib - cmake -G "Ninja" . -DCMAKE_INSTALL_PREFIX=%PACKAGE_INSTALL_ROOT% -DBUILD_SHARED_LIBS=ON - - cmake --build . - cmake --build . --target install - cd %APPVEYOR_BUILD_FOLDER% - tree %PACKAGE_INSTALL_ROOT% /f # finally... - mkdir 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 . --target install # fixme: I don't know how to NOT make the binary installed to the ./bin/ folder... diff --git a/lyricsmanager.cpp b/lyricsmanager.cpp index eb508d3..4b6c83d 100644 --- a/lyricsmanager.cpp +++ b/lyricsmanager.cpp @@ -7,6 +7,14 @@ #include #include #include +#include + +#ifndef NO_UCHARDET +#include +#endif + +Q_LOGGING_CATEGORY(lcLyrics, "pmusic.lyrics") +Q_LOGGING_CATEGORY(lcLyricsParser, "pmusic.lyrics.parser") LyricsManager::LyricsManager(QObject *parent) : QObject(parent) @@ -35,8 +43,25 @@ bool LyricsManager::loadLyrics(QString filepath) if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { return false; } - QTextStream stream(&file); - QStringList lines = QString(stream.readAll()).split('\n'); + QByteArray fileContent(file.readAll()); +#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(); // parse lyrics timestamp @@ -55,19 +80,28 @@ bool LyricsManager::loadLyrics(QString filepath) if (tag == QLatin1String("offset")) { // The value is prefixed with either + or -, with + causing lyrics to appear sooner 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; } } + QList timestamps; + QString currentLrc; QRegularExpressionMatch match = lrcRegex.match(line); - if (match.hasMatch()) { + while (match.hasMatch()) { tagSectionPassed = true; QTime timestamp(QTime::fromString(match.captured(1), "m:s.zz")); - m_lyricsMap.insert(timestamp.msecsSinceStartOfDay(), match.captured(2)); - qDebug() << "[lrc]" << match.captured(1) << match.captured(2); + timestamps.append(timestamp.msecsSinceStartOfDay()); + 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()) { diff --git a/lyricsmanager.h b/lyricsmanager.h index 107cf8b..fc24300 100644 --- a/lyricsmanager.h +++ b/lyricsmanager.h @@ -6,8 +6,12 @@ #include #include +#include #include +Q_DECLARE_LOGGING_CATEGORY(lcLyrics) +Q_DECLARE_LOGGING_CATEGORY(lcLyricsParser) + class LyricsManager : public QObject { Q_OBJECT