online_stream.dart
1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright (c) 2024 Xiaomi Corporation
import 'dart:ffi';
import 'dart:typed_data';
import 'package:ffi/ffi.dart';
import './sherpa_onnx_bindings.dart';
class OnlineStream {
/// The user has to call OnlineStream.free() to avoid memory leak.
OnlineStream({required this.ptr});
void free() {
SherpaOnnxBindings.destroyOnlineStream?.call(ptr);
ptr = nullptr;
}
/// If you have List<double> data, then you can use
/// Float32List.fromList(data) to convert data to Float32List
///
/// See
/// https://api.flutter.dev/flutter/dart-core/List-class.html
/// and
/// https://api.flutter.dev/flutter/dart-typed_data/Float32List-class.html
void acceptWaveform({required Float32List samples, required int sampleRate}) {
final n = samples.length;
final Pointer<Float> p = calloc<Float>(n);
final pList = p.asTypedList(n);
pList.setAll(0, samples);
SherpaOnnxBindings.onlineStreamAcceptWaveform?.call(ptr, sampleRate, p, n);
calloc.free(p);
}
void inputFinished() {
SherpaOnnxBindings.onlineStreamInputFinished?.call(this.ptr);
}
Pointer<SherpaOnnxOnlineStream> ptr;
}