As the title states, I have had difficulty converting a PyDub AudioSegment to a NumPy array and back. I am aware of how to convert a PyDub AudioSegment to a NumPy array, and have a hazy idea of how to convert a NumPy array to a PyDub AudioSegment, but the methods I have learned of are varied and do not pair with eachother. So, how could I reliably get an AudioSegment to an array and back?
This is the code I used to get the array:
audio= AudioSegment.from_file("/file/path/sillysong.wav")
data = audio.get_array_of_samples()
data = np.array(data)
data = data.reshape(audio.channels, -1, order='F')
data
I do not know how to get the array in this form back. For context, I am using TensorFlow and I need the data to be in array form. Thank you for your help! (I'm a new coder so there's probably something obvious I'm missing.)
Your approach is correct. I have an example of LowRider.wav and I read it using pydub:
This gives you
data, which has the data from the two channels. Here is the plot of the two:To convert back to
.wav, use the following code, I included an export for you to test if the conversion happened successfully:Change the name of the file and let me know if it works :D