From 94ef6929bbab7391e6f35c2a935efa54abdf6574 Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Thu, 23 Nov 2023 21:38:32 +0800 Subject: [PATCH] Text-to-speech for iOS (#443) --- .gitignore | 7 + .../SherpaOnnxTts.xcodeproj/project.pbxproj | 374 ++++++++++++++++++ .../contents.xcworkspacedata | 7 + .../xcshareddata/IDEWorkspaceChecks.plist | 8 + .../AccentColor.colorset/Contents.json | 11 + .../AppIcon.appiconset/Contents.json | 13 + .../Assets.xcassets/Contents.json | 6 + .../SherpaOnnxTts/ContentView.swift | 95 +++++ .../Preview Assets.xcassets/Contents.json | 6 + .../SherpaOnnxTts/SherpaOnnxTtsApp.swift | 17 + .../SherpaOnnxTts/ViewModel.swift | 68 ++++ swift-api-examples/SherpaOnnx.swift | 4 +- 12 files changed, 614 insertions(+), 2 deletions(-) create mode 100644 ios-swiftui/SherpaOnnxTts/SherpaOnnxTts.xcodeproj/project.pbxproj create mode 100644 ios-swiftui/SherpaOnnxTts/SherpaOnnxTts.xcodeproj/project.xcworkspace/contents.xcworkspacedata create mode 100644 ios-swiftui/SherpaOnnxTts/SherpaOnnxTts.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist create mode 100644 ios-swiftui/SherpaOnnxTts/SherpaOnnxTts/Assets.xcassets/AccentColor.colorset/Contents.json create mode 100644 ios-swiftui/SherpaOnnxTts/SherpaOnnxTts/Assets.xcassets/AppIcon.appiconset/Contents.json create mode 100644 ios-swiftui/SherpaOnnxTts/SherpaOnnxTts/Assets.xcassets/Contents.json create mode 100644 ios-swiftui/SherpaOnnxTts/SherpaOnnxTts/ContentView.swift create mode 100644 ios-swiftui/SherpaOnnxTts/SherpaOnnxTts/Preview Content/Preview Assets.xcassets/Contents.json create mode 100644 ios-swiftui/SherpaOnnxTts/SherpaOnnxTts/SherpaOnnxTtsApp.swift create mode 100644 ios-swiftui/SherpaOnnxTts/SherpaOnnxTts/ViewModel.swift diff --git a/.gitignore b/.gitignore index 256ff406e..0b429133d 100644 --- a/.gitignore +++ b/.gitignore @@ -67,6 +67,13 @@ swift-api-examples/k2fsa-* run-*.sh two-pass-*.sh build-* + +## User settings +xcuserdata/ + +## Xcode 8 and earlier +*.xcscmblueprint +*.xccheckout vits-vctk vits-zh-aishell3 jslint.mjs diff --git a/ios-swiftui/SherpaOnnxTts/SherpaOnnxTts.xcodeproj/project.pbxproj b/ios-swiftui/SherpaOnnxTts/SherpaOnnxTts.xcodeproj/project.pbxproj new file mode 100644 index 000000000..4a36fb64e --- /dev/null +++ b/ios-swiftui/SherpaOnnxTts/SherpaOnnxTts.xcodeproj/project.pbxproj @@ -0,0 +1,374 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 56; + objects = { + +/* Begin PBXBuildFile section */ + C917B4E52B0EEF3B005245AC /* SherpaOnnxTtsApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = C917B4E42B0EEF3B005245AC /* SherpaOnnxTtsApp.swift */; }; + C917B4E72B0EEF3B005245AC /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C917B4E62B0EEF3B005245AC /* ContentView.swift */; }; + C917B4E92B0EEF3C005245AC /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C917B4E82B0EEF3C005245AC /* Assets.xcassets */; }; + C917B4EC2B0EEF3C005245AC /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C917B4EB2B0EEF3C005245AC /* Preview Assets.xcassets */; }; + C9FE9FE52B0F33CD009F1003 /* ViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9FE9FE42B0F33CD009F1003 /* ViewModel.swift */; }; + C9FE9FE72B0F3620009F1003 /* SherpaOnnx.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9FE9FE62B0F3620009F1003 /* SherpaOnnx.swift */; }; + C9FE9FEA2B0F3754009F1003 /* sherpa-onnx.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = C9FE9FE92B0F3754009F1003 /* sherpa-onnx.xcframework */; }; + C9FE9FEF2B0F3EFB009F1003 /* onnxruntime.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = C9FE9FEB2B0F3785009F1003 /* onnxruntime.xcframework */; }; +/* End PBXBuildFile section */ + +/* Begin PBXFileReference section */ + C917B4E12B0EEF3B005245AC /* SherpaOnnxTts.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SherpaOnnxTts.app; sourceTree = BUILT_PRODUCTS_DIR; }; + C917B4E42B0EEF3B005245AC /* SherpaOnnxTtsApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SherpaOnnxTtsApp.swift; sourceTree = ""; }; + C917B4E62B0EEF3B005245AC /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; + C917B4E82B0EEF3C005245AC /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; + C917B4EB2B0EEF3C005245AC /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = ""; }; + C9FE9FE42B0F33CD009F1003 /* ViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewModel.swift; sourceTree = ""; }; + C9FE9FE62B0F3620009F1003 /* SherpaOnnx.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SherpaOnnx.swift; path = "../../../swift-api-examples/SherpaOnnx.swift"; sourceTree = ""; }; + C9FE9FE92B0F3754009F1003 /* sherpa-onnx.xcframework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcframework; name = "sherpa-onnx.xcframework"; path = "../../build-ios/sherpa-onnx.xcframework"; sourceTree = ""; }; + C9FE9FEB2B0F3785009F1003 /* onnxruntime.xcframework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcframework; name = onnxruntime.xcframework; path = "../../build-ios/ios-onnxruntime/1.16.2/onnxruntime.xcframework"; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + C917B4DE2B0EEF3B005245AC /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + C9FE9FEF2B0F3EFB009F1003 /* onnxruntime.xcframework in Frameworks */, + C9FE9FEA2B0F3754009F1003 /* sherpa-onnx.xcframework in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + C917B4D82B0EEF3B005245AC = { + isa = PBXGroup; + children = ( + C917B4E32B0EEF3B005245AC /* SherpaOnnxTts */, + C917B4E22B0EEF3B005245AC /* Products */, + C9FE9FE82B0F3754009F1003 /* Frameworks */, + ); + sourceTree = ""; + }; + C917B4E22B0EEF3B005245AC /* Products */ = { + isa = PBXGroup; + children = ( + C917B4E12B0EEF3B005245AC /* SherpaOnnxTts.app */, + ); + name = Products; + sourceTree = ""; + }; + C917B4E32B0EEF3B005245AC /* SherpaOnnxTts */ = { + isa = PBXGroup; + children = ( + C9FE9FE62B0F3620009F1003 /* SherpaOnnx.swift */, + C9FE9FE42B0F33CD009F1003 /* ViewModel.swift */, + C917B4E42B0EEF3B005245AC /* SherpaOnnxTtsApp.swift */, + C917B4E62B0EEF3B005245AC /* ContentView.swift */, + C917B4E82B0EEF3C005245AC /* Assets.xcassets */, + C917B4EA2B0EEF3C005245AC /* Preview Content */, + ); + path = SherpaOnnxTts; + sourceTree = ""; + }; + C917B4EA2B0EEF3C005245AC /* Preview Content */ = { + isa = PBXGroup; + children = ( + C917B4EB2B0EEF3C005245AC /* Preview Assets.xcassets */, + ); + path = "Preview Content"; + sourceTree = ""; + }; + C9FE9FE82B0F3754009F1003 /* Frameworks */ = { + isa = PBXGroup; + children = ( + C9FE9FEB2B0F3785009F1003 /* onnxruntime.xcframework */, + C9FE9FE92B0F3754009F1003 /* sherpa-onnx.xcframework */, + ); + name = Frameworks; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + C917B4E02B0EEF3B005245AC /* SherpaOnnxTts */ = { + isa = PBXNativeTarget; + buildConfigurationList = C917B4EF2B0EEF3C005245AC /* Build configuration list for PBXNativeTarget "SherpaOnnxTts" */; + buildPhases = ( + C917B4DD2B0EEF3B005245AC /* Sources */, + C917B4DE2B0EEF3B005245AC /* Frameworks */, + C917B4DF2B0EEF3B005245AC /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = SherpaOnnxTts; + productName = SherpaOnnxTts; + productReference = C917B4E12B0EEF3B005245AC /* SherpaOnnxTts.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + C917B4D92B0EEF3B005245AC /* Project object */ = { + isa = PBXProject; + attributes = { + BuildIndependentTargetsInParallel = 1; + LastSwiftUpdateCheck = 1420; + LastUpgradeCheck = 1420; + TargetAttributes = { + C917B4E02B0EEF3B005245AC = { + CreatedOnToolsVersion = 14.2; + }; + }; + }; + buildConfigurationList = C917B4DC2B0EEF3B005245AC /* Build configuration list for PBXProject "SherpaOnnxTts" */; + compatibilityVersion = "Xcode 14.0"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = C917B4D82B0EEF3B005245AC; + productRefGroup = C917B4E22B0EEF3B005245AC /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + C917B4E02B0EEF3B005245AC /* SherpaOnnxTts */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + C917B4DF2B0EEF3B005245AC /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + C917B4EC2B0EEF3C005245AC /* Preview Assets.xcassets in Resources */, + C917B4E92B0EEF3C005245AC /* Assets.xcassets in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + C917B4DD2B0EEF3B005245AC /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + C917B4E72B0EEF3B005245AC /* ContentView.swift in Sources */, + C9FE9FE72B0F3620009F1003 /* SherpaOnnx.swift in Sources */, + C9FE9FE52B0F33CD009F1003 /* ViewModel.swift in Sources */, + C917B4E52B0EEF3B005245AC /* SherpaOnnxTtsApp.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + C917B4ED2B0EEF3C005245AC /* 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.2; + 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; + }; + C917B4EE2B0EEF3C005245AC /* 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.2; + MTL_ENABLE_DEBUG_INFO = NO; + MTL_FAST_MATH = YES; + SDKROOT = iphoneos; + SWIFT_COMPILATION_MODE = wholemodule; + SWIFT_OPTIMIZATION_LEVEL = "-O"; + VALIDATE_PRODUCT = YES; + }; + name = Release; + }; + C917B4F02B0EEF3C005245AC /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; + CODE_SIGN_STYLE = Automatic; + CURRENT_PROJECT_VERSION = 1; + DEVELOPMENT_ASSET_PATHS = "\"SherpaOnnxTts/Preview Content\""; + ENABLE_PREVIEWS = YES; + FRAMEWORK_SEARCH_PATHS = "${PROJECT_DIR}/../../build-ios"; + GENERATE_INFOPLIST_FILE = YES; + HEADER_SEARCH_PATHS = "${PROJECT_DIR}/../../build-ios/sherpa-onnx.xcframework/Headers"; + INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; + INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; + INFOPLIST_KEY_UILaunchScreen_Generation = YES; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + MARKETING_VERSION = 1.0; + OTHER_LDFLAGS = "-lc++"; + PRODUCT_BUNDLE_IDENTIFIER = "com.k2-fsa.org.SherpaOnnxTts"; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_EMIT_LOC_STRINGS = YES; + SWIFT_OBJC_BRIDGING_HEADER = "${PROJECT_DIR}/../../swift-api-examples/SherpaOnnx-Bridging-Header.h"; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Debug; + }; + C917B4F12B0EEF3C005245AC /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; + CODE_SIGN_STYLE = Automatic; + CURRENT_PROJECT_VERSION = 1; + DEVELOPMENT_ASSET_PATHS = "\"SherpaOnnxTts/Preview Content\""; + ENABLE_PREVIEWS = YES; + FRAMEWORK_SEARCH_PATHS = "${PROJECT_DIR}/../../build-ios"; + GENERATE_INFOPLIST_FILE = YES; + HEADER_SEARCH_PATHS = "${PROJECT_DIR}/../../build-ios/sherpa-onnx.xcframework/Headers"; + INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; + INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; + INFOPLIST_KEY_UILaunchScreen_Generation = YES; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + MARKETING_VERSION = 1.0; + OTHER_LDFLAGS = "-lc++"; + PRODUCT_BUNDLE_IDENTIFIER = "com.k2-fsa.org.SherpaOnnxTts"; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_EMIT_LOC_STRINGS = YES; + SWIFT_OBJC_BRIDGING_HEADER = "${PROJECT_DIR}/../../swift-api-examples/SherpaOnnx-Bridging-Header.h"; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + C917B4DC2B0EEF3B005245AC /* Build configuration list for PBXProject "SherpaOnnxTts" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + C917B4ED2B0EEF3C005245AC /* Debug */, + C917B4EE2B0EEF3C005245AC /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + C917B4EF2B0EEF3C005245AC /* Build configuration list for PBXNativeTarget "SherpaOnnxTts" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + C917B4F02B0EEF3C005245AC /* Debug */, + C917B4F12B0EEF3C005245AC /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = C917B4D92B0EEF3B005245AC /* Project object */; +} diff --git a/ios-swiftui/SherpaOnnxTts/SherpaOnnxTts.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/ios-swiftui/SherpaOnnxTts/SherpaOnnxTts.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 000000000..919434a62 --- /dev/null +++ b/ios-swiftui/SherpaOnnxTts/SherpaOnnxTts.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/ios-swiftui/SherpaOnnxTts/SherpaOnnxTts.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/ios-swiftui/SherpaOnnxTts/SherpaOnnxTts.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 000000000..18d981003 --- /dev/null +++ b/ios-swiftui/SherpaOnnxTts/SherpaOnnxTts.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/ios-swiftui/SherpaOnnxTts/SherpaOnnxTts/Assets.xcassets/AccentColor.colorset/Contents.json b/ios-swiftui/SherpaOnnxTts/SherpaOnnxTts/Assets.xcassets/AccentColor.colorset/Contents.json new file mode 100644 index 000000000..eb8789700 --- /dev/null +++ b/ios-swiftui/SherpaOnnxTts/SherpaOnnxTts/Assets.xcassets/AccentColor.colorset/Contents.json @@ -0,0 +1,11 @@ +{ + "colors" : [ + { + "idiom" : "universal" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/ios-swiftui/SherpaOnnxTts/SherpaOnnxTts/Assets.xcassets/AppIcon.appiconset/Contents.json b/ios-swiftui/SherpaOnnxTts/SherpaOnnxTts/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 000000000..13613e3ee --- /dev/null +++ b/ios-swiftui/SherpaOnnxTts/SherpaOnnxTts/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/ios-swiftui/SherpaOnnxTts/SherpaOnnxTts/Assets.xcassets/Contents.json b/ios-swiftui/SherpaOnnxTts/SherpaOnnxTts/Assets.xcassets/Contents.json new file mode 100644 index 000000000..73c00596a --- /dev/null +++ b/ios-swiftui/SherpaOnnxTts/SherpaOnnxTts/Assets.xcassets/Contents.json @@ -0,0 +1,6 @@ +{ + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/ios-swiftui/SherpaOnnxTts/SherpaOnnxTts/ContentView.swift b/ios-swiftui/SherpaOnnxTts/SherpaOnnxTts/ContentView.swift new file mode 100644 index 000000000..debc8cb5e --- /dev/null +++ b/ios-swiftui/SherpaOnnxTts/SherpaOnnxTts/ContentView.swift @@ -0,0 +1,95 @@ +// +// ContentView.swift +// SherpaOnnxTts +// +// Created by fangjun on 2023/11/23. +// +// Speech-to-text with Next-gen Kaldi on iOS without Internet connection + +import SwiftUI +import AVFoundation + +struct ContentView: View { + @State private var sid = "0" + @State private var speed = 1.0 + @State private var text = "" + @State private var showAlert = false + @State var filename: URL = NSURL() as URL + @State var audioPlayer: AVAudioPlayer! + + private var tts = createOfflineTts() + + var body: some View { + + VStack(alignment: .leading) { + HStack { + Spacer() + Text("Next-gen Kaldi: TTS").font(.title) + Spacer() + } + HStack{ + Text("Speaker ID") + TextField("Please input a speaker ID", text: $sid).textFieldStyle(.roundedBorder) + .keyboardType(.numberPad) + } + HStack{ + Text("Speed \(String(format: "%.1f", speed))") + .padding(.trailing) + Slider(value: $speed, in: 0.5...2.0, step: 0.1) { + Text("Speech speed") + } + } + + Text("Please input your text below").padding([.trailing, .top, .bottom]) + + TextEditor(text: $text) + .font(.body) + .opacity(self.text.isEmpty ? 0.25 : 1) + .disableAutocorrection(true) + .border(Color.black) + + Spacer() + HStack { + Spacer() + Button(action: { + let speakerId = Int(self.sid) ?? 0 + let t = self.text.trimmingCharacters(in: .whitespacesAndNewlines) + if t.isEmpty { + self.showAlert = true + return + } + + let audio = tts.generate(text: t, sid: speakerId, speed: Float(self.speed)) + if self.filename.absoluteString.isEmpty { + let tempDirectoryURL = NSURL.fileURL(withPath: NSTemporaryDirectory(), isDirectory: true) + self.filename = tempDirectoryURL.appendingPathComponent("test.wav") + } + + let ret = audio.save(filename: filename.path) + + self.audioPlayer = try! AVAudioPlayer(contentsOf: filename) + self.audioPlayer.play() + }) { + Text("Generate") + }.alert(isPresented: $showAlert) { + Alert(title: Text("Empty text"), message: Text("Please input your text before clicking the Generate button")) + } + Spacer() + Button (action: { + self.audioPlayer.play() + }) { + Text("Play") + }.disabled(filename.absoluteString.isEmpty) + Spacer() + } + Spacer() + } + .padding() + } +} + +struct ContentView_Previews: PreviewProvider { + static var previews: some View { + ContentView() + } +} diff --git a/ios-swiftui/SherpaOnnxTts/SherpaOnnxTts/Preview Content/Preview Assets.xcassets/Contents.json b/ios-swiftui/SherpaOnnxTts/SherpaOnnxTts/Preview Content/Preview Assets.xcassets/Contents.json new file mode 100644 index 000000000..73c00596a --- /dev/null +++ b/ios-swiftui/SherpaOnnxTts/SherpaOnnxTts/Preview Content/Preview Assets.xcassets/Contents.json @@ -0,0 +1,6 @@ +{ + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/ios-swiftui/SherpaOnnxTts/SherpaOnnxTts/SherpaOnnxTtsApp.swift b/ios-swiftui/SherpaOnnxTts/SherpaOnnxTts/SherpaOnnxTtsApp.swift new file mode 100644 index 000000000..bd0be8dab --- /dev/null +++ b/ios-swiftui/SherpaOnnxTts/SherpaOnnxTts/SherpaOnnxTtsApp.swift @@ -0,0 +1,17 @@ +// +// SherpaOnnxTtsApp.swift +// SherpaOnnxTts +// +// Created by fangjun on 2023/11/23. +// + +import SwiftUI + +@main +struct SherpaOnnxTtsApp: App { + var body: some Scene { + WindowGroup { + ContentView() + } + } +} diff --git a/ios-swiftui/SherpaOnnxTts/SherpaOnnxTts/ViewModel.swift b/ios-swiftui/SherpaOnnxTts/SherpaOnnxTts/ViewModel.swift new file mode 100644 index 000000000..a42ecc78b --- /dev/null +++ b/ios-swiftui/SherpaOnnxTts/SherpaOnnxTts/ViewModel.swift @@ -0,0 +1,68 @@ +// +// ViewModel.swift +// SherpaOnnxTts +// +// Created by fangjun on 2023/11/23. +// + +import Foundation + +func getResource(_ forResource: String, _ ofType: String) -> String { + let path = Bundle.main.path(forResource: forResource, ofType: ofType) + precondition( + path != nil, + "\(forResource).\(ofType) does not exist!\n" + "Remember to change \n" + + " Build Phases -> Copy Bundle Resources\n" + "to add it!" + ) + return path! +} + +/// Please refer to +/// https://k2-fsa.github.io/sherpa/onnx/tts/pretrained_models/index.html +/// to download pre-trained models + +func getTtsForVCTK() -> SherpaOnnxOfflineTtsWrapper { + // See the following link + // https://k2-fsa.github.io/sherpa/onnx/tts/pretrained_models/vits.html#vctk-english-multi-speaker-109-speakers + + // vits-vctk.onnx + let model = getResource("vits-vctk", "onnx") + + // lexicon.txt + let lexicon = getResource("lexicon", "txt") + + // tokens.txt + let tokens = getResource("tokens", "txt") + + let vits = sherpaOnnxOfflineTtsVitsModelConfig(model: model, lexicon: lexicon, tokens: tokens) + let modelConfig = sherpaOnnxOfflineTtsModelConfig(vits: vits) + var config = sherpaOnnxOfflineTtsConfig(model: modelConfig) + return SherpaOnnxOfflineTtsWrapper(config: &config) +} + +func getTtsForAishell3() -> SherpaOnnxOfflineTtsWrapper { + // See the following link + // https://k2-fsa.github.io/sherpa/onnx/tts/pretrained_models/vits.html#vits-model-aishell3 + + // vits-vctk.onnx + let model = getResource("vits-aishell3", "onnx") + + // lexicon.txt + let lexicon = getResource("lexicon", "txt") + + // tokens.txt + let tokens = getResource("tokens", "txt") + + let vits = sherpaOnnxOfflineTtsVitsModelConfig(model: model, lexicon: lexicon, tokens: tokens) + let modelConfig = sherpaOnnxOfflineTtsModelConfig(vits: vits) + var config = sherpaOnnxOfflineTtsConfig(model: modelConfig) + return SherpaOnnxOfflineTtsWrapper(config: &config) +} + +func createOfflineTts() -> SherpaOnnxOfflineTtsWrapper { + return getTtsForVCTK() + + // return getTtsForAishell3() + + // please add more models on need by following the above two examples +} diff --git a/swift-api-examples/SherpaOnnx.swift b/swift-api-examples/SherpaOnnx.swift index 206e95bde..2b310b039 100644 --- a/swift-api-examples/SherpaOnnx.swift +++ b/swift-api-examples/SherpaOnnx.swift @@ -650,8 +650,8 @@ class SherpaOnnxGeneratedAudioWrapper { } } - func save(filename: String) { - SherpaOnnxWriteWave(audio.pointee.samples, n, sampleRate, toCPointer(filename)) + func save(filename: String) -> Int32 { + return SherpaOnnxWriteWave(audio.pointee.samples, n, sampleRate, toCPointer(filename)) } }