Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read multiple .wav file from computer and merge them to numpy arrays

Tags:

python

numpy

wav

I need to read multiple wav files into separate numpy arrays, then merge the numpy arrays into one and save it as a wav file.

How do I do that?

like image 540
Abdul Rafay Avatar asked Oct 17 '25 16:10

Abdul Rafay


1 Answers

Here is a solution:

from scipy.io.wavfile import read, write
import numpy as np

fs, x = read('test1.wav')
f2, y = read('test2.wav')

#z = x + y                    # this is to "mix" the 2 sounds, probably not what you want
z = np.concatenate((x, y))    # this will add the sounds one after another

write('out.wav', fs, z)

When doing x + y, if the 2 arrays don't have the same length, you need to zero-pad the shortest array, so that they finally have to same length before summing them.

like image 176
Basj Avatar answered Oct 20 '25 06:10

Basj