diff --git a/runtime/core/cmake/libtorch.cmake b/runtime/core/cmake/libtorch.cmake index 40a64ff84..e6716a68a 100644 --- a/runtime/core/cmake/libtorch.cmake +++ b/runtime/core/cmake/libtorch.cmake @@ -5,6 +5,9 @@ if(TORCH) add_definitions(-DUSE_GPU) set(CUDA_NAME "cu113") endif() + if(IOS) + add_definitions(-DIOS) + endif() if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows") if(GPU) message(FATAL_ERROR "GPU on Windows is unsupported, you can use CPU version") @@ -40,17 +43,24 @@ if(TORCH) endif() set(LIBTORCH_URL "https://download.pytorch.org/libtorch/cpu/libtorch-macos-${PYTORCH_VERSION}.zip") set(URL_HASH "SHA256=07cac2c36c34f13065cb9559ad5270109ecbb468252fb0aeccfd89322322a2b5") + elseif(${CMAKE_SYSTEM_NAME} STREQUAL "iOS") + if(GPU) + message(FATAL_ERROR "GPU on iOS is unsupported, you can use CPU version") + endif() else() message(FATAL_ERROR "Unsupported CMake System Name '${CMAKE_SYSTEM_NAME}' (expected 'Windows', 'Linux' or 'Darwin')") endif() - FetchContent_Declare(libtorch - URL ${LIBTORCH_URL} - URL_HASH ${URL_HASH} - ) - FetchContent_MakeAvailable(libtorch) - find_package(Torch REQUIRED PATHS ${libtorch_SOURCE_DIR} NO_DEFAULT_PATH) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS} -DC10_USE_GLOG") + # iOS use LibTorch from pod install + if(NOT IOS) + FetchContent_Declare(libtorch + URL ${LIBTORCH_URL} + URL_HASH ${URL_HASH} + ) + FetchContent_MakeAvailable(libtorch) + find_package(Torch REQUIRED PATHS ${libtorch_SOURCE_DIR} NO_DEFAULT_PATH) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS} -DC10_USE_GLOG") + endif() if(MSVC) file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll") diff --git a/runtime/core/decoder/CMakeLists.txt b/runtime/core/decoder/CMakeLists.txt index 098fdcdb5..49503a220 100644 --- a/runtime/core/decoder/CMakeLists.txt +++ b/runtime/core/decoder/CMakeLists.txt @@ -7,10 +7,10 @@ set(decoder_srcs ctc_endpoint.cc ) -if(NOT TORCH AND NOT ONNX AND NOT XPU) - message(FATAL_ERROR "Please build with TORCH or ONNX or XPU!!!") +if(NOT TORCH AND NOT ONNX AND NOT XPU AND NOT IOS) + message(FATAL_ERROR "Please build with TORCH or ONNX or XPU or IOS!!!") endif() -if(TORCH) +if(TORCH OR IOS) list(APPEND decoder_srcs torch_asr_model.cc) endif() if(ONNX) diff --git a/runtime/core/decoder/torch_asr_model.cc b/runtime/core/decoder/torch_asr_model.cc index 4775761ac..f41c5cdaf 100644 --- a/runtime/core/decoder/torch_asr_model.cc +++ b/runtime/core/decoder/torch_asr_model.cc @@ -22,10 +22,13 @@ #include #include "torch/script.h" +#ifndef IOS #include "torch/torch.h" +#endif namespace wenet { +#ifndef IOS void TorchAsrModel::InitEngineThreads(int num_threads) { // For multi-thread performance at::set_num_threads(num_threads); @@ -36,6 +39,7 @@ void TorchAsrModel::InitEngineThreads(int num_threads) { VLOG(1) << "Num intra-op threads: " << at::get_num_threads(); VLOG(1) << "Num inter-op threads: " << at::get_num_interop_threads(); } +#endif void TorchAsrModel::Read(const std::string& model_path) { torch::DeviceType device = at::kCPU; diff --git a/runtime/core/decoder/torch_asr_model.h b/runtime/core/decoder/torch_asr_model.h index a29cc998a..9c6e79a18 100644 --- a/runtime/core/decoder/torch_asr_model.h +++ b/runtime/core/decoder/torch_asr_model.h @@ -22,7 +22,9 @@ #include #include "torch/script.h" +#ifndef IOS #include "torch/torch.h" +#endif #include "decoder/asr_model.h" #include "utils/utils.h" @@ -31,8 +33,10 @@ namespace wenet { class TorchAsrModel : public AsrModel { public: +#ifndef IOS // Note: Do not call the InitEngineThreads function more than once. static void InitEngineThreads(int num_threads = 1); +#endif public: using TorchModule = torch::jit::script::Module; diff --git a/runtime/core/patch/openfst/src/bin/CMakeLists.txt b/runtime/core/patch/openfst/src/bin/CMakeLists.txt new file mode 100644 index 000000000..e69de29bb diff --git a/runtime/core/toolchains/ios.toolchain.cmake b/runtime/core/toolchains/ios.toolchain.cmake new file mode 100644 index 000000000..2bcb0adf7 --- /dev/null +++ b/runtime/core/toolchains/ios.toolchain.cmake @@ -0,0 +1,1014 @@ +# This file is part of the ios-cmake project. It was retrieved from +# https://github.com/leetal/ios-cmake.git, which is a fork of +# https://github.com/gerstrong/ios-cmake.git, which is a fork of +# https://github.com/cristeab/ios-cmake.git, which is a fork of +# https://code.google.com/p/ios-cmake/. Which in turn is based off of +# the Platform/Darwin.cmake and Platform/UnixPaths.cmake files which +# are included with CMake 2.8.4 +# +# The ios-cmake project is licensed under the new BSD license. +# +# Copyright (c) 2014, Bogdan Cristea and LTE Engineering Software, +# Kitware, Inc., Insight Software Consortium. All rights reserved. +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its +# contributors may be used to endorse or promote products derived from +# this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +# This file is based off of the Platform/Darwin.cmake and +# Platform/UnixPaths.cmake files which are included with CMake 2.8.4 +# It has been altered for iOS development. +# +# Updated by Alex Stewart (alexs.mac@gmail.com) +# +# ***************************************************************************** +# Now maintained by Alexander Widerberg (widerbergaren [at] gmail.com) +# under the BSD-3-Clause license +# https://github.com/leetal/ios-cmake +# ***************************************************************************** +# +# INFORMATION / HELP +# +############################################################################### +# OPTIONS # +############################################################################### +# +# PLATFORM: (default "OS64") +# OS = Build for iPhoneOS. +# OS64 = Build for arm64 iphoneOS. +# OS64COMBINED = Build for arm64 x86_64 iphoneOS + iphoneOS Simulator. Combined into FAT STATIC lib (only supported on 3.14+ of CMake with "-G Xcode" argument in combination with the "cmake --install" CMake build step) +# SIMULATOR = Build for x86 i386 iphoneOS Simulator. +# SIMULATOR64 = Build for x86_64 iphoneOS Simulator. +# SIMULATORARM64 = Build for arm64 iphoneOS Simulator. +# TVOS = Build for arm64 tvOS. +# TVOSCOMBINED = Build for arm64 x86_64 tvOS + tvOS Simulator. Combined into FAT STATIC lib (only supported on 3.14+ of CMake with "-G Xcode" argument in combination with the "cmake --install" CMake build step) +# SIMULATOR_TVOS = Build for x86_64 tvOS Simulator. +# WATCHOS = Build for armv7k arm64_32 for watchOS. +# WATCHOSCOMBINED = Build for armv7k arm64_32 x86_64 watchOS + watchOS Simulator. Combined into FAT STATIC lib (only supported on 3.14+ of CMake with "-G Xcode" argument in combination with the "cmake --install" CMake build step) +# SIMULATOR_WATCHOS = Build for x86_64 for watchOS Simulator. +# MAC = Build for x86_64 macOS. +# MAC_ARM64 = Build for Apple Silicon macOS. +# MAC_CATALYST = Build for x86_64 macOS with Catalyst support (iOS toolchain on macOS). +# Note: The build argument "MACOSX_DEPLOYMENT_TARGET" can be used to control min-version of macOS +# MAC_CATALYST_ARM64 = Build for Apple Silicon macOS with Catalyst support (iOS toolchain on macOS). +# Note: The build argument "MACOSX_DEPLOYMENT_TARGET" can be used to control min-version of macOS +# +# CMAKE_OSX_SYSROOT: Path to the SDK to use. By default this is +# automatically determined from PLATFORM and xcodebuild, but +# can also be manually specified (although this should not be required). +# +# CMAKE_DEVELOPER_ROOT: Path to the Developer directory for the platform +# being compiled for. By default this is automatically determined from +# CMAKE_OSX_SYSROOT, but can also be manually specified (although this should +# not be required). +# +# DEPLOYMENT_TARGET: Minimum SDK version to target. Default 2.0 on watchOS and 9.0 on tvOS+iOS +# +# NAMED_LANGUAGE_SUPPORT: +# ON (default) = Will require "enable_language(OBJC) and/or enable_language(OBJCXX)" for full OBJC|OBJCXX support +# OFF = Will embed the OBJC and OBJCXX flags into the CMAKE_C_FLAGS and CMAKE_CXX_FLAGS (legacy behaviour, CMake version < 3.16) +# +# ENABLE_BITCODE: (ON|OFF) Enables or disables bitcode support. Default ON +# +# ENABLE_ARC: (ON|OFF) Enables or disables ARC support. Default ON (ARC enabled by default) +# +# ENABLE_VISIBILITY: (ON|OFF) Enables or disables symbol visibility support. Default OFF (visibility hidden by default) +# +# ENABLE_STRICT_TRY_COMPILE: (ON|OFF) Enables or disables strict try_compile() on all Check* directives (will run linker +# to actually check if linking is possible). Default OFF (will set CMAKE_TRY_COMPILE_TARGET_TYPE to STATIC_LIBRARY) +# +# ARCHS: (armv7 armv7s armv7k arm64 arm64_32 i386 x86_64) If specified, will override the default architectures for the given PLATFORM +# OS = armv7 armv7s arm64 (if applicable) +# OS64 = arm64 (if applicable) +# SIMULATOR = i386 +# SIMULATOR64 = x86_64 +# SIMULATORARM64 = arm64 +# TVOS = arm64 +# SIMULATOR_TVOS = x86_64 (i386 has since long been deprecated) +# WATCHOS = armv7k arm64_32 (if applicable) +# SIMULATOR_WATCHOS = x86_64 (i386 has since long been deprecated) +# MAC = x86_64 +# MAC_ARM64 = arm64 +# MAC_CATALYST = x86_64 +# MAC_CATALYST_ARM64 = arm64 +# +# NOTE: When manually specifying ARCHS, put a semi-colon between the entries. E.g., -DARCHS="armv7;arm64" +# +############################################################################### +# END OPTIONS # +############################################################################### +# +# This toolchain defines the following properties (available via get_property()) for use externally: +# +# PLATFORM: The currently targeted platform. +# XCODE_VERSION: Version number (not including Build version) of Xcode detected. +# SDK_VERSION: Version of SDK being used. +# OSX_ARCHITECTURES: Architectures being compiled for (generated from PLATFORM). +# APPLE_TARGET_TRIPLE: Used by autoconf build systems. NOTE: If "ARCHS" are overridden, this will *NOT* be set! +# +# This toolchain defines the following macros for use externally: +# +# set_xcode_property (TARGET XCODE_PROPERTY XCODE_VALUE XCODE_VARIANT) +# A convenience macro for setting xcode specific properties on targets. +# Available variants are: All, Release, RelWithDebInfo, Debug, MinSizeRel +# example: set_xcode_property (myioslib IPHONEOS_DEPLOYMENT_TARGET "3.1" "all"). +# +# find_host_package (PROGRAM ARGS) +# A macro used to find executable programs on the host system, not within the +# environment. Thanks to the android-cmake project for providing the +# command. +# + +cmake_minimum_required(VERSION 3.8.0) + +# CMake invokes the toolchain file twice during the first build, but only once during subsequent rebuilds. +if(DEFINED ENV{_IOS_TOOLCHAIN_HAS_RUN}) + return() +endif() +set(ENV{_IOS_TOOLCHAIN_HAS_RUN} true) + +# List of supported platform values +list(APPEND _supported_platforms + "OS" "OS64" "OS64COMBINED" "SIMULATOR" "SIMULATOR64" "SIMULATORARM64" + "TVOS" "TVOSCOMBINED" "SIMULATOR_TVOS" + "WATCHOS" "WATCHOSCOMBINED" "SIMULATOR_WATCHOS" + "MAC" "MAC_ARM64" + "MAC_CATALYST" "MAC_CATALYST_ARM64") + +# Cache what generator is used +set(USED_CMAKE_GENERATOR "${CMAKE_GENERATOR}") + +# Check if using a CMake version capable of building combined FAT builds (simulator and target slices combined in one static lib) +if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.14") + set(MODERN_CMAKE YES) +endif() + +# Get the Xcode version being used. +# Problem: CMake runs toolchain files multiple times, but can't read cache variables on some runs. +# Workaround: On first run (in which cache variables are always accessible), set an intermediary environment variable. +# +# NOTE: This pattern is used i many places in this toolchain to speed up checks of all sorts +if(DEFINED XCODE_VERSION_INT) + # Environment variables are always preserved. + set(ENV{_XCODE_VERSION_INT} "${XCODE_VERSION_INT}") +elseif(DEFINED ENV{_XCODE_VERSION_INT}) + set(XCODE_VERSION_INT "$ENV{_XCODE_VERSION_INT}") +elseif(NOT DEFINED XCODE_VERSION_INT) + find_program(XCODEBUILD_EXECUTABLE xcodebuild) + if(NOT XCODEBUILD_EXECUTABLE) + message(FATAL_ERROR "xcodebuild not found. Please install either the standalone commandline tools or Xcode.") + endif() + execute_process(COMMAND ${XCODEBUILD_EXECUTABLE} -version + OUTPUT_VARIABLE XCODE_VERSION_INT + ERROR_QUIET + OUTPUT_STRIP_TRAILING_WHITESPACE) + string(REGEX MATCH "Xcode [0-9\\.]+" XCODE_VERSION_INT "${XCODE_VERSION_INT}") + string(REGEX REPLACE "Xcode ([0-9\\.]+)" "\\1" XCODE_VERSION_INT "${XCODE_VERSION_INT}") + set(XCODE_VERSION_INT "${XCODE_VERSION_INT}" CACHE INTERNAL "") +endif() + +# Assuming that xcode 12.0 is installed you most probably have ios sdk 14.0 or later installed (tested on Big Sur) +# if you don't set a deployment target it will be set the way you only get 64-bit builds +if(NOT DEFINED DEPLOYMENT_TARGET AND XCODE_VERSION_INT VERSION_GREATER 12.0) + # Temporarily fix the arm64 issues in CMake install-combined by excluding arm64 for simulator builds (needed for Apple Silicon...) + set(CMAKE_XCODE_ATTRIBUTE_EXCLUDED_ARCHS[sdk=iphonesimulator*] "arm64") +endif() + +# Check if the platform variable is set +if(DEFINED PLATFORM) + # Environment variables are always preserved. + set(ENV{_PLATFORM} "${PLATFORM}") +elseif(DEFINED ENV{_PLATFORM}) + set(PLATFORM "$ENV{_PLATFORM}") +elseif(NOT DEFINED PLATFORM) + message(FATAL_ERROR "PLATFORM argument not set. Bailing configure since I don't know what target you want to build for!") +endif () + +if(PLATFORM MATCHES ".*COMBINED" AND NOT CMAKE_GENERATOR MATCHES "Xcode") + message(FATAL_ERROR "The combined builds support requires Xcode to be used as generator via '-G Xcode' command-line argument in CMake") +endif() + +# Safeguard that the platform value is set and is one of the supported values +list(FIND _supported_platforms ${PLATFORM} contains_PLATFORM) +if("${contains_PLATFORM}" EQUAL "-1") + string(REPLACE ";" "\n * " _supported_platforms_formatted "${_supported_platforms}") + message(FATAL_ERROR " Invalid PLATFORM specified! Current value: ${PLATFORM}.\n" + " Supported PLATFORM values: \n * ${_supported_platforms_formatted}") +endif() + +# Check if Apple Silicon is supported +if(PLATFORM MATCHES "^(MAC_ARM64)$|^(MAC_CATALYST_ARM64)$" AND ${CMAKE_VERSION} VERSION_LESS "3.19.5") + message(FATAL_ERROR "Apple Silicon builds requires a minimum of CMake 3.19.5") +endif() + +# Touch toolchain variable to suppress "unused variable" warning. +# This happens if CMake is invoked with the same command line the second time. +if(CMAKE_TOOLCHAIN_FILE) +endif() + +# Fix for PThread library not in path +set(CMAKE_THREAD_LIBS_INIT "-lpthread") +set(CMAKE_HAVE_THREADS_LIBRARY 1) +set(CMAKE_USE_WIN32_THREADS_INIT 0) +set(CMAKE_USE_PTHREADS_INIT 1) + +# Specify named language support defaults. +if(NOT DEFINED NAMED_LANGUAGE_SUPPORT AND ${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.16") + set(NAMED_LANGUAGE_SUPPORT ON) + message(STATUS "[DEFAULTS] Using explicit named language support! E.g., enable_language(CXX) is needed in the project files.") +elseif(NOT DEFINED NAMED_LANGUAGE_SUPPORT AND ${CMAKE_VERSION} VERSION_LESS "3.16") + set(NAMED_LANGUAGE_SUPPORT OFF) + message(STATUS "[DEFAULTS] Disabling explicit named language support. Falling back to legacy behaviour.") +elseif(DEFINED NAMED_LANGUAGE_SUPPORT AND ${CMAKE_VERSION} VERSION_LESS "3.16") + message(FATAL_ERROR "CMake named language support for OBJC and OBJCXX was added in CMake 3.16.") +endif() +set(NAMED_LANGUAGE_SUPPORT_INT ${NAMED_LANGUAGE_SUPPORT} CACHE BOOL + "Whether or not to enable explicit named language support" FORCE) + +# Specify minimum version of deployment target. +if(NOT DEFINED DEPLOYMENT_TARGET) + if (PLATFORM MATCHES "WATCHOS") + # Unless specified, SDK version 4.0 is used by default as minimum target version (watchOS). + set(DEPLOYMENT_TARGET "4.0") + elseif(PLATFORM STREQUAL "MAC") + # Unless specified, SDK version 10.13 (High sierra) is used by default as minimum target version (macos). + set(DEPLOYMENT_TARGET "10.13") + elseif(PLATFORM STREQUAL "MAC_ARM64") + # Unless specified, SDK version 11.0 (Big Sur) is used by default as minimum target version (macos on arm). + set(DEPLOYMENT_TARGET "11.0") + elseif(PLATFORM STREQUAL "MAC_CATALYST" OR PLATFORM STREQUAL "MAC_CATALYST_ARM64") + # Unless specified, SDK version 13.0 is used by default as minimum target version (mac catalyst minimum requirement). + set(DEPLOYMENT_TARGET "13.1") + else() + # Unless specified, SDK version 11.0 is used by default as minimum target version (iOS, tvOS). + set(DEPLOYMENT_TARGET "11.0") + endif() + message(STATUS "[DEFAULTS] Using the default min-version since DEPLOYMENT_TARGET not provided!") +elseif(DEFINED DEPLOYMENT_TARGET AND PLATFORM MATCHES "^MAC_CATALYST" AND ${DEPLOYMENT_TARGET} VERSION_LESS "13.1") + message(FATAL_ERROR "Mac Catalyst builds requires a minimum deployment target of 13.1!") +endif() + +# Store the DEPLOYMENT_TARGET in the cache +set(DEPLOYMENT_TARGET "${DEPLOYMENT_TARGET}" CACHE INTERNAL "") + +# Handle the case where we are targeting iOS and a version above 10.3.4 (32-bit support dropped officially) +if(PLATFORM STREQUAL "OS" AND DEPLOYMENT_TARGET VERSION_GREATER_EQUAL 10.3.4) + set(PLATFORM "OS64") + message(STATUS "Targeting minimum SDK version ${DEPLOYMENT_TARGET}. Dropping 32-bit support.") +elseif(PLATFORM STREQUAL "SIMULATOR" AND DEPLOYMENT_TARGET VERSION_GREATER_EQUAL 10.3.4) + set(PLATFORM "SIMULATOR64") + message(STATUS "Targeting minimum SDK version ${DEPLOYMENT_TARGET}. Dropping 32-bit support.") +endif() + +set(PLATFORM_INT "${PLATFORM}") + +if(DEFINED ARCHS) + string(REPLACE ";" "-" ARCHS_SPLIT "${ARCHS}") +endif() + +# Determine the platform name and architectures for use in xcodebuild commands +# from the specified PLATFORM_INT name. +if(PLATFORM_INT STREQUAL "OS") + set(SDK_NAME iphoneos) + if(NOT ARCHS) + set(ARCHS armv7 armv7s arm64) + set(APPLE_TARGET_TRIPLE_INT arm-apple-ios${DEPLOYMENT_TARGET}) + else() + set(APPLE_TARGET_TRIPLE_INT ${ARCHS_SPLIT}-apple-ios${DEPLOYMENT_TARGET}) + endif() +elseif(PLATFORM_INT STREQUAL "OS64") + set(SDK_NAME iphoneos) + if(NOT ARCHS) + if (XCODE_VERSION_INT VERSION_GREATER 10.0) + set(ARCHS arm64) # FIXME: Add arm64e when Apple have fixed the integration issues with it, libarclite_iphoneos.a is currently missung bitcode markers for example + else() + set(ARCHS arm64) + endif() + set(APPLE_TARGET_TRIPLE_INT aarch64-apple-ios${DEPLOYMENT_TARGET}) + else() + set(APPLE_TARGET_TRIPLE_INT ${ARCHS_SPLIT}-apple-ios${DEPLOYMENT_TARGET}) + endif() +elseif(PLATFORM_INT STREQUAL "OS64COMBINED") + set(SDK_NAME iphoneos) + if(MODERN_CMAKE) + if(NOT ARCHS) + if (XCODE_VERSION_INT VERSION_GREATER 10.0) + set(ARCHS arm64 x86_64) # FIXME: Add arm64e when Apple have fixed the integration issues with it, libarclite_iphoneos.a is currently missung bitcode markers for example + set(CMAKE_XCODE_ATTRIBUTE_ARCHS[sdk=iphoneos*] "arm64") + set(CMAKE_XCODE_ATTRIBUTE_ARCHS[sdk=iphonesimulator*] "x86_64") + set(CMAKE_XCODE_ATTRIBUTE_VALID_ARCHS[sdk=iphoneos*] "arm64") + set(CMAKE_XCODE_ATTRIBUTE_VALID_ARCHS[sdk=iphonesimulator*] "x86_64") + else() + set(ARCHS arm64 x86_64) + set(CMAKE_XCODE_ATTRIBUTE_ARCHS[sdk=iphoneos*] "arm64") + set(CMAKE_XCODE_ATTRIBUTE_ARCHS[sdk=iphonesimulator*] "x86_64") + set(CMAKE_XCODE_ATTRIBUTE_VALID_ARCHS[sdk=iphoneos*] "arm64") + set(CMAKE_XCODE_ATTRIBUTE_VALID_ARCHS[sdk=iphonesimulator*] "x86_64") + endif() + set(APPLE_TARGET_TRIPLE_INT aarch64-x86_64-apple-ios${DEPLOYMENT_TARGET}) + else() + set(APPLE_TARGET_TRIPLE_INT ${ARCHS_SPLIT}-apple-ios${DEPLOYMENT_TARGET}) + endif() + else() + message(FATAL_ERROR "Please make sure that you are running CMake 3.14+ to make the OS64COMBINED setting work") + endif() +elseif(PLATFORM_INT STREQUAL "SIMULATOR") + set(SDK_NAME iphonesimulator) + if(NOT ARCHS) + set(ARCHS i386) + set(APPLE_TARGET_TRIPLE_INT i386-apple-ios${DEPLOYMENT_TARGET}-simulator) + else() + set(APPLE_TARGET_TRIPLE_INT ${ARCHS_SPLIT}-apple-ios${DEPLOYMENT_TARGET}-simulator) + endif() + message(DEPRECATION "SIMULATOR IS DEPRECATED. Consider using SIMULATOR64 instead.") +elseif(PLATFORM_INT STREQUAL "SIMULATOR64") + set(SDK_NAME iphonesimulator) + if(NOT ARCHS) + set(ARCHS x86_64) + set(APPLE_TARGET_TRIPLE_INT x86_64-apple-ios${DEPLOYMENT_TARGET}-simulator) + else() + set(APPLE_TARGET_TRIPLE_INT ${ARCHS_SPLIT}-apple-ios${DEPLOYMENT_TARGET}-simulator) + endif() +elseif(PLATFORM_INT STREQUAL "SIMULATORARM64") + set(SDK_NAME iphonesimulator) + if(NOT ARCHS) + set(ARCHS arm64) + set(APPLE_TARGET_TRIPLE_INT aarch64-apple-ios${DEPLOYMENT_TARGET}-simulator) + else() + set(APPLE_TARGET_TRIPLE_INT ${ARCHS_SPLIT}-apple-ios${DEPLOYMENT_TARGET}-simulator) + endif() +elseif(PLATFORM_INT STREQUAL "TVOS") + set(SDK_NAME appletvos) + if(NOT ARCHS) + set(ARCHS arm64) + set(APPLE_TARGET_TRIPLE_INT aarch64-apple-tvos${DEPLOYMENT_TARGET}) + else() + set(APPLE_TARGET_TRIPLE_INT ${ARCHS_SPLIT}-apple-tvos${DEPLOYMENT_TARGET}) + endif() +elseif (PLATFORM_INT STREQUAL "TVOSCOMBINED") + set(SDK_NAME appletvos) + if(MODERN_CMAKE) + if(NOT ARCHS) + set(ARCHS arm64 x86_64) + set(APPLE_TARGET_TRIPLE_INT aarch64-x86_64-apple-tvos${DEPLOYMENT_TARGET}) + set(CMAKE_XCODE_ATTRIBUTE_ARCHS[sdk=appletvos*] "arm64") + set(CMAKE_XCODE_ATTRIBUTE_ARCHS[sdk=appletvsimulator*] "x86_64") + set(CMAKE_XCODE_ATTRIBUTE_VALID_ARCHS[sdk=appletvos*] "arm64") + set(CMAKE_XCODE_ATTRIBUTE_VALID_ARCHS[sdk=appletvsimulator*] "x86_64") + else() + set(APPLE_TARGET_TRIPLE_INT ${ARCHS_SPLIT}-apple-tvos${DEPLOYMENT_TARGET}) + endif() + else() + message(FATAL_ERROR "Please make sure that you are running CMake 3.14+ to make the TVOSCOMBINED setting work") + endif() +elseif(PLATFORM_INT STREQUAL "SIMULATOR_TVOS") + set(SDK_NAME appletvsimulator) + if(NOT ARCHS) + set(ARCHS x86_64) + set(APPLE_TARGET_TRIPLE_INT x86_64-apple-tvos${DEPLOYMENT_TARGET}-simulator) + else() + set(APPLE_TARGET_TRIPLE_INT ${ARCHS_SPLIT}-apple-tvos${DEPLOYMENT_TARGET}-simulator) + endif() +elseif(PLATFORM_INT STREQUAL "WATCHOS") + set(SDK_NAME watchos) + if(NOT ARCHS) + if (XCODE_VERSION_INT VERSION_GREATER 10.0) + set(ARCHS armv7k arm64_32) + set(APPLE_TARGET_TRIPLE_INT aarch64_32-apple-watchos${DEPLOYMENT_TARGET}) + else() + set(ARCHS armv7k) + set(APPLE_TARGET_TRIPLE_INT arm-apple-watchos${DEPLOYMENT_TARGET}) + endif() + else() + set(APPLE_TARGET_TRIPLE_INT ${ARCHS_SPLIT}-apple-watchos${DEPLOYMENT_TARGET}) + endif() +elseif(PLATFORM_INT STREQUAL "WATCHOSCOMBINED") + set(SDK_NAME watchos) + if(MODERN_CMAKE) + if(NOT ARCHS) + if (XCODE_VERSION_INT VERSION_GREATER 10.0) + set(ARCHS armv7k arm64_32 i386) + set(APPLE_TARGET_TRIPLE_INT aarch64_32-i386-apple-watchos${DEPLOYMENT_TARGET}) + set(CMAKE_XCODE_ATTRIBUTE_ARCHS[sdk=watchos*] "armv7k arm64_32") + set(CMAKE_XCODE_ATTRIBUTE_ARCHS[sdk=watchsimulator*] "i386") + set(CMAKE_XCODE_ATTRIBUTE_VALID_ARCHS[sdk=watchos*] "armv7k arm64_32") + set(CMAKE_XCODE_ATTRIBUTE_VALID_ARCHS[sdk=watchsimulator*] "i386") + else() + set(ARCHS armv7k i386) + set(APPLE_TARGET_TRIPLE_INT arm-i386-apple-watchos${DEPLOYMENT_TARGET}) + set(CMAKE_XCODE_ATTRIBUTE_ARCHS[sdk=watchos*] "armv7k") + set(CMAKE_XCODE_ATTRIBUTE_ARCHS[sdk=watchsimulator*] "i386") + set(CMAKE_XCODE_ATTRIBUTE_VALID_ARCHS[sdk=watchos*] "armv7k") + set(CMAKE_XCODE_ATTRIBUTE_VALID_ARCHS[sdk=watchsimulator*] "i386") + endif() + else() + set(APPLE_TARGET_TRIPLE_INT ${ARCHS_SPLIT}-apple-watchos${DEPLOYMENT_TARGET}) + endif() + else() + message(FATAL_ERROR "Please make sure that you are running CMake 3.14+ to make the WATCHOSCOMBINED setting work") + endif() +elseif(PLATFORM_INT STREQUAL "SIMULATOR_WATCHOS") + set(SDK_NAME watchsimulator) + if(NOT ARCHS) + set(ARCHS i386) + set(APPLE_TARGET_TRIPLE_INT i386-apple-watchos${DEPLOYMENT_TARGET}-simulator) + else() + set(APPLE_TARGET_TRIPLE_INT ${ARCHS_SPLIT}-apple-watchos${DEPLOYMENT_TARGET}-simulator) + endif() +elseif(PLATFORM_INT STREQUAL "MAC" OR PLATFORM_INT STREQUAL "MAC_CATALYST") + set(SDK_NAME macosx) + if(NOT ARCHS) + set(ARCHS x86_64) + endif() + string(REPLACE ";" "-" ARCHS_SPLIT "${ARCHS}") + if(PLATFORM_INT STREQUAL "MAC") + set(APPLE_TARGET_TRIPLE_INT ${ARCHS_SPLIT}-apple-macosx${DEPLOYMENT_TARGET}) + elseif(PLATFORM_INT STREQUAL "MAC_CATALYST") + set(APPLE_TARGET_TRIPLE_INT ${ARCHS_SPLIT}-apple-ios${DEPLOYMENT_TARGET}-macabi) + endif() +elseif(PLATFORM_INT MATCHES "^(MAC_ARM64)$|^(MAC_CATALYST_ARM64)$") + set(SDK_NAME macosx) + if(NOT ARCHS) + set(ARCHS arm64) + endif() + string(REPLACE ";" "-" ARCHS_SPLIT "${ARCHS}") + if(PLATFORM_INT STREQUAL "MAC_ARM64") + set(APPLE_TARGET_TRIPLE_INT ${ARCHS_SPLIT}-apple-macosx${DEPLOYMENT_TARGET}) + elseif(PLATFORM_INT STREQUAL "MAC_CATALYST_ARM64") + set(APPLE_TARGET_TRIPLE_INT ${ARCHS_SPLIT}-apple-ios${DEPLOYMENT_TARGET}-macabi) + endif() +else() + message(FATAL_ERROR "Invalid PLATFORM: ${PLATFORM_INT}") +endif() + +string(REPLACE ";" " " ARCHS_SPACED "${ARCHS}") + +if(MODERN_CMAKE AND PLATFORM_INT MATCHES ".*COMBINED" AND NOT CMAKE_GENERATOR MATCHES "Xcode") + message(FATAL_ERROR "The COMBINED options only work with Xcode generator, -G Xcode") +endif() + +if(CMAKE_GENERATOR MATCHES "Xcode" AND PLATFORM_INT MATCHES "^MAC_CATALYST") + set(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY "libc++") + set(CMAKE_XCODE_ATTRIBUTE_SUPPORTED_PLATFORMS "macosx") + set(CMAKE_XCODE_EFFECTIVE_PLATFORMS "-maccatalyst") + if(NOT DEFINED MACOSX_DEPLOYMENT_TARGET) + set(CMAKE_XCODE_ATTRIBUTE_MACOSX_DEPLOYMENT_TARGET "10.15") + else() + set(CMAKE_XCODE_ATTRIBUTE_MACOSX_DEPLOYMENT_TARGET "${MACOSX_DEPLOYMENT_TARGET}") + endif() +elseif(CMAKE_GENERATOR MATCHES "Xcode") + set(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY "libc++") + set(CMAKE_XCODE_ATTRIBUTE_IPHONEOS_DEPLOYMENT_TARGET "${DEPLOYMENT_TARGET}") + if(NOT PLATFORM_INT MATCHES ".*COMBINED") + set(CMAKE_XCODE_ATTRIBUTE_ARCHS[sdk=${SDK_NAME}*] "${ARCHS_SPACED}") + set(CMAKE_XCODE_ATTRIBUTE_VALID_ARCHS[sdk=${SDK_NAME}*] "${ARCHS_SPACED}") + endif() +endif() + +# If user did not specify the SDK root to use, then query xcodebuild for it. +if(DEFINED CMAKE_OSX_SYSROOT_INT) + # Environment variables are always preserved. + set(ENV{_CMAKE_OSX_SYSROOT_INT} "${CMAKE_OSX_SYSROOT_INT}") +elseif(DEFINED ENV{_CMAKE_OSX_SYSROOT_INT}) + set(CMAKE_OSX_SYSROOT_INT "$ENV{_CMAKE_OSX_SYSROOT_INT}") +elseif(NOT DEFINED CMAKE_OSX_SYSROOT_INT) + execute_process(COMMAND ${XCODEBUILD_EXECUTABLE} -version -sdk ${SDK_NAME} Path + OUTPUT_VARIABLE CMAKE_OSX_SYSROOT_INT + ERROR_QUIET + OUTPUT_STRIP_TRAILING_WHITESPACE) +endif() + +if (NOT DEFINED CMAKE_OSX_SYSROOT_INT AND NOT DEFINED CMAKE_OSX_SYSROOT) + message(SEND_ERROR "Please make sure that Xcode is installed and that the toolchain" + "is pointing to the correct path. Please run:" + "sudo xcode-select -s /Applications/Xcode.app/Contents/Developer" + "and see if that fixes the problem for you.") + message(FATAL_ERROR "Invalid CMAKE_OSX_SYSROOT: ${CMAKE_OSX_SYSROOT} " + "does not exist.") +elseif(DEFINED CMAKE_OSX_SYSROOT_INT) + set(CMAKE_OSX_SYSROOT_INT "${CMAKE_OSX_SYSROOT_INT}" CACHE INTERNAL "") + # Specify the location or name of the platform SDK to be used in CMAKE_OSX_SYSROOT. + set(CMAKE_OSX_SYSROOT "${CMAKE_OSX_SYSROOT_INT}" CACHE INTERNAL "") +endif() + +# Use bitcode or not +if(NOT DEFINED ENABLE_BITCODE AND NOT ARCHS MATCHES "((^|;|, )(i386|x86_64))+") + # Unless specified, enable bitcode support by default + message(STATUS "[DEFAULTS] Enabling bitcode support by default. ENABLE_BITCODE not provided!") + set(ENABLE_BITCODE ON) +elseif(NOT DEFINED ENABLE_BITCODE) + message(STATUS "[DEFAULTS] Disabling bitcode support by default on simulators. ENABLE_BITCODE not provided for override!") + set(ENABLE_BITCODE OFF) +endif() +set(ENABLE_BITCODE_INT ${ENABLE_BITCODE} CACHE BOOL + "Whether or not to enable bitcode" FORCE) +# Use ARC or not +if(NOT DEFINED ENABLE_ARC) + # Unless specified, enable ARC support by default + set(ENABLE_ARC ON) + message(STATUS "[DEFAULTS] Enabling ARC support by default. ENABLE_ARC not provided!") +endif() +set(ENABLE_ARC_INT ${ENABLE_ARC} CACHE BOOL "Whether or not to enable ARC" FORCE) +# Use hidden visibility or not +if(NOT DEFINED ENABLE_VISIBILITY) + # Unless specified, disable symbols visibility by default + set(ENABLE_VISIBILITY OFF) + message(STATUS "[DEFAULTS] Hiding symbols visibility by default. ENABLE_VISIBILITY not provided!") +endif() +set(ENABLE_VISIBILITY_INT ${ENABLE_VISIBILITY} CACHE BOOL "Whether or not to hide symbols from the dynamic linker (-fvisibility=hidden)" FORCE) +# Set strict compiler checks or not +if(NOT DEFINED ENABLE_STRICT_TRY_COMPILE) + # Unless specified, disable strict try_compile() + set(ENABLE_STRICT_TRY_COMPILE OFF) + message(STATUS "[DEFAULTS] Using NON-strict compiler checks by default. ENABLE_STRICT_TRY_COMPILE not provided!") +endif() +set(ENABLE_STRICT_TRY_COMPILE_INT ${ENABLE_STRICT_TRY_COMPILE} CACHE BOOL + "Whether or not to use strict compiler checks" FORCE) + +# Get the SDK version information. +if(DEFINED SDK_VERSION) + # Environment variables are always preserved. + set(ENV{_SDK_VERSION} "${SDK_VERSION}") +elseif(DEFINED ENV{_SDK_VERSION}) + set(SDK_VERSION "$ENV{_SDK_VERSION}") +elseif(NOT DEFINED SDK_VERSION) + execute_process(COMMAND ${XCODEBUILD_EXECUTABLE} -sdk ${CMAKE_OSX_SYSROOT_INT} -version SDKVersion + OUTPUT_VARIABLE SDK_VERSION + ERROR_QUIET + OUTPUT_STRIP_TRAILING_WHITESPACE) +endif() + +# Find the Developer root for the specific iOS platform being compiled for +# from CMAKE_OSX_SYSROOT. Should be ../../ from SDK specified in +# CMAKE_OSX_SYSROOT. There does not appear to be a direct way to obtain +# this information from xcrun or xcodebuild. +if (NOT DEFINED CMAKE_DEVELOPER_ROOT AND NOT CMAKE_GENERATOR MATCHES "Xcode") + get_filename_component(PLATFORM_SDK_DIR ${CMAKE_OSX_SYSROOT_INT} PATH) + get_filename_component(CMAKE_DEVELOPER_ROOT ${PLATFORM_SDK_DIR} PATH) + if (NOT EXISTS "${CMAKE_DEVELOPER_ROOT}") + message(FATAL_ERROR "Invalid CMAKE_DEVELOPER_ROOT: ${CMAKE_DEVELOPER_ROOT} does not exist.") + endif() +endif() + +# Find the C & C++ compilers for the specified SDK. +if(DEFINED CMAKE_C_COMPILER) + # Environment variables are always preserved. + set(ENV{_CMAKE_C_COMPILER} "${CMAKE_C_COMPILER}") +elseif(DEFINED ENV{_CMAKE_C_COMPILER}) + set(CMAKE_C_COMPILER "$ENV{_CMAKE_C_COMPILER}") + set(CMAKE_ASM_COMPILER ${CMAKE_C_COMPILER}) +elseif(NOT DEFINED CMAKE_C_COMPILER) + execute_process(COMMAND xcrun -sdk ${CMAKE_OSX_SYSROOT_INT} -find clang + OUTPUT_VARIABLE CMAKE_C_COMPILER + ERROR_QUIET + OUTPUT_STRIP_TRAILING_WHITESPACE) + set(CMAKE_ASM_COMPILER ${CMAKE_C_COMPILER}) +endif() +if(DEFINED CMAKE_CXX_COMPILER) + # Environment variables are always preserved. + set(ENV{_CMAKE_CXX_COMPILER} "${CMAKE_CXX_COMPILER}") +elseif(DEFINED ENV{_CMAKE_CXX_COMPILER}) + set(CMAKE_CXX_COMPILER "$ENV{_CMAKE_CXX_COMPILER}") +elseif(NOT DEFINED CMAKE_CXX_COMPILER) + execute_process(COMMAND xcrun -sdk ${CMAKE_OSX_SYSROOT_INT} -find clang++ + OUTPUT_VARIABLE CMAKE_CXX_COMPILER + ERROR_QUIET + OUTPUT_STRIP_TRAILING_WHITESPACE) +endif() +# Find (Apple's) libtool. +if(DEFINED BUILD_LIBTOOL) + # Environment variables are always preserved. + set(ENV{_BUILD_LIBTOOL} "${BUILD_LIBTOOL}") +elseif(DEFINED ENV{_BUILD_LIBTOOL}) + set(BUILD_LIBTOOL "$ENV{_BUILD_LIBTOOL}") +elseif(NOT DEFINED BUILD_LIBTOOL) + execute_process(COMMAND xcrun -sdk ${CMAKE_OSX_SYSROOT_INT} -find libtool + OUTPUT_VARIABLE BUILD_LIBTOOL + ERROR_QUIET + OUTPUT_STRIP_TRAILING_WHITESPACE) +endif() +# Find the toolchain's provided install_name_tool if none is found on the host +if(DEFINED CMAKE_INSTALL_NAME_TOOL) + # Environment variables are always preserved. + set(ENV{_CMAKE_INSTALL_NAME_TOOL} "${CMAKE_INSTALL_NAME_TOOL}") +elseif(DEFINED ENV{_CMAKE_INSTALL_NAME_TOOL}) + set(CMAKE_INSTALL_NAME_TOOL "$ENV{_CMAKE_INSTALL_NAME_TOOL}") +elseif(NOT DEFINED CMAKE_INSTALL_NAME_TOOL) + execute_process(COMMAND xcrun -sdk ${CMAKE_OSX_SYSROOT_INT} -find install_name_tool + OUTPUT_VARIABLE CMAKE_INSTALL_NAME_TOOL_INT + ERROR_QUIET + OUTPUT_STRIP_TRAILING_WHITESPACE) + set(CMAKE_INSTALL_NAME_TOOL ${CMAKE_INSTALL_NAME_TOOL_INT} CACHE INTERNAL "") +endif() + +# Configure libtool to be used instead of ar + ranlib to build static libraries. +# This is required on Xcode 7+, but should also work on previous versions of +# Xcode. +get_property(languages GLOBAL PROPERTY ENABLED_LANGUAGES) +foreach(lang ${languages}) + set(CMAKE_${lang}_CREATE_STATIC_LIBRARY "${BUILD_LIBTOOL} -static -o " CACHE INTERNAL "") +endforeach() + +# CMake 3.14+ support building for iOS, watchOS and tvOS out of the box. +if(MODERN_CMAKE) + if(SDK_NAME MATCHES "iphone") + set(CMAKE_SYSTEM_NAME iOS) + elseif(SDK_NAME MATCHES "macosx") + set(CMAKE_SYSTEM_NAME Darwin) + elseif(SDK_NAME MATCHES "appletv") + set(CMAKE_SYSTEM_NAME tvOS) + elseif(SDK_NAME MATCHES "watch") + set(CMAKE_SYSTEM_NAME watchOS) + endif() + # Provide flags for a combined FAT library build on newer CMake versions + if(PLATFORM_INT MATCHES ".*COMBINED") + set(CMAKE_XCODE_ATTRIBUTE_ONLY_ACTIVE_ARCH "NO") + set(CMAKE_IOS_INSTALL_COMBINED YES) + endif() +elseif(NOT DEFINED CMAKE_SYSTEM_NAME AND ${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.10") + # Legacy code path prior to CMake 3.14 or fallback if no CMAKE_SYSTEM_NAME specified + set(CMAKE_SYSTEM_NAME iOS) +elseif(NOT DEFINED CMAKE_SYSTEM_NAME) + # Legacy code path prior to CMake 3.14 or fallback if no CMAKE_SYSTEM_NAME specified + set(CMAKE_SYSTEM_NAME Darwin) +endif() +# Standard settings. +set(CMAKE_SYSTEM_VERSION ${SDK_VERSION} CACHE INTERNAL "") +set(UNIX ON CACHE BOOL "") +set(APPLE ON CACHE BOOL "") +if(PLATFORM STREQUAL "MAC" OR PLATFORM STREQUAL "MAC_ARM64") + set(IOS OFF CACHE BOOL "") + set(MACOS ON CACHE BOOL "") +elseif(PLATFORM STREQUAL "MAC_CATALYST" OR PLATFORM STREQUAL "MAC_CATALYST_ARM64") + set(IOS ON CACHE BOOL "") + set(MACOS ON CACHE BOOL "") +else() + set(IOS ON CACHE BOOL "") +endif() +set(CMAKE_AR ar CACHE FILEPATH "" FORCE) +set(CMAKE_RANLIB ranlib CACHE FILEPATH "" FORCE) +set(CMAKE_STRIP strip CACHE FILEPATH "" FORCE) +# Set the architectures for which to build. +set(CMAKE_OSX_ARCHITECTURES ${ARCHS} CACHE INTERNAL "") +# Change the type of target generated for try_compile() so it'll work when cross-compiling, weak compiler checks +if(NOT ENABLE_STRICT_TRY_COMPILE_INT) + set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) +endif() +# All iOS/Darwin specific settings - some may be redundant. +set(CMAKE_MACOSX_BUNDLE YES) +set(CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED "NO") +set(CMAKE_SHARED_LIBRARY_PREFIX "lib") +set(CMAKE_SHARED_LIBRARY_SUFFIX ".dylib") +set(CMAKE_SHARED_MODULE_PREFIX "lib") +set(CMAKE_SHARED_MODULE_SUFFIX ".so") +set(CMAKE_C_COMPILER_ABI ELF) +set(CMAKE_CXX_COMPILER_ABI ELF) +set(CMAKE_C_HAS_ISYSROOT 1) +set(CMAKE_CXX_HAS_ISYSROOT 1) +set(CMAKE_MODULE_EXISTS 1) +set(CMAKE_DL_LIBS "") +set(CMAKE_C_OSX_COMPATIBILITY_VERSION_FLAG "-compatibility_version ") +set(CMAKE_C_OSX_CURRENT_VERSION_FLAG "-current_version ") +set(CMAKE_CXX_OSX_COMPATIBILITY_VERSION_FLAG "${CMAKE_C_OSX_COMPATIBILITY_VERSION_FLAG}") +set(CMAKE_CXX_OSX_CURRENT_VERSION_FLAG "${CMAKE_C_OSX_CURRENT_VERSION_FLAG}") + +if(ARCHS MATCHES "((^|;|, )(arm64|arm64e|x86_64))+") + set(CMAKE_C_SIZEOF_DATA_PTR 8) + set(CMAKE_CXX_SIZEOF_DATA_PTR 8) + if(ARCHS MATCHES "((^|;|, )(arm64|arm64e))+") + set(CMAKE_SYSTEM_PROCESSOR "aarch64") + else() + set(CMAKE_SYSTEM_PROCESSOR "x86_64") + endif() +else() + set(CMAKE_C_SIZEOF_DATA_PTR 4) + set(CMAKE_CXX_SIZEOF_DATA_PTR 4) + set(CMAKE_SYSTEM_PROCESSOR "arm") +endif() + +# Note that only Xcode 7+ supports the newer more specific: +# -m${SDK_NAME}-version-min flags, older versions of Xcode use: +# -m(ios/ios-simulator)-version-min instead. +if(${CMAKE_VERSION} VERSION_LESS "3.11") + if(PLATFORM_INT STREQUAL "OS" OR PLATFORM_INT STREQUAL "OS64") + if(XCODE_VERSION_INT VERSION_LESS 7.0) + set(SDK_NAME_VERSION_FLAGS + "-mios-version-min=${DEPLOYMENT_TARGET}") + else() + # Xcode 7.0+ uses flags we can build directly from SDK_NAME. + set(SDK_NAME_VERSION_FLAGS + "-m${SDK_NAME}-version-min=${DEPLOYMENT_TARGET}") + endif() + elseif(PLATFORM_INT STREQUAL "TVOS") + set(SDK_NAME_VERSION_FLAGS + "-mtvos-version-min=${DEPLOYMENT_TARGET}") + elseif(PLATFORM_INT STREQUAL "SIMULATOR_TVOS") + set(SDK_NAME_VERSION_FLAGS + "-mtvos-simulator-version-min=${DEPLOYMENT_TARGET}") + elseif(PLATFORM_INT STREQUAL "WATCHOS") + set(SDK_NAME_VERSION_FLAGS + "-mwatchos-version-min=${DEPLOYMENT_TARGET}") + elseif(PLATFORM_INT STREQUAL "SIMULATOR_WATCHOS") + set(SDK_NAME_VERSION_FLAGS + "-mwatchos-simulator-version-min=${DEPLOYMENT_TARGET}") + elseif(PLATFORM_INT STREQUAL "MAC") + set(SDK_NAME_VERSION_FLAGS + "-mmacosx-version-min=${DEPLOYMENT_TARGET}") + else() + # SIMULATOR or SIMULATOR64 both use -mios-simulator-version-min. + set(SDK_NAME_VERSION_FLAGS + "-mios-simulator-version-min=${DEPLOYMENT_TARGET}") + endif() +elseif(NOT PLATFORM_INT MATCHES "^MAC_CATALYST") + # Newer versions of CMake sets the version min flags correctly, skip this for Mac Catalyst targets + set(CMAKE_OSX_DEPLOYMENT_TARGET ${DEPLOYMENT_TARGET}) +endif() + +if(DEFINED APPLE_TARGET_TRIPLE_INT) + set(APPLE_TARGET_TRIPLE ${APPLE_TARGET_TRIPLE_INT} CACHE INTERNAL "") + set(CMAKE_C_COMPILER_TARGET ${APPLE_TARGET_TRIPLE}) + set(CMAKE_CXX_COMPILER_TARGET ${APPLE_TARGET_TRIPLE}) + set(CMAKE_ASM_COMPILER_TARGET ${APPLE_TARGET_TRIPLE}) +endif() + +if(PLATFORM_INT MATCHES "^MAC_CATALYST") + set(C_TARGET_FLAGS "-isystem ${CMAKE_OSX_SYSROOT_INT}/System/iOSSupport/usr/include -iframework ${CMAKE_OSX_SYSROOT_INT}/System/iOSSupport/System/Library/Frameworks") +endif() + +if(ENABLE_BITCODE_INT) + set(BITCODE "-fembed-bitcode") + set(CMAKE_XCODE_ATTRIBUTE_BITCODE_GENERATION_MODE "bitcode") + set(CMAKE_XCODE_ATTRIBUTE_ENABLE_BITCODE "YES") +else() + set(BITCODE "") + set(CMAKE_XCODE_ATTRIBUTE_ENABLE_BITCODE "NO") +endif() + +if(ENABLE_ARC_INT) + set(FOBJC_ARC "-fobjc-arc") + set(CMAKE_XCODE_ATTRIBUTE_CLANG_ENABLE_OBJC_ARC "YES") +else() + set(FOBJC_ARC "-fno-objc-arc") + set(CMAKE_XCODE_ATTRIBUTE_CLANG_ENABLE_OBJC_ARC "NO") +endif() + +if(NAMED_LANGUAGE_SUPPORT_INT) + set(OBJC_VARS "-fobjc-abi-version=2 -DOBJC_OLD_DISPATCH_PROTOTYPES=0") + set(OBJC_LEGACY_VARS "") +else() + set(OBJC_VARS "") + set(OBJC_LEGACY_VARS "-fobjc-abi-version=2 -DOBJC_OLD_DISPATCH_PROTOTYPES=0") +endif() + +if(NOT ENABLE_VISIBILITY_INT) + foreach(lang ${languages}) + set(CMAKE_${lang}_VISIBILITY_PRESET "hidden" CACHE INTERNAL "") + endforeach() + set(CMAKE_XCODE_ATTRIBUTE_GCC_SYMBOLS_PRIVATE_EXTERN "YES") + set(VISIBILITY "-fvisibility=hidden -fvisibility-inlines-hidden") +else() + foreach(lang ${languages}) + set(CMAKE_${lang}_VISIBILITY_PRESET "default" CACHE INTERNAL "") + endforeach() + set(CMAKE_XCODE_ATTRIBUTE_GCC_SYMBOLS_PRIVATE_EXTERN "NO") + set(VISIBILITY "-fvisibility=default") +endif() + +if(DEFINED APPLE_TARGET_TRIPLE) + set(APPLE_TARGET_TRIPLE_FLAG "-target ${APPLE_TARGET_TRIPLE}") +endif() + +#Check if Xcode generator is used, since that will handle these flags automagically +if(CMAKE_GENERATOR MATCHES "Xcode") + message(STATUS "Not setting any manual command-line buildflags, since Xcode is selected as generator. Modifying the Xcode build-settings directly instead.") +else() + set(CMAKE_C_FLAGS "${C_TARGET_FLAGS} ${APPLE_TARGET_TRIPLE_FLAG} ${SDK_NAME_VERSION_FLAGS} ${OBJC_LEGACY_VARS} ${BITCODE} ${VISIBILITY} ${CMAKE_C_FLAGS}") + set(CMAKE_C_FLAGS_DEBUG "-O0 -g ${CMAKE_C_FLAGS_DEBUG}") + set(CMAKE_C_FLAGS_MINSIZEREL "-DNDEBUG -Os ${CMAKE_C_FLAGS_MINSIZEREL}") + set(CMAKE_C_FLAGS_RELWITHDEBINFO "-DNDEBUG -O2 -g ${CMAKE_C_FLAGS_RELWITHDEBINFO}") + set(CMAKE_C_FLAGS_RELEASE "-DNDEBUG -O3 ${CMAKE_C_FLAGS_RELEASE}") + set(CMAKE_CXX_FLAGS "${C_TARGET_FLAGS} ${APPLE_TARGET_TRIPLE_FLAG} ${SDK_NAME_VERSION_FLAGS} ${OBJC_LEGACY_VARS} ${BITCODE} ${VISIBILITY} ${CMAKE_CXX_FLAGS}") + set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g ${CMAKE_CXX_FLAGS_DEBUG}") + set(CMAKE_CXX_FLAGS_MINSIZEREL "-DNDEBUG -Os ${CMAKE_CXX_FLAGS_MINSIZEREL}") + set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-DNDEBUG -O2 -g ${CMAKE_CXX_FLAGS_RELWITHDEBINFO}") + set(CMAKE_CXX_FLAGS_RELEASE "-DNDEBUG -O3 ${CMAKE_CXX_FLAGS_RELEASE}") + if(NAMED_LANGUAGE_SUPPORT_INT) + set(CMAKE_OBJC_FLAGS "${C_TARGET_FLAGS} ${APPLE_TARGET_TRIPLE_FLAG} ${SDK_NAME_VERSION_FLAGS} ${BITCODE} ${VISIBILITY} ${FOBJC_ARC} ${OBJC_VARS} ${CMAKE_OBJC_FLAGS}") + set(CMAKE_OBJC_FLAGS_DEBUG "-O0 -g ${CMAKE_OBJC_FLAGS_DEBUG}") + set(CMAKE_OBJC_FLAGS_MINSIZEREL "-DNDEBUG -Os ${CMAKE_OBJC_FLAGS_MINSIZEREL}") + set(CMAKE_OBJC_FLAGS_RELWITHDEBINFO "-DNDEBUG -O2 -g ${CMAKE_OBJC_FLAGS_RELWITHDEBINFO}") + set(CMAKE_OBJC_FLAGS_RELEASE "-DNDEBUG -O3 ${CMAKE_OBJC_FLAGS_RELEASE}") + set(CMAKE_OBJCXX_FLAGS "${C_TARGET_FLAGS} ${APPLE_TARGET_TRIPLE_FLAG} ${SDK_NAME_VERSION_FLAGS} ${BITCODE} ${VISIBILITY} ${FOBJC_ARC} ${OBJC_VARS} ${CMAKE_OBJCXX_FLAGS}") + set(CMAKE_OBJCXX_FLAGS_DEBUG "-O0 -g ${CMAKE_OBJCXX_FLAGS_DEBUG}") + set(CMAKE_OBJCXX_FLAGS_MINSIZEREL "-DNDEBUG -Os ${CMAKE_OBJCXX_FLAGS_MINSIZEREL}") + set(CMAKE_OBJCXX_FLAGS_RELWITHDEBINFO "-DNDEBUG -O2 -g ${CMAKE_OBJCXX_FLAGS_RELWITHDEBINFO}") + set(CMAKE_OBJCXX_FLAGS_RELEASE "-DNDEBUG -O3 ${CMAKE_OBJCXX_FLAGS_RELEASE}") + endif() + set(CMAKE_C_LINK_FLAGS "${C_TARGET_FLAGS} ${SDK_NAME_VERSION_FLAGS} -Wl,-search_paths_first ${CMAKE_C_LINK_FLAGS}") + set(CMAKE_CXX_LINK_FLAGS "${C_TARGET_FLAGS} ${SDK_NAME_VERSION_FLAGS} -Wl,-search_paths_first ${CMAKE_CXX_LINK_FLAGS}") + if(NAMED_LANGUAGE_SUPPORT_INT) + set(CMAKE_OBJC_LINK_FLAGS "${C_TARGET_FLAGS} ${SDK_NAME_VERSION_FLAGS} -Wl,-search_paths_first ${CMAKE_OBJC_LINK_FLAGS}") + set(CMAKE_OBJCXX_LINK_FLAGS "${C_TARGET_FLAGS} ${SDK_NAME_VERSION_FLAGS} -Wl,-search_paths_first ${CMAKE_OBJCXX_LINK_FLAGS}") + endif() + set(CMAKE_ASM_FLAGS "${CMAKE_C_FLAGS} -x assembler-with-cpp -arch ${CMAKE_OSX_ARCHITECTURES} ${APPLE_TARGET_TRIPLE_FLAG}") +endif() + +## Print status messages to inform of the current state +message(STATUS "Configuring ${SDK_NAME} build for platform: ${PLATFORM_INT}, architecture(s): ${ARCHS}") +message(STATUS "Using SDK: ${CMAKE_OSX_SYSROOT_INT}") +message(STATUS "Using C compiler: ${CMAKE_C_COMPILER}") +message(STATUS "Using CXX compiler: ${CMAKE_CXX_COMPILER}") +message(STATUS "Using libtool: ${BUILD_LIBTOOL}") +message(STATUS "Using install name tool: ${CMAKE_INSTALL_NAME_TOOL}") +if(DEFINED APPLE_TARGET_TRIPLE) + message(STATUS "Autoconf target triple: ${APPLE_TARGET_TRIPLE}") +endif() +message(STATUS "Using minimum deployment version: ${DEPLOYMENT_TARGET}" + " (SDK version: ${SDK_VERSION})") +if(MODERN_CMAKE) + message(STATUS "Merging integrated CMake 3.14+ iOS,tvOS,watchOS,macOS toolchain(s) with this toolchain!") + if(PLATFORM_INT MATCHES ".*COMBINED") + message(STATUS "Will combine built (static) artifacts into FAT lib...") + endif() +endif() +if(CMAKE_GENERATOR MATCHES "Xcode") + message(STATUS "Using Xcode version: ${XCODE_VERSION_INT}") +endif() +message(STATUS "CMake version: ${CMAKE_VERSION}") +if(DEFINED SDK_NAME_VERSION_FLAGS) + message(STATUS "Using version flags: ${SDK_NAME_VERSION_FLAGS}") +endif() +message(STATUS "Using a data_ptr size of: ${CMAKE_CXX_SIZEOF_DATA_PTR}") +if(ENABLE_BITCODE_INT) + message(STATUS "Bitcode: Enabled") +else() + message(STATUS "Bitcode: Disabled") +endif() + +if(ENABLE_ARC_INT) + message(STATUS "ARC: Enabled") +else() + message(STATUS "ARC: Disabled") +endif() + +if(ENABLE_VISIBILITY_INT) + message(STATUS "Hiding symbols: Disabled") +else() + message(STATUS "Hiding symbols: Enabled") +endif() + +# Set global properties +set_property(GLOBAL PROPERTY PLATFORM "${PLATFORM}") +set_property(GLOBAL PROPERTY APPLE_TARGET_TRIPLE "${APPLE_TARGET_TRIPLE_INT}") +set_property(GLOBAL PROPERTY SDK_VERSION "${SDK_VERSION}") +set_property(GLOBAL PROPERTY XCODE_VERSION "${XCODE_VERSION_INT}") +set_property(GLOBAL PROPERTY OSX_ARCHITECTURES "${CMAKE_OSX_ARCHITECTURES}") + +# Export configurable variables for the try_compile() command. +set(CMAKE_TRY_COMPILE_PLATFORM_VARIABLES + PLATFORM + XCODE_VERSION_INT + SDK_VERSION + NAMED_LANGUAGE_SUPPORT + DEPLOYMENT_TARGET + CMAKE_DEVELOPER_ROOT + CMAKE_OSX_SYSROOT_INT + ENABLE_BITCODE + ENABLE_ARC + CMAKE_ASM_COMPILER + CMAKE_C_COMPILER + CMAKE_C_COMPILER_TARGET + CMAKE_CXX_COMPILER + CMAKE_CXX_COMPILER_TARGET + BUILD_LIBTOOL + CMAKE_INSTALL_NAME_TOOL + CMAKE_C_FLAGS + CMAKE_C_DEBUG + CMAKE_C_MINSIZEREL + CMAKE_C_RELWITHDEBINFO + CMAKE_C_RELEASE + CMAKE_CXX_FLAGS + CMAKE_CXX_FLAGS_DEBUG + CMAKE_CXX_FLAGS_MINSIZEREL + CMAKE_CXX_FLAGS_RELWITHDEBINFO + CMAKE_CXX_FLAGS_RELEASE + CMAKE_C_LINK_FLAGS + CMAKE_CXX_LINK_FLAGS + CMAKE_ASM_FLAGS +) + +if(NAMED_LANGUAGE_SUPPORT_INT) + list(APPEND CMAKE_TRY_COMPILE_PLATFORM_VARIABLES + CMAKE_OBJC_FLAGS + CMAKE_OBJC_DEBUG + CMAKE_OBJC_MINSIZEREL + CMAKE_OBJC_RELWITHDEBINFO + CMAKE_OBJC_RELEASE + CMAKE_OBJCXX_FLAGS + CMAKE_OBJCXX_DEBUG + CMAKE_OBJCXX_MINSIZEREL + CMAKE_OBJCXX_RELWITHDEBINFO + CMAKE_OBJCXX_RELEASE + CMAKE_OBJC_LINK_FLAGS + CMAKE_OBJCXX_LINK_FLAGS + ) +endif() + +set(CMAKE_PLATFORM_HAS_INSTALLNAME 1) +set(CMAKE_SHARED_LINKER_FLAGS "-rpath @executable_path/Frameworks -rpath @loader_path/Frameworks") +set(CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS "-dynamiclib -Wl,-headerpad_max_install_names") +set(CMAKE_SHARED_MODULE_CREATE_C_FLAGS "-bundle -Wl,-headerpad_max_install_names") +set(CMAKE_SHARED_MODULE_LOADER_C_FLAG "-Wl,-bundle_loader,") +set(CMAKE_SHARED_MODULE_LOADER_CXX_FLAG "-Wl,-bundle_loader,") +set(CMAKE_FIND_LIBRARY_SUFFIXES ".tbd" ".dylib" ".so" ".a") +set(CMAKE_SHARED_LIBRARY_SONAME_C_FLAG "-install_name") + +# Set the find root to the SDK developer roots. +# Note: CMAKE_FIND_ROOT_PATH is only useful when cross-compiling. Thus, do not set on macOS builds. +if(NOT PLATFORM_INT MATCHES "^MAC.*$") + list(APPEND CMAKE_FIND_ROOT_PATH "${CMAKE_OSX_SYSROOT_INT}" CACHE INTERNAL "") + set(CMAKE_IGNORE_PATH "/System/Library/Frameworks;/usr/local/lib" CACHE INTERNAL "") +endif() + +# Default to searching for frameworks first. +set(CMAKE_FIND_FRAMEWORK FIRST) + +# Set up the default search directories for frameworks. +if(PLATFORM_INT MATCHES "^MAC_CATALYST") + set(CMAKE_FRAMEWORK_PATH + ${CMAKE_DEVELOPER_ROOT}/Library/PrivateFrameworks + ${CMAKE_OSX_SYSROOT_INT}/System/Library/Frameworks + ${CMAKE_OSX_SYSROOT_INT}/System/iOSSupport/System/Library/Frameworks + ${CMAKE_FRAMEWORK_PATH} CACHE INTERNAL "") +else() + set(CMAKE_FRAMEWORK_PATH + ${CMAKE_DEVELOPER_ROOT}/Library/PrivateFrameworks + ${CMAKE_OSX_SYSROOT_INT}/System/Library/Frameworks + ${CMAKE_FRAMEWORK_PATH} CACHE INTERNAL "") +endif() + +# By default, search both the specified iOS SDK and the remainder of the host filesystem. +if(NOT CMAKE_FIND_ROOT_PATH_MODE_PROGRAM) + set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM BOTH CACHE INTERNAL "") +endif() +if(NOT CMAKE_FIND_ROOT_PATH_MODE_LIBRARY) + set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH CACHE INTERNAL "") +endif() +if(NOT CMAKE_FIND_ROOT_PATH_MODE_INCLUDE) + set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH CACHE INTERNAL "") +endif() +if(NOT CMAKE_FIND_ROOT_PATH_MODE_PACKAGE) + set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE BOTH CACHE INTERNAL "") +endif() + +# +# Some helper-macros below to simplify and beautify the CMakeFile +# + +# This little macro lets you set any Xcode specific property. +macro(set_xcode_property TARGET XCODE_PROPERTY XCODE_VALUE XCODE_RELVERSION) + set(XCODE_RELVERSION_I "${XCODE_RELVERSION}") + if(XCODE_RELVERSION_I STREQUAL "All") + set_property(TARGET ${TARGET} PROPERTY XCODE_ATTRIBUTE_${XCODE_PROPERTY} "${XCODE_VALUE}") + else() + set_property(TARGET ${TARGET} PROPERTY XCODE_ATTRIBUTE_${XCODE_PROPERTY}[variant=${XCODE_RELVERSION_I}] "${XCODE_VALUE}") + endif() +endmacro(set_xcode_property) + +# This macro lets you find executable programs on the host system. +macro(find_host_package) + set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) + set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY NEVER) + set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE NEVER) + set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE NEVER) + set(_TOOLCHAIN_IOS ${IOS}) + set(IOS OFF) + find_package(${ARGN}) + set(IOS ${_TOOLCHAIN_IOS}) + set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM BOTH) + set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH) + set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH) + set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE BOTH) +endmacro(find_host_package) diff --git a/runtime/ios/CMakeLists.txt b/runtime/ios/CMakeLists.txt new file mode 100644 index 000000000..3fd20cd62 --- /dev/null +++ b/runtime/ios/CMakeLists.txt @@ -0,0 +1,59 @@ +cmake_minimum_required(VERSION 3.14 FATAL_ERROR) + +project(wenet VERSION 0.1) + +option(CXX11_ABI "whether to use CXX11_ABI libtorch" OFF) +option(GRAPH_TOOLS "whether to build TLG graph tools" OFF) +option(BUILD_TESTING "whether to build unit test" ON) + +option(GRPC "whether to build with gRPC" OFF) +# TODO(Binbin Zhang): Change websocket to OFF since it depends on boost +# which is a very big library +option(WEBSOCKET "whether to build with websocket" ON) +option(TORCH "whether to build with Torch" ON) +option(ONNX "whether to build with ONNX" OFF) +option(GPU "whether to build with GPU" OFF) + +set(CMAKE_VERBOSE_MAKEFILE OFF) + +include(FetchContent) +set(FETCHCONTENT_QUIET OFF) +get_filename_component(fc_base "fc_base" REALPATH BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}") +set(FETCHCONTENT_BASE_DIR ${fc_base}) + +list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) + +if(NOT MSVC) + # Keep the same with openfst, -fPIC or -fpic + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -pthread -fPIC") +else() + set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) + add_compile_options("$<$:/utf-8>") +endif() + +# Include all dependency +if(TORCH) + include(libtorch) +endif() +if(ONNX) + include(onnx) +endif() +include(openfst) +include_directories( + ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/kaldi + ${CMAKE_CURRENT_SOURCE_DIR}/build/Pods/LibTorch/install/include +) + +# Build all libraries +add_subdirectory(utils) +add_subdirectory(frontend) +add_subdirectory(post_processor) +add_subdirectory(kaldi) # kaldi: wfst based decoder +add_subdirectory(decoder) + +# Unit Test +if(BUILD_TESTING) + include(gtest) + add_subdirectory(test) +endif() diff --git a/runtime/ios/README.md b/runtime/ios/README.md new file mode 100644 index 000000000..1a9f091de --- /dev/null +++ b/runtime/ios/README.md @@ -0,0 +1,34 @@ +# WeNet On-device ASR iOS Demo + +## Build application from source code + +### 1) Generate cmake project, install LibTorch pod and build static libraries + +``` +cd runtime/ios/build +cmake .. -G Xcode -DTORCH=ON -DONNX=OFF -DIOS=ON -DGRAPH_TOOLS=OFF -DBUILD_TESTING=OFF -DCMAKE_TOOLCHAIN_FILE=../toolchains/ios.toolchain.cmake -DPLATFORM=OS64 -DENABLE_BITCODE=FALSE +pod install + +# Build debug version +cmake --build . --config Debug + +# Build release version +cmake --build . --config Release +``` + +### 2) Build and run iOS application + +You can use our pretrained model (click the following link to download): + +[AISHELL-1](https://wenet-1256283475.cos.ap-shanghai.myqcloud.com/models/aishell/20210601_u2%2B%2B_conformer_libtorch.tar.gz) +| [AISHELL-2](https://wenet-1256283475.cos.ap-shanghai.myqcloud.com/models/aishell2/20210618_u2pp_conformer_libtorch.tar.gz) +| [GigaSpeech](https://wenet-1256283475.cos.ap-shanghai.myqcloud.com/models/gigaspeech/20210728_u2pp_conformer_libtorch.tar.gz) +| [LibriSpeech](https://wenet-1256283475.cos.ap-shanghai.myqcloud.com/models/librispeech/20210610_u2pp_conformer_libtorch.tar.gz) +| [Multi-CN](https://wenet-1256283475.cos.ap-shanghai.myqcloud.com/models/multi_cn/20210815_unified_conformer_libtorch.tar.gz) + + +Or you can train your own model using WeNet training pipeline on your data. + +When your model is ready, put `final.zip` and `units.txt` into model (`WenetDemo/WenetDemo/model`) folder. + +Open WenetDemo.xcodeproj with Xcode, build and run on iOS device. diff --git a/runtime/ios/WenetDemo/WenetDemo.xcodeproj/project.pbxproj b/runtime/ios/WenetDemo/WenetDemo.xcodeproj/project.pbxproj new file mode 100644 index 000000000..f39d10f70 --- /dev/null +++ b/runtime/ios/WenetDemo/WenetDemo.xcodeproj/project.pbxproj @@ -0,0 +1,563 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 56; + objects = { + +/* Begin PBXBuildFile section */ + 374FEB34291019EE00513F88 /* libeigen_blas.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 37FD7E1E2910198A00853C23 /* libeigen_blas.a */; }; + 37C64C892912A94B00BFCE0B /* final.zip in Resources */ = {isa = PBXBuildFile; fileRef = 37C64C882912A94A00BFCE0B /* final.zip */; }; + 37C64C8B2912ABE900BFCE0B /* units.txt in Resources */ = {isa = PBXBuildFile; fileRef = 37C64C8A2912ABE900BFCE0B /* units.txt */; }; + 37C64C8F2912ADDD00BFCE0B /* libfst.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 37C64C8E2912ADDD00BFCE0B /* libfst.a */; }; + 37C64C942912AF7F00BFCE0B /* libkaldi-decoder.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 37C64C8C2912AD3300BFCE0B /* libkaldi-decoder.a */; }; + 37C64C962912AFC900BFCE0B /* libpost_processor.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 37C64C952912AFC900BFCE0B /* libpost_processor.a */; }; + 37C64C982912B06800BFCE0B /* libutils.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 37C64C972912B06800BFCE0B /* libutils.a */; }; + 37C64C9A2912B0A000BFCE0B /* libfrontend.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 37C64C992912B0A000BFCE0B /* libfrontend.a */; }; + 37FD7E032910094300853C23 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37FD7E022910094300853C23 /* AppDelegate.swift */; }; + 37FD7E052910094300853C23 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37FD7E042910094300853C23 /* SceneDelegate.swift */; }; + 37FD7E072910094300853C23 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37FD7E062910094300853C23 /* ViewController.swift */; }; + 37FD7E0A2910094300853C23 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 37FD7E082910094300853C23 /* Main.storyboard */; }; + 37FD7E0C2910094500853C23 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 37FD7E0B2910094500853C23 /* Assets.xcassets */; }; + 37FD7E0F2910094500853C23 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 37FD7E0D2910094500853C23 /* LaunchScreen.storyboard */; }; + 37FD7E1A291009EC00853C23 /* wenet.mm in Sources */ = {isa = PBXBuildFile; fileRef = 37FD7E18291009EC00853C23 /* wenet.mm */; }; + 37FD7E1D2910195900853C23 /* libdecoder.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 37FD7E1C2910195900853C23 /* libdecoder.a */; }; + 37FD7E282910198B00853C23 /* libpthreadpool.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 37FD7E1F2910198B00853C23 /* libpthreadpool.a */; }; + 37FD7E292910198B00853C23 /* libc10.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 37FD7E202910198B00853C23 /* libc10.a */; }; + 37FD7E2A2910198B00853C23 /* libtorch_cpu.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 37FD7E212910198B00853C23 /* libtorch_cpu.a */; }; + 37FD7E2B2910198B00853C23 /* libtorch.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 37FD7E222910198B00853C23 /* libtorch.a */; }; + 37FD7E2C2910198B00853C23 /* libclog.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 37FD7E232910198B00853C23 /* libclog.a */; }; + 37FD7E2D2910198B00853C23 /* libcpuinfo.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 37FD7E242910198B00853C23 /* libcpuinfo.a */; }; + 37FD7E2E2910198C00853C23 /* libXNNPACK.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 37FD7E252910198B00853C23 /* libXNNPACK.a */; }; + 37FD7E2F2910198C00853C23 /* libpytorch_qnnpack.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 37FD7E262910198B00853C23 /* libpytorch_qnnpack.a */; }; +/* End PBXBuildFile section */ + +/* Begin PBXFileReference section */ + 373FDD93291BE53D00DF4BEA /* libglog.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libglog.a; path = "../fc_base/glog-build/Release-iphoneos/libglog.a"; sourceTree = ""; }; + 373FDD95291BE5A800DF4BEA /* libgflags_nothreads.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libgflags_nothreads.a; path = "../fc_base/gflags-build/Release-iphoneos/libgflags_nothreads.a"; sourceTree = ""; }; + 37C64C882912A94A00BFCE0B /* final.zip */ = {isa = PBXFileReference; lastKnownFileType = archive.zip; path = final.zip; sourceTree = ""; }; + 37C64C8A2912ABE900BFCE0B /* units.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = units.txt; sourceTree = ""; }; + 37C64C8C2912AD3300BFCE0B /* libkaldi-decoder.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = "libkaldi-decoder.a"; path = "../build/kaldi/Debug-iphoneos/libkaldi-decoder.a"; sourceTree = ""; }; + 37C64C8E2912ADDD00BFCE0B /* libfst.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libfst.a; path = "../fc_base/openfst-build/src/lib/Debug-iphoneos/libfst.a"; sourceTree = ""; }; + 37C64C902912AE2B00BFCE0B /* libgflags_nothreads_debug.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libgflags_nothreads_debug.a; path = "../fc_base/gflags-build/Debug-iphoneos/libgflags_nothreads_debug.a"; sourceTree = ""; }; + 37C64C922912AE7E00BFCE0B /* libglogd.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libglogd.a; path = "../fc_base/glog-build/Debug-iphoneos/libglogd.a"; sourceTree = ""; }; + 37C64C952912AFC900BFCE0B /* libpost_processor.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libpost_processor.a; path = "../build/post_processor/Debug-iphoneos/libpost_processor.a"; sourceTree = ""; }; + 37C64C972912B06800BFCE0B /* libutils.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libutils.a; path = "../build/utils/Debug-iphoneos/libutils.a"; sourceTree = ""; }; + 37C64C992912B0A000BFCE0B /* libfrontend.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libfrontend.a; path = "../build/frontend/Debug-iphoneos/libfrontend.a"; sourceTree = ""; }; + 37FD7DFF2910094300853C23 /* WenetDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = WenetDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; + 37FD7E022910094300853C23 /* AppDelegate.swift */ = {isa = PBXFileReference; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; tabWidth = 2; }; + 37FD7E042910094300853C23 /* SceneDelegate.swift */ = {isa = PBXFileReference; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; tabWidth = 2; }; + 37FD7E062910094300853C23 /* ViewController.swift */ = {isa = PBXFileReference; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; tabWidth = 2; }; + 37FD7E092910094300853C23 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; + 37FD7E0B2910094500853C23 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; + 37FD7E0E2910094500853C23 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; + 37FD7E102910094500853C23 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 37FD7E17291009EC00853C23 /* WenetDemo-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "WenetDemo-Bridging-Header.h"; sourceTree = ""; }; + 37FD7E18291009EC00853C23 /* wenet.mm */ = {isa = PBXFileReference; indentWidth = 2; lastKnownFileType = sourcecode.cpp.objcpp; path = wenet.mm; sourceTree = ""; tabWidth = 2; }; + 37FD7E19291009EC00853C23 /* wenet.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = wenet.h; sourceTree = ""; }; + 37FD7E1C2910195900853C23 /* libdecoder.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libdecoder.a; path = "../wenet/runtime/ios/build/decoder/Debug-iphoneos/libdecoder.a"; sourceTree = ""; }; + 37FD7E1E2910198A00853C23 /* libeigen_blas.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libeigen_blas.a; path = ../wenet/runtime/ios/build/Pods/LibTorch/install/lib/libeigen_blas.a; sourceTree = ""; }; + 37FD7E1F2910198B00853C23 /* libpthreadpool.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libpthreadpool.a; path = ../wenet/runtime/ios/build/Pods/LibTorch/install/lib/libpthreadpool.a; sourceTree = ""; }; + 37FD7E202910198B00853C23 /* libc10.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libc10.a; path = ../wenet/runtime/ios/build/Pods/LibTorch/install/lib/libc10.a; sourceTree = ""; }; + 37FD7E212910198B00853C23 /* libtorch_cpu.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libtorch_cpu.a; path = ../wenet/runtime/ios/build/Pods/LibTorch/install/lib/libtorch_cpu.a; sourceTree = ""; }; + 37FD7E222910198B00853C23 /* libtorch.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libtorch.a; path = ../wenet/runtime/ios/build/Pods/LibTorch/install/lib/libtorch.a; sourceTree = ""; }; + 37FD7E232910198B00853C23 /* libclog.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libclog.a; path = ../wenet/runtime/ios/build/Pods/LibTorch/install/lib/libclog.a; sourceTree = ""; }; + 37FD7E242910198B00853C23 /* libcpuinfo.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libcpuinfo.a; path = ../wenet/runtime/ios/build/Pods/LibTorch/install/lib/libcpuinfo.a; sourceTree = ""; }; + 37FD7E252910198B00853C23 /* libXNNPACK.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libXNNPACK.a; path = ../wenet/runtime/ios/build/Pods/LibTorch/install/lib/libXNNPACK.a; sourceTree = ""; }; + 37FD7E262910198B00853C23 /* libpytorch_qnnpack.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libpytorch_qnnpack.a; path = ../wenet/runtime/ios/build/Pods/LibTorch/install/lib/libpytorch_qnnpack.a; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 37FD7DFC2910094300853C23 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + 37C64C9A2912B0A000BFCE0B /* libfrontend.a in Frameworks */, + 37C64C982912B06800BFCE0B /* libutils.a in Frameworks */, + 37C64C962912AFC900BFCE0B /* libpost_processor.a in Frameworks */, + 37C64C942912AF7F00BFCE0B /* libkaldi-decoder.a in Frameworks */, + 37C64C8F2912ADDD00BFCE0B /* libfst.a in Frameworks */, + 374FEB34291019EE00513F88 /* libeigen_blas.a in Frameworks */, + 37FD7E282910198B00853C23 /* libpthreadpool.a in Frameworks */, + 37FD7E292910198B00853C23 /* libc10.a in Frameworks */, + 37FD7E2A2910198B00853C23 /* libtorch_cpu.a in Frameworks */, + 37FD7E2B2910198B00853C23 /* libtorch.a in Frameworks */, + 37FD7E2C2910198B00853C23 /* libclog.a in Frameworks */, + 37FD7E2D2910198B00853C23 /* libcpuinfo.a in Frameworks */, + 37FD7E2E2910198C00853C23 /* libXNNPACK.a in Frameworks */, + 37FD7E2F2910198C00853C23 /* libpytorch_qnnpack.a in Frameworks */, + 37FD7E1D2910195900853C23 /* libdecoder.a in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 37C64C812912A21300BFCE0B /* model */ = { + isa = PBXGroup; + children = ( + 37C64C8A2912ABE900BFCE0B /* units.txt */, + 37C64C882912A94A00BFCE0B /* final.zip */, + ); + path = model; + sourceTree = ""; + }; + 37FD7DF62910094300853C23 = { + isa = PBXGroup; + children = ( + 37FD7E012910094300853C23 /* WenetDemo */, + 37FD7E002910094300853C23 /* Products */, + 37FD7E1B2910195900853C23 /* Frameworks */, + ); + sourceTree = ""; + }; + 37FD7E002910094300853C23 /* Products */ = { + isa = PBXGroup; + children = ( + 37FD7DFF2910094300853C23 /* WenetDemo.app */, + ); + name = Products; + sourceTree = ""; + }; + 37FD7E012910094300853C23 /* WenetDemo */ = { + isa = PBXGroup; + children = ( + 37C64C812912A21300BFCE0B /* model */, + 37FD7E16291009B900853C23 /* wenet */, + 37FD7E022910094300853C23 /* AppDelegate.swift */, + 37FD7E042910094300853C23 /* SceneDelegate.swift */, + 37FD7E062910094300853C23 /* ViewController.swift */, + 37FD7E082910094300853C23 /* Main.storyboard */, + 37FD7E0B2910094500853C23 /* Assets.xcassets */, + 37FD7E0D2910094500853C23 /* LaunchScreen.storyboard */, + 37FD7E102910094500853C23 /* Info.plist */, + ); + path = WenetDemo; + sourceTree = ""; + }; + 37FD7E16291009B900853C23 /* wenet */ = { + isa = PBXGroup; + children = ( + 37FD7E18291009EC00853C23 /* wenet.mm */, + 37FD7E19291009EC00853C23 /* wenet.h */, + 37FD7E17291009EC00853C23 /* WenetDemo-Bridging-Header.h */, + ); + path = wenet; + sourceTree = ""; + }; + 37FD7E1B2910195900853C23 /* Frameworks */ = { + isa = PBXGroup; + children = ( + 373FDD95291BE5A800DF4BEA /* libgflags_nothreads.a */, + 373FDD93291BE53D00DF4BEA /* libglog.a */, + 37C64C992912B0A000BFCE0B /* libfrontend.a */, + 37C64C972912B06800BFCE0B /* libutils.a */, + 37C64C952912AFC900BFCE0B /* libpost_processor.a */, + 37C64C922912AE7E00BFCE0B /* libglogd.a */, + 37C64C902912AE2B00BFCE0B /* libgflags_nothreads_debug.a */, + 37C64C8E2912ADDD00BFCE0B /* libfst.a */, + 37C64C8C2912AD3300BFCE0B /* libkaldi-decoder.a */, + 37FD7E202910198B00853C23 /* libc10.a */, + 37FD7E232910198B00853C23 /* libclog.a */, + 37FD7E242910198B00853C23 /* libcpuinfo.a */, + 37FD7E1E2910198A00853C23 /* libeigen_blas.a */, + 37FD7E1F2910198B00853C23 /* libpthreadpool.a */, + 37FD7E262910198B00853C23 /* libpytorch_qnnpack.a */, + 37FD7E212910198B00853C23 /* libtorch_cpu.a */, + 37FD7E222910198B00853C23 /* libtorch.a */, + 37FD7E252910198B00853C23 /* libXNNPACK.a */, + 37FD7E1C2910195900853C23 /* libdecoder.a */, + ); + name = Frameworks; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 37FD7DFE2910094300853C23 /* WenetDemo */ = { + isa = PBXNativeTarget; + buildConfigurationList = 37FD7E132910094500853C23 /* Build configuration list for PBXNativeTarget "WenetDemo" */; + buildPhases = ( + 37FD7DFB2910094300853C23 /* Sources */, + 37FD7DFC2910094300853C23 /* Frameworks */, + 37FD7DFD2910094300853C23 /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = WenetDemo; + productName = WenetDemo; + productReference = 37FD7DFF2910094300853C23 /* WenetDemo.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 37FD7DF72910094300853C23 /* Project object */ = { + isa = PBXProject; + attributes = { + BuildIndependentTargetsInParallel = 1; + LastSwiftUpdateCheck = 1400; + LastUpgradeCheck = 1400; + TargetAttributes = { + 37FD7DFE2910094300853C23 = { + CreatedOnToolsVersion = 14.0.1; + LastSwiftMigration = 1400; + }; + }; + }; + buildConfigurationList = 37FD7DFA2910094300853C23 /* Build configuration list for PBXProject "WenetDemo" */; + compatibilityVersion = "Xcode 14.0"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = 37FD7DF62910094300853C23; + productRefGroup = 37FD7E002910094300853C23 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 37FD7DFE2910094300853C23 /* WenetDemo */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + 37FD7DFD2910094300853C23 /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 37C64C8B2912ABE900BFCE0B /* units.txt in Resources */, + 37FD7E0F2910094500853C23 /* LaunchScreen.storyboard in Resources */, + 37FD7E0C2910094500853C23 /* Assets.xcassets in Resources */, + 37C64C892912A94B00BFCE0B /* final.zip in Resources */, + 37FD7E0A2910094300853C23 /* Main.storyboard in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + 37FD7DFB2910094300853C23 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 37FD7E072910094300853C23 /* ViewController.swift in Sources */, + 37FD7E1A291009EC00853C23 /* wenet.mm in Sources */, + 37FD7E032910094300853C23 /* AppDelegate.swift in Sources */, + 37FD7E052910094300853C23 /* SceneDelegate.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin PBXVariantGroup section */ + 37FD7E082910094300853C23 /* Main.storyboard */ = { + isa = PBXVariantGroup; + children = ( + 37FD7E092910094300853C23 /* Base */, + ); + name = Main.storyboard; + sourceTree = ""; + }; + 37FD7E0D2910094500853C23 /* LaunchScreen.storyboard */ = { + isa = PBXVariantGroup; + children = ( + 37FD7E0E2910094500853C23 /* Base */, + ); + name = LaunchScreen.storyboard; + sourceTree = ""; + }; +/* End PBXVariantGroup section */ + +/* Begin XCBuildConfiguration section */ + 37FD7E112910094500853C23 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 16.0; + MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; + MTL_FAST_MATH = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = iphoneos; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + 37FD7E122910094500853C23 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 16.0; + MTL_ENABLE_DEBUG_INFO = NO; + MTL_FAST_MATH = YES; + SDKROOT = iphoneos; + SWIFT_COMPILATION_MODE = wholemodule; + SWIFT_OPTIMIZATION_LEVEL = "-O"; + VALIDATE_PRODUCT = YES; + }; + name = Release; + }; + 37FD7E142910094500853C23 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; + CLANG_CXX_LANGUAGE_STANDARD = "c++14"; + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_STYLE = Automatic; + CURRENT_PROJECT_VERSION = 1; + DEVELOPMENT_TEAM = LXGRKDMEQU; + GCC_C_LANGUAGE_STANDARD = gnu11; + GENERATE_INFOPLIST_FILE = YES; + HEADER_SEARCH_PATHS = ( + ../, + "../fc_base/openfst-src/src/include", + "../fc_base/gflags-build/include", + "../fc_base/glog-build", + "../fc_base/glog-src/src", + ../kaldi, + ../build/Pods/LibTorch/install/include, + ); + INFOPLIST_FILE = WenetDemo/Info.plist; + INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; + INFOPLIST_KEY_UILaunchStoryboardName = LaunchScreen; + INFOPLIST_KEY_UIMainStoryboardFile = Main; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + IPHONEOS_DEPLOYMENT_TARGET = 14.6; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + LIBRARY_SEARCH_PATHS = ( + "../build/decoder/Debug-iphoneos", + ../build/Pods/LibTorch/install/lib, + "../fc_base/openfst-build/src/lib/Debug-iphoneos", + "../fc_base/gflags-build/Debug-iphoneos", + "../fc_base/glog-build/Debug-iphoneos", + "../build/kaldi/Debug-iphoneos", + "../build/post_processor/Debug-iphoneos", + "../build/utils/Debug-iphoneos", + "../build/frontend/Debug-iphoneos", + ); + MARKETING_VERSION = 1.0; + OTHER_LDFLAGS = ( + "-ObjC", + "-l\"XNNPACK\"", + "-l\"c++\"", + "-l\"c10\"", + "-l\"clog\"", + "-l\"cpuinfo\"", + "-l\"eigen_blas\"", + "-l\"pthreadpool\"", + "-l\"pytorch_qnnpack\"", + "-l\"stdc++\"", + "-l\"torch\"", + "-l\"torch_cpu\"", + "-force_load", + ../build/Pods/LibTorch/install/lib/libtorch.a, + "-force_load", + ../build/Pods/LibTorch/install/lib/libtorch_cpu.a, + "-l\"gflags_nothreads_debug\"", + "-l\"glogd\"", + ); + PRODUCT_BUNDLE_IDENTIFIER = com.wenet.WenetDemo; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_EMIT_LOC_STRINGS = YES; + SWIFT_OBJC_BRIDGING_HEADER = "WenetDemo/wenet/WenetDemo-Bridging-Header.h"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Debug; + }; + 37FD7E152910094500853C23 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; + CLANG_CXX_LANGUAGE_STANDARD = "c++14"; + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_STYLE = Automatic; + CURRENT_PROJECT_VERSION = 1; + DEVELOPMENT_TEAM = LXGRKDMEQU; + GCC_C_LANGUAGE_STANDARD = gnu11; + GENERATE_INFOPLIST_FILE = YES; + HEADER_SEARCH_PATHS = ( + ../, + "../fc_base/openfst-src/src/include", + "../fc_base/gflags-build/include", + "../fc_base/glog-build", + "../fc_base/glog-src/src", + ../kaldi, + ../build/Pods/LibTorch/install/include, + ); + INFOPLIST_FILE = WenetDemo/Info.plist; + INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; + INFOPLIST_KEY_UILaunchStoryboardName = LaunchScreen; + INFOPLIST_KEY_UIMainStoryboardFile = Main; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + IPHONEOS_DEPLOYMENT_TARGET = 14.6; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + LIBRARY_SEARCH_PATHS = ( + "../build/decoder/Release-iphoneos", + ../build/Pods/LibTorch/install/lib, + "../fc_base/openfst-build/src/lib/Release-iphoneos", + "../fc_base/gflags-build/Release-iphoneos", + "../fc_base/glog-build/Release-iphoneos", + "../build/kaldi/Release-iphoneos", + "../build/post_processor/Release-iphoneos", + "../build/utils/Release-iphoneos", + "../build/frontend/Release-iphoneos", + ); + MARKETING_VERSION = 1.0; + OTHER_LDFLAGS = ( + "-ObjC", + "-l\"XNNPACK\"", + "-l\"c++\"", + "-l\"c10\"", + "-l\"clog\"", + "-l\"cpuinfo\"", + "-l\"eigen_blas\"", + "-l\"pthreadpool\"", + "-l\"pytorch_qnnpack\"", + "-l\"stdc++\"", + "-l\"torch\"", + "-l\"torch_cpu\"", + "-force_load", + ../build/Pods/LibTorch/install/lib/libtorch.a, + "-force_load", + ../build/Pods/LibTorch/install/lib/libtorch_cpu.a, + "-l\"gflags_nothreads\"", + "-l\"glog\"", + ); + PRODUCT_BUNDLE_IDENTIFIER = com.wenet.WenetDemo; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_EMIT_LOC_STRINGS = YES; + SWIFT_OBJC_BRIDGING_HEADER = "WenetDemo/wenet/WenetDemo-Bridging-Header.h"; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 37FD7DFA2910094300853C23 /* Build configuration list for PBXProject "WenetDemo" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 37FD7E112910094500853C23 /* Debug */, + 37FD7E122910094500853C23 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 37FD7E132910094500853C23 /* Build configuration list for PBXNativeTarget "WenetDemo" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 37FD7E142910094500853C23 /* Debug */, + 37FD7E152910094500853C23 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = 37FD7DF72910094300853C23 /* Project object */; +} diff --git a/runtime/ios/WenetDemo/WenetDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/runtime/ios/WenetDemo/WenetDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 000000000..919434a62 --- /dev/null +++ b/runtime/ios/WenetDemo/WenetDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/runtime/ios/WenetDemo/WenetDemo.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/runtime/ios/WenetDemo/WenetDemo.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 000000000..3d4c1e552 --- /dev/null +++ b/runtime/ios/WenetDemo/WenetDemo.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/runtime/ios/WenetDemo/WenetDemo/AppDelegate.swift b/runtime/ios/WenetDemo/WenetDemo/AppDelegate.swift new file mode 100644 index 000000000..19ae3160e --- /dev/null +++ b/runtime/ios/WenetDemo/WenetDemo/AppDelegate.swift @@ -0,0 +1,58 @@ +// Copyright (c) 2022 Dan Ma (1067837450@qq.com) +// +// AppDelegate.swift +// WenetDemo +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import UIKit + +@main +class AppDelegate: UIResponder, UIApplicationDelegate { + + + + func application(_ application: UIApplication, didFinishLaunchingWithOptions + launchOptions: [UIApplication.LaunchOptionsKey: Any]?) + -> Bool { + // Override point for customization after application launch. + return true + } + + // MARK: UISceneSession Lifecycle + + func application(_ application: UIApplication, configurationForConnecting + connectingSceneSession: UISceneSession, + options: UIScene.ConnectionOptions) + -> UISceneConfiguration { + // Called when a new scene session is being created. + // Use this method to select a configuration + // to create the new scene with. + return UISceneConfiguration(name: "Default Configuration", + sessionRole: connectingSceneSession.role) + } + + func application(_ application: UIApplication, + didDiscardSceneSessions + sceneSessions: Set) { + // Called when the user discards a scene session. + // If any sessions were discarded while the application was not running, + // this will be called shortly after + // application:didFinishLaunchingWithOptions. + // Use this method to release any resources that were specific to + // the discarded scenes, as they will not return. + } + + +} + diff --git a/runtime/ios/WenetDemo/WenetDemo/Assets.xcassets/AccentColor.colorset/Contents.json b/runtime/ios/WenetDemo/WenetDemo/Assets.xcassets/AccentColor.colorset/Contents.json new file mode 100644 index 000000000..eb8789700 --- /dev/null +++ b/runtime/ios/WenetDemo/WenetDemo/Assets.xcassets/AccentColor.colorset/Contents.json @@ -0,0 +1,11 @@ +{ + "colors" : [ + { + "idiom" : "universal" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/runtime/ios/WenetDemo/WenetDemo/Assets.xcassets/AppIcon.appiconset/Contents.json b/runtime/ios/WenetDemo/WenetDemo/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 000000000..13613e3ee --- /dev/null +++ b/runtime/ios/WenetDemo/WenetDemo/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,13 @@ +{ + "images" : [ + { + "idiom" : "universal", + "platform" : "ios", + "size" : "1024x1024" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/runtime/ios/WenetDemo/WenetDemo/Assets.xcassets/Contents.json b/runtime/ios/WenetDemo/WenetDemo/Assets.xcassets/Contents.json new file mode 100644 index 000000000..73c00596a --- /dev/null +++ b/runtime/ios/WenetDemo/WenetDemo/Assets.xcassets/Contents.json @@ -0,0 +1,6 @@ +{ + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/runtime/ios/WenetDemo/WenetDemo/Base.lproj/LaunchScreen.storyboard b/runtime/ios/WenetDemo/WenetDemo/Base.lproj/LaunchScreen.storyboard new file mode 100644 index 000000000..865e9329f --- /dev/null +++ b/runtime/ios/WenetDemo/WenetDemo/Base.lproj/LaunchScreen.storyboard @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/runtime/ios/WenetDemo/WenetDemo/Base.lproj/Main.storyboard b/runtime/ios/WenetDemo/WenetDemo/Base.lproj/Main.storyboard new file mode 100644 index 000000000..24468cc4f --- /dev/null +++ b/runtime/ios/WenetDemo/WenetDemo/Base.lproj/Main.storyboard @@ -0,0 +1,55 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/runtime/ios/WenetDemo/WenetDemo/Info.plist b/runtime/ios/WenetDemo/WenetDemo/Info.plist new file mode 100644 index 000000000..16ad1537f --- /dev/null +++ b/runtime/ios/WenetDemo/WenetDemo/Info.plist @@ -0,0 +1,27 @@ + + + + + UIApplicationSceneManifest + + UIApplicationSupportsMultipleScenes + + UISceneConfigurations + + UIWindowSceneSessionRoleApplication + + + UISceneConfigurationName + Default Configuration + UISceneDelegateClassName + $(PRODUCT_MODULE_NAME).SceneDelegate + UISceneStoryboardFile + Main + + + + + NSMicrophoneUsageDescription + Need microphone access for recording speech + + diff --git a/runtime/ios/WenetDemo/WenetDemo/SceneDelegate.swift b/runtime/ios/WenetDemo/WenetDemo/SceneDelegate.swift new file mode 100644 index 000000000..1c61b7853 --- /dev/null +++ b/runtime/ios/WenetDemo/WenetDemo/SceneDelegate.swift @@ -0,0 +1,67 @@ +// Copyright (c) 2022 Dan Ma (1067837450@qq.com) +// +// SceneDelegate.swift +// WenetDemo +// + +import UIKit + +class SceneDelegate: UIResponder, UIWindowSceneDelegate { + + var window: UIWindow? + + + func scene(_ scene: UIScene, willConnectTo session: UISceneSession, + options connectionOptions: UIScene.ConnectionOptions) { + // Use this method to optionally configure and attach the UIWindow + // `window` to the provided UIWindowScene `scene`. + // If using a storyboard, the `window` property will + // automatically be initialized and attached to the scene. + // This delegate does not imply the connecting scene or session + // are new (see + // `application:configurationForConnectingSceneSession` instead). + guard let _ = (scene as? UIWindowScene) else { return } + } + + func sceneDidDisconnect(_ scene: UIScene) { + // Called as the scene is being released by the system. + // This occurs shortly after the scene enters the background, or when + // its session is discarded. + // Release any resources associated with this scene that can be + // re-created the next time the scene connects. + // The scene may re-connect later, as its session was not necessarily + // discarded (see `application:didDiscardSceneSessions` instead). + } + + func sceneDidBecomeActive(_ scene: UIScene) { + // Called when the scene has moved from an inactive state + // to an active state. + // Use this method to restart any tasks that were + // paused (or not yet started) when the scene was inactive. + } + + func sceneWillResignActive(_ scene: UIScene) { + // Called when the scene will move from an active state to + // an inactive state. + // This may occur due to temporary interruptions + // (ex. an incoming phone call). + } + + func sceneWillEnterForeground(_ scene: UIScene) { + // Called as the scene transitions from the background + // to the foreground. + // Use this method to undo the changes made on + // entering the background. + } + + func sceneDidEnterBackground(_ scene: UIScene) { + // Called as the scene transitions from the foreground to + // the background. + // Use this method to save data, release shared resources, + // and store enough scene-specific state information + // to restore the scene back to its current state. + } + + +} + diff --git a/runtime/ios/WenetDemo/WenetDemo/ViewController.swift b/runtime/ios/WenetDemo/WenetDemo/ViewController.swift new file mode 100644 index 000000000..707073a2d --- /dev/null +++ b/runtime/ios/WenetDemo/WenetDemo/ViewController.swift @@ -0,0 +1,143 @@ +// Copyright (c) 2022 Dan Ma (1067837450@qq.com) +// +// ViewController.swift +// WenetDemo +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import UIKit +import AVFoundation + +class ViewController: UIViewController { + + @IBOutlet weak var label: UILabel! + @IBOutlet weak var button: UIButton! + + var wenetModel: Wenet? + var audioEngine: AVAudioEngine? + var startRecord: Bool? + private var workItem: DispatchWorkItem? + + override func viewDidLoad() { + super.viewDidLoad() + // Do any additional setup after loading the view. + + initModel() + + initRecorder() + } + + func initModel() { + let modelPath = Bundle.main.path(forResource: "final", ofType: "zip") + let dictPath = Bundle.main.path(forResource: "units", ofType: "txt") + wenetModel = Wenet(modelPath:modelPath, dictPath:dictPath)! + + wenetModel?.reset() + } + + func initRecorder() { + startRecord = false + + audioEngine = AVAudioEngine() + let inputNode = self.audioEngine?.inputNode + let bus = 0 + let inputFormat = inputNode?.outputFormat(forBus: bus) + let outputFormat = AVAudioFormat(commonFormat: .pcmFormatFloat32, + sampleRate: 16000, channels: 1, + interleaved: false)! + let converter = AVAudioConverter(from: inputFormat!, to: outputFormat)! + inputNode!.installTap(onBus: bus, + bufferSize: 1024, + format: inputFormat) { + (buffer: AVAudioPCMBuffer, when: AVAudioTime) in + var newBufferAvailable = true + + let inputCallback: AVAudioConverterInputBlock = { + inNumPackets, outStatus in + if newBufferAvailable { + outStatus.pointee = .haveData + newBufferAvailable = false + + return buffer + } else { + outStatus.pointee = .noDataNow + return nil + } + } + + let convertedBuffer = AVAudioPCMBuffer( + pcmFormat: outputFormat, + frameCapacity: + AVAudioFrameCount(outputFormat.sampleRate) + * buffer.frameLength + / AVAudioFrameCount(buffer.format.sampleRate))! + + var error: NSError? + let status = converter.convert( + to: convertedBuffer, + error: &error, withInputFrom: inputCallback) + + // 16000 Hz buffer + let actualSampleCount = Int(convertedBuffer.frameLength) + guard let floatChannelData = convertedBuffer.floatChannelData + else { return } + + self.wenetModel?.acceptWaveForm(floatChannelData[0], + Int32(actualSampleCount)) + } + } + + @IBAction func btnClicked(_ sender: Any) { + if(!startRecord!) { + //Clear result + self.setResult(text: "") + + //Reset model + self.wenetModel?.reset() + + //Start record + do { + try self.audioEngine?.start() + } catch let error as NSError { + print("Got an error starting audioEngine: \(error.domain), \(error)") + } + + //Start decode thread + workItem = DispatchWorkItem { + while(!self.workItem!.isCancelled) { + self.wenetModel?.decode() + DispatchQueue.main.sync { + self.setResult(text: (self.wenetModel?.get_result())!) + } + } + } + DispatchQueue.global().async(execute: workItem!) + + startRecord = true + button.setTitle("Stop Record", for: UIControl.State.normal) + } else { + //Stop record + self.audioEngine?.stop() + + //Stop decode thread + workItem!.cancel() + + startRecord = false + button.setTitle("Start Record", for: UIControl.State.normal) + } + } + + @objc func setResult(text: String) { + label.text = text + } +} diff --git a/runtime/ios/WenetDemo/WenetDemo/model/.gitkeep b/runtime/ios/WenetDemo/WenetDemo/model/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/runtime/ios/WenetDemo/WenetDemo/wenet/WenetDemo-Bridging-Header.h b/runtime/ios/WenetDemo/WenetDemo/wenet/WenetDemo-Bridging-Header.h new file mode 100644 index 000000000..5cec9898b --- /dev/null +++ b/runtime/ios/WenetDemo/WenetDemo/wenet/WenetDemo-Bridging-Header.h @@ -0,0 +1,23 @@ +// Copyright (c) 2022 Dan Ma (1067837450@qq.com) +// +// Use this file to import your target's public headers +// that you would like to expose to Swift. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef RUNTIME_IOS_WENETDEMO_WENETDEMO_WENET_WENETDEMO_BRIDGING_HEADER_H_ +#define RUNTIME_IOS_WENETDEMO_WENETDEMO_WENET_WENETDEMO_BRIDGING_HEADER_H_ + +#import "wenet.h" + +#endif // RUNTIME_IOS_WENETDEMO_WENETDEMO_WENET_WENETDEMO_BRIDGING_HEADER_H_ diff --git a/runtime/ios/WenetDemo/WenetDemo/wenet/wenet.h b/runtime/ios/WenetDemo/WenetDemo/wenet/wenet.h new file mode 100644 index 000000000..0d430e357 --- /dev/null +++ b/runtime/ios/WenetDemo/WenetDemo/wenet/wenet.h @@ -0,0 +1,40 @@ +// Copyright (c) 2022 Dan Ma (1067837450@qq.com) +// +// wenet.h +// WenetDemo +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef RUNTIME_IOS_WENETDEMO_WENETDEMO_WENET_WENET_H_ +#define RUNTIME_IOS_WENETDEMO_WENETDEMO_WENET_WENET_H_ + +#include + +#import + +@interface Wenet : NSObject + +- (nullable instancetype)initWithModelPath: +(NSString*)modelPath DictPath:(NSString*)dictPath; // NOLINT + +- (void)reset; + +- (void)acceptWaveForm: (float*)pcm: (int)size; // NOLINT + +- (void)decode; + +- (NSString*)get_result; // NOLINT + +@end + +#endif // RUNTIME_IOS_WENETDEMO_WENETDEMO_WENET_WENET_H_ diff --git a/runtime/ios/WenetDemo/WenetDemo/wenet/wenet.mm b/runtime/ios/WenetDemo/WenetDemo/wenet/wenet.mm new file mode 100644 index 000000000..bab9a085c --- /dev/null +++ b/runtime/ios/WenetDemo/WenetDemo/wenet/wenet.mm @@ -0,0 +1,135 @@ +// Copyright (c) 2022 Dan Ma (1067837450@qq.com) +// +// wenet.mm +// WenetDemo +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "wenet.h" + +#define IOS + +#include "decoder/asr_decoder.h" +#include "decoder/torch_asr_model.h" +#include "frontend/feature_pipeline.h" +#include "frontend/wav.h" +#include "post_processor/post_processor.h" +#include "utils/log.h" +#include "utils/string.h" + +using namespace wenet; + +@implementation Wenet { +@protected + std::shared_ptr decode_config; + std::shared_ptr feature_config; + std::shared_ptr feature_pipeline; + std::shared_ptr decoder; + std::shared_ptr resource; + DecodeState state; + std::string total_result; +} + +- (nullable instancetype)initWithModelPath: +(NSString*)modelPath DictPath:(NSString*)dictPath { + self = [super init]; + if (self) { + try { + auto qengines = at::globalContext().supportedQEngines(); + if (std::find(qengines.begin(), qengines.end(), at::QEngine::QNNPACK) + != qengines.end()) { + at::globalContext().setQEngine(at::QEngine::QNNPACK); + } + auto model = std::make_shared(); + model->Read(modelPath.UTF8String); + resource = std::make_shared(); + resource->model = model; + resource->symbol_table = std::shared_ptr + (fst::SymbolTable::ReadText(dictPath.UTF8String)); + + PostProcessOptions post_process_opts; + resource->post_processor = + std::make_shared(post_process_opts); + + feature_config = std::make_shared(80, 16000); + feature_pipeline = std::make_shared(*feature_config); + + decode_config = std::make_shared(); + decode_config->chunk_size = 16; + decoder = std::make_shared(feature_pipeline, + resource, + *decode_config); + + state = kEndBatch; + } catch (const std::exception& exception) { + NSLog(@"%s", exception.what()); + return nil; + } + } + + return self; +} + +- (void)reset { + decoder->Reset(); + state = kEndBatch; + total_result = ""; +} + +- (void)acceptWaveForm: (float*)pcm: (int)size { + auto* float_pcm = new float[size]; + for (size_t i = 0; i < size; i++) { + float_pcm[i] = pcm[i] * 65535; + } + feature_pipeline->AcceptWaveform(float_pcm, size); +} + +- (void)decode { + state = decoder->Decode(); + if (state == kEndFeats || state == kEndpoint) { + decoder->Rescoring(); + } + + std::string result; + if (decoder->DecodedSomething()) { + result = decoder->result()[0].sentence; + } + + if (state == kEndFeats) { + LOG(INFO) << "wenet endfeats final result: " << result; + NSLog(@"wenet endfeats final result: %s", result.c_str()); + total_result += result; + } else if (state == kEndpoint) { + LOG(INFO) << "wenet endpoint final result: " << result; + NSLog(@"wenet endpoint final result: %s", result.c_str()); + total_result += result + ","; + decoder->ResetContinuousDecoding(); + } else { + if (decoder->DecodedSomething()) { + LOG(INFO) << "wenet partial result: " << result; + NSLog(@"wenet partial result: %s", result.c_str()); + } + } +} + +- (NSString*)get_result { + std::string result; + if (decoder->DecodedSomething()) { + result = decoder->result()[0].sentence; + } + LOG(INFO) << "wenet ui result: " << total_result + result; + NSLog(@"wenet ui result: %s", (total_result + result).c_str()); + return [NSString stringWithUTF8String:(total_result + result).c_str()]; +} + +@end diff --git a/runtime/ios/build/Podfile b/runtime/ios/build/Podfile new file mode 100644 index 000000000..f839cc082 --- /dev/null +++ b/runtime/ios/build/Podfile @@ -0,0 +1,2 @@ +platform :ios, '14.3' +pod 'LibTorch', '~>1.9.0' diff --git a/runtime/ios/cmake b/runtime/ios/cmake new file mode 120000 index 000000000..17afee87d --- /dev/null +++ b/runtime/ios/cmake @@ -0,0 +1 @@ +../core/cmake \ No newline at end of file diff --git a/runtime/ios/decoder b/runtime/ios/decoder new file mode 120000 index 000000000..3088ea48b --- /dev/null +++ b/runtime/ios/decoder @@ -0,0 +1 @@ +../core/decoder \ No newline at end of file diff --git a/runtime/ios/frontend b/runtime/ios/frontend new file mode 120000 index 000000000..0292335d1 --- /dev/null +++ b/runtime/ios/frontend @@ -0,0 +1 @@ +../core/frontend \ No newline at end of file diff --git a/runtime/ios/kaldi b/runtime/ios/kaldi new file mode 120000 index 000000000..764a9d445 --- /dev/null +++ b/runtime/ios/kaldi @@ -0,0 +1 @@ +../core/kaldi \ No newline at end of file diff --git a/runtime/ios/patch b/runtime/ios/patch new file mode 120000 index 000000000..69789fa5e --- /dev/null +++ b/runtime/ios/patch @@ -0,0 +1 @@ +../core/patch \ No newline at end of file diff --git a/runtime/ios/post_processor b/runtime/ios/post_processor new file mode 120000 index 000000000..4e434a5cc --- /dev/null +++ b/runtime/ios/post_processor @@ -0,0 +1 @@ +../core/post_processor \ No newline at end of file diff --git a/runtime/ios/test b/runtime/ios/test new file mode 120000 index 000000000..e60cf87a7 --- /dev/null +++ b/runtime/ios/test @@ -0,0 +1 @@ +../core/test \ No newline at end of file diff --git a/runtime/ios/toolchains b/runtime/ios/toolchains new file mode 120000 index 000000000..c4b2ab76f --- /dev/null +++ b/runtime/ios/toolchains @@ -0,0 +1 @@ +../core/toolchains \ No newline at end of file diff --git a/runtime/ios/utils b/runtime/ios/utils new file mode 120000 index 000000000..9e19e7af5 --- /dev/null +++ b/runtime/ios/utils @@ -0,0 +1 @@ +../core/utils \ No newline at end of file