Skip to content

Commit

Permalink
Adding specific tests for detect_silence.
Browse files Browse the repository at this point in the history
detect_silence finds separate slices of silence and in a last step
combines subsequent silent slices into ranges of continuous silence.
The added tests specifically ensure the correct function of this
combination step.
  • Loading branch information
lumip committed Jul 23, 2023
1 parent 996cec4 commit eef2e46
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,46 @@ def test_realistic_audio(self):
self.assertTrue(start > prev_end)
prev_end = end

def test_detect_silence_noncontinous_without_gap(self):
raw_data = b"\xFF\x7F\xFF\x7F\x00\x00\x00\x00\xFF\x7F\x00\x00\xFF\x7F\x00\x00\x00\x00\xFF\x7F\xFF\x7F"
seg = AudioSegment(raw_data, frame_rate=1000, channels=1, sample_width=2)

self.assertEqual(seg.frame_count(), 11.0)
self.assertEqual(len(seg), 11)

silent_ranges = detect_silence(seg, min_silence_len=3, silence_thresh=-4, seek_step=1)
self.assertEqual(silent_ranges, [[1, 10]])

def test_detect_silence_noncontinuous_with_gap(self):
raw_data = b"\xFF\x7F\xFF\x7F\x00\x00\x00\x00\xFF\x7F\xFF\x7F\xFF\x7F\x00\x00\x00\x00\xFF\x7F\xFF\x7F"
seg = AudioSegment(raw_data, frame_rate=1000, channels=1, sample_width=2)

self.assertEqual(seg.frame_count(), 11.0)
self.assertEqual(len(seg), 11)

silent_ranges = detect_silence(seg, min_silence_len=3, silence_thresh=-4, seek_step=1)
self.assertEqual(silent_ranges, [[1, 5], [6, 10]])

def test_detect_silence_noncontinuous_without_gap_last_slice(self):
raw_data = b"\xFF\x7F\xFF\x7F\x00\x00\x00\x00\xFF\x7F\xFF\x7F\x00\x00\x00\x00"
seg = AudioSegment(raw_data, frame_rate=1000, channels=1, sample_width=2)

self.assertEqual(seg.frame_count(), 8.0)
self.assertEqual(len(seg), 8)

silent_ranges = detect_silence(seg, min_silence_len=3, silence_thresh=-4, seek_step=2)
self.assertEqual(silent_ranges, [[2, 8]])

def test_detect_silence_noncontinuous_with_gap_last_slice(self):
raw_data = b"\xFF\x7F\xFF\x7F\x00\x00\x00\x00\xFF\x7F\xFF\x7F\xFF\x7F\x00\x00\xFF\x7F\x00\x00"
seg = AudioSegment(raw_data, frame_rate=1000, channels=1, sample_width=2)

self.assertEqual(seg.frame_count(), 10.0)
self.assertEqual(len(seg), 10)

silent_ranges = detect_silence(seg, min_silence_len=3, silence_thresh=-4, seek_step=2)
self.assertEqual(silent_ranges, [[2, 5], [7, 10]])


class GeneratorTests(unittest.TestCase):

Expand Down

0 comments on commit eef2e46

Please sign in to comment.