This commit is contained in:
wangjiyu 2025-04-10 10:00:50 +08:00
parent a47809f315
commit fe7f636055
1 changed files with 12 additions and 12 deletions

View File

@ -1,5 +1,6 @@
#include <boost/python/module.hpp> #include <boost/python/module.hpp>
#include <boost/python/def.hpp> #include <boost/python/def.hpp>
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <numpy/arrayobject.h> #include <numpy/arrayobject.h>
#include <stdio.h> #include <stdio.h>
@ -33,24 +34,23 @@ int initSend(const char* destRoomId, const int16_t destChannelIndex) {
} }
} }
py::object create_numpy_array() { py::object create_numpy_array() {
// 初始化 NumPy C API必须调用 // 确保已调用 import_array()
import_array(); if (PyArray_API == nullptr) {
throw std::runtime_error("NumPy API not initialized");
}
// 创建 1D 数组int32 类型) // 创建数组
npy_intp dims[1] = {4}; npy_intp dims[1] = {4};
int32_t data[4] = {1, 2, 3, 4}; int32_t data[4] = {1, 2, 3, 4};
// 从现有内存创建数组(不拷贝数据) PyObject* py_array = PyArray_SimpleNewFromData(1, dims, NPY_INT32, data);
PyObject* py_array = PyArray_SimpleNewFromData( if (!py_array) {
1, dims, NPY_INT32, data throw std::runtime_error("Failed to create NumPy array");
); }
// 转换为 boost::python::object return py::object(py::handle<>(py_array));
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, int sendCustomAudioData(const int16_t destChannelIndex, py::object pD, int32_t sampleRate, uint64_t channelNum,
uint64_t dataLen) { uint64_t dataLen) {
try { try {