alphacep / vosk-flutter

Apache License 2.0
50 stars 30 forks source link

How can I get VOSK-IOS connected to flutter? #2

Open furqanhermawan23 opened 2 years ago

furqanhermawan23 commented 2 years ago

Hi,

How can I get VOSK-IOS connected to flutter?

Thank you

nshmyrev commented 2 years ago

You need to mail us at contact@alphacephei.com and provide information about your project and will share the build.

misbullah commented 2 years ago

Hi @nshmyrev,

We got the package through email.

Thanks Alim

jaumard commented 2 years ago

as flutter/dart support FFI, if you provide that implementation you can support iOS, android, linux, windows and Mac for free with one implementation could be huge ^^

nshmyrev commented 2 years ago

@jaumard thank you, we'd love to have it as well. Working on iOS now

gfhjjk commented 1 year ago

hello,@nshmyrev I think your vosk is very good,I also want a ios_flutter_plugin.I mail you two days ago. Do you receiver it? Thank you

abarragan-millicom commented 1 year ago

Publication of Flutter-iOS demo will be extremely appreciated!

jtkeyva commented 1 year ago

+1 for working iOS example :) awesome work!

sergsavchuk commented 1 year ago

@nshmyrev Since Flutter's main platforms are still Android&iOS it would be great to implement iOS support. We could for example serve an iOS binary at https://alphacephei.com/vosk/binaries/ios/vosk-ios-0.3.45.zip and restrict access to everyone except those who received an ACCESS_KEY after contacting contact@alphacephei.com. This will require just a simple web server that will check the ACCESS_KEY using any connected DB.

wunderbrick commented 10 months ago

@nshmyrev I sent an email but haven't heard back. Is getting the iOS demo still a possibility?

nshmyrev commented 10 months ago

@wunderbrick I'm sorry I can't associate you with the emails we've got. If you haven't got the link already, drop another email please with your github account mentioned.

nshmyrev commented 10 months ago

Since Flutter's main platforms are still Android&iOS it would be great to implement iOS support.

its ok, most guys who request the package get it. No need for access keys, the library is fully offline.

wunderbrick commented 10 months ago

@nshmyrev Thanks for your reply. I sent another email as a reply to my original email with my Github username. My email address is the Tutanota address that starts with lambda.

jtdLab commented 7 months ago

What is the recommended way to integrate the vosk ios build into a flutter app?

sergsavchuk commented 7 months ago

@jtdLab Hi, unfortunately, the iOS part has not been implemented yet. You can implement it yourself and do PR. But first, you should get the iOS lib by contacting contact@alphacephei.com

jtdLab commented 7 months ago

Hi thanks for the reply.

I obtained the iOS library. When attempting to use the .h file describing the API through FFI, i encountered missing symbols "image not found" error thrown by dart:ffi.

Following this here's what I've tried:

  1. Cloned the vosk-flutter repository.
  2. Added the libvosk.xcframework to ios/Frameworks.
  3. Added a forward.h to ios/Classes that forwards to vosk-api/src/vosk-api.h (necessary because .podspec does not support relative paths).
  4. Added the framework to .podspec so it looks like this:
# To learn more about a Podspec, see http://guides.cocoapods.org/syntax/podspec.html
Pod::Spec.new do |s|
  s.name             = 'vosk_flutter'
  s.version          = '0.0.1'
  s.summary          = 'An iOS implementation of the vosk plugin.'
  s.description      = <<-DESC
  An iOS implementation of the vosk plugin.
                       DESC
  s.homepage         = 'http://example.com'
  s.license          = { :type => 'BSD', :file => '../LICENSE' }
  s.author           = { 'Your Company' => 'email@example.com' }
  # This ensures that the source files in Classes/ are included in the native builds of apps using this FFI plugin. Podspec does not support relative paths, so Classes contains a forwarder C file that relatively imports `../../vosk-api/src/*` so that the C sources can be shared among all target platforms.
  s.source           = { :path => '.' }
  s.public_header_files = 'Classes/**/*.h'
  s.dependency 'Flutter'
  s.platform = :ios, '9.0'

  # Flutter.framework does not contain an i386 slice.
  s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES', 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'i386' }
  s.swift_version = '5.0'
  s.vendored_frameworks = 'Frameworks/libvosk.xcframework'
end

In Dart, I only edited src/vosk_flutter.dart: a) _supportsFFI to return true on iOS. b) _loadVoskLibrary to load the library via FFI using DynamicLibrary.executable() to load the static libvosk.

Inside pubspec.yaml, I enabled iOS as an FFI plugin, similar to what was done with Linux and Windows already.

While investigating the current Android implementation, I'm also considering mirroring it, as it seems to use multiple threads. Is this required to not get lags in the UI?

sergsavchuk commented 7 months ago

@jtdLab Firstly, there's no need to use vosk-api.h, because it's only used to generate generated_vosk_bindings.dart. Secondly, looks like you need to do the same thing I did for Linux according to the information: image

And you'll have something like this in Dart:

    String libraryPath;
    if (Platform.isLinux || Platform.isIOS) {
      libraryPath = Platform.environment['LIBVOSK_PATH']!;
    } ....

Thirdly, yes, we use multiple threads in Android to not block the main thread. Btw, there's a branch with Android FFI implemented.

jtdLab commented 7 months ago

@sergsavchuk Thanks a lot for the response Im still running into an error following the guide u mentioned using the following my_flutter_app/ios/libvosk.podspec

Inspired by this and this

Pod::Spec.new do |s|
    s.name             = 'libvosk'
    s.version          = '0.3.38'
    s.summary          = 'Vosk iOS.'
    s.description      = "Vosk for iOS."
    s.homepage         = 'http://example.com'
    s.license          = { :type => 'BSD', :file => '../LICENSE' }
    s.author           = { 'Your Company' => 'email@example.com' }
    s.platform = :ios, '9.0'
    s.source           = { :http => "https://alphacephei.com/path/to/vosk/api.zip" }
    s.vendored_frameworks = "**/libvosk.xcframework"
  end

Hint: Same result when using s.source = { :path => '.' } and copy the libvosk.xcframework next to the my_flutter_app/ios/libvosk.podspec.

to include the binaries but its not getting added to the pods projects. I also had to put the my_flutter_app/ios/libvosk.podspec into the app and reference it inside my_flutter_app/ios/Podfile to make pod install work (which is not good as it should be included inside the vosk_flutter plugin and not a consuming app like my_flutter_app).

The vosk_flutter/ios/vosk_flutter.podspec looks like this:

Pod::Spec.new do |s|
    s.name             = 'vosk_flutter'
    s.version          = '0.0.1'
    s.summary          = 'An iOS implementation of the vosk plugin.'
    s.description      = <<-DESC
    An iOS implementation of the vosk plugin.
                         DESC
    s.homepage         = 'http://example.com'
    s.license          = { :type => 'BSD', :file => '../LICENSE' }
    s.author           = { 'Your Company' => 'email@example.com' }
    s.source           = { :path => '.' }
    s.source_files     = 'Classes/**/*'
    s.dependency 'Flutter'
    s.dependency 'libvosk'
    s.platform = :ios, '11.0'

    # Flutter.framework does not contain an i386 slice.
    s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES', 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'i386' }
    s.swift_version = '5.0'
  end

The vosk_flutter/ios/Classes/VoskFlutterPlugin.swift looks like:

import Flutter

public class VoskFlutterPlugin: NSObject, FlutterPlugin {
  public static func register(with registrar: FlutterPluginRegistrar) {
    setenv("LIBVOSK_PATH", Bundle.main.privateFrameworksPath! + "/libvosk.framework/libvosk", 0);
    let channel = FlutterMethodChannel(name: "vosk_flutter", binaryMessenger: registrar.messenger())
    let instance = VoskFlutterPlugin()
    registrar.addMethodCallDelegate(instance, channel: channel)
  }
}

The vosk_flutter/pubspec.yaml looks like this

...
flutter:
  plugin:
    platforms:
...
      ios:
        pluginClass: VoskFlutterPlugin
        ffiPlugin: true
...

Hint: To read the env var set on the native side from dart i had to use stdlibc instead of Platform.environment (which returned an empty map). So lib/src/vosk_flutter.dart was changed to

if (Platform.isLinux || Platform.isIOS) {
 libraryPath = getenv('LIBVOSK_PATH')!;
}

Help would be appreciated.

jtdLab commented 6 months ago

@nshmyrev is it possible to get a version of vosk-ios containing dynamic libs instead of static ones. Dart FFI seems to not support static libs.

mateuszboryn commented 4 months ago

Hi, can I get the iOS library, please? Last week I sent the request to the email mentioned above, but not response yet.

dkjazz commented 4 months ago

I am also interested in iOS support by this plugin.

@jtdLab do you mind sharing a repo with the changes you tried? I can play with it as well to see if we can make it work