From 6336eb921511706e6980a0507a0c937cf6351ab0 Mon Sep 17 00:00:00 2001 From: Keith Edmunds Date: Sun, 15 Aug 2021 00:21:40 +0100 Subject: [PATCH] add test file for fades --- archive/play.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100755 archive/play.py diff --git a/archive/play.py b/archive/play.py new file mode 100755 index 0000000..93e378f --- /dev/null +++ b/archive/play.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python + +from pydub import AudioSegment + + +def fade_point(audio_segment, fade_threshold=-12, chunk_size=10): + """ + Returns the millisecond/index of the point where the fade is down to + fade_threshold and doesn't get louder again. + audio_segment - the sdlg_search_database_uiegment to find silence in + fade_threshold - the upper bound for how quiet is silent in dFBS + chunk_size - chunk size for interating over the segment in ms + """ + + assert chunk_size > 0 # to avoid infinite loop + + segment_length = audio_segment.duration_seconds * 1000 # ms + print(f"segment_length={int(segment_length/1000)}") + trim_ms = segment_length - chunk_size + + max_vol = audio_segment.dBFS + print(f"{max_vol=}") + fade_threshold = max_vol + while ( + audio_segment[trim_ms:trim_ms + chunk_size].dBFS < fade_threshold + and trim_ms > 0): # noqa W503 + trim_ms -= chunk_size + + # if there is no trailing silence, return lenght of track (it's less + # the chunk_size, but for chunk_size = 10ms, this may be ignored) + print(f"Fade last {int(segment_length - trim_ms)/1000} seconds") + + +print("Shout:") +segment = AudioSegment.from_mp3("../archive/shout.mp3") +fade_point(segment) +print("Champagne:") +segment = AudioSegment.from_mp3("../archive/champ.mp3") +fade_point(segment) +print("Be good:") +segment = AudioSegment.from_mp3("../archive/wibg.mp3") +fade_point(segment)