offline-stream.cc
2.3 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// sherpa-onnx/python/csrc/offline-stream.cc
//
// Copyright (c) 2023 by manyeyes
#include "sherpa-onnx/python/csrc/offline-stream.h"
#include <vector>
#include "sherpa-onnx/csrc/offline-stream.h"
namespace sherpa_onnx {
constexpr const char *kAcceptWaveformUsage = R"(
Process audio samples.
Args:
sample_rate:
Sample rate of the input samples. If it is different from the one
expected by the model, we will do resampling inside.
waveform:
A 1-D float32 tensor containing audio samples. It must be normalized
to the range [-1, 1].
)";
static void PybindOfflineRecognitionResult(py::module *m) { // NOLINT
using PyClass = OfflineRecognitionResult;
py::class_<PyClass>(*m, "OfflineRecognitionResult")
.def("__str__", &PyClass::AsJsonString)
.def_property_readonly(
"text",
[](const PyClass &self) -> py::str {
return py::str(PyUnicode_DecodeUTF8(self.text.c_str(),
self.text.size(), "ignore"));
})
.def_property_readonly("lang",
[](const PyClass &self) { return self.lang; })
.def_property_readonly("emotion",
[](const PyClass &self) { return self.emotion; })
.def_property_readonly("event",
[](const PyClass &self) { return self.event; })
.def_property_readonly("tokens",
[](const PyClass &self) { return self.tokens; })
.def_property_readonly("words",
[](const PyClass &self) { return self.words; })
.def_property_readonly("timestamps",
[](const PyClass &self) { return self.timestamps; })
.def_property_readonly("durations",
[](const PyClass &self) { return self.durations; });
}
void PybindOfflineStream(py::module *m) {
PybindOfflineRecognitionResult(m);
using PyClass = OfflineStream;
py::class_<PyClass>(*m, "OfflineStream")
.def(
"accept_waveform",
[](PyClass &self, float sample_rate,
const std::vector<float> &waveform) {
self.AcceptWaveform(sample_rate, waveform.data(), waveform.size());
},
py::arg("sample_rate"), py::arg("waveform"), kAcceptWaveformUsage,
py::call_guard<py::gil_scoped_release>())
.def_property_readonly("result", &PyClass::GetResult);
}
} // namespace sherpa_onnx