Skip to content

Commit

Permalink
When converting stereo to mono, divide samples by two to avoid clipping
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen Williams committed Nov 21, 2017
1 parent 0cd5dbb commit 76cd25b
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pydub/audio_segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -752,11 +752,13 @@ def set_channels(self, channels):
if channels == 2 and self.channels == 1:
fn = audioop.tostereo
frame_width = self.frame_width * 2
fac = 1
elif channels == 1 and self.channels == 2:
fn = audioop.tomono
frame_width = self.frame_width // 2
fac = 0.5

converted = fn(self._data, self.sample_width, 1, 1)
converted = fn(self._data, self.sample_width, fac, fac)

return self._spawn(data=converted,
overrides={
Expand Down

0 comments on commit 76cd25b

Please sign in to comment.