Grey Faulkenberry, MD MPH
Committed by GitHub

Flutter OnlinePunctuation (#1854)

@@ -5,12 +5,13 @@ import 'dart:ffi'; @@ -5,12 +5,13 @@ import 'dart:ffi';
5 export 'src/audio_tagging.dart'; 5 export 'src/audio_tagging.dart';
6 export 'src/feature_config.dart'; 6 export 'src/feature_config.dart';
7 export 'src/keyword_spotter.dart'; 7 export 'src/keyword_spotter.dart';
  8 +export 'src/offline_punctuation.dart';
8 export 'src/offline_recognizer.dart'; 9 export 'src/offline_recognizer.dart';
9 export 'src/offline_speaker_diarization.dart'; 10 export 'src/offline_speaker_diarization.dart';
10 export 'src/offline_stream.dart'; 11 export 'src/offline_stream.dart';
  12 +export 'src/online_punctuation.dart';
11 export 'src/online_recognizer.dart'; 13 export 'src/online_recognizer.dart';
12 export 'src/online_stream.dart'; 14 export 'src/online_stream.dart';
13 -export 'src/punctuation.dart';  
14 export 'src/speaker_identification.dart'; 15 export 'src/speaker_identification.dart';
15 export 'src/tts.dart'; 16 export 'src/tts.dart';
16 export 'src/vad.dart'; 17 export 'src/vad.dart';
  1 +import 'dart:ffi';
  2 +import 'package:ffi/ffi.dart';
  3 +
  4 +import './sherpa_onnx_bindings.dart';
  5 +
  6 +class OnlinePunctuationModelConfig {
  7 + OnlinePunctuationModelConfig(
  8 + {required this.cnnBiLstm,
  9 + required this.bpeVocab,
  10 + this.numThreads = 1,
  11 + this.provider = 'cpu',
  12 + this.debug = true});
  13 +
  14 + @override
  15 + String toString() {
  16 + return 'OnlinePunctuationModelConfig(cnnBiLstm: $cnnBiLstm, '
  17 + 'bpeVocab: $bpeVocab, numThreads: $numThreads, '
  18 + 'provider: $provider, debug: $debug)';
  19 + }
  20 +
  21 + final String cnnBiLstm;
  22 + final String bpeVocab;
  23 + final int numThreads;
  24 + final String provider;
  25 + final bool debug;
  26 +}
  27 +
  28 +class OnlinePunctuationConfig {
  29 + OnlinePunctuationConfig({
  30 + required this.model,
  31 + });
  32 +
  33 + @override
  34 + String toString() {
  35 + return 'OnlinePunctuationConfig(model: $model)';
  36 + }
  37 +
  38 + final OnlinePunctuationModelConfig model;
  39 +}
  40 +
  41 +class OnlinePunctuation {
  42 + OnlinePunctuation.fromPtr({required this.ptr, required this.config});
  43 +
  44 + OnlinePunctuation._({required this.ptr, required this.config});
  45 +
  46 + // The user has to invoke OnlinePunctuation.free() to avoid memory leak.
  47 + factory OnlinePunctuation({required OnlinePunctuationConfig config}) {
  48 + final c = calloc<SherpaOnnxOnlinePunctuationConfig>();
  49 +
  50 + final cnnBiLstmPtr = config.model.cnnBiLstm.toNativeUtf8();
  51 + final bpeVocabPtr = config.model.bpeVocab.toNativeUtf8();
  52 + c.ref.model.cnnBiLstm = cnnBiLstmPtr;
  53 + c.ref.model.bpeVocab = bpeVocabPtr;
  54 + c.ref.model.numThreads = config.model.numThreads;
  55 + c.ref.model.debug = config.model.debug ? 1 : 0;
  56 +
  57 + final providerPtr = config.model.provider.toNativeUtf8();
  58 + c.ref.model.provider = providerPtr;
  59 +
  60 + final ptr = SherpaOnnxBindings.sherpaOnnxCreateOnlinePunctuation?.call(c) ??
  61 + nullptr;
  62 +
  63 + // Free the allocated strings and struct memory
  64 + calloc.free(providerPtr);
  65 + calloc.free(cnnBiLstmPtr);
  66 + calloc.free(bpeVocabPtr);
  67 + calloc.free(c);
  68 +
  69 + return OnlinePunctuation._(ptr: ptr, config: config);
  70 + }
  71 +
  72 + void free() {
  73 + SherpaOnnxBindings.sherpaOnnxDestroyOnlinePunctuation?.call(ptr);
  74 + ptr = nullptr;
  75 + }
  76 +
  77 + String addPunct(String text) {
  78 + final textPtr = text.toNativeUtf8();
  79 +
  80 + final p = SherpaOnnxBindings.sherpaOnnxOnlinePunctuationAddPunct
  81 + ?.call(ptr, textPtr) ??
  82 + nullptr;
  83 +
  84 + calloc.free(textPtr);
  85 +
  86 + if (p == nullptr) {
  87 + return '';
  88 + }
  89 +
  90 + final ans = p.toDartString();
  91 +
  92 + SherpaOnnxBindings.sherpaOnnxOnlinePunctuationFreeText?.call(p);
  93 +
  94 + return ans;
  95 + }
  96 +
  97 + Pointer<SherpaOnnxOnlinePunctuation> ptr;
  98 + final OnlinePunctuationConfig config;
  99 +}
@@ -78,6 +78,20 @@ final class SherpaOnnxOfflinePunctuationConfig extends Struct { @@ -78,6 +78,20 @@ final class SherpaOnnxOfflinePunctuationConfig extends Struct {
78 external SherpaOnnxOfflinePunctuationModelConfig model; 78 external SherpaOnnxOfflinePunctuationModelConfig model;
79 } 79 }
80 80
  81 +final class SherpaOnnxOnlinePunctuationModelConfig extends Struct {
  82 + external Pointer<Utf8> cnnBiLstm;
  83 + external Pointer<Utf8> bpeVocab;
  84 + @Int32()
  85 + external int numThreads;
  86 + @Int32()
  87 + external int debug;
  88 + external Pointer<Utf8> provider;
  89 +}
  90 +
  91 +final class SherpaOnnxOnlinePunctuationConfig extends Struct {
  92 + external SherpaOnnxOnlinePunctuationModelConfig model;
  93 +}
  94 +
81 final class SherpaOnnxOfflineZipformerAudioTaggingModelConfig extends Struct { 95 final class SherpaOnnxOfflineZipformerAudioTaggingModelConfig extends Struct {
82 external Pointer<Utf8> model; 96 external Pointer<Utf8> model;
83 } 97 }
@@ -469,6 +483,8 @@ final class SherpaOnnxKeywordSpotterConfig extends Struct { @@ -469,6 +483,8 @@ final class SherpaOnnxKeywordSpotterConfig extends Struct {
469 483
470 final class SherpaOnnxOfflinePunctuation extends Opaque {} 484 final class SherpaOnnxOfflinePunctuation extends Opaque {}
471 485
  486 +final class SherpaOnnxOnlinePunctuation extends Opaque {}
  487 +
472 final class SherpaOnnxAudioTagging extends Opaque {} 488 final class SherpaOnnxAudioTagging extends Opaque {}
473 489
474 final class SherpaOnnxKeywordSpotter extends Opaque {} 490 final class SherpaOnnxKeywordSpotter extends Opaque {}
@@ -512,6 +528,10 @@ typedef SherpaOnnxCreateOfflinePunctuationNative @@ -512,6 +528,10 @@ typedef SherpaOnnxCreateOfflinePunctuationNative
512 = Pointer<SherpaOnnxOfflinePunctuation> Function( 528 = Pointer<SherpaOnnxOfflinePunctuation> Function(
513 Pointer<SherpaOnnxOfflinePunctuationConfig>); 529 Pointer<SherpaOnnxOfflinePunctuationConfig>);
514 530
  531 +typedef SherpaOnnxCreateOnlinePunctuationNative
  532 + = Pointer<SherpaOnnxOnlinePunctuation> Function(
  533 + Pointer<SherpaOnnxOnlinePunctuationConfig>);
  534 +
515 typedef SherpaOnnxOfflineSpeakerDiarizationGetSampleRateNative = Int32 Function( 535 typedef SherpaOnnxOfflineSpeakerDiarizationGetSampleRateNative = Int32 Function(
516 Pointer<SherpaOnnxOfflineSpeakerDiarization>); 536 Pointer<SherpaOnnxOfflineSpeakerDiarization>);
517 537
@@ -605,6 +625,26 @@ typedef SherpaOfflinePunctuationFreeTextNative = Void Function(Pointer<Utf8>); @@ -605,6 +625,26 @@ typedef SherpaOfflinePunctuationFreeTextNative = Void Function(Pointer<Utf8>);
605 625
606 typedef SherpaOfflinePunctuationFreeText = void Function(Pointer<Utf8>); 626 typedef SherpaOfflinePunctuationFreeText = void Function(Pointer<Utf8>);
607 627
  628 +typedef SherpaOnnxCreateOnlinePunctuation
  629 + = SherpaOnnxCreateOnlinePunctuationNative;
  630 +
  631 +typedef SherpaOnnxDestroyOnlinePunctuationNative = Void Function(
  632 + Pointer<SherpaOnnxOnlinePunctuation>);
  633 +
  634 +typedef SherpaOnnxDestroyOnlinePunctuation = void Function(
  635 + Pointer<SherpaOnnxOnlinePunctuation>);
  636 +
  637 +typedef SherpaOnnxOnlinePunctuationAddPunctNative = Pointer<Utf8> Function(
  638 + Pointer<SherpaOnnxOnlinePunctuation>, Pointer<Utf8>);
  639 +
  640 +typedef SherpaOnnxOnlinePunctuationAddPunct
  641 + = SherpaOnnxOnlinePunctuationAddPunctNative;
  642 +
  643 +typedef SherpaOnnxOnlinePunctuationFreeTextNative = Void Function(
  644 + Pointer<Utf8>);
  645 +
  646 +typedef SherpaOnnxOnlinePunctuationFreeText = void Function(Pointer<Utf8>);
  647 +
608 typedef SherpaOnnxCreateAudioTaggingNative = Pointer<SherpaOnnxAudioTagging> 648 typedef SherpaOnnxCreateAudioTaggingNative = Pointer<SherpaOnnxAudioTagging>
609 Function(Pointer<SherpaOnnxAudioTaggingConfig>); 649 Function(Pointer<SherpaOnnxAudioTaggingConfig>);
610 650
@@ -1155,6 +1195,13 @@ class SherpaOnnxBindings { @@ -1155,6 +1195,13 @@ class SherpaOnnxBindings {
1155 static SherpaOfflinePunctuationAddPunct? sherpaOfflinePunctuationAddPunct; 1195 static SherpaOfflinePunctuationAddPunct? sherpaOfflinePunctuationAddPunct;
1156 static SherpaOfflinePunctuationFreeText? sherpaOfflinePunctuationFreeText; 1196 static SherpaOfflinePunctuationFreeText? sherpaOfflinePunctuationFreeText;
1157 1197
  1198 + static SherpaOnnxCreateOnlinePunctuation? sherpaOnnxCreateOnlinePunctuation;
  1199 + static SherpaOnnxDestroyOnlinePunctuation? sherpaOnnxDestroyOnlinePunctuation;
  1200 + static SherpaOnnxOnlinePunctuationAddPunct?
  1201 + sherpaOnnxOnlinePunctuationAddPunct;
  1202 + static SherpaOnnxOnlinePunctuationFreeText?
  1203 + sherpaOnnxOnlinePunctuationFreeText;
  1204 +
1158 static SherpaOnnxCreateAudioTagging? sherpaOnnxCreateAudioTagging; 1205 static SherpaOnnxCreateAudioTagging? sherpaOnnxCreateAudioTagging;
1159 static SherpaOnnxDestroyAudioTagging? sherpaOnnxDestroyAudioTagging; 1206 static SherpaOnnxDestroyAudioTagging? sherpaOnnxDestroyAudioTagging;
1160 static SherpaOnnxAudioTaggingCreateOfflineStream? 1207 static SherpaOnnxAudioTaggingCreateOfflineStream?
@@ -1414,6 +1461,26 @@ class SherpaOnnxBindings { @@ -1414,6 +1461,26 @@ class SherpaOnnxBindings {
1414 'SherpaOfflinePunctuationFreeText') 1461 'SherpaOfflinePunctuationFreeText')
1415 .asFunction(); 1462 .asFunction();
1416 1463
  1464 + sherpaOnnxCreateOnlinePunctuation ??= dynamicLibrary
  1465 + .lookup<NativeFunction<SherpaOnnxCreateOnlinePunctuationNative>>(
  1466 + 'SherpaOnnxCreateOnlinePunctuation')
  1467 + .asFunction();
  1468 +
  1469 + sherpaOnnxDestroyOnlinePunctuation ??= dynamicLibrary
  1470 + .lookup<NativeFunction<SherpaOnnxDestroyOnlinePunctuationNative>>(
  1471 + 'SherpaOnnxDestroyOnlinePunctuation')
  1472 + .asFunction();
  1473 +
  1474 + sherpaOnnxOnlinePunctuationAddPunct ??= dynamicLibrary
  1475 + .lookup<NativeFunction<SherpaOnnxOnlinePunctuationAddPunctNative>>(
  1476 + 'SherpaOnnxOnlinePunctuationAddPunct')
  1477 + .asFunction();
  1478 +
  1479 + sherpaOnnxOnlinePunctuationFreeText ??= dynamicLibrary
  1480 + .lookup<NativeFunction<SherpaOnnxOnlinePunctuationFreeTextNative>>(
  1481 + 'SherpaOnnxOnlinePunctuationFreeText')
  1482 + .asFunction();
  1483 +
1417 sherpaOnnxCreateAudioTagging ??= dynamicLibrary 1484 sherpaOnnxCreateAudioTagging ??= dynamicLibrary
1418 .lookup<NativeFunction<SherpaOnnxCreateAudioTaggingNative>>( 1485 .lookup<NativeFunction<SherpaOnnxCreateAudioTaggingNative>>(
1419 'SherpaOnnxCreateAudioTagging') 1486 'SherpaOnnxCreateAudioTagging')