I'm using Chaquopy
in my android project. I have a function in my python class which it returns a 2D array as a PyObject
type. Now, I want to convert it to 2D array in my java class. How can I achieve this?
EDIT : Here is my Python code :
import numpy as np
from scipy.io import wavfile
def get_python_audio(file_path):
fs, data_test = wavfile.read(file_path)
print('data_test:', data_test.shape)
data = data_test[:, 0]
data = data[:, np.newaxis]
print('data:', data.shape)
return data
Chaquopy 8.x and newer:
NumPy arrays and Java arrays of the same type can now be converted directly. Assuming
get_python_audio
returns a 1-dimensional int16 array:Chaquopy 7.x and older:
The fastest way to convert numerical arrays between Java and Python is to use a byte array.
We'll assume that
wavfile.read
returns a 1-dimensional int16 array, but this technique is easily adapted to other data types. Then in Python, you can do this:And in Java:
If your audio has multiple channels, then you'll have to convert each channel separately.