Stereo to Mono wav in Python
Solution 1
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.
Solution 2
turns out, all i had to change was
(right+left)/2
to
(right/2) + (left/2)
user2145312
Updated on July 24, 2022Comments
-
user2145312 almost 2 years
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?
-
WinEunuuchs2Unix over 3 yearsShouldn't you accept the answer you went with?
-
b-rad15 almost 3 years@WinEunuuchs2Unix not when the accepted answer is better. Converting to a float saves some loss when the integers are odd
-
Peter Julian almost 3 yearsWhy not
audiodata.mean(axis=1)
?