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?
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.
Conclusion: Playing and Recording Sound in PythonYou are now able to: Play a large range of audio formats, including WAV, MP3 and NumPy arrays.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With