diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 3a997e0..a4041cc 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -7,10 +7,10 @@ jobs: build-lib-debian-x86_64: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - uses: actions/download-artifact@v3 + - uses: actions/checkout@v4 + - uses: actions/download-artifact@v4 - name: Docker Setup Buildx - uses: docker/setup-buildx-action@v1.6.0 + uses: docker/setup-buildx-action@v3 with: install: true - name: Setup project dir @@ -21,7 +21,7 @@ jobs: docker build --build-arg="RUN_TESTS=true" -f dockerfile . -t libfvadjni_binary:x86_64 --load docker run --platform=amd64 -v $(pwd)/dist:/out libfvadjni_binary:x86_64 bash -c "cp src/main/resources/debian-amd64/*.so /out/" - name: Artifact debian x86_64 - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: debian-binary-amd64 path: dist/*.so @@ -29,12 +29,12 @@ jobs: build-lib-debian-arm64: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - uses: actions/download-artifact@v3 + - uses: actions/checkout@v4 + - uses: actions/download-artifact@v4 - name: Set up QEMU - uses: docker/setup-qemu-action@v1 + uses: docker/setup-qemu-action@v3 - name: Docker Setup Buildx - uses: docker/setup-buildx-action@v1.6.0 + uses: docker/setup-buildx-action@v3 with: install: true - name: Setup project dir @@ -45,7 +45,7 @@ jobs: docker build -f dockerfile . -t libfvadjni_binary:arm64 --platform arm64 --load DOCKER_BUILDKIT=1 docker run --platform=arm64 -v $(pwd)/dist:/out libfvadjni_binary:arm64 bash -c "cp src/main/resources/debian-arm64/*.so /out/" - name: Artifact debian arm64 - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: debian-binary-arm64 path: dist/*.so @@ -53,12 +53,12 @@ jobs: build-lib-debian-arm: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - uses: actions/download-artifact@v3 + - uses: actions/checkout@v4 + - uses: actions/download-artifact@v4 - name: Set up QEMU - uses: docker/setup-qemu-action@v1 + uses: docker/setup-qemu-action@v3 - name: Docker Setup Buildx - uses: docker/setup-buildx-action@v1.6.0 + uses: docker/setup-buildx-action@v3 with: install: true - name: Setup project dir @@ -69,7 +69,7 @@ jobs: docker build -f dockerfile . -t libfvadjni_binary:armv7l --platform arm --load DOCKER_BUILDKIT=1 docker run --platform=arm -v $(pwd)/dist:/out libfvadjni_binary:armv7l bash -c "cp src/main/resources/debian-armv7l/*.so /out/" - name: Artifact debian armv7l - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: debian-binary-armv7l path: dist/*.so @@ -77,9 +77,9 @@ jobs: build-lib-windows: runs-on: windows-latest steps: - - uses: actions/checkout@v3 - - uses: actions/download-artifact@v3 - - uses: actions/setup-java@v3 + - uses: actions/checkout@v4 + - uses: actions/download-artifact@v4 + - uses: actions/setup-java@v4 with: distribution: 'zulu' java-version: '17' @@ -92,60 +92,73 @@ jobs: shell: powershell run: | .\build_win.ps1 - mv src\main\resources\win-amd64\libfvadjni.dll .\dist\ + mv src\main\resources\win-amd64\libfvad-jni.dll .\dist\ - name: Artifact windows x86_64 - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: windows-binary-x86_64 - path: dist/libfvadjni.dll + path: dist/*.dll - build-lib-macos: - runs-on: macos-latest + build-lib-macos-amd64: + runs-on: macos-13 steps: - - uses: actions/checkout@v3 - - uses: actions/download-artifact@v3 - - uses: actions/setup-java@v3 + - uses: actions/checkout@v4 + - uses: actions/download-artifact@v4 + - uses: actions/setup-java@v4 with: distribution: 'zulu' java-version: '17' - # - name: Set up Maven - # uses: stCarolas/setup-maven@v4.5 - # with: - # maven-version: 3.9.2 - name: Setup project dir run: | export DYLD_LIBRARY_PATH=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/ mkdir -p dist - mkdir -p dist_arm64 git submodule update --init - name: Build binary run: | export DYLD_LIBRARY_PATH=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/ ./build_macos.sh amd64 - ./build_macos.sh arm64 - # mvn test - mv src/main/resources/macos-amd64/libfvadjni.dylib dist/ - mv src/main/resources/macos-arm64/libfvadjni.dylib dist_arm64/ + mv src/main/resources/macos-amd64/*.dylib dist/ - name: Artifact macos amd64 - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: macos-binary-amd64 - path: dist/libfvadjni.dylib + path: dist/*.dylib + + + build-lib-macos-arm64: + runs-on: macos-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/download-artifact@v4 + - uses: actions/setup-java@v4 + with: + distribution: 'zulu' + java-version: '17' + - name: Setup project dir + run: | + export DYLD_LIBRARY_PATH=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/ + mkdir -p dist_arm64 + git submodule update --init + - name: Build binary + run: | + export DYLD_LIBRARY_PATH=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/ + ./build_macos.sh arm64 + mv src/main/resources/macos-arm64/*.dylib dist_arm64/ - name: Artifact macos arm64 - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: macos-binary-arm64 - path: dist_arm64/libfvadjni.dylib + path: dist_arm64/*.dylib deploy: if: startsWith(github.ref, 'refs/tags/') - needs: [build-lib-debian-x86_64,build-lib-debian-arm,build-lib-debian-arm64,build-lib-windows,build-lib-macos] + needs: [build-lib-debian-x86_64,build-lib-debian-arm,build-lib-debian-arm64,build-lib-windows,build-lib-macos-amd64,build-lib-macos-arm64] runs-on: ubuntu-latest permissions: contents: write steps: - uses: actions/checkout@v2 - - uses: actions/download-artifact@v3 + - uses: actions/download-artifact@v4 - name: check tree run: ls -R - name: setup resources @@ -153,16 +166,17 @@ jobs: mv ./debian-binary-amd64/*.so ./src/main/resources/debian-amd64/ mv ./debian-binary-arm64/*.so ./src/main/resources/debian-arm64/ mv ./debian-binary-armv7l/*.so ./src/main/resources/debian-armv7l/ - mv ./windows-binary-x86_64/libfvadjni.dll ./src/main/resources/win-amd64/libfvadjni.dll - mv ./macos-binary-amd64/libfvadjni.dylib ./src/main/resources/macos-amd64/libfvadjni.dylib - mv ./macos-binary-arm64/libfvadjni.dylib ./src/main/resources/macos-arm64/libfvadjni.dylib + mv ./windows-binary-x86_64/*.dll ./src/main/resources/win-amd64/ + mv ./macos-binary-amd64/*.dylib ./src/main/resources/macos-amd64/ + mv ./macos-binary-arm64/*.dylib ./src/main/resources/macos-arm64/ - name: Set up Maven Central Repository - uses: actions/setup-java@v1 + uses: actions/setup-java@v4 with: java-version: 17 + distribution: 'zulu' server-id: sonatype-nexus-staging server-username: MAVEN_USERNAME - server-password: MAVEN_PASSWORD + server-password: MAVEN_CENTRAL_TOKEN gpg-private-key: ${{ secrets.MAVEN_GPG_PRIVATE_KEY }} gpg-passphrase: MAVEN_GPG_PASSPHRASE - name: Deploy with Maven @@ -175,7 +189,7 @@ jobs: echo "LIBFVAD_VERSION=$LIBFVAD_VERSION" >> $GITHUB_OUTPUT env: MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }} - MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }} + MAVEN_CENTRAL_TOKEN: ${{ secrets.OSSRH_TOKEN }} MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }} - name: Release uses: softprops/action-gh-release@v1 diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..64aa432 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,101 @@ + cmake_minimum_required(VERSION 3.5) + +project(libfvad-jni C CXX) + +# ---- Set options ---- + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +set(JAVA_AWT_LIBRARY NotNeeded) +set(JAVA_JVM_LIBRARY NotNeeded) +find_package(JNI REQUIRED) +if (JNI_FOUND) + message (STATUS "JNI_INCLUDE_DIRS=${JNI_INCLUDE_DIRS}") +endif() + +if(MSVC) + # Force compiler to use UTF-8 for IPA constants + add_compile_options("$<$:/utf-8>") + add_compile_options("$<$:/utf-8>") +elseif(NOT APPLE) + string(APPEND CMAKE_CXX_FLAGS " -Wall -Wextra -Wl,-rpath,'$ORIGIN'") + string(APPEND CMAKE_C_FLAGS " -Wall -Wextra") + set(CMAKE_INSTALL_RPATH '$ORIGIN') +elseif(APPLE) + set(CMAKE_INSTALL_RPATH "@loader_path") +endif() + +# ---- Declare libraries ---- + +add_library(libfvad-jni SHARED src/main/native/io_github_givimad_libfvadjni_VoiceActivityDetector.cpp) + +if(NOT DEFINED LIBFVAD_DIR) +set(LIBFVAD_DIR "${CMAKE_CURRENT_BINARY_DIR}/libfvad.s") +set(LIBFVAD_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/libfvad") +include(ExternalProject) + ExternalProject_Add( + libfvad-external + PREFIX "${LIBFVAD_INSTALL_DIR}" + URL "${CMAKE_CURRENT_LIST_DIR}/src/main/native/libfvad" + CMAKE_ARGS -DCMAKE_INSTALL_RPATH:STRING=${CMAKE_INSTALL_RPATH} -DCMAKE_INSTALL_PREFIX:PATH=${LIBFVAD_DIR} -DCMAKE_OSX_ARCHITECTURES:STRING=${CMAKE_OSX_ARCHITECTURES} -DCMAKE_OSX_DEPLOYMENT_TARGET:STRING=${CMAKE_OSX_DEPLOYMENT_TARGET} + ) + add_dependencies(libfvad-jni libfvad-external) +endif() + +# ---- Declare executable ---- + +if((NOT MSVC) AND (NOT APPLE)) + # Linux flags + string(APPEND CMAKE_CXX_FLAGS " -Wall -Wextra -Wl,-rpath,'$ORIGIN'") + string(APPEND CMAKE_C_FLAGS " -Wall -Wextra") + # target_link_libraries(libfvad-jni -static-libgcc -static-libstdc++) +endif() + +if(MSVC) +target_link_libraries(libfvad-jni + fvad +) +else() +target_link_libraries(libfvad-jni + libfvad.a +) +endif() +target_link_directories(libfvad-jni PUBLIC + ${LIBFVAD_DIR}/lib + ${LIBFVAD_DIR}/lib/static +) + +target_include_directories(libfvad-jni PUBLIC + ${JNI_INCLUDE_DIRS} + src/main/native + ${LIBFVAD_DIR}/include +) + +# ---- Declare install targets ---- + +install( + TARGETS libfvad-jni + DESTINATION ${CMAKE_INSTALL_PREFIX} +) + +# Dependencies + +install( + DIRECTORY ${LIBFVAD_DIR}/lib/ + DESTINATION ${CMAKE_INSTALL_PREFIX} + USE_SOURCE_PERMISSIONS + FILES_MATCHING + PATTERN "*.so.*" + PATTERN "*.so" + PATTERN "*.dylib" +) + +install( + DIRECTORY ${LIBFVAD_DIR}/bin/ + DESTINATION ${CMAKE_INSTALL_PREFIX} + USE_SOURCE_PERMISSIONS + FILES_MATCHING + PATTERN "*.dll" +) + diff --git a/build_debian.sh b/build_debian.sh index cb019d6..16f40ae 100755 --- a/build_debian.sh +++ b/build_debian.sh @@ -1,15 +1,11 @@ #!/bin/bash set -xe build_lib() { - (cd src/main/native/libfvad && cmake . && cmake --build .) - #exit 0 - g++ -c -std=c++11 -O3 -DNDEBUG -fPIC -pthread -I${JAVA_HOME}/include -I${JAVA_HOME}/include/linux -I src/main/native/libfvad/include $CXXFLAGS \ - src/main/native/io_github_givimad_libfvadjni_VoiceActivityDetector.cpp -o src/main/native/io_github_givimad_libfvadjni_VoiceActivityDetector.o - - g++ -shared -fPIC -pthread $CXXFLAGS -I src/main/native -I src/main/native/libfvad/include -o src/main/resources/debian-$AARCH/libfvadjni.so src/main/native/libfvad/src/CMakeFiles/fvad.dir/*.o src/main/native/libfvad/src/CMakeFiles/fvad.dir/**/*.o src/main/native/libfvad/src/libfvad.a src/main/native/io_github_givimad_libfvadjni_VoiceActivityDetector.o -lc $LDFLAGS - - rm -rf src/main/native/*.o - (cd src/main/native/libfvad && git clean -d -f -x) + TARGET_DIR=src/main/resources/debian-$AARCH + cmake -B build $CMAKE_ARGS -DCMAKE_C_FLAGS="$CMAKE_CFLAGS" -DCMAKE_INSTALL_PREFIX=$TARGET_DIR + cmake --build build --config Release + cmake --install build + rm -rf build } AARCH=$(dpkg --print-architecture) case $AARCH in diff --git a/build_macos.sh b/build_macos.sh index d5f7c73..1833954 100755 --- a/build_macos.sh +++ b/build_macos.sh @@ -27,13 +27,9 @@ esac INCLUDE_JAVA="-I $JAVA_HOME/include -I $JAVA_HOME/include/darwin" TARGET=$AARCH-apple-macosx$TARGET_VERSION -(cd src/main/native/libfvad && cmake -DCMAKE_OSX_DEPLOYMENT_TARGET=$TARGET_VERSION -DCMAKE_OSX_ARCHITECTURES=$AARCH . && cmake --build .) +TARGET_DIR=src/main/resources/macos-$AARCH_NAME -g++ -c -std=c++11 -arch "$AARCH" -O3 -DNDEBUG -fPIC $INCLUDE_JAVA -I src/main/native/libfvad/include $CXXFLAGS \ -$CXXFLAGS --target="$TARGET" \ -src/main/native/io_github_givimad_libfvadjni_VoiceActivityDetector.cpp -o src/main/native/io_github_givimad_libfvadjni_VoiceActivityDetector.o - -g++ -arch "$AARCH" --target="$TARGET" -dynamiclib -I src/main/native -o src/main/resources/macos-$AARCH_NAME/libfvadjni.dylib src/main/native/libfvad/src/libfvad.a src/main/native/io_github_givimad_libfvadjni_VoiceActivityDetector.o -lc $LDFLAGS - -rm -rf src/main/native/*.o -(cd src/main/native/libfvad && git clean -d -f -x) +cmake -Bbuild -DCMAKE_INSTALL_PREFIX=$TARGET_DIR -DCMAKE_OSX_DEPLOYMENT_TARGET=$TARGET_VERSION -DCMAKE_OSX_ARCHITECTURES=$AARCH +cmake --build build --config Release +cmake --install build +rm -rf build \ No newline at end of file diff --git a/build_win.ps1 b/build_win.ps1 index a042616..d4348be 100755 --- a/build_win.ps1 +++ b/build_win.ps1 @@ -1,21 +1,5 @@ Set-PSDebug -Trace 1 - -cd src\main\native\libfvad -cmake . -G "MinGW Makefiles" -cmake --build . -cd ..\..\..\..\ - -g++ -c -std=c++11 -O3 -DNDEBUG -fPIC -I $env:JAVA_HOME\include -I $env:JAVA_HOME\include\win32 -I src\main\native\libfvad\include -I src\main\native src\main\native\io_github_givimad_libfvadjni_VoiceActivityDetector.cpp -o src\main\native\io_github_givimad_libfvadjni_VoiceActivityDetector.o - -g++ -shared -static -I src\main\native\libfvad\include -I src\main\native -o src\main\resources\win-amd64\libfvadjni.dll src\main\native\libfvad\src\CMakeFiles\fvad.dir\*.c.obj src\main\native\libfvad\src\CMakeFiles\fvad.dir\vad\*.c.obj src\main\native\libfvad\src\CMakeFiles\fvad.dir\signal_processing\*.c.obj src\main\native\libfvad\src\libfvad.a src\main\native\io_github_givimad_libfvadjni_VoiceActivityDetector.o - -if ($LastExitCode -ne 0) { - Write-Error "Unable to build library" - Exit 1 -} - -rm -fo src\main\native\*.o -cd src\main\native\libfvad -git clean -d -f -x -cd ..\..\..\..\ -Set-PSDebug -Trace 0 \ No newline at end of file +cmake -B build -DCMAKE_INSTALL_PREFIX=src/main/resources/win-amd64 +cmake --build build --config Release +cmake --install build +rm -r -fo build \ No newline at end of file diff --git a/pom.xml b/pom.xml index f563e7f..432bc6e 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ libfvad-jni libfvad-jni https://github.com/GiviMAD/libfvad-jni - 1.0.0-0 + 1.0.0-1 A JNI wrapper for using libfvad diff --git a/src/main/java/io/github/givimad/libfvadjni/VoiceActivityDetector.java b/src/main/java/io/github/givimad/libfvadjni/VoiceActivityDetector.java index 3f70b93..62f200e 100644 --- a/src/main/java/io/github/givimad/libfvadjni/VoiceActivityDetector.java +++ b/src/main/java/io/github/givimad/libfvadjni/VoiceActivityDetector.java @@ -113,11 +113,11 @@ public enum SampleRate { /** 8000 samples per seconds */ S8000(8000), /** 8000 samples per seconds */ - S16000(16000), + S16000(16000), /** 8000 samples per seconds */ - S32000(32000), + S32000(32000), /** 8000 samples per seconds */ - S48000(48000); + S48000(48000); SampleRate(final int value) { this.value = value; @@ -196,21 +196,21 @@ public static void loadLibrary() throws IOException { String osArch = System.getProperty("os.arch").toLowerCase(); if (osName.contains("win")) { if (osArch.contains("amd64") || osArch.contains("x86_64")) { - bundleLibraryPath = "/win-amd64/libfvadjni.dll"; + bundleLibraryPath = "/win-amd64/libfvad-jni.dll"; } } else if (osName.contains("nix") || osName.contains("nux") || osName.contains("aix")) { if (osArch.contains("amd64") || osArch.contains("x86_64")) { - bundleLibraryPath = "/debian-amd64/libfvadjni.so"; + bundleLibraryPath = "/debian-amd64/liblibfvad-jni.so"; } else if (osArch.contains("aarch64") || osArch.contains("arm64")) { - bundleLibraryPath = "/debian-arm64/libfvadjni.so"; + bundleLibraryPath = "/debian-arm64/liblibfvad-jni.so"; } else if (osArch.contains("armv7") || osArch.contains("arm")) { - bundleLibraryPath = "/debian-armv7l/libfvadjni.so"; + bundleLibraryPath = "/debian-armv7l/liblibfvad-jni.so"; } } else if (osName.contains("mac") || osName.contains("darwin")) { if (osArch.contains("amd64") || osArch.contains("x86_64")) { - bundleLibraryPath = "/macos-amd64/libfvadjni.dylib"; + bundleLibraryPath = "/macos-amd64/liblibfvad-jni.dylib"; } else if (osArch.contains("aarch64") || osArch.contains("arm64")) { - bundleLibraryPath = "/macos-arm64/libfvadjni.dylib"; + bundleLibraryPath = "/macos-arm64/liblibfvad-jni.dylib"; } } if (bundleLibraryPath == null) {