Stereo to monophonic wav in Python

I load wav using the scipy wavefile.read () method, which gives me fetch and audio data

I know that this audio data, if the stereo is stored as a multidimensional array, such as

audiodata[[left right] [left right] ... [left right]] 

Then I use this method to create a new array of monaural audio data using (right + left) / 2

 def stereoToMono(audiodata) newaudiodata = [] for i in range(len(audiodata)): d = (audiodata[i][0] + audiodata[i][1])/2 newaudiodata.append(d) return np.array(newaudiodata, dtype='int16') 

and then I will write this to a file using

 wavfile.write(newfilename, sr, newaudiodata) 

This creates a Mono wav file, however the sound is dirty and has a click, etc. in all

what am I doing wrong?

+5
source share
2 answers

First, what is the audiodata data audiodata ? I assume this is an integer format of fixed width, and therefore you get overflow. If you convert it to floating point format, it will work fine:

 audiodata = audiodata.astype(float) 

Secondly, do not write your Python code element by element; vectorize it:

 d = (audiodata[:,0] + audiodata[:,1]) / 2 

or even better

 d = audiodata.sum(axis=1) / 2 

This will be much faster than the recorded cycle of the element after element.

+5
source

all i needed to change was

(right + left) / 2

to

(right / 2) + (left / 2)

+2
source

All Articles