From bb1ee266d23ac9567a97ca551d4e36dcd6b62868 Mon Sep 17 00:00:00 2001 From: Georgi Gerganov Date: Sun, 23 Oct 2022 11:10:15 +0300 Subject: [PATCH] ios : whisper.objc example --- .gitignore | 4 + .../whisper.objc.xcodeproj/project.pbxproj | 382 ++++++++++++++++++ .../contents.xcworkspacedata | 7 + .../xcshareddata/IDEWorkspaceChecks.plist | 8 + .../whisper.objc/whisper.objc/AppDelegate.h | 14 + .../whisper.objc/whisper.objc/AppDelegate.m | 40 ++ .../AccentColor.colorset/Contents.json | 11 + .../AppIcon.appiconset/Contents.json | 13 + .../Assets.xcassets/Contents.json | 6 + .../Base.lproj/LaunchScreen.storyboard | 25 ++ .../whisper.objc/Base.lproj/Main.storyboard | 88 ++++ examples/whisper.objc/whisper.objc/Info.plist | 27 ++ .../whisper.objc/whisper.objc/SceneDelegate.h | 15 + .../whisper.objc/whisper.objc/SceneDelegate.m | 57 +++ .../whisper.objc/ViewController.h | 41 ++ .../whisper.objc/ViewController.m | 229 +++++++++++ examples/whisper.objc/whisper.objc/main.m | 18 + 17 files changed, 985 insertions(+) create mode 100644 examples/whisper.objc/whisper.objc.xcodeproj/project.pbxproj create mode 100644 examples/whisper.objc/whisper.objc.xcodeproj/project.xcworkspace/contents.xcworkspacedata create mode 100644 examples/whisper.objc/whisper.objc.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist create mode 100644 examples/whisper.objc/whisper.objc/AppDelegate.h create mode 100644 examples/whisper.objc/whisper.objc/AppDelegate.m create mode 100644 examples/whisper.objc/whisper.objc/Assets.xcassets/AccentColor.colorset/Contents.json create mode 100644 examples/whisper.objc/whisper.objc/Assets.xcassets/AppIcon.appiconset/Contents.json create mode 100644 examples/whisper.objc/whisper.objc/Assets.xcassets/Contents.json create mode 100644 examples/whisper.objc/whisper.objc/Base.lproj/LaunchScreen.storyboard create mode 100644 examples/whisper.objc/whisper.objc/Base.lproj/Main.storyboard create mode 100644 examples/whisper.objc/whisper.objc/Info.plist create mode 100644 examples/whisper.objc/whisper.objc/SceneDelegate.h create mode 100644 examples/whisper.objc/whisper.objc/SceneDelegate.m create mode 100644 examples/whisper.objc/whisper.objc/ViewController.h create mode 100644 examples/whisper.objc/whisper.objc/ViewController.m create mode 100644 examples/whisper.objc/whisper.objc/main.m diff --git a/.gitignore b/.gitignore index f7e302c..c86dd4f 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,7 @@ out/ .vscode/ compile_commands.json .DS_Store + +examples/whisper.objc/whisper.objc.xcodeproj/xcshareddata +examples/whisper.objc/whisper.objc.xcodeproj/xcuserdata/ +examples/whisper.objc/whisper.objc.xcodeproj/project.xcworkspace/xcuserdata diff --git a/examples/whisper.objc/whisper.objc.xcodeproj/project.pbxproj b/examples/whisper.objc/whisper.objc.xcodeproj/project.pbxproj new file mode 100644 index 0000000..bef9ce1 --- /dev/null +++ b/examples/whisper.objc/whisper.objc.xcodeproj/project.pbxproj @@ -0,0 +1,382 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 56; + objects = { + +/* Begin PBXBuildFile section */ + 18627C7B29052BDF00BD2A04 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 18627C7A29052BDF00BD2A04 /* AppDelegate.m */; }; + 18627C7E29052BDF00BD2A04 /* SceneDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 18627C7D29052BDF00BD2A04 /* SceneDelegate.m */; }; + 18627C8129052BDF00BD2A04 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 18627C8029052BDF00BD2A04 /* ViewController.m */; }; + 18627C8429052BDF00BD2A04 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 18627C8229052BDF00BD2A04 /* Main.storyboard */; }; + 18627C8629052BE000BD2A04 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 18627C8529052BE000BD2A04 /* Assets.xcassets */; }; + 18627C8929052BE000BD2A04 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 18627C8729052BE000BD2A04 /* LaunchScreen.storyboard */; }; + 18627C8C29052BE000BD2A04 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 18627C8B29052BE000BD2A04 /* main.m */; }; + 18627C9429052C4900BD2A04 /* whisper.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 18627C9329052C4900BD2A04 /* whisper.cpp */; }; + 18627C9629052C5800BD2A04 /* ggml.c in Sources */ = {isa = PBXBuildFile; fileRef = 18627C9529052C5800BD2A04 /* ggml.c */; settings = {COMPILER_FLAGS = "-DGGML_USE_ACCELERATE"; }; }; + 18627C9B29052CFF00BD2A04 /* ggml-base.en.bin in Resources */ = {isa = PBXBuildFile; fileRef = 18627C9A29052CFF00BD2A04 /* ggml-base.en.bin */; }; +/* End PBXBuildFile section */ + +/* Begin PBXFileReference section */ + 18627C7629052BDF00BD2A04 /* whisper.objc.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = whisper.objc.app; sourceTree = BUILT_PRODUCTS_DIR; }; + 18627C7929052BDF00BD2A04 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; + 18627C7A29052BDF00BD2A04 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; + 18627C7C29052BDF00BD2A04 /* SceneDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SceneDelegate.h; sourceTree = ""; }; + 18627C7D29052BDF00BD2A04 /* SceneDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SceneDelegate.m; sourceTree = ""; }; + 18627C7F29052BDF00BD2A04 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; + 18627C8029052BDF00BD2A04 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; + 18627C8329052BDF00BD2A04 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; + 18627C8529052BE000BD2A04 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; + 18627C8829052BE000BD2A04 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; + 18627C8A29052BE000BD2A04 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 18627C8B29052BE000BD2A04 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; + 18627C9229052C2B00BD2A04 /* whisper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = whisper.h; path = ../../../whisper.h; sourceTree = ""; }; + 18627C9329052C4900BD2A04 /* whisper.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = whisper.cpp; path = ../../../whisper.cpp; sourceTree = ""; }; + 18627C9529052C5800BD2A04 /* ggml.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = ggml.c; path = ../../../ggml.c; sourceTree = ""; }; + 18627C9729052C6600BD2A04 /* ggml.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ggml.h; path = ../../../ggml.h; sourceTree = ""; }; + 18627C9A29052CFF00BD2A04 /* ggml-base.en.bin */ = {isa = PBXFileReference; lastKnownFileType = archive.macbinary; name = "ggml-base.en.bin"; path = "../../../models/ggml-base.en.bin"; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 18627C7329052BDF00BD2A04 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 18627C6D29052BDF00BD2A04 = { + isa = PBXGroup; + children = ( + 18627C7829052BDF00BD2A04 /* whisper.objc */, + 18627C7729052BDF00BD2A04 /* Products */, + ); + sourceTree = ""; + }; + 18627C7729052BDF00BD2A04 /* Products */ = { + isa = PBXGroup; + children = ( + 18627C7629052BDF00BD2A04 /* whisper.objc.app */, + ); + name = Products; + sourceTree = ""; + }; + 18627C7829052BDF00BD2A04 /* whisper.objc */ = { + isa = PBXGroup; + children = ( + 18627C9A29052CFF00BD2A04 /* ggml-base.en.bin */, + 18627C9729052C6600BD2A04 /* ggml.h */, + 18627C9529052C5800BD2A04 /* ggml.c */, + 18627C9329052C4900BD2A04 /* whisper.cpp */, + 18627C9229052C2B00BD2A04 /* whisper.h */, + 18627C7929052BDF00BD2A04 /* AppDelegate.h */, + 18627C7A29052BDF00BD2A04 /* AppDelegate.m */, + 18627C7C29052BDF00BD2A04 /* SceneDelegate.h */, + 18627C7D29052BDF00BD2A04 /* SceneDelegate.m */, + 18627C7F29052BDF00BD2A04 /* ViewController.h */, + 18627C8029052BDF00BD2A04 /* ViewController.m */, + 18627C8229052BDF00BD2A04 /* Main.storyboard */, + 18627C8529052BE000BD2A04 /* Assets.xcassets */, + 18627C8729052BE000BD2A04 /* LaunchScreen.storyboard */, + 18627C8A29052BE000BD2A04 /* Info.plist */, + 18627C8B29052BE000BD2A04 /* main.m */, + ); + path = whisper.objc; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 18627C7529052BDF00BD2A04 /* whisper.objc */ = { + isa = PBXNativeTarget; + buildConfigurationList = 18627C8F29052BE000BD2A04 /* Build configuration list for PBXNativeTarget "whisper.objc" */; + buildPhases = ( + 18627C7229052BDF00BD2A04 /* Sources */, + 18627C7329052BDF00BD2A04 /* Frameworks */, + 18627C7429052BDF00BD2A04 /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = whisper.objc; + productName = whisper.objc; + productReference = 18627C7629052BDF00BD2A04 /* whisper.objc.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 18627C6E29052BDF00BD2A04 /* Project object */ = { + isa = PBXProject; + attributes = { + BuildIndependentTargetsInParallel = 1; + LastUpgradeCheck = 1400; + TargetAttributes = { + 18627C7529052BDF00BD2A04 = { + CreatedOnToolsVersion = 14.0.1; + }; + }; + }; + buildConfigurationList = 18627C7129052BDF00BD2A04 /* Build configuration list for PBXProject "whisper.objc" */; + compatibilityVersion = "Xcode 14.0"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = 18627C6D29052BDF00BD2A04; + productRefGroup = 18627C7729052BDF00BD2A04 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 18627C7529052BDF00BD2A04 /* whisper.objc */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + 18627C7429052BDF00BD2A04 /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 18627C8929052BE000BD2A04 /* LaunchScreen.storyboard in Resources */, + 18627C8629052BE000BD2A04 /* Assets.xcassets in Resources */, + 18627C8429052BDF00BD2A04 /* Main.storyboard in Resources */, + 18627C9B29052CFF00BD2A04 /* ggml-base.en.bin in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + 18627C7229052BDF00BD2A04 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 18627C8129052BDF00BD2A04 /* ViewController.m in Sources */, + 18627C9429052C4900BD2A04 /* whisper.cpp in Sources */, + 18627C9629052C5800BD2A04 /* ggml.c in Sources */, + 18627C7B29052BDF00BD2A04 /* AppDelegate.m in Sources */, + 18627C8C29052BE000BD2A04 /* main.m in Sources */, + 18627C7E29052BDF00BD2A04 /* SceneDelegate.m in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin PBXVariantGroup section */ + 18627C8229052BDF00BD2A04 /* Main.storyboard */ = { + isa = PBXVariantGroup; + children = ( + 18627C8329052BDF00BD2A04 /* Base */, + ); + name = Main.storyboard; + sourceTree = ""; + }; + 18627C8729052BE000BD2A04 /* LaunchScreen.storyboard */ = { + isa = PBXVariantGroup; + children = ( + 18627C8829052BE000BD2A04 /* Base */, + ); + name = LaunchScreen.storyboard; + sourceTree = ""; + }; +/* End PBXVariantGroup section */ + +/* Begin XCBuildConfiguration section */ + 18627C8D29052BE000BD2A04 /* 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; + }; + name = Debug; + }; + 18627C8E29052BE000BD2A04 /* 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; + VALIDATE_PRODUCT = YES; + }; + name = Release; + }; + 18627C9029052BE000BD2A04 /* 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_TEAM = P8JZH34X63; + GENERATE_INFOPLIST_FILE = YES; + INFOPLIST_FILE = whisper.objc/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"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + MARKETING_VERSION = 1.0; + PRODUCT_BUNDLE_IDENTIFIER = "com.ggerganov.whisper-objc"; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_EMIT_LOC_STRINGS = YES; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Debug; + }; + 18627C9129052BE000BD2A04 /* 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_TEAM = P8JZH34X63; + GENERATE_INFOPLIST_FILE = YES; + INFOPLIST_FILE = whisper.objc/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"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + MARKETING_VERSION = 1.0; + PRODUCT_BUNDLE_IDENTIFIER = "com.ggerganov.whisper-objc"; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_EMIT_LOC_STRINGS = YES; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 18627C7129052BDF00BD2A04 /* Build configuration list for PBXProject "whisper.objc" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 18627C8D29052BE000BD2A04 /* Debug */, + 18627C8E29052BE000BD2A04 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 18627C8F29052BE000BD2A04 /* Build configuration list for PBXNativeTarget "whisper.objc" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 18627C9029052BE000BD2A04 /* Debug */, + 18627C9129052BE000BD2A04 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = 18627C6E29052BDF00BD2A04 /* Project object */; +} diff --git a/examples/whisper.objc/whisper.objc.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/examples/whisper.objc/whisper.objc.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/examples/whisper.objc/whisper.objc.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/examples/whisper.objc/whisper.objc.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/examples/whisper.objc/whisper.objc.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/examples/whisper.objc/whisper.objc.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/examples/whisper.objc/whisper.objc/AppDelegate.h b/examples/whisper.objc/whisper.objc/AppDelegate.h new file mode 100644 index 0000000..a5c10cc --- /dev/null +++ b/examples/whisper.objc/whisper.objc/AppDelegate.h @@ -0,0 +1,14 @@ +// +// AppDelegate.h +// whisper.objc +// +// Created by Georgi Gerganov on 23.10.22. +// + +#import + +@interface AppDelegate : UIResponder + + +@end + diff --git a/examples/whisper.objc/whisper.objc/AppDelegate.m b/examples/whisper.objc/whisper.objc/AppDelegate.m new file mode 100644 index 0000000..913bffe --- /dev/null +++ b/examples/whisper.objc/whisper.objc/AppDelegate.m @@ -0,0 +1,40 @@ +// +// AppDelegate.m +// whisper.objc +// +// Created by Georgi Gerganov on 23.10.22. +// + +#import "AppDelegate.h" + +@interface AppDelegate () + +@end + +@implementation AppDelegate + + +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { + // Override point for customization after application launch. + return YES; +} + + +#pragma mark - UISceneSession lifecycle + + +- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options { + // Called when a new scene session is being created. + // Use this method to select a configuration to create the new scene with. + return [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role]; +} + + +- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet *)sceneSessions { + // 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. +} + + +@end diff --git a/examples/whisper.objc/whisper.objc/Assets.xcassets/AccentColor.colorset/Contents.json b/examples/whisper.objc/whisper.objc/Assets.xcassets/AccentColor.colorset/Contents.json new file mode 100644 index 0000000..eb87897 --- /dev/null +++ b/examples/whisper.objc/whisper.objc/Assets.xcassets/AccentColor.colorset/Contents.json @@ -0,0 +1,11 @@ +{ + "colors" : [ + { + "idiom" : "universal" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/examples/whisper.objc/whisper.objc/Assets.xcassets/AppIcon.appiconset/Contents.json b/examples/whisper.objc/whisper.objc/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 0000000..13613e3 --- /dev/null +++ b/examples/whisper.objc/whisper.objc/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/examples/whisper.objc/whisper.objc/Assets.xcassets/Contents.json b/examples/whisper.objc/whisper.objc/Assets.xcassets/Contents.json new file mode 100644 index 0000000..73c0059 --- /dev/null +++ b/examples/whisper.objc/whisper.objc/Assets.xcassets/Contents.json @@ -0,0 +1,6 @@ +{ + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/examples/whisper.objc/whisper.objc/Base.lproj/LaunchScreen.storyboard b/examples/whisper.objc/whisper.objc/Base.lproj/LaunchScreen.storyboard new file mode 100644 index 0000000..865e932 --- /dev/null +++ b/examples/whisper.objc/whisper.objc/Base.lproj/LaunchScreen.storyboard @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/whisper.objc/whisper.objc/Base.lproj/Main.storyboard b/examples/whisper.objc/whisper.objc/Base.lproj/Main.storyboard new file mode 100644 index 0000000..0079b25 --- /dev/null +++ b/examples/whisper.objc/whisper.objc/Base.lproj/Main.storyboard @@ -0,0 +1,88 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/whisper.objc/whisper.objc/Info.plist b/examples/whisper.objc/whisper.objc/Info.plist new file mode 100644 index 0000000..e17ebee --- /dev/null +++ b/examples/whisper.objc/whisper.objc/Info.plist @@ -0,0 +1,27 @@ + + + + + NSMicrophoneUsageDescription + This app requires microphone access in order to transcribe speech + UIApplicationSceneManifest + + UIApplicationSupportsMultipleScenes + + UISceneConfigurations + + UIWindowSceneSessionRoleApplication + + + UISceneConfigurationName + Default Configuration + UISceneDelegateClassName + SceneDelegate + UISceneStoryboardFile + Main + + + + + + diff --git a/examples/whisper.objc/whisper.objc/SceneDelegate.h b/examples/whisper.objc/whisper.objc/SceneDelegate.h new file mode 100644 index 0000000..75f3ea0 --- /dev/null +++ b/examples/whisper.objc/whisper.objc/SceneDelegate.h @@ -0,0 +1,15 @@ +// +// SceneDelegate.h +// whisper.objc +// +// Created by Georgi Gerganov on 23.10.22. +// + +#import + +@interface SceneDelegate : UIResponder + +@property (strong, nonatomic) UIWindow * window; + +@end + diff --git a/examples/whisper.objc/whisper.objc/SceneDelegate.m b/examples/whisper.objc/whisper.objc/SceneDelegate.m new file mode 100644 index 0000000..3cbd93e --- /dev/null +++ b/examples/whisper.objc/whisper.objc/SceneDelegate.m @@ -0,0 +1,57 @@ +// +// SceneDelegate.m +// whisper.objc +// +// Created by Georgi Gerganov on 23.10.22. +// + +#import "SceneDelegate.h" + +@interface SceneDelegate () + +@end + +@implementation SceneDelegate + + +- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)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). +} + + +- (void)sceneDidDisconnect:(UIScene *)scene { + // 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). +} + + +- (void)sceneDidBecomeActive:(UIScene *)scene { + // 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. +} + + +- (void)sceneWillResignActive:(UIScene *)scene { + // 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). +} + + +- (void)sceneWillEnterForeground:(UIScene *)scene { + // Called as the scene transitions from the background to the foreground. + // Use this method to undo the changes made on entering the background. +} + + +- (void)sceneDidEnterBackground:(UIScene *)scene { + // 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. +} + + +@end diff --git a/examples/whisper.objc/whisper.objc/ViewController.h b/examples/whisper.objc/whisper.objc/ViewController.h new file mode 100644 index 0000000..3595518 --- /dev/null +++ b/examples/whisper.objc/whisper.objc/ViewController.h @@ -0,0 +1,41 @@ +// +// ViewController.h +// whisper.objc +// +// Created by Georgi Gerganov on 23.10.22. +// + +#import + +#import +#import + +#define NUM_BUFFERS 3 +#define MAX_AUDIO_SEC 30 +#define SAMPLE_RATE 16000 + +struct whisper_context; + +typedef struct +{ + int ggwaveId; + bool isCapturing; + UILabel * labelReceived; + + AudioQueueRef queue; + AudioStreamBasicDescription dataFormat; + AudioQueueBufferRef buffers[NUM_BUFFERS]; + + int n_samples; + int16_t * audioBufferI16; + float * audioBufferF32; + + struct whisper_context * ctx; +} StateInp; + +@interface ViewController : UIViewController +{ + StateInp stateInp; +} + +@end diff --git a/examples/whisper.objc/whisper.objc/ViewController.m b/examples/whisper.objc/whisper.objc/ViewController.m new file mode 100644 index 0000000..6d3219b --- /dev/null +++ b/examples/whisper.objc/whisper.objc/ViewController.m @@ -0,0 +1,229 @@ +// +// ViewController.m +// whisper.objc +// +// Created by Georgi Gerganov on 23.10.22. +// + +#import "ViewController.h" + +#import "whisper.h" + +#define NUM_BYTES_PER_BUFFER 16*1024 + +// callback used to process captured audio +void AudioInputCallback(void * inUserData, + AudioQueueRef inAQ, + AudioQueueBufferRef inBuffer, + const AudioTimeStamp * inStartTime, + UInt32 inNumberPacketDescriptions, + const AudioStreamPacketDescription * inPacketDescs); + +@interface ViewController () + +@property (weak, nonatomic) IBOutlet UILabel *labelStatusInp; +@property (weak, nonatomic) IBOutlet UIButton *buttonToggleCapture; +@property (weak, nonatomic) IBOutlet UIButton *buttonTranscribe; +@property (weak, nonatomic) IBOutlet UITextView *textviewResult; + +@end + +@implementation ViewController + +- (void)setupAudioFormat:(AudioStreamBasicDescription*)format +{ + format->mSampleRate = 16000; + format->mFormatID = kAudioFormatLinearPCM; + format->mFramesPerPacket = 1; + format->mChannelsPerFrame = 1; + format->mBytesPerFrame = 2; + format->mBytesPerPacket = 2; + format->mBitsPerChannel = 16; + format->mReserved = 0; + format->mFormatFlags = kLinearPCMFormatFlagIsSignedInteger; +} + +- (void)viewDidLoad { + [super viewDidLoad]; + + // whisper.cpp initialization + { + // load the model + NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"ggml-base.en" ofType:@"bin"]; + + // check if the model exists + if (![[NSFileManager defaultManager] fileExistsAtPath:modelPath]) { + NSLog(@"Model file not found"); + return; + } + + NSLog(@"Loading model from %@", modelPath); + + // create ggml context + stateInp.ctx = whisper_init([modelPath UTF8String]); + + // check if the model was loaded successfully + if (stateInp.ctx == NULL) { + NSLog(@"Failed to load model"); + return; + } + } + + // initialize audio format and buffers + { + [self setupAudioFormat:&stateInp.dataFormat]; + + stateInp.n_samples = 0; + stateInp.audioBufferI16 = malloc(MAX_AUDIO_SEC*SAMPLE_RATE*sizeof(int16_t)); + stateInp.audioBufferF32 = malloc(MAX_AUDIO_SEC*SAMPLE_RATE*sizeof(float)); + } +} + +-(IBAction) stopCapturing { + NSLog(@"Stop capturing"); + + _labelStatusInp.text = @"Status: Idle"; + + stateInp.isCapturing = false; + + AudioQueueStop(stateInp.queue, true); + for (int i = 0; i < NUM_BUFFERS; i++) { + AudioQueueFreeBuffer(stateInp.queue, stateInp.buffers[i]); + } + + AudioQueueDispose(stateInp.queue, true); +} + +- (IBAction)toggleCapture:(id)sender { + if (stateInp.isCapturing) { + // stop capturing + [self stopCapturing]; + [sender setTitle:@"Start Capturing" forState:UIControlStateNormal]; + + return; + } + + // initiate audio capturing + NSLog(@"Start capturing"); + + stateInp.n_samples = 0; + + OSStatus status = AudioQueueNewInput(&stateInp.dataFormat, + AudioInputCallback, + &stateInp, + CFRunLoopGetCurrent(), + kCFRunLoopCommonModes, + 0, + &stateInp.queue); + + if (status == 0) { + for (int i = 0; i < NUM_BUFFERS; i++) { + AudioQueueAllocateBuffer(stateInp.queue, NUM_BYTES_PER_BUFFER, &stateInp.buffers[i]); + AudioQueueEnqueueBuffer (stateInp.queue, stateInp.buffers[i], 0, NULL); + } + + stateInp.isCapturing = true; + status = AudioQueueStart(stateInp.queue, NULL); + if (status == 0) { + _labelStatusInp.text = @"Status: Capturing"; + [sender setTitle:@"Stop Capturing" forState:UIControlStateNormal]; + } + } + + if (status != 0) { + [self stopCapturing]; + } +} + +- (IBAction)onTranscribePrepare:(id)sender { + _textviewResult.text = @"Processing - please wait ..."; + + if (stateInp.isCapturing) { + // stop capturing + [self stopCapturing]; + [sender setTitle:@"Start Capturing" forState:UIControlStateNormal]; + + return; + } +} + +- (IBAction)onTranscribe:(id)sender { + NSLog(@"Processing %d samples", stateInp.n_samples); + + // process captured audio + // convert I16 to F32 + for (int i = 0; i < stateInp.n_samples; i++) { + stateInp.audioBufferF32[i] = (float)stateInp.audioBufferI16[i] / 32768.0f; + } + + // run the model + struct whisper_full_params params = whisper_full_default_params(WHISPER_SAMPLING_GREEDY); + + params.print_realtime = true; + params.print_progress = false; + params.print_timestamps = true; + params.print_special_tokens = false; + params.translate = false; + params.language = "en"; + params.n_threads = 4; + params.offset_ms = 0; + + if (whisper_full(stateInp.ctx, params, stateInp.audioBufferF32, stateInp.n_samples) != 0) { + NSLog(@"Failed to run the model"); + _textviewResult.text = @"Failed to run the model"; + + return; + } + + // clear the text in the textview + _textviewResult.text = @""; + + int n_segments = whisper_full_n_segments(stateInp.ctx); + for (int i = 0; i < n_segments; i++) { + const char * text_cur = whisper_full_get_segment_text(stateInp.ctx, i); + + // append the text to the textview + _textviewResult.text = [_textviewResult.text stringByAppendingString:[NSString stringWithUTF8String:text_cur]]; + } + + whisper_print_timings(stateInp.ctx); +} + +// +// Callback implmentation +// + +void AudioInputCallback(void * inUserData, + AudioQueueRef inAQ, + AudioQueueBufferRef inBuffer, + const AudioTimeStamp * inStartTime, + UInt32 inNumberPacketDescriptions, + const AudioStreamPacketDescription * inPacketDescs) +{ + StateInp * stateInp = (StateInp*)inUserData; + + if (!stateInp->isCapturing) { + NSLog(@"Not capturing, ignoring audio"); + return; + } + + const int n = inBuffer->mAudioDataByteSize / 2; + + NSLog(@"Captured %d new samples", n); + + if (stateInp->n_samples + n > MAX_AUDIO_SEC*SAMPLE_RATE) { + NSLog(@"Too much audio data, ignoring"); + return; + } + + for (int i = 0; i < n; i++) { + stateInp->audioBufferI16[stateInp->n_samples + i] = ((short*)inBuffer->mAudioData)[i]; + } + + stateInp->n_samples += n; + + // put the buffer back in the queue + AudioQueueEnqueueBuffer(stateInp->queue, inBuffer, 0, NULL); +} + +@end diff --git a/examples/whisper.objc/whisper.objc/main.m b/examples/whisper.objc/whisper.objc/main.m new file mode 100644 index 0000000..d31b4d1 --- /dev/null +++ b/examples/whisper.objc/whisper.objc/main.m @@ -0,0 +1,18 @@ +// +// main.m +// whisper.objc +// +// Created by Georgi Gerganov on 23.10.22. +// + +#import +#import "AppDelegate.h" + +int main(int argc, char * argv[]) { + NSString * appDelegateClassName; + @autoreleasepool { + // Setup code that might create autoreleased objects goes here. + appDelegateClassName = NSStringFromClass([AppDelegate class]); + } + return UIApplicationMain(argc, argv, nil, appDelegateClassName); +}