2023-02-19 19:36:03 +08:00
|
|
|
// sherpa-onnx/python/csrc/online-stream.cc
|
|
|
|
|
//
|
|
|
|
|
// Copyright (c) 2023 Xiaomi Corporation
|
|
|
|
|
|
|
|
|
|
#include "sherpa-onnx/python/csrc/online-stream.h"
|
2024-04-08 21:36:47 +08:00
|
|
|
|
|
|
|
|
#include <vector>
|
2023-02-19 19:36:03 +08:00
|
|
|
|
|
|
|
|
#include "sherpa-onnx/csrc/online-stream.h"
|
|
|
|
|
|
|
|
|
|
namespace sherpa_onnx {
|
|
|
|
|
|
2023-03-03 16:42:33 +08:00
|
|
|
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].
|
|
|
|
|
)";
|
|
|
|
|
|
2025-03-04 12:41:09 +01:00
|
|
|
|
|
|
|
|
constexpr const char *kGetFramesUsage = R"(
|
|
|
|
|
Get n frames starting from the given frame index.
|
|
|
|
|
(hint: intended for debugging, for comparing FBANK features across pipelines)
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
frame_index:
|
|
|
|
|
The starting frame index
|
|
|
|
|
n:
|
|
|
|
|
Number of frames to get.
|
|
|
|
|
Return:
|
|
|
|
|
Return a 2-D tensor of shape (n, feature_dim).
|
|
|
|
|
which is flattened into a 1-D vector (flattened in row major).
|
|
|
|
|
Unflatten in python with:
|
|
|
|
|
`features = np.reshape(arr, (n, feature_dim))`
|
|
|
|
|
)";
|
|
|
|
|
|
2023-02-19 19:36:03 +08:00
|
|
|
void PybindOnlineStream(py::module *m) {
|
|
|
|
|
using PyClass = OnlineStream;
|
|
|
|
|
py::class_<PyClass>(*m, "OnlineStream")
|
2023-03-03 16:42:33 +08:00
|
|
|
.def(
|
|
|
|
|
"accept_waveform",
|
2024-04-08 17:22:48 +08:00
|
|
|
[](PyClass &self, float sample_rate,
|
|
|
|
|
const std::vector<float> &waveform) {
|
2023-03-03 16:42:33 +08:00
|
|
|
self.AcceptWaveform(sample_rate, waveform.data(), waveform.size());
|
|
|
|
|
},
|
2023-11-27 13:44:03 +08:00
|
|
|
py::arg("sample_rate"), py::arg("waveform"), kAcceptWaveformUsage,
|
|
|
|
|
py::call_guard<py::gil_scoped_release>())
|
|
|
|
|
.def("input_finished", &PyClass::InputFinished,
|
2025-03-04 12:41:09 +01:00
|
|
|
py::call_guard<py::gil_scoped_release>())
|
|
|
|
|
.def("get_frames", &PyClass::GetFrames,
|
|
|
|
|
py::arg("frame_index"), py::arg("n"), kGetFramesUsage,
|
2023-11-27 13:44:03 +08:00
|
|
|
py::call_guard<py::gil_scoped_release>());
|
2023-02-19 19:36:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace sherpa_onnx
|