diff --git a/.eslintrc b/.eslintrc index 6d2d9e0..f2cda9d 100644 --- a/.eslintrc +++ b/.eslintrc @@ -32,7 +32,17 @@ "@typescript-eslint/no-shadow": 1, "no-undef": "off", "func-call-spacing": "off", - "@typescript-eslint/func-call-spacing": 1 + "@typescript-eslint/func-call-spacing": 1, + "import/extensions": [ + "error", + "ignorePackages", + { + "js": "never", + "jsx": "never", + "ts": "never", + "tsx": "never" + } + ] } } ], diff --git a/android/build.gradle b/android/build.gradle index ee1566c..32bec0d 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -59,6 +59,15 @@ android { targetCompatibility JavaVersion.VERSION_1_8 } + sourceSets { + main { + if (isNewArchitectureEnabled()) { + java.srcDirs += ['src/newarch'] + } else { + java.srcDirs += ['src/oldarch'] + } + } + } } repositories { diff --git a/android/src/main/java/com/rnwhisper/RNWhisperPackage.java b/android/src/main/java/com/rnwhisper/RNWhisperPackage.java index 295e981..5a8ce22 100644 --- a/android/src/main/java/com/rnwhisper/RNWhisperPackage.java +++ b/android/src/main/java/com/rnwhisper/RNWhisperPackage.java @@ -1,28 +1,48 @@ package com.rnwhisper; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; -import com.facebook.react.ReactPackage; import com.facebook.react.bridge.NativeModule; import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.uimanager.ViewManager; +import com.facebook.react.module.model.ReactModuleInfo; +import com.facebook.react.module.model.ReactModuleInfoProvider; +import com.facebook.react.TurboReactPackage; -import java.util.ArrayList; -import java.util.Collections; import java.util.List; +import java.util.HashMap; +import java.util.Map; -public class RNWhisperPackage implements ReactPackage { - @NonNull +public class RNWhisperPackage extends TurboReactPackage { + + @Nullable @Override - public List createNativeModules(@NonNull ReactApplicationContext reactContext) { - List modules = new ArrayList<>(); - modules.add(new RNWhisperModule(reactContext)); - return modules; + public NativeModule getModule(String name, ReactApplicationContext reactContext) { + if (name.equals(RNWhisperModule.NAME)) { + return new com.rnwhisper.RNWhisperModule(reactContext); + } else { + return null; + } } - @NonNull @Override - public List createViewManagers(@NonNull ReactApplicationContext reactContext) { - return Collections.emptyList(); + public ReactModuleInfoProvider getReactModuleInfoProvider() { + return () -> { + final Map moduleInfos = new HashMap<>(); + boolean isTurboModule = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED; + moduleInfos.put( + RNWhisperModule.NAME, + new ReactModuleInfo( + RNWhisperModule.NAME, + RNWhisperModule.NAME, + false, // canOverrideExistingModule + false, // needsEagerInit + true, // hasConstants + false, // isCxxModule + isTurboModule // isTurboModule + ) + ); + return moduleInfos; + }; } } diff --git a/android/src/newarch/java/com/rnwhisper/RNWhisperModule.java b/android/src/newarch/java/com/rnwhisper/RNWhisperModule.java new file mode 100644 index 0000000..6164aaa --- /dev/null +++ b/android/src/newarch/java/com/rnwhisper/RNWhisperModule.java @@ -0,0 +1,232 @@ +package com.rnwhisper; + +import androidx.annotation.NonNull; +import android.util.Log; +import android.os.Build; +import android.os.Handler; +import android.os.AsyncTask; +import android.media.AudioRecord; + +import com.facebook.react.bridge.Promise; +import com.facebook.react.bridge.ReactApplicationContext; +import com.facebook.react.bridge.ReactMethod; +import com.facebook.react.bridge.LifecycleEventListener; +import com.facebook.react.bridge.ReadableMap; +import com.facebook.react.bridge.WritableMap; +import com.facebook.react.module.annotations.ReactModule; + +import java.util.HashMap; +import java.util.Random; + +@ReactModule(name = RNWhisperModule.NAME) +public class RNWhisperModule extends NativeRNWhisperSpec implements LifecycleEventListener { + public static final String NAME = "RNWhisper"; + + private ReactApplicationContext reactContext; + + public RNWhisperModule(ReactApplicationContext reactContext) { + super(reactContext); + reactContext.addLifecycleEventListener(this); + this.reactContext = reactContext; + } + + @Override + @NonNull + public String getName() { + return NAME; + } + + @Override + public HashMap getTypedExportedConstants() { + HashMap constants = new HashMap<>(); + + // iOS only constants, put for passing type checks + constants.put("useCoreML", false); + constants.put("coreMLAllowFallback", false); + + return constants; + } + + private HashMap contexts = new HashMap<>(); + + @ReactMethod + public void initContext(final String modelPath, final boolean isBundleAsset, final Promise promise) { + new AsyncTask() { + private Exception exception; + + @Override + protected Integer doInBackground(Void... voids) { + try { + long context; + if (isBundleAsset) { + context = WhisperContext.initContextWithAsset(reactContext.getAssets(), modelPath); + } else { + context = WhisperContext.initContext(modelPath); + } + if (context == 0) { + throw new Exception("Failed to initialize context"); + } + int id = Math.abs(new Random().nextInt()); + WhisperContext whisperContext = new WhisperContext(id, reactContext, context); + contexts.put(id, whisperContext); + return id; + } catch (Exception e) { + exception = e; + return null; + } + } + + @Override + protected void onPostExecute(Integer id) { + if (exception != null) { + promise.reject(exception); + return; + } + promise.resolve(id); + } + }.execute(); + } + + @ReactMethod + public void transcribeFile(double id, double jobId, String filePath, ReadableMap options, Promise promise) { + final WhisperContext context = contexts.get((int) id); + if (context == null) { + promise.reject("Context not found"); + return; + } + if (context.isCapturing()) { + promise.reject("The context is in realtime transcribe mode"); + return; + } + if (context.isTranscribing()) { + promise.reject("Context is already transcribing"); + return; + } + new AsyncTask() { + private Exception exception; + + @Override + protected WritableMap doInBackground(Void... voids) { + try { + return context.transcribeFile((int) jobId, filePath, options); + } catch (Exception e) { + exception = e; + return null; + } + } + + @Override + protected void onPostExecute(WritableMap data) { + if (exception != null) { + promise.reject(exception); + return; + } + promise.resolve(data); + } + }.execute(); + } + + @ReactMethod + public void startRealtimeTranscribe(double id, double jobId, ReadableMap options, Promise promise) { + final WhisperContext context = contexts.get((int) id); + if (context == null) { + promise.reject("Context not found"); + return; + } + if (context.isCapturing()) { + promise.reject("Context is already in capturing"); + return; + } + int state = context.startRealtimeTranscribe((int) jobId, options); + if (state == AudioRecord.STATE_INITIALIZED) { + promise.resolve(null); + return; + } + promise.reject("Failed to start realtime transcribe. State: " + state); + } + + @ReactMethod + public void abortTranscribe(double contextId, double jobId, Promise promise) { + WhisperContext context = contexts.get((int) contextId); + if (context == null) { + promise.reject("Context not found"); + return; + } + context.stopTranscribe((int) jobId); + } + + @ReactMethod + public void releaseContext(double id, Promise promise) { + final int contextId = (int) id; + new AsyncTask() { + private Exception exception; + + @Override + protected Void doInBackground(Void... voids) { + try { + WhisperContext context = contexts.get(contextId); + if (context == null) { + throw new Exception("Context " + id + " not found"); + } + context.release(); + contexts.remove(contextId); + } catch (Exception e) { + exception = e; + } + return null; + } + + @Override + protected void onPostExecute(Void result) { + if (exception != null) { + promise.reject(exception); + return; + } + promise.resolve(null); + } + }.execute(); + } + + @ReactMethod + public void releaseAllContexts(Promise promise) { + new AsyncTask() { + private Exception exception; + + @Override + protected Void doInBackground(Void... voids) { + try { + onHostDestroy(); + } catch (Exception e) { + exception = e; + } + return null; + } + + @Override + protected void onPostExecute(Void result) { + if (exception != null) { + promise.reject(exception); + return; + } + promise.resolve(null); + } + }.execute(); + } + + @Override + public void onHostResume() { + } + + @Override + public void onHostPause() { + } + + @Override + public void onHostDestroy() { + WhisperContext.abortAllTranscribe(); + for (WhisperContext context : contexts.values()) { + context.release(); + } + contexts.clear(); + } +} diff --git a/android/src/main/java/com/rnwhisper/RNWhisperModule.java b/android/src/oldarch/java/com/rnwhisper/RNWhisperModule.java similarity index 100% rename from android/src/main/java/com/rnwhisper/RNWhisperModule.java rename to android/src/oldarch/java/com/rnwhisper/RNWhisperModule.java diff --git a/docs/README.md b/docs/README.md index e11c9dd..7553603 100644 --- a/docs/README.md +++ b/docs/README.md @@ -56,7 +56,7 @@ whisper.rn #### Defined in -[index.ts:36](https://github.com/mybigday/whisper.rn/blob/e16f2a4/src/index.ts#L36) +[NativeRNWhisper.ts:4](https://github.com/mybigday/whisper.rn/blob/9b07569/src/NativeRNWhisper.ts#L4) ___ @@ -81,7 +81,7 @@ ___ #### Defined in -[index.ts:92](https://github.com/mybigday/whisper.rn/blob/e16f2a4/src/index.ts#L92) +[index.ts:40](https://github.com/mybigday/whisper.rn/blob/9b07569/src/index.ts#L40) ___ @@ -99,7 +99,7 @@ ___ #### Defined in -[index.ts:125](https://github.com/mybigday/whisper.rn/blob/e16f2a4/src/index.ts#L125) +[index.ts:73](https://github.com/mybigday/whisper.rn/blob/9b07569/src/index.ts#L73) ___ @@ -123,7 +123,7 @@ ___ #### Defined in -[index.ts:112](https://github.com/mybigday/whisper.rn/blob/e16f2a4/src/index.ts#L112) +[index.ts:60](https://github.com/mybigday/whisper.rn/blob/9b07569/src/index.ts#L60) ___ @@ -133,7 +133,7 @@ ___ #### Defined in -[index.ts:68](https://github.com/mybigday/whisper.rn/blob/e16f2a4/src/index.ts#L68) +[index.ts:25](https://github.com/mybigday/whisper.rn/blob/9b07569/src/index.ts#L25) ___ @@ -150,31 +150,31 @@ ___ #### Defined in -[index.ts:83](https://github.com/mybigday/whisper.rn/blob/e16f2a4/src/index.ts#L83) +[NativeRNWhisper.ts:36](https://github.com/mybigday/whisper.rn/blob/9b07569/src/NativeRNWhisper.ts#L36) ## Variables ### isCoreMLAllowFallback -• `Const` **isCoreMLAllowFallback**: `boolean` = `!!RNWhisper.WHISPER_COREML_ALLOW_FALLBACK` +• `Const` **isCoreMLAllowFallback**: `boolean` = `!!coreMLAllowFallback` Is allow fallback to CPU if load CoreML model failed #### Defined in -[index.ts:277](https://github.com/mybigday/whisper.rn/blob/e16f2a4/src/index.ts#L277) +[index.ts:227](https://github.com/mybigday/whisper.rn/blob/9b07569/src/index.ts#L227) ___ ### isUseCoreML -• `Const` **isUseCoreML**: `boolean` = `!!RNWhisper.WHISPER_USE_COREML` +• `Const` **isUseCoreML**: `boolean` = `!!useCoreML` Is use CoreML models on iOS #### Defined in -[index.ts:274](https://github.com/mybigday/whisper.rn/blob/e16f2a4/src/index.ts#L274) +[index.ts:224](https://github.com/mybigday/whisper.rn/blob/9b07569/src/index.ts#L224) ___ @@ -186,20 +186,20 @@ Current version of whisper.cpp #### Defined in -[index.ts:271](https://github.com/mybigday/whisper.rn/blob/e16f2a4/src/index.ts#L271) +[index.ts:219](https://github.com/mybigday/whisper.rn/blob/9b07569/src/index.ts#L219) ## Functions ### initWhisper -▸ **initWhisper**(`«destructured»?`): `Promise`<[`WhisperContext`](classes/WhisperContext.md)\> +▸ **initWhisper**(`«destructured»`): `Promise`<[`WhisperContext`](classes/WhisperContext.md)\> #### Parameters | Name | Type | | :------ | :------ | | `«destructured»` | `Object` | -| › `filePath?` | `string` | +| › `filePath` | `string` | | › `isBundleAsset?` | `boolean` | #### Returns @@ -208,7 +208,7 @@ Current version of whisper.cpp #### Defined in -[index.ts:259](https://github.com/mybigday/whisper.rn/blob/e16f2a4/src/index.ts#L259) +[index.ts:207](https://github.com/mybigday/whisper.rn/blob/9b07569/src/index.ts#L207) ___ @@ -222,4 +222,4 @@ ___ #### Defined in -[index.ts:266](https://github.com/mybigday/whisper.rn/blob/e16f2a4/src/index.ts#L266) +[index.ts:214](https://github.com/mybigday/whisper.rn/blob/9b07569/src/index.ts#L214) diff --git a/docs/classes/WhisperContext.md b/docs/classes/WhisperContext.md index 651fdd2..35d4db4 100644 --- a/docs/classes/WhisperContext.md +++ b/docs/classes/WhisperContext.md @@ -32,7 +32,7 @@ #### Defined in -[index.ts:134](https://github.com/mybigday/whisper.rn/blob/e16f2a4/src/index.ts#L134) +[index.ts:82](https://github.com/mybigday/whisper.rn/blob/9b07569/src/index.ts#L82) ## Properties @@ -42,7 +42,7 @@ #### Defined in -[index.ts:132](https://github.com/mybigday/whisper.rn/blob/e16f2a4/src/index.ts#L132) +[index.ts:80](https://github.com/mybigday/whisper.rn/blob/9b07569/src/index.ts#L80) ## Methods @@ -56,7 +56,7 @@ #### Defined in -[index.ts:254](https://github.com/mybigday/whisper.rn/blob/e16f2a4/src/index.ts#L254) +[index.ts:202](https://github.com/mybigday/whisper.rn/blob/9b07569/src/index.ts#L202) ___ @@ -84,7 +84,7 @@ Transcribe audio file #### Defined in -[index.ts:139](https://github.com/mybigday/whisper.rn/blob/e16f2a4/src/index.ts#L139) +[index.ts:87](https://github.com/mybigday/whisper.rn/blob/9b07569/src/index.ts#L87) ___ @@ -106,4 +106,4 @@ Transcribe the microphone audio stream, the microphone user permission is requir #### Defined in -[index.ts:153](https://github.com/mybigday/whisper.rn/blob/e16f2a4/src/index.ts#L153) +[index.ts:101](https://github.com/mybigday/whisper.rn/blob/9b07569/src/index.ts#L101) diff --git a/example/android/gradle.properties b/example/android/gradle.properties index e4af465..e1ddc51 100644 --- a/example/android/gradle.properties +++ b/example/android/gradle.properties @@ -37,7 +37,7 @@ reactNativeArchitectures=armeabi-v7a,arm64-v8a,x86,x86_64 # your application. You should enable this flag either if you want # to write custom TurboModules/Fabric components OR use libraries that # are providing them. -newArchEnabled=false +newArchEnabled=true # Use this property to enable or disable the Hermes JS engine. # If set to false, you will be using JSC instead. diff --git a/example/ios/Podfile b/example/ios/Podfile index 26e95e4..7cc8e3f 100644 --- a/example/ios/Podfile +++ b/example/ios/Podfile @@ -21,6 +21,8 @@ if linkage != nil use_frameworks! :linkage => linkage.to_sym end +ENV['RCT_NEW_ARCH_ENABLED'] = '1' + target 'RNWhisperExample' do # Tip: You can use RNWHISPER_DISABLE_COREML = '1' to disable CoreML support. ENV['RNWHISPER_DISABLE_COREML'] = '0' diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index 804194d..ce96ae0 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -89,6 +89,11 @@ PODS: - DoubleConversion - fmt (~> 6.2.1) - glog + - RCT-Folly/Fabric (2021.07.22.00): + - boost + - DoubleConversion + - fmt (~> 6.2.1) + - glog - RCT-Folly/Futures (2021.07.22.00): - boost - DoubleConversion @@ -121,8 +126,10 @@ PODS: - RCTRequired - RCTTypeSafety - React-Core + - React-graphics - React-jsi - React-jsiexecutor + - React-rncore - ReactCommon/turbomodule/bridging - ReactCommon/turbomodule/core - React-Core (0.71.11): @@ -301,6 +308,326 @@ PODS: - React-logger (= 0.71.11) - React-perflogger (= 0.71.11) - React-runtimeexecutor (= 0.71.11) + - React-Fabric (0.71.11): + - RCT-Folly/Fabric (= 2021.07.22.00) + - RCTRequired (= 0.71.11) + - RCTTypeSafety (= 0.71.11) + - React-Fabric/animations (= 0.71.11) + - React-Fabric/attributedstring (= 0.71.11) + - React-Fabric/butter (= 0.71.11) + - React-Fabric/componentregistry (= 0.71.11) + - React-Fabric/componentregistrynative (= 0.71.11) + - React-Fabric/components (= 0.71.11) + - React-Fabric/config (= 0.71.11) + - React-Fabric/core (= 0.71.11) + - React-Fabric/debug_core (= 0.71.11) + - React-Fabric/debug_renderer (= 0.71.11) + - React-Fabric/imagemanager (= 0.71.11) + - React-Fabric/leakchecker (= 0.71.11) + - React-Fabric/mapbuffer (= 0.71.11) + - React-Fabric/mounting (= 0.71.11) + - React-Fabric/runtimescheduler (= 0.71.11) + - React-Fabric/scheduler (= 0.71.11) + - React-Fabric/telemetry (= 0.71.11) + - React-Fabric/templateprocessor (= 0.71.11) + - React-Fabric/textlayoutmanager (= 0.71.11) + - React-Fabric/uimanager (= 0.71.11) + - React-Fabric/utils (= 0.71.11) + - React-graphics (= 0.71.11) + - React-jsi (= 0.71.11) + - React-jsiexecutor (= 0.71.11) + - ReactCommon/turbomodule/core (= 0.71.11) + - React-Fabric/animations (0.71.11): + - RCT-Folly/Fabric (= 2021.07.22.00) + - RCTRequired (= 0.71.11) + - RCTTypeSafety (= 0.71.11) + - React-graphics (= 0.71.11) + - React-jsi (= 0.71.11) + - React-jsiexecutor (= 0.71.11) + - ReactCommon/turbomodule/core (= 0.71.11) + - React-Fabric/attributedstring (0.71.11): + - RCT-Folly/Fabric (= 2021.07.22.00) + - RCTRequired (= 0.71.11) + - RCTTypeSafety (= 0.71.11) + - React-graphics (= 0.71.11) + - React-jsi (= 0.71.11) + - React-jsiexecutor (= 0.71.11) + - ReactCommon/turbomodule/core (= 0.71.11) + - React-Fabric/butter (0.71.11): + - RCT-Folly/Fabric (= 2021.07.22.00) + - RCTRequired (= 0.71.11) + - RCTTypeSafety (= 0.71.11) + - React-graphics (= 0.71.11) + - React-jsi (= 0.71.11) + - React-jsiexecutor (= 0.71.11) + - ReactCommon/turbomodule/core (= 0.71.11) + - React-Fabric/componentregistry (0.71.11): + - RCT-Folly/Fabric (= 2021.07.22.00) + - RCTRequired (= 0.71.11) + - RCTTypeSafety (= 0.71.11) + - React-graphics (= 0.71.11) + - React-jsi (= 0.71.11) + - React-jsiexecutor (= 0.71.11) + - ReactCommon/turbomodule/core (= 0.71.11) + - React-Fabric/componentregistrynative (0.71.11): + - RCT-Folly/Fabric (= 2021.07.22.00) + - RCTRequired (= 0.71.11) + - RCTTypeSafety (= 0.71.11) + - React-graphics (= 0.71.11) + - React-jsi (= 0.71.11) + - React-jsiexecutor (= 0.71.11) + - ReactCommon/turbomodule/core (= 0.71.11) + - React-Fabric/components (0.71.11): + - RCT-Folly/Fabric (= 2021.07.22.00) + - RCTRequired (= 0.71.11) + - RCTTypeSafety (= 0.71.11) + - React-Fabric/components/activityindicator (= 0.71.11) + - React-Fabric/components/image (= 0.71.11) + - React-Fabric/components/inputaccessory (= 0.71.11) + - React-Fabric/components/legacyviewmanagerinterop (= 0.71.11) + - React-Fabric/components/modal (= 0.71.11) + - React-Fabric/components/root (= 0.71.11) + - React-Fabric/components/safeareaview (= 0.71.11) + - React-Fabric/components/scrollview (= 0.71.11) + - React-Fabric/components/slider (= 0.71.11) + - React-Fabric/components/text (= 0.71.11) + - React-Fabric/components/textinput (= 0.71.11) + - React-Fabric/components/unimplementedview (= 0.71.11) + - React-Fabric/components/view (= 0.71.11) + - React-graphics (= 0.71.11) + - React-jsi (= 0.71.11) + - React-jsiexecutor (= 0.71.11) + - ReactCommon/turbomodule/core (= 0.71.11) + - React-Fabric/components/activityindicator (0.71.11): + - RCT-Folly/Fabric (= 2021.07.22.00) + - RCTRequired (= 0.71.11) + - RCTTypeSafety (= 0.71.11) + - React-graphics (= 0.71.11) + - React-jsi (= 0.71.11) + - React-jsiexecutor (= 0.71.11) + - ReactCommon/turbomodule/core (= 0.71.11) + - React-Fabric/components/image (0.71.11): + - RCT-Folly/Fabric (= 2021.07.22.00) + - RCTRequired (= 0.71.11) + - RCTTypeSafety (= 0.71.11) + - React-graphics (= 0.71.11) + - React-jsi (= 0.71.11) + - React-jsiexecutor (= 0.71.11) + - ReactCommon/turbomodule/core (= 0.71.11) + - React-Fabric/components/inputaccessory (0.71.11): + - RCT-Folly/Fabric (= 2021.07.22.00) + - RCTRequired (= 0.71.11) + - RCTTypeSafety (= 0.71.11) + - React-graphics (= 0.71.11) + - React-jsi (= 0.71.11) + - React-jsiexecutor (= 0.71.11) + - ReactCommon/turbomodule/core (= 0.71.11) + - React-Fabric/components/legacyviewmanagerinterop (0.71.11): + - RCT-Folly/Fabric (= 2021.07.22.00) + - RCTRequired (= 0.71.11) + - RCTTypeSafety (= 0.71.11) + - React-graphics (= 0.71.11) + - React-jsi (= 0.71.11) + - React-jsiexecutor (= 0.71.11) + - ReactCommon/turbomodule/core (= 0.71.11) + - React-Fabric/components/modal (0.71.11): + - RCT-Folly/Fabric (= 2021.07.22.00) + - RCTRequired (= 0.71.11) + - RCTTypeSafety (= 0.71.11) + - React-graphics (= 0.71.11) + - React-jsi (= 0.71.11) + - React-jsiexecutor (= 0.71.11) + - ReactCommon/turbomodule/core (= 0.71.11) + - React-Fabric/components/root (0.71.11): + - RCT-Folly/Fabric (= 2021.07.22.00) + - RCTRequired (= 0.71.11) + - RCTTypeSafety (= 0.71.11) + - React-graphics (= 0.71.11) + - React-jsi (= 0.71.11) + - React-jsiexecutor (= 0.71.11) + - ReactCommon/turbomodule/core (= 0.71.11) + - React-Fabric/components/safeareaview (0.71.11): + - RCT-Folly/Fabric (= 2021.07.22.00) + - RCTRequired (= 0.71.11) + - RCTTypeSafety (= 0.71.11) + - React-graphics (= 0.71.11) + - React-jsi (= 0.71.11) + - React-jsiexecutor (= 0.71.11) + - ReactCommon/turbomodule/core (= 0.71.11) + - React-Fabric/components/scrollview (0.71.11): + - RCT-Folly/Fabric (= 2021.07.22.00) + - RCTRequired (= 0.71.11) + - RCTTypeSafety (= 0.71.11) + - React-graphics (= 0.71.11) + - React-jsi (= 0.71.11) + - React-jsiexecutor (= 0.71.11) + - ReactCommon/turbomodule/core (= 0.71.11) + - React-Fabric/components/slider (0.71.11): + - RCT-Folly/Fabric (= 2021.07.22.00) + - RCTRequired (= 0.71.11) + - RCTTypeSafety (= 0.71.11) + - React-graphics (= 0.71.11) + - React-jsi (= 0.71.11) + - React-jsiexecutor (= 0.71.11) + - ReactCommon/turbomodule/core (= 0.71.11) + - React-Fabric/components/text (0.71.11): + - RCT-Folly/Fabric (= 2021.07.22.00) + - RCTRequired (= 0.71.11) + - RCTTypeSafety (= 0.71.11) + - React-graphics (= 0.71.11) + - React-jsi (= 0.71.11) + - React-jsiexecutor (= 0.71.11) + - ReactCommon/turbomodule/core (= 0.71.11) + - React-Fabric/components/textinput (0.71.11): + - RCT-Folly/Fabric (= 2021.07.22.00) + - RCTRequired (= 0.71.11) + - RCTTypeSafety (= 0.71.11) + - React-graphics (= 0.71.11) + - React-jsi (= 0.71.11) + - React-jsiexecutor (= 0.71.11) + - ReactCommon/turbomodule/core (= 0.71.11) + - React-Fabric/components/unimplementedview (0.71.11): + - RCT-Folly/Fabric (= 2021.07.22.00) + - RCTRequired (= 0.71.11) + - RCTTypeSafety (= 0.71.11) + - React-graphics (= 0.71.11) + - React-jsi (= 0.71.11) + - React-jsiexecutor (= 0.71.11) + - ReactCommon/turbomodule/core (= 0.71.11) + - React-Fabric/components/view (0.71.11): + - RCT-Folly/Fabric (= 2021.07.22.00) + - RCTRequired (= 0.71.11) + - RCTTypeSafety (= 0.71.11) + - React-graphics (= 0.71.11) + - React-jsi (= 0.71.11) + - React-jsiexecutor (= 0.71.11) + - ReactCommon/turbomodule/core (= 0.71.11) + - Yoga + - React-Fabric/config (0.71.11): + - RCT-Folly/Fabric (= 2021.07.22.00) + - RCTRequired (= 0.71.11) + - RCTTypeSafety (= 0.71.11) + - React-graphics (= 0.71.11) + - React-jsi (= 0.71.11) + - React-jsiexecutor (= 0.71.11) + - ReactCommon/turbomodule/core (= 0.71.11) + - React-Fabric/core (0.71.11): + - RCT-Folly/Fabric (= 2021.07.22.00) + - RCTRequired (= 0.71.11) + - RCTTypeSafety (= 0.71.11) + - React-graphics (= 0.71.11) + - React-jsi (= 0.71.11) + - React-jsiexecutor (= 0.71.11) + - ReactCommon/turbomodule/core (= 0.71.11) + - React-Fabric/debug_core (0.71.11): + - RCT-Folly/Fabric (= 2021.07.22.00) + - RCTRequired (= 0.71.11) + - RCTTypeSafety (= 0.71.11) + - React-graphics (= 0.71.11) + - React-jsi (= 0.71.11) + - React-jsiexecutor (= 0.71.11) + - ReactCommon/turbomodule/core (= 0.71.11) + - React-Fabric/debug_renderer (0.71.11): + - RCT-Folly/Fabric (= 2021.07.22.00) + - RCTRequired (= 0.71.11) + - RCTTypeSafety (= 0.71.11) + - React-graphics (= 0.71.11) + - React-jsi (= 0.71.11) + - React-jsiexecutor (= 0.71.11) + - ReactCommon/turbomodule/core (= 0.71.11) + - React-Fabric/imagemanager (0.71.11): + - RCT-Folly/Fabric (= 2021.07.22.00) + - RCTRequired (= 0.71.11) + - RCTTypeSafety (= 0.71.11) + - React-graphics (= 0.71.11) + - React-jsi (= 0.71.11) + - React-jsiexecutor (= 0.71.11) + - React-RCTImage (= 0.71.11) + - ReactCommon/turbomodule/core (= 0.71.11) + - React-Fabric/leakchecker (0.71.11): + - RCT-Folly/Fabric (= 2021.07.22.00) + - RCTRequired (= 0.71.11) + - RCTTypeSafety (= 0.71.11) + - React-graphics (= 0.71.11) + - React-jsi (= 0.71.11) + - React-jsiexecutor (= 0.71.11) + - ReactCommon/turbomodule/core (= 0.71.11) + - React-Fabric/mapbuffer (0.71.11): + - RCT-Folly/Fabric (= 2021.07.22.00) + - RCTRequired (= 0.71.11) + - RCTTypeSafety (= 0.71.11) + - React-graphics (= 0.71.11) + - React-jsi (= 0.71.11) + - React-jsiexecutor (= 0.71.11) + - ReactCommon/turbomodule/core (= 0.71.11) + - React-Fabric/mounting (0.71.11): + - RCT-Folly/Fabric (= 2021.07.22.00) + - RCTRequired (= 0.71.11) + - RCTTypeSafety (= 0.71.11) + - React-graphics (= 0.71.11) + - React-jsi (= 0.71.11) + - React-jsiexecutor (= 0.71.11) + - ReactCommon/turbomodule/core (= 0.71.11) + - React-Fabric/runtimescheduler (0.71.11): + - RCT-Folly/Fabric (= 2021.07.22.00) + - RCTRequired (= 0.71.11) + - RCTTypeSafety (= 0.71.11) + - React-graphics (= 0.71.11) + - React-jsi (= 0.71.11) + - React-jsiexecutor (= 0.71.11) + - ReactCommon/turbomodule/core (= 0.71.11) + - React-Fabric/scheduler (0.71.11): + - RCT-Folly/Fabric (= 2021.07.22.00) + - RCTRequired (= 0.71.11) + - RCTTypeSafety (= 0.71.11) + - React-graphics (= 0.71.11) + - React-jsi (= 0.71.11) + - React-jsiexecutor (= 0.71.11) + - ReactCommon/turbomodule/core (= 0.71.11) + - React-Fabric/telemetry (0.71.11): + - RCT-Folly/Fabric (= 2021.07.22.00) + - RCTRequired (= 0.71.11) + - RCTTypeSafety (= 0.71.11) + - React-graphics (= 0.71.11) + - React-jsi (= 0.71.11) + - React-jsiexecutor (= 0.71.11) + - ReactCommon/turbomodule/core (= 0.71.11) + - React-Fabric/templateprocessor (0.71.11): + - RCT-Folly/Fabric (= 2021.07.22.00) + - RCTRequired (= 0.71.11) + - RCTTypeSafety (= 0.71.11) + - React-graphics (= 0.71.11) + - React-jsi (= 0.71.11) + - React-jsiexecutor (= 0.71.11) + - ReactCommon/turbomodule/core (= 0.71.11) + - React-Fabric/textlayoutmanager (0.71.11): + - RCT-Folly/Fabric (= 2021.07.22.00) + - RCTRequired (= 0.71.11) + - RCTTypeSafety (= 0.71.11) + - React-Fabric/uimanager + - React-graphics (= 0.71.11) + - React-jsi (= 0.71.11) + - React-jsiexecutor (= 0.71.11) + - ReactCommon/turbomodule/core (= 0.71.11) + - React-Fabric/uimanager (0.71.11): + - RCT-Folly/Fabric (= 2021.07.22.00) + - RCTRequired (= 0.71.11) + - RCTTypeSafety (= 0.71.11) + - React-graphics (= 0.71.11) + - React-jsi (= 0.71.11) + - React-jsiexecutor (= 0.71.11) + - ReactCommon/turbomodule/core (= 0.71.11) + - React-Fabric/utils (0.71.11): + - RCT-Folly/Fabric (= 2021.07.22.00) + - RCTRequired (= 0.71.11) + - RCTTypeSafety (= 0.71.11) + - React-graphics (= 0.71.11) + - React-jsi (= 0.71.11) + - React-jsiexecutor (= 0.71.11) + - ReactCommon/turbomodule/core (= 0.71.11) + - React-graphics (0.71.11): + - RCT-Folly/Fabric (= 2021.07.22.00) + - React-Core/Default (= 0.71.11) - React-hermes (0.71.11): - DoubleConversion - glog @@ -344,6 +671,8 @@ PODS: - RCTRequired - RCTTypeSafety - React-Core + - React-graphics + - React-RCTFabric - ReactCommon/turbomodule/core - React-RCTBlob (0.71.11): - hermes-engine @@ -354,6 +683,11 @@ PODS: - React-jsi (= 0.71.11) - React-RCTNetwork (= 0.71.11) - ReactCommon/turbomodule/core (= 0.71.11) + - React-RCTFabric (0.71.11): + - RCT-Folly/Fabric (= 2021.07.22.00) + - React-Core (= 0.71.11) + - React-Fabric (= 0.71.11) + - React-RCTImage (= 0.71.11) - React-RCTImage (0.71.11): - RCT-Folly (= 2021.07.22.00) - RCTTypeSafety (= 0.71.11) @@ -389,6 +723,7 @@ PODS: - React-Core/RCTVibrationHeaders (= 0.71.11) - React-jsi (= 0.71.11) - ReactCommon/turbomodule/core (= 0.71.11) + - React-rncore (0.71.11) - React-runtimeexecutor (0.71.11): - React-jsi (= 0.71.11) - ReactCommon/turbomodule/bridging (0.71.11): @@ -417,7 +752,12 @@ PODS: - React-Core - SocketRocket (0.6.0) - whisper-rn (0.3.0-rc.4): + - RCT-Folly + - RCTRequired + - RCTTypeSafety + - React-Codegen - React-Core + - ReactCommon/turbomodule/core - Yoga (1.14.0) - YogaKit (1.18.1): - Yoga (~> 1.14) @@ -453,6 +793,7 @@ DEPENDENCIES: - libevent (~> 2.1.12) - OpenSSL-Universal (= 1.1.1100) - RCT-Folly (from `../node_modules/react-native/third-party-podspecs/RCT-Folly.podspec`) + - RCT-Folly/Fabric (from `../node_modules/react-native/third-party-podspecs/RCT-Folly.podspec`) - RCTRequired (from `../node_modules/react-native/Libraries/RCTRequired`) - RCTTypeSafety (from `../node_modules/react-native/Libraries/TypeSafety`) - React (from `../node_modules/react-native/`) @@ -463,6 +804,8 @@ DEPENDENCIES: - React-Core/RCTWebSocket (from `../node_modules/react-native/`) - React-CoreModules (from `../node_modules/react-native/React/CoreModules`) - React-cxxreact (from `../node_modules/react-native/ReactCommon/cxxreact`) + - React-Fabric (from `../node_modules/react-native/ReactCommon`) + - React-graphics (from `../node_modules/react-native/ReactCommon/react/renderer/graphics`) - React-hermes (from `../node_modules/react-native/ReactCommon/hermes`) - React-jsi (from `../node_modules/react-native/ReactCommon/jsi`) - React-jsiexecutor (from `../node_modules/react-native/ReactCommon/jsiexecutor`) @@ -473,12 +816,14 @@ DEPENDENCIES: - React-RCTAnimation (from `../node_modules/react-native/Libraries/NativeAnimation`) - React-RCTAppDelegate (from `../node_modules/react-native/Libraries/AppDelegate`) - React-RCTBlob (from `../node_modules/react-native/Libraries/Blob`) + - React-RCTFabric (from `../node_modules/react-native/React`) - React-RCTImage (from `../node_modules/react-native/Libraries/Image`) - React-RCTLinking (from `../node_modules/react-native/Libraries/LinkingIOS`) - React-RCTNetwork (from `../node_modules/react-native/Libraries/Network`) - React-RCTSettings (from `../node_modules/react-native/Libraries/Settings`) - React-RCTText (from `../node_modules/react-native/Libraries/Text`) - React-RCTVibration (from `../node_modules/react-native/Libraries/Vibration`) + - React-rncore (from `../node_modules/react-native/ReactCommon`) - React-runtimeexecutor (from `../node_modules/react-native/ReactCommon/runtimeexecutor`) - ReactCommon/turbomodule/core (from `../node_modules/react-native/ReactCommon`) - RNFS (from `../node_modules/react-native-fs`) @@ -534,6 +879,10 @@ EXTERNAL SOURCES: :path: "../node_modules/react-native/React/CoreModules" React-cxxreact: :path: "../node_modules/react-native/ReactCommon/cxxreact" + React-Fabric: + :path: "../node_modules/react-native/ReactCommon" + React-graphics: + :path: "../node_modules/react-native/ReactCommon/react/renderer/graphics" React-hermes: :path: "../node_modules/react-native/ReactCommon/hermes" React-jsi: @@ -554,6 +903,8 @@ EXTERNAL SOURCES: :path: "../node_modules/react-native/Libraries/AppDelegate" React-RCTBlob: :path: "../node_modules/react-native/Libraries/Blob" + React-RCTFabric: + :path: "../node_modules/react-native/React" React-RCTImage: :path: "../node_modules/react-native/Libraries/Image" React-RCTLinking: @@ -566,6 +917,8 @@ EXTERNAL SOURCES: :path: "../node_modules/react-native/Libraries/Text" React-RCTVibration: :path: "../node_modules/react-native/Libraries/Vibration" + React-rncore: + :path: "../node_modules/react-native/ReactCommon" React-runtimeexecutor: :path: "../node_modules/react-native/ReactCommon/runtimeexecutor" ReactCommon: @@ -582,7 +935,7 @@ SPEC CHECKSUMS: CocoaAsyncSocket: 065fd1e645c7abab64f7a6a2007a48038fdc6a99 DoubleConversion: 5189b271737e1565bdce30deb4a08d647e3f5f54 FBLazyVector: c511d4cd0210f416cb5c289bd5ae6b36d909b048 - FBReactNativeSpec: a911fb22def57aef1d74215e8b6b8761d25c1c54 + FBReactNativeSpec: 489f54d2f849db05f2cd70f575eba660f48337b7 Flipper: 26fc4b7382499f1281eb8cb921e5c3ad6de91fe0 Flipper-Boost-iOSX: fd1e2b8cbef7e662a122412d7ac5f5bea715403c Flipper-DoubleConversion: 2dc99b02f658daf147069aad9dbd29d8feb06d30 @@ -602,10 +955,12 @@ SPEC CHECKSUMS: RCTTypeSafety: a01aca2dd3b27fa422d5239252ad38e54e958750 React: 741b4f5187e7a2137b69c88e65f940ba40600b4b React-callinvoker: 72ba74b2d5d690c497631191ae6eeca0c043d9cf - React-Codegen: 8a7cda1633e4940de8a710f6bf5cae5dd673546e + React-Codegen: c7c359cae5b349d5bf1543acd151e6781b1343d5 React-Core: 72bb19702c465b6451a40501a2879532bec9acee React-CoreModules: ffd19b082fc36b9b463fedf30955138b5426c053 React-cxxreact: 8b3dd87e3b8ea96dd4ad5c7bac8f31f1cc3da97f + React-Fabric: 3fc38d31a7153a8a9e0e5de0197b2820c324e7cd + React-graphics: 4506414f51362f05eb96fb054bd019d0b4f55bbb React-hermes: be95942c3f47fc032da1387360413f00dae0ea68 React-jsi: 9978e2a64c2a4371b40e109f4ef30a33deaa9bcb React-jsiexecutor: 18b5b33c5f2687a784a61bc8176611b73524ae77 @@ -614,22 +969,24 @@ SPEC CHECKSUMS: React-perflogger: e706562ab7eb8eb590aa83a224d26fa13963d7f2 React-RCTActionSheet: 57d4bd98122f557479a3359ad5dad8e109e20c5a React-RCTAnimation: ccf3ef00101ea74bda73a045d79a658b36728a60 - React-RCTAppDelegate: d0c28a35c65e9a0aef287ac0dafe1b71b1ac180c + React-RCTAppDelegate: 757d21537189b573eec42b8f0df886fd8bd57a80 React-RCTBlob: 1700b92ece4357af0a49719c9638185ad2902e95 + React-RCTFabric: e6f198f05196b684964d64994b4d21baa538b89b React-RCTImage: f2e4904566ccccaa4b704170fcc5ae144ca347bf React-RCTLinking: 52a3740e3651e30aa11dff5a6debed7395dd8169 React-RCTNetwork: ea0976f2b3ffc7877cd7784e351dc460adf87b12 React-RCTSettings: ed5ac992b23e25c65c3cc31f11b5c940ae5e3e60 React-RCTText: c9dfc6722621d56332b4f3a19ac38105e7504145 React-RCTVibration: f09f08de63e4122deb32506e20ca4cae6e4e14c1 + React-rncore: fcf3558280eaeac247a24b81c8715aa351ae3172 React-runtimeexecutor: 4817d63dbc9d658f8dc0ec56bd9b83ce531129f0 ReactCommon: 08723d2ed328c5cbcb0de168f231bc7bae7f8aa1 RNFS: 4ac0f0ea233904cb798630b3c077808c06931688 SocketRocket: fccef3f9c5cedea1353a9ef6ada904fde10d6608 - whisper-rn: f0ba4131fcf8c1ff7568049d6f833ee3f7cf1ccd + whisper-rn: 2db2242a3ca98a89f350075ef08dee94c870253a Yoga: f7decafdc5e8c125e6fa0da38a687e35238420fa YogaKit: f782866e155069a2cca2517aafea43200b01fd5a -PODFILE CHECKSUM: 74c803fe52ead33303df8797ef6aec85cc2771da +PODFILE CHECKSUM: 677b457bf12eed8992064019165a3f085d943e70 COCOAPODS: 1.11.3 diff --git a/example/ios/RNWhisperExample.xcodeproj/xcshareddata/xcschemes/RNWhisperExample.xcscheme b/example/ios/RNWhisperExample.xcodeproj/xcshareddata/xcschemes/RNWhisperExample.xcscheme index 42ba0bb..9227461 100644 --- a/example/ios/RNWhisperExample.xcodeproj/xcshareddata/xcschemes/RNWhisperExample.xcscheme +++ b/example/ios/RNWhisperExample.xcodeproj/xcshareddata/xcschemes/RNWhisperExample.xcscheme @@ -41,7 +41,7 @@ #include +#ifdef RCT_NEW_ARCH_ENABLED +#import +#endif + @implementation RNWhisper NSMutableDictionary *contexts; RCT_EXPORT_MODULE() ++ (BOOL)requiresMainQueueSetup +{ + return YES; +} + - (NSDictionary *)constantsToExport { return @{ #if WHISPER_USE_COREML - @"WHISPER_USE_COREML": @YES, + @"useCoreML": @YES, +#else + @"useCoreML": @NO, #endif #if WHISPER_COREML_ALLOW_FALLBACK - @"WHISPER_COREML_ALLOW_FALLBACK": @YES, + @"coreMLAllowFallback": @YES, +#else + @"coreMLAllowFallback": @NO, #endif }; } @@ -208,4 +221,12 @@ - (void)invalidate { contexts = nil; } +#ifdef RCT_NEW_ARCH_ENABLED +- (std::shared_ptr)getTurboModule: + (const facebook::react::ObjCTurboModule::InitParams &)params +{ + return std::make_shared(params); +} +#endif + @end diff --git a/package.json b/package.json index a34a8f9..5c11257 100644 --- a/package.json +++ b/package.json @@ -136,5 +136,10 @@ } ] ] + }, + "codegenConfig": { + "name": "RNWhisperSpec", + "type": "all", + "jsSrcsDir": "./src/" } } diff --git a/src/NativeRNWhisper.ts b/src/NativeRNWhisper.ts new file mode 100644 index 0000000..9d0fd86 --- /dev/null +++ b/src/NativeRNWhisper.ts @@ -0,0 +1,67 @@ +import type { TurboModule } from 'react-native/Libraries/TurboModule/RCTExport' +import { TurboModuleRegistry } from 'react-native' + +export type TranscribeOptions = { + /** Spoken language (Default: 'auto' for auto-detect) */ + language?: string, + /** Translate from source language to english (Default: false) */ + translate?: boolean, + /** Number of threads to use during computation (Default: 2 for 4-core devices, 4 for more cores) */ + maxThreads?: number, + /** Maximum number of text context tokens to store */ + maxContext?: number, + /** Maximum segment length in characters */ + maxLen?: number, + /** Enable token-level timestamps */ + tokenTimestamps?: boolean, + /** Word timestamp probability threshold */ + wordThold?: number, + /** Time offset in milliseconds */ + offset?: number, + /** Duration of audio to process in milliseconds */ + duration?: number, + /** Tnitial decoding temperature */ + temperature?: number, + temperatureInc?: number, + /** Beam size for beam search */ + beamSize?: number, + /** Number of best candidates to keep */ + bestOf?: number, + /** Speed up audio by x2 (reduced accuracy) */ + speedUp?: boolean, + /** Initial Prompt */ + prompt?: string, +} + +export type TranscribeResult = { + result: string, + segments: Array<{ + text: string, + t0: number, + t1: number, + }>, +} + +export interface Spec extends TurboModule { + getConstants(): { + useCoreML: boolean + coreMLAllowFallback: boolean + }; + initContext(filePath: string, isBundleAsset: boolean): Promise; + releaseContext(contextId: number): Promise; + releaseAllContexts(): Promise; + transcribeFile( + contextId: number, + jobId: number, + path: string, + options: TranscribeOptions, + ): Promise; + startRealtimeTranscribe( + contextId: number, + jobId: number, + options: TranscribeOptions, + ): Promise; + abortTranscribe(contextId: number, jobId: number): Promise; +} + +export default TurboModuleRegistry.get('RNWhisper') as Spec diff --git a/src/__tests__/index.test.ts b/src/__tests__/index.test.ts index 6ef8932..ba68fde 100644 --- a/src/__tests__/index.test.ts +++ b/src/__tests__/index.test.ts @@ -5,7 +5,9 @@ jest.mock('..', () => require('../../jest/mock')) Math.random = () => 0.5 test('Mock', async () => { - const context = await initWhisper() + const context = await initWhisper({ + filePath: 'test.bin', + }) expect(context.id).toBe(1) const { promise } = context.transcribe('test.wav') expect(await promise).toEqual({ diff --git a/src/index.ts b/src/index.ts index 5e46841..e7bb186 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,70 +1,27 @@ import { NativeEventEmitter, DeviceEventEmitter, - NativeModules, Platform, DeviceEventEmitterStatic, } from 'react-native' +import RNWhisper from './NativeRNWhisper' +import type { TranscribeOptions, TranscribeResult } from './NativeRNWhisper' import { version } from './version.json' -const LINKING_ERROR = - `The package 'whisper.rn' doesn't seem to be linked. Make sure: \n\n${Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) - }- You rebuilt the app after installing the package` - -const RNWhisper = NativeModules.RNWhisper - ? NativeModules.RNWhisper - : new Proxy( - {}, - { - get() { - throw new Error(LINKING_ERROR) - }, - }, - ) - let EventEmitter: NativeEventEmitter | DeviceEventEmitterStatic if (Platform.OS === 'ios') { + // @ts-ignore EventEmitter = new NativeEventEmitter(RNWhisper) } if (Platform.OS === 'android') { EventEmitter = DeviceEventEmitter } +export type { TranscribeOptions, TranscribeResult } + const EVENT_ON_REALTIME_TRANSCRIBE = '@RNWhisper_onRealtimeTranscribe' const EVENT_ON_REALTIME_TRANSCRIBE_END = '@RNWhisper_onRealtimeTranscribeEnd' -export type TranscribeOptions = { - /** Spoken language (Default: 'auto' for auto-detect) */ - language?: string, - /** Translate from source language to english (Default: false) */ - translate?: boolean, - /** Number of threads to use during computation (Default: 2 for 4-core devices, 4 for more cores) */ - maxThreads?: number, - /** Maximum number of text context tokens to store */ - maxContext?: number, - /** Maximum segment length in characters */ - maxLen?: number, - /** Enable token-level timestamps */ - tokenTimestamps?: boolean, - /** Word timestamp probability threshold */ - wordThold?: number, - /** Time offset in milliseconds */ - offset?: number, - /** Duration of audio to process in milliseconds */ - duration?: number, - /** Tnitial decoding temperature */ - temperature?: number, - temperatureInc?: number, - /** Beam size for beam search */ - beamSize?: number, - /** Number of best candidates to keep */ - bestOf?: number, - /** Speed up audio by x2 (reduced accuracy) */ - speedUp?: boolean, - /** Initial Prompt */ - prompt?: string, -} - export type TranscribeRealtimeOptions = TranscribeOptions & { /** * Realtime record max duration in seconds. @@ -80,15 +37,6 @@ export type TranscribeRealtimeOptions = TranscribeOptions & { realtimeAudioSliceSec?: number } -export type TranscribeResult = { - result: string, - segments: Array<{ - text: string, - t0: number, - t1: number, - }>, -} - export type TranscribeRealtimeEvent = { contextId: number, jobId: number, @@ -257,7 +205,7 @@ export class WhisperContext { } export async function initWhisper( - { filePath, isBundleAsset }: { filePath?: string, isBundleAsset?: boolean } = {} + { filePath, isBundleAsset }: { filePath: string; isBundleAsset?: boolean } ): Promise { const id = await RNWhisper.initContext(filePath, !!isBundleAsset) return new WhisperContext(id) @@ -270,8 +218,10 @@ export async function releaseAllWhisper(): Promise { /** Current version of whisper.cpp */ export const libVersion: string = version +const { useCoreML, coreMLAllowFallback } = RNWhisper.getConstants?.() || {} + /** Is use CoreML models on iOS */ -export const isUseCoreML: boolean = !!RNWhisper.WHISPER_USE_COREML +export const isUseCoreML: boolean = !!useCoreML /** Is allow fallback to CPU if load CoreML model failed */ -export const isCoreMLAllowFallback: boolean = !!RNWhisper.WHISPER_COREML_ALLOW_FALLBACK +export const isCoreMLAllowFallback: boolean = !!coreMLAllowFallback