This commit is contained in:
wangjiyu 2025-04-10 10:29:57 +08:00
parent d1a3495378
commit d556d8f294
1 changed files with 22 additions and 1 deletions

View File

@ -51,11 +51,32 @@ py::object create_numpy_array() {
return py::object(py::handle<>(py_array)); return py::object(py::handle<>(py_array));
} }
py::object create_int16_array() {
// 1. 定义数组维度1维长度为 4
npy_intp dims[1] = {4};
// 2. 创建原生 C 数组int16_t 数据)
int16_t data[4] = {1, 2, -3, 4}; // 示例数据
// 3. 通过 NumPy C API 创建 PyObject*
PyObject* py_array = PyArray_SimpleNewFromData(
1, // 维度数
dims, // 各维度大小
NPY_INT16, // 数据类型np.int16
data // 数据指针
);
if (!py_array) {
throw std::runtime_error("Failed to create NumPy array");
}
// 4. 转换为 py::object自动管理引用计数
return py::object(py::handle<>(py_array));
}
int sendCustomAudioData(const int16_t destChannelIndex, py::object pD, int32_t sampleRate, uint64_t channelNum, int sendCustomAudioData(const int16_t destChannelIndex, py::object pD, int32_t sampleRate, uint64_t channelNum,
uint64_t dataLen) { uint64_t dataLen) {
try { try {
py::object pyData = create_numpy_array(); py::object pyData = create_int16_array();
std::cout << "step 1" << std::endl; std::cout << "step 1" << std::endl;
// 1. 检查输入有效性 // 1. 检查输入有效性
if (pyData.ptr() == nullptr) { if (pyData.ptr() == nullptr) {