From e242bbc96b83dfd2b18afec7a8500b7bab05b7ac Mon Sep 17 00:00:00 2001 From: alexlnkp Date: Wed, 2 Aug 2023 02:47:17 +0700 Subject: [PATCH] added check for filetype and file length for preprocessing Co-Authored-By: kalomaze <66376113+kalomaze@users.noreply.github.com> --- my_utils.py | 25 +++++++++++++ tensorlowest.py | 2 +- trainset_preprocess_pipeline_print.py | 52 ++++++++++++++++----------- 3 files changed, 57 insertions(+), 22 deletions(-) diff --git a/my_utils.py b/my_utils.py index 05491c2..cc03613 100644 --- a/my_utils.py +++ b/my_utils.py @@ -108,3 +108,28 @@ def load_audio(file, sr, DoFormant, Quefrency, Timbre): converted = False return np.frombuffer(out, np.float32).flatten() + + +def check_audio_duration(file): + try: + # Strip whitespaces and unnecessary characters from the file name + file = file.strip(" ").strip('"').strip("\n").strip('"').strip(" ") + + # Probe the audio file for information + probe = ffmpeg.probe(file) + + # Extract the duration from the probe result + duration = float(probe['streams'][0]['duration']) + + # If the duration is less than 0.75 seconds, print the message and exit the loop + if duration < 0.76: + print( + f"\n------------\n" + f"Audio file, {file.split('/')[-1]}, under ~0.76s detected - file is too short. Target at least 1-2s for best results." + f"\n------------\n\n" + ) + return False + + return True + except Exception as e: + raise RuntimeError(f"Failed to check audio duration: {e}") \ No newline at end of file diff --git a/tensorlowest.py b/tensorlowest.py index ef06372..eccd4db 100644 --- a/tensorlowest.py +++ b/tensorlowest.py @@ -47,7 +47,7 @@ def main(model_name, save_freq, lastmdls): tfile = os.path.join(tensordir, latest_file) ea = event_accumulator.EventAccumulator(tfile, - size_guidance={ # see below regarding this argument + size_guidance={ event_accumulator.COMPRESSED_HISTOGRAMS: 500, event_accumulator.IMAGES: 4, event_accumulator.AUDIO: 4, diff --git a/trainset_preprocess_pipeline_print.py b/trainset_preprocess_pipeline_print.py index 376888a..1bd15de 100644 --- a/trainset_preprocess_pipeline_print.py +++ b/trainset_preprocess_pipeline_print.py @@ -14,7 +14,7 @@ from slicer2 import Slicer import librosa, traceback from scipy.io import wavfile import multiprocessing -from my_utils import load_audio +from my_utils import load_audio, check_audio_duration import tqdm DoFormant = False @@ -80,28 +80,38 @@ class PreProcess: ) def pipeline(self, path, idx0): + + file_extension = path.split('.')[-1] + supported_file_extensions = {'wav', 'mp3', 'flac', 'ogg', 'opus', + 'm4a', 'mp4', 'aac', 'alac', 'wma', + 'aiff', 'webm', 'ac3'} + try: - audio = load_audio(path, self.sr, DoFormant, Quefrency, Timbre) - # zero phased digital filter cause pre-ringing noise... - # audio = signal.filtfilt(self.bh, self.ah, audio) - audio = signal.lfilter(self.bh, self.ah, audio) + if file_extension in supported_file_extensions: + if not check_audio_duration(path): return + audio = load_audio(path, self.sr, DoFormant, Quefrency, Timbre) + # zero phased digital filter cause pre-ringing noise... + # audio = signal.filtfilt(self.bh, self.ah, audio) + audio = signal.lfilter(self.bh, self.ah, audio) - idx1 = 0 - for audio in self.slicer.slice(audio): - i = 0 - while 1: - start = int(self.sr * (self.per - self.overlap) * i) - i += 1 - if len(audio[start:]) > self.tail * self.sr: - tmp_audio = audio[start : start + int(self.per * self.sr)] - self.norm_write(tmp_audio, idx0, idx1) - idx1 += 1 - else: - tmp_audio = audio[start:] - idx1 += 1 - break - self.norm_write(tmp_audio, idx0, idx1) - # println("%s->Suc." % path) + idx1 = 0 + for audio in self.slicer.slice(audio): + i = 0 + while 1: + start = int(self.sr * (self.per - self.overlap) * i) + i += 1 + if len(audio[start:]) > self.tail * self.sr: + tmp_audio = audio[start : start + int(self.per * self.sr)] + self.norm_write(tmp_audio, idx0, idx1) + idx1 += 1 + else: + tmp_audio = audio[start:] + idx1 += 1 + break + self.norm_write(tmp_audio, idx0, idx1) + # println("%s->Suc." % path) + else: + print(f"Unsupported audio format! - {path.split('/')[-1]}") except: println("%s->%s" % (path, traceback.format_exc()))