- initial import
This commit is contained in:
62
src/bt_signal.py
Normal file
62
src/bt_signal.py
Normal file
@@ -0,0 +1,62 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
import sys
|
||||
import os
|
||||
import pathlib
|
||||
|
||||
from pydub import silence, AudioSegment
|
||||
|
||||
class SignalBoundaries:
|
||||
# Offset from start (in seconds)
|
||||
offset_start: float
|
||||
|
||||
# Offset from finish (in seconds)
|
||||
offset_finish: float
|
||||
|
||||
def __init__(self, offset_start = 0.0, offset_finish = 0.0) -> None:
|
||||
self.offset_start = offset_start
|
||||
self.offset_finish = offset_finish
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f'[offset_start: {round(self.offset_start, 3)}, offset_finish : {round(self.offset_finish, 3)}]'
|
||||
|
||||
|
||||
def find_reference_signal(input_file: pathlib.Path, output_file: pathlib.Path = None, use_end_offset: bool = True) -> SignalBoundaries:
|
||||
myaudio = AudioSegment.from_wav(str(input_file))
|
||||
dBFS = myaudio.dBFS
|
||||
|
||||
# Find silence intervals
|
||||
intervals = silence.detect_nonsilent(myaudio, min_silence_len=1000, silence_thresh=dBFS-17, seek_step=50)
|
||||
|
||||
# Translate to seconds
|
||||
intervals = [((start/1000),(stop/1000)) for start,stop in intervals] #in sec
|
||||
|
||||
# print(intervals)
|
||||
|
||||
# Example of intervals: [(5.4, 6.4), (18.7, 37.05)]
|
||||
for p in intervals:
|
||||
if p[1] - p[0] > 17:
|
||||
bounds = SignalBoundaries(offset_start=p[0], offset_finish=p[1])
|
||||
if output_file is not None:
|
||||
signal = myaudio[bounds.offset_start * 1000 : bounds.offset_finish * 1000]
|
||||
signal.export(str(output_file), format='wav', parameters=['-ar', '44100', '-sample_fmt', 's16'])
|
||||
|
||||
if use_end_offset:
|
||||
bounds.offset_finish = myaudio.duration_seconds - bounds.offset_finish
|
||||
|
||||
return bounds
|
||||
|
||||
return SignalBoundaries()
|
||||
|
||||
if __name__ == '__main__':
|
||||
if len(sys.argv) < 2:
|
||||
print(f'Please specify input filename.')
|
||||
exit(os.EX_NOINPUT)
|
||||
|
||||
# Output file
|
||||
output_file = pathlib.Path(sys.argv[2]) if len(sys.argv) > 2 else None
|
||||
|
||||
# Input file
|
||||
input_file = sys.argv[1]
|
||||
bounds: SignalBoundaries = find_reference_signal(pathlib.Path(input_file), output_file)
|
||||
print (bounds)
|
||||
Reference in New Issue
Block a user