4

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?

5
  • I guess you want to concatenate arrays with the numpy.concatenate() function. Commented Aug 29, 2018 at 9:13
  • First thanks tif for answer me....second Yes i want to concatenate them but i think first i read those numpy arrays form computer...isn't i Commented Aug 29, 2018 at 9:22
  • 1
    Use scipy to read/write the wav data docs.scipy.org/doc/scipy-0.14.0/reference/generated/… Commented Aug 29, 2018 at 10:05
  • 1
    @AbdulRafay by merging do you mean 1) mixing them or 2) adding one sound after another? Commented Aug 29, 2018 at 10:12
  • @Basj i need to mege them one after the other Commented Aug 29, 2018 at 10:24

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Should i put all my .wav files to the folder where my .py file exist to ream them...actually i am using anaconda for python?
1st question: yes. 2nd question: you have to use a loop for 200 files
Sorry I have no time to continue so I won't be able to answer on further questions. Just a few hints: try first with 2 files, then 3 files, and once it works, use import glob for f in glob.glob('*.wav'):, it will loop on all sound files in the folder. Search SackOverflow questions about looping on files with Python for further help. Best of luck for your project! You can "accept" this answer with the tick mark if it helped ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.