Skip to content

Commit

Permalink
Merge pull request jiaaro#399 from DriverX/patch-1
Browse files Browse the repository at this point in the history
Fix read headers from empty wav
  • Loading branch information
jiaaro committed Jun 18, 2019
2 parents f181b3f + e8eee26 commit 30b42d8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pydub/audio_segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def extract_wav_headers(data):
# def search_subchunk(data, subchunk_id):
pos = 12 # The size of the RIFF chunk descriptor
subchunks = []
while pos + 8 < len(data) and len(subchunks) < 10:
while pos + 8 <= len(data) and len(subchunks) < 10:
subchunk_id = data[pos:pos + 4]
subchunk_size = struct.unpack_from('<I', data[pos + 4:pos + 8])[0]
subchunks.append(WavSubChunk(subchunk_id, pos, subchunk_size))
Expand Down
Binary file added test/data/test1_empty.wav
Binary file not shown.
14 changes: 14 additions & 0 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,7 @@ class NoConverterTests(unittest.TestCase):
def setUp(self):
self.wave_file = os.path.join(data_dir, 'test1.wav')
self.wave24_file = os.path.join(data_dir, 'test1-24bit.wav')
self.wave_empty = os.path.join(data_dir, 'test1_empty.wav')
self.mp3_file = os.path.join(data_dir, 'test1.mp3')
self.raw_file = os.path.join(data_dir, 'test1.raw')
AudioSegment.converter = "definitely-not-a-path-to-anything-asdjklqwop"
Expand Down Expand Up @@ -1195,6 +1196,19 @@ def test_exporting(self):

self.assertEqual(len(exported), len(seg))

def test_opening_empty_wav_file(self):
seg = AudioSegment.from_wav(self.wave_empty)
self.assertTrue(len(seg) == 0)

seg = AudioSegment.from_file(self.wave_empty)
self.assertTrue(len(seg) == 0)

seg = AudioSegment.from_file(self.wave_empty, "wav")
self.assertTrue(len(seg) == 0)

seg = AudioSegment.from_file(self.wave_empty, format="wav")
self.assertTrue(len(seg) == 0)


class FilterTests(unittest.TestCase):

Expand Down

0 comments on commit 30b42d8

Please sign in to comment.