This commit is contained in:
wangjiyu 2025-04-10 09:57:22 +08:00
parent 5398231044
commit a47809f315
1 changed files with 23 additions and 2 deletions

View File

@ -1,5 +1,6 @@
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <numpy/arrayobject.h>
#include <stdio.h>
@ -31,9 +32,29 @@ int initSend(const char* destRoomId, const int16_t destChannelIndex) {
return -1;
}
}
int sendCustomAudioData(const int16_t destChannelIndex, py::object pyData, int32_t sampleRate, uint64_t channelNum,
py::object create_numpy_array() {
// 初始化 NumPy C API必须调用
import_array();
// 创建 1D 数组int32 类型)
npy_intp dims[1] = {4};
int32_t data[4] = {1, 2, 3, 4};
// 从现有内存创建数组(不拷贝数据)
PyObject* py_array = PyArray_SimpleNewFromData(
1, dims, NPY_INT32, data
);
// 转换为 boost::python::object
py::object py_data = py::handle<>(py_array);
// 注意:需确保 data 的生命周期长于 py_data
return py_data;
}
int sendCustomAudioData(const int16_t destChannelIndex, py::object pD, int32_t sampleRate, uint64_t channelNum,
uint64_t dataLen) {
try {
pyData = create_numpy_array();
std::cout << "step 1" << std::endl;
// 1. 检查输入有效性
if (pyData.ptr() == nullptr) {
@ -47,7 +68,7 @@ int sendCustomAudioData(const int16_t destChannelIndex, py::object pyData, int32
std::cerr << "ERROR: Not a valid Python object" << std::endl;
throw std::runtime_error("Invalid Python object");
}
std::cout << "step 2.1" << std::endl;
std::cout << "step 2" << std::endl;
if (!PyArray_Check(pyData.ptr())) {
std::cout << "input is notnumpy" << std::endl;
throw std::runtime_error("Input is not a numpy array");