added check for filetype and file length

for preprocessing

Co-Authored-By: kalomaze <66376113+kalomaze@users.noreply.github.com>
This commit is contained in:
alexlnkp
2023-08-02 02:47:17 +07:00
parent b5bd047d81
commit e242bbc96b
3 changed files with 57 additions and 22 deletions

View File

@@ -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}")

View File

@@ -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,

View File

@@ -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()))