Skip to content

Commit

Permalink
Merge pull request #205 from jiaaro/fix-193
Browse files Browse the repository at this point in the history
fix #193
  • Loading branch information
jiaaro authored Aug 5, 2017
2 parents 13c9d04 + 8d11649 commit 098f6c9
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# v0.20.0
- Add new parameter `gain_during_overlay` to `pydub.AudioSegment.overlay` which allows users to adjust the volume of the target AudioSegment during the portion of the segment which is overlaid with the additional AudioSegment.
- Add new parameter `gain_during_overlay` to `pydub.AudioSegment.overlay` which allows users to adjust the volume of the target AudioSegment during the portion of the segment which is overlaid with the additional AudioSegment.
- `pydub.playback.play()` No longer displays the (very verbose) playback "banner" when using ffplay
- Fix a confusing error message when using invalid crossfade durations (issue #193)

# v0.19.0
- Allow codec and ffmpeg/avconv parameters to be set in the `pydub.AudioSegment.from_file()` for more control while decoding audio files
Expand Down
10 changes: 9 additions & 1 deletion pydub/audio_segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ def is_format(f):
if codec:
# force audio decoder
conversion_command += ["-acodec", codec]

conversion_command += [
"-i", input_file.name, # input_file options (filename last)
"-vn", # Drop any video streams if there are any
Expand Down Expand Up @@ -943,6 +943,14 @@ def append(self, seg, crossfade=100):

if not crossfade:
return seg1._spawn(seg1._data + seg2._data)
elif crossfade > len(self):
raise ValueError("Crossfade is longer than the original AudioSegment ({}ms > {}ms)".format(
crossfade, len(self)
))
elif crossfade > len(seg):
raise ValueError("Crossfade is longer than the appended AudioSegment ({}ms > {}ms)".format(
crossfade, len(seg)
))

xf = seg1[-crossfade:].fade(to_gain=-120, start=0, end=float('inf'))
xf *= seg2[:crossfade].fade(from_gain=-120, start=0, end=float('inf'))
Expand Down
10 changes: 5 additions & 5 deletions pydub/playback.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Support for playing AudioSegments. Pyaudio will be used if it's installed,
otherwise will fallback to ffplay. Pyaudio is a *much* nicer solution, but
is tricky to install. See my notes on installing pyaudio in a virtualenv (on
is tricky to install. See my notes on installing pyaudio in a virtualenv (on
OSX 10.10): https://gist.github.com/jiaaro/9767512210a1d80a8a0d
"""

Expand All @@ -23,7 +23,7 @@ def _play_with_pyaudio(seg):
import pyaudio

p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(seg.sample_width),
stream = p.open(format=p.get_format_from_width(seg.sample_width),
channels=seg.channels,
rate=seg.frame_rate,
output=True)
Expand All @@ -32,10 +32,10 @@ def _play_with_pyaudio(seg):
for chunk in make_chunks(seg, 500):
stream.write(chunk._data)

stream.stop_stream()
stream.close()
stream.stop_stream()
stream.close()

p.terminate()
p.terminate()


def play(audio_segment):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setup(
name='pydub',
version='0.19.0',
version='0.20.0',
author='James Robert',
author_email='[email protected]',
description='Manipulate audio with an simple and easy high level interface',
Expand Down
5 changes: 5 additions & 0 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ def test_append(self):
self.assertEqual(len(merged1), len(self.seg1) + len(self.seg3) - 100)
self.assertEqual(len(merged2), len(self.seg2) + len(self.seg3) - 100)

def test_too_long_crossfade(self):
s1 = self.seg1[:1000]
s2 = self.seg2[:500]
self.assertRaises(ValueError, lambda: s1.append(s2, crossfade=len(s1) + 10))

def test_sum(self):
def gen():
yield self.seg1
Expand Down

0 comments on commit 098f6c9

Please sign in to comment.