This commit is contained in:
wangjiyu 2025-04-16 19:30:53 +08:00
parent d910833a9c
commit af0a29f3dc
4 changed files with 14 additions and 11 deletions

View File

@ -56,8 +56,8 @@ namespace np = boost::python::numpy;
np::ndarray getNumpyData() {
return RTCContext::instance().getNumpyData();
}
bp::bytes getByteData() {
return RTCContext::instance().getByteData();
std::vector<int16_t> getListData() {
return RTCContext::instance().getListData();
}
int16_t getDataCount() {
return RTCContext::instance().getDataCount();
@ -216,7 +216,7 @@ BOOST_PYTHON_MODULE(rtc_plugins) {
py::def("getSize", &getSize);
py::def("getData", &getData);
py::def("getNumpyData", &getNumpyData);
py::def("getByteData", &getByteData);
py::def("getListData", &getListData);
py::def("getDataCount", &getDataCount);
} catch (...) {
PyErr_SetString(PyExc_RuntimeError, "Module initialization failed");

View File

@ -45,7 +45,7 @@ while True:
size = rtc_plugins.getSize()
print(f"data size:{size}")
#frame = rtc_plugins.getNumpyData()
frame = rtc_plugins.getByteData()
frame = rtc_plugins.getListData()
print("get frame")
samples = struct.unpack(f"{len(data_bytes)//2}h", data_bytes)
#print(f"get samples:{samples}")

View File

@ -706,7 +706,7 @@ np::ndarray RTCContext::getNumpyData() {
std::lock_guard<std::mutex> lock(dataMutex_);
RetAudioFrame frame = getData();
std::cout << "step2" << std::endl;
int16_t* data_ptr = frame.data.get(); // 你的数据指针
int16_t* dataPtr = frame.data.get(); // 你的数据指针
std::cout << "step3" << std::endl;
size_t length = frame.dataCount; // 数据长度
std::cout << "step4" << std::endl;
@ -714,11 +714,11 @@ np::ndarray RTCContext::getNumpyData() {
PyGILState_STATE gstate = PyGILState_Ensure();
np::ndarray result = np::empty(bp::make_tuple(length), np::dtype::get_builtin<int16_t>());
try {
if (!data_ptr || length == 0) {
if (!dataPtr || length == 0) {
result = np::zeros(bp::make_tuple(0), np::dtype::get_builtin<int16_t>());
} else {
result = np::empty(bp::make_tuple(length), np::dtype::get_builtin<int16_t>());
std::memcpy(result.get_data(), data_ptr, length * sizeof(int16_t));
std::memcpy(result.get_data(), dataPtr, length * sizeof(int16_t));
}
} catch (...) {
PyGILState_Release(gstate); // 异常时释放GIL
@ -728,16 +728,19 @@ np::ndarray RTCContext::getNumpyData() {
return result;
}
bp::bytes RTCContext::getByteData() {
std::vector<int16_t> RTCContext::getListData() {
std::cout << "step1" << std::endl;
std::lock_guard<std::mutex> lock(dataMutex_);
RetAudioFrame frame = getData();
std::cout << "step2" << std::endl;
int16_t* data_ptr = frame.data.get(); // 你的数据指针
int16_t* dataPtr = frame.data.get(); // 你的数据指针
std::cout << "step3" << std::endl;
size_t length = frame.dataCount; // 数据长度
std::cout << "step4" << std::endl;
return bp::bytes(reinterpret_cast<char*>(data_ptr), length * sizeof(int16_t));
if (!dataPtr || length == 0) {
return {}; // 返回空 vector
}
return std::vector<int16_t>(dataPtr, dataPtr + length); // 深拷贝
}
int16_t RTCContext::getDataCount() {
std::lock_guard<std::mutex> lock(dataMutex_);

View File

@ -101,7 +101,7 @@ public:
void setData(const mrtc::MRTCAudioFrame& frame);
RetAudioFrame getData();
np::ndarray getNumpyData();
bp::bytes getByteData();
std::vector<int16_t> getListData();
int16_t getDataCount();
void* getpData() const;