Python | pydub: how to load wav sample into pydub from np.array instead of wav file?

How to upload an audio file np.arrayto PyDub library? I am currently using AudioSegment.from_wav(file_path), but it is not convenient if I already have a wav file loaded as a numpy array:

sample_rate, wav_sample = scipy.io.wavfile.read(file_path)

UPDATE: my wav files are all 16 bits, one channel.

+4
source share
1 answer

Ok, take this answer with a piece of salt, as I don’t know pydubenough to make sure that it works correctly, but you should be able to do this from the class initializer, providing all the parameters it needs: / p>

sample_rate, wav_sample = scipy.io.wavfile.read(file_path) 
segment = AudioSegment(data=wav_sample.tobytes(),
                       sample_width=2,
                       frame_rate=sample_rate, channels=1)

It seems to work as it should, assuming a 16-bit single-channel sample.

(- wav_sample.nbytes() / len(wav_sample)).

!

: , pydub , scipy . , numpy pydub, - ( )?

np.vstack((wav_sample[:,0],wav_sample[:,1])).reshape((-1,), order='F') 
+2

Source: https://habr.com/ru/post/1691603/


All Articles