Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stereo to Mono wav in Python

I am loading a wav with the scipy method wavefile.read() which gives me the samplerate and the audiodata

I know that this audio data if stereo is stored as a multi-dimensional array such as

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

I am then using this method to create a new array of mono audio data by taking (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 write this to file using

wavfile.write(newfilename, sr, newaudiodata)

This is producing a Mono wav file, however the sound is dirty and has clickd etc throughout

what am I doing wrong?

like image 393
user2145312 Avatar asked May 22 '15 15:05

user2145312


People also ask

How do you know if a sound is mono or stereo in python?

Our first trick is by looking at the waveforms of each channel. If the waveforms are exactly the same, chances are the recording was mono. If there are obvious differences between the waveforms of each channel, you're dealing with a stereo file.

Can Python play WAV file?

Conclusion: Playing and Recording Sound in PythonYou are now able to: Play a large range of audio formats, including WAV, MP3 and NumPy arrays.


1 Answers

First, what is the datatype of audiodata? I assume it's some fixed-width integer format and you therefore get overflow. If you convert it to a floating point format before processing, it will work fine:

audiodata = audiodata.astype(float)

Second, don't 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 vastly faster than the element-by-element loop you wrote.

like image 138
cfh Avatar answered Oct 07 '22 00:10

cfh