Skip to content

Commit

Permalink
Add seek step to enable faster silence detection
Browse files Browse the repository at this point in the history
  • Loading branch information
marciomazza committed Aug 21, 2017
1 parent 098f6c9 commit 203a9e1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
35 changes: 20 additions & 15 deletions pydub/silence.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from .utils import (
db_to_float,
)
from .utils import db_to_float


def detect_silence(audio_segment, min_silence_len=1000, silence_thresh=-16):
def detect_silence(audio_segment, min_silence_len=1000, silence_thresh=-16, seek_step=1):
seg_len = len(audio_segment)

# you can't have a silent portion of a sound that is longer than the sound
Expand All @@ -16,11 +14,18 @@ def detect_silence(audio_segment, min_silence_len=1000, silence_thresh=-16):
# find silence and add start and end indicies to the to_cut list
silence_starts = []

# check every (1 sec by default) chunk of sound for silence
slice_starts = seg_len - min_silence_len

for i in range(slice_starts + 1):
audio_slice = audio_segment[i:i+min_silence_len]
# check successive (1 sec by default) chunk of sound for silence
# try a chunk at every "seek step" (or every chunk for a seek step == 1)
#
# make sure the last portion of the audio is included in the seach
# even when seek step is greater than 1
last_slice_start = seg_len - min_silence_len
slice_starts = range(0, last_slice_start + 1, seek_step)
if slice_starts[-1] != last_slice_start:
slice_starts.append(last_slice_start)

for i in slice_starts:
audio_slice = audio_segment[i:i + min_silence_len]
if audio_slice.rms < silence_thresh:
silence_starts.append(i)

Expand Down Expand Up @@ -54,8 +59,8 @@ def detect_silence(audio_segment, min_silence_len=1000, silence_thresh=-16):
return silent_ranges


def detect_nonsilent(audio_segment, min_silence_len=1000, silence_thresh=-16):
silent_ranges = detect_silence(audio_segment, min_silence_len, silence_thresh)
def detect_nonsilent(audio_segment, min_silence_len=1000, silence_thresh=-16, seek_step=1):
silent_ranges = detect_silence(audio_segment, min_silence_len, silence_thresh, seek_step)
len_seg = len(audio_segment)

# if there is no silence, the whole thing is nonsilent
Expand All @@ -81,8 +86,8 @@ def detect_nonsilent(audio_segment, min_silence_len=1000, silence_thresh=-16):
return nonsilent_ranges



def split_on_silence(audio_segment, min_silence_len=1000, silence_thresh=-16, keep_silence=100):
def split_on_silence(audio_segment, min_silence_len=1000, silence_thresh=-16, keep_silence=100,
seek_step=1):
"""
audio_segment - original pydub.AudioSegment() object
Expand All @@ -97,7 +102,7 @@ def split_on_silence(audio_segment, min_silence_len=1000, silence_thresh=-16, ke
abruptly cut off. (default: 100ms)
"""

not_silence_ranges = detect_nonsilent(audio_segment, min_silence_len, silence_thresh)
not_silence_ranges = detect_nonsilent(audio_segment, min_silence_len, silence_thresh, seek_step)

chunks = []
for start_i, end_i in not_silence_ranges:
Expand All @@ -106,4 +111,4 @@ def split_on_silence(audio_segment, min_silence_len=1000, silence_thresh=-16, ke

chunks.append(audio_segment[start_i:end_i])

return chunks
return chunks
5 changes: 4 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
[wheel]
universal = 1
universal = 1

[pep8]
max-line-length = 100
5 changes: 5 additions & 0 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,11 @@ def test_detect_silence_seg1(self):
silent_ranges = detect_silence(self.seg1, min_silence_len=500, silence_thresh=-20)
self.assertEqual(silent_ranges, [[0, 775], [3141, 4033], [5516, 6051]])

def test_detect_silence_seg1_with_seek_split(self):
silent_ranges = detect_silence(self.seg1, min_silence_len=500, silence_thresh=-20,
seek_step=10)
self.assertEqual(silent_ranges, [[0, 770], [3150, 4030], [5520, 6050]])

def test_realistic_audio(self):
silent_ranges = detect_silence(self.seg4, min_silence_len=1000, silence_thresh=self.seg4.dBFS)

Expand Down

0 comments on commit 203a9e1

Please sign in to comment.