Add Python ASR examples with alsa (#646)

This commit is contained in:
Fangjun Kuang
2024-03-08 11:34:48 +08:00
committed by GitHub
parent e9e8d755d9
commit d3287f9494
12 changed files with 326 additions and 10 deletions

View File

@@ -1,6 +1,6 @@
include_directories(${CMAKE_SOURCE_DIR})
pybind11_add_module(_sherpa_onnx
set(srcs
circular-buffer.cc
display.cc
endpoint.cc
@@ -37,6 +37,13 @@ pybind11_add_module(_sherpa_onnx
vad-model.cc
voice-activity-detector.cc
)
if(SHERPA_ONNX_HAS_ALSA)
list(APPEND srcs ${CMAKE_SOURCE_DIR}/sherpa-onnx/csrc/alsa.cc alsa.cc)
else()
list(APPEND srcs faked-alsa.cc)
endif()
pybind11_add_module(_sherpa_onnx ${srcs})
if(APPLE)
execute_process(
@@ -54,6 +61,14 @@ endif()
target_link_libraries(_sherpa_onnx PRIVATE sherpa-onnx-core)
if(SHERPA_ONNX_HAS_ALSA)
if(DEFINED ENV{SHERPA_ONNX_ALSA_LIB_DIR})
target_link_libraries(_sherpa_onnx PRIVATE -L$ENV{SHERPA_ONNX_ALSA_LIB_DIR} -lasound)
else()
target_link_libraries(_sherpa_onnx PRIVATE asound)
endif()
endif()
install(TARGETS _sherpa_onnx
DESTINATION ../
)

View File

@@ -0,0 +1,30 @@
// sherpa-onnx/python/csrc/alsa.cc
//
// Copyright (c) 2024 Xiaomi Corporation
#include "sherpa-onnx/python/csrc/alsa.h"
#include <vector>
#include "sherpa-onnx/csrc/alsa.h"
namespace sherpa_onnx {
void PybindAlsa(py::module *m) {
using PyClass = Alsa;
py::class_<PyClass>(*m, "Alsa")
.def(py::init<const char *>(), py::arg("device_name"),
py::call_guard<py::gil_scoped_release>())
.def(
"read",
[](PyClass &self, int32_t num_samples) -> std::vector<float> {
return self.Read(num_samples);
},
py::arg("num_samples"), py::call_guard<py::gil_scoped_release>())
.def_property_readonly("expected_sample_rate",
&PyClass::GetExpectedSampleRate)
.def_property_readonly("actual_sample_rate",
&PyClass::GetActualSampleRate);
}
} // namespace sherpa_onnx

View File

@@ -0,0 +1,16 @@
// sherpa-onnx/python/csrc/alsa.h
//
// Copyright (c) 2024 Xiaomi Corporation
#ifndef SHERPA_ONNX_PYTHON_CSRC_ALSA_H_
#define SHERPA_ONNX_PYTHON_CSRC_ALSA_H_
#include "sherpa-onnx/python/csrc/sherpa-onnx.h"
namespace sherpa_onnx {
void PybindAlsa(py::module *m);
} // namespace sherpa_onnx
#endif // SHERPA_ONNX_PYTHON_CSRC_ALSA_H_

View File

@@ -0,0 +1,47 @@
// sherpa-onnx/python/csrc/faked-alsa.cc
//
// Copyright (c) 2024 Xiaomi Corporation
#include "sherpa-onnx/csrc/macros.h"
#include "sherpa-onnx/python/csrc/alsa.h"
namespace sherpa_onnx {
class FakedAlsa {
public:
explicit FakedAlsa(const char *) {
SHERPA_ONNX_LOGE("This function is for Linux only.");
#if (SHERPA_ONNX_ENABLE_ALSA == 0) && (defined(__unix__) || defined(__unix))
SHERPA_ONNX_LOGE(R"doc(
sherpa-onnx is compiled without alsa support. To enable that, please run
(1) sudo apt-get install alsa-utils libasound2-dev
(2) rebuild sherpa-onnx
)doc");
#endif
exit(-1);
}
std::vector<float> Read(int32_t) const { return {}; }
int32_t GetExpectedSampleRate() const { return -1; }
int32_t GetActualSampleRate() const { return -1; }
};
void PybindAlsa(py::module *m) {
using PyClass = FakedAlsa;
py::class_<PyClass>(*m, "Alsa")
.def(py::init<const char *>(), py::arg("device_name"))
.def(
"read",
[](PyClass &self, int32_t num_samples) -> std::vector<float> {
return self.Read(num_samples);
},
py::arg("num_samples"), py::call_guard<py::gil_scoped_release>())
.def_property_readonly("expected_sample_rate",
&PyClass::GetExpectedSampleRate)
.def_property_readonly("actual_sample_rate",
&PyClass::GetActualSampleRate);
}
} // namespace sherpa_onnx
#endif // SHERPA_ONNX_PYTHON_CSRC_FAKED_ALSA_H_

View File

@@ -4,6 +4,7 @@
#include "sherpa-onnx/python/csrc/sherpa-onnx.h"
#include "sherpa-onnx/python/csrc/alsa.h"
#include "sherpa-onnx/python/csrc/circular-buffer.h"
#include "sherpa-onnx/python/csrc/display.h"
#include "sherpa-onnx/python/csrc/endpoint.h"
@@ -54,6 +55,8 @@ PYBIND11_MODULE(_sherpa_onnx, m) {
PybindOfflineTts(&m);
PybindSpeakerEmbeddingExtractor(&m);
PybindSpeakerEmbeddingManager(&m);
PybindAlsa(&m);
}
} // namespace sherpa_onnx