diff --git a/requirements.txt b/requirements.txt index 73912d01..2d9e9bd1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -25,4 +25,5 @@ jieba_fast jieba LangSegment>=0.2.0 Faster_Whisper -wordsegment \ No newline at end of file +wordsegment +pyloudnorm \ No newline at end of file diff --git a/tools/loudness_norm.py b/tools/loudness_norm.py new file mode 100644 index 00000000..e702e736 --- /dev/null +++ b/tools/loudness_norm.py @@ -0,0 +1,132 @@ +# modifiled from https://github.com/fishaudio/audio-preprocess/blob/main/fish_audio_preprocess/cli/loudness_norm.py + +from pathlib import Path +from typing import Union +import numpy as np +import pyloudnorm as pyln +import soundfile as sf +from tqdm import tqdm +from concurrent.futures import ProcessPoolExecutor, as_completed +import argparse +import os + + +def loudness_norm_( + input_dir: str, + peak: float, + loudness: float, + block_size: float, + num_workers: int, +): + """Perform loudness normalization (ITU-R BS.1770-4) on audio files.""" + + if isinstance(input_dir, str): + path = Path(input_dir) + input_dir, output_dir = Path(input_dir), Path(input_dir) + + if not path.exists(): + raise FileNotFoundError(f"Directory {path} does not exist.") + files = ( + [f for f in path.glob("*") if f.is_file() and f.suffix == ".wav"] + ) + + + print(f"Found {len(files)} files, normalizing loudness") + + + with ProcessPoolExecutor(max_workers=num_workers) as executor: + tasks = [] + + for file in tqdm(files, desc="Preparing tasks"): + # Get relative path to input_dir + relative_path = file.relative_to(input_dir) + new_file = output_dir / relative_path + + if new_file.parent.exists() is False: + new_file.parent.mkdir(parents=True) + + tasks.append( + executor.submit( + loudness_norm_file, file, new_file, peak, loudness, block_size + ) + ) + + for i in tqdm(as_completed(tasks), total=len(tasks), desc="Processing"): + assert i.exception() is None, i.exception() + + print("Done!") + + + +def loudness_norm_file( + input_file: Union[str, Path], + output_file: Union[str, Path], + peak=-1.0, + loudness=-23.0, + block_size=0.400, +) -> None: + """ + Perform loudness normalization (ITU-R BS.1770-4) on audio files. + + Args: + input_file: input audio file + output_file: output audio file + peak: peak normalize audio to N dB. Defaults to -1.0. + loudness: loudness normalize audio to N dB LUFS. Defaults to -23.0. + block_size: block size for loudness measurement. Defaults to 0.400. (400 ms) + """ + + # Thanks to .against's feedback + # https://github.com/librosa/librosa/issues/1236 + + input_file, output_file = str(input_file), str(output_file) + + audio, rate = sf.read(input_file) + audio = loudness_norm(audio, rate, peak, loudness, block_size) + sf.write(output_file, audio, rate) + + + + +def loudness_norm( + audio: np.ndarray, rate: int, peak=-1.0, loudness=-23.0, block_size=0.400 +) -> np.ndarray: + """ + Perform loudness normalization (ITU-R BS.1770-4) on audio files. + + Args: + audio: audio data + rate: sample rate + peak: peak normalize audio to N dB. Defaults to -1.0. + loudness: loudness normalize audio to N dB LUFS. Defaults to -23.0. + block_size: block size for loudness measurement. Defaults to 0.400. (400 ms) + + Returns: + loudness normalized audio + """ + + # peak normalize audio to [peak] dB + audio = pyln.normalize.peak(audio, peak) + + # measure the loudness first + meter = pyln.Meter(rate, block_size=block_size) # create BS.1770 meter + _loudness = meter.integrated_loudness(audio) + + return pyln.normalize.loudness(audio, _loudness, loudness) + + + + +parser = argparse.ArgumentParser() +parser.add_argument("-i","--input_dir",help="匹配响度输入文件夹") +parser.add_argument("-l","--loudness",help="响度") +parser.add_argument("-p","--peak",help="响度峰值") +parser.add_argument("-n","--num_worker") +args = parser.parse_args() +input_dir = args.input_dir +loudness = float(args.loudness) +peak = float(args.peak) +num_worker = int(args.num_worker) + +if __name__ == "__main__": + loudness_norm_(input_dir=input_dir,peak=peak,loudness=loudness,block_size=0.4,num_workers=num_worker) \ No newline at end of file diff --git a/tools/slice_audio.py b/tools/slice_audio.py index 46ee408a..fb6ef413 100644 --- a/tools/slice_audio.py +++ b/tools/slice_audio.py @@ -1,48 +1,478 @@ -import os,sys,numpy as np -import traceback -from scipy.io import wavfile -# parent_directory = os.path.dirname(os.path.abspath(__file__)) -# sys.path.append(parent_directory) -from my_utils import load_audio -from slicer2 import Slicer - -def slice(inp,opt_root,threshold,min_length,min_interval,hop_size,max_sil_kept,_max,alpha,i_part,all_part): - os.makedirs(opt_root,exist_ok=True) - if os.path.isfile(inp): - input=[inp] - elif os.path.isdir(inp): - input=[os.path.join(inp, name) for name in sorted(list(os.listdir(inp)))] - else: - return "输入路径存在但既不是文件也不是文件夹" - slicer = Slicer( - sr=32000, # 长音频采样率 - threshold= int(threshold), # 音量小于这个值视作静音的备选切割点 - min_length= int(min_length), # 每段最小多长,如果第一段太短一直和后面段连起来直到超过这个值 - min_interval= int(min_interval), # 最短切割间隔 - hop_size= int(hop_size), # 怎么算音量曲线,越小精度越大计算量越高(不是精度越大效果越好) - max_sil_kept= int(max_sil_kept), # 切完后静音最多留多长 - ) - _max=float(_max) - alpha=float(alpha) - for inp_path in input[int(i_part)::int(all_part)]: - # print(inp_path) - try: - name = os.path.basename(inp_path) - audio = load_audio(inp_path, 32000) - # print(audio.shape) - for chunk, start, end in slicer.slice(audio): # start和end是帧数 - tmp_max = np.abs(chunk).max() - if(tmp_max>1):chunk/=tmp_max - chunk = (chunk / tmp_max * (_max * alpha)) + (1 - alpha) * chunk - wavfile.write( - "%s/%s_%010d_%010d.wav" % (opt_root, name, start, end), - 32000, - # chunk.astype(np.float32), - (chunk * 32767).astype(np.int16), - ) - except: - print(inp_path,"->fail->",traceback.format_exc()) - return "执行完毕,请检查输出文件" - -print(slice(*sys.argv[1:])) - +# modified from https://github.com/fishaudio/audio-preprocess/blob/main/fish_audio_preprocess/cli/slice_audio.py + +from pathlib import Path +from typing import Union, Iterable +from concurrent.futures import ProcessPoolExecutor, as_completed +from tqdm import tqdm +import librosa +import numpy as np +import soundfile as sf +import math +from my_utils import load_audio +import argparse +import os + + +AUDIO_EXTENSIONS = { + ".mp3", + ".wav", + ".flac", + ".ogg", + ".m4a", + ".wma", + ".aac", + ".aiff", + ".aif", + ".aifc", +} + + +def list_files( + path: Union[Path, str], + extensions: set[str] = None, + sort: bool = True, +) -> list[Path]: + """List files in a directory. + + Args: + path (Path): Path to the directory. + extensions (set, optional): Extensions to filter. Defaults to None. + sort (bool, optional): Whether to sort the files. Defaults to True. + + Returns: + list: List of files. + """ + + if isinstance(path, str): + path = Path(path) + + if not path.exists(): + raise FileNotFoundError(f"Directory {path} does not exist.") + + files = [f for f in path.glob("*") if f.is_file()] + + if extensions is not None: + files = [f for f in files if f.suffix in extensions] + + if sort: + files = sorted(files) + + return files + + +def make_dirs(path: Union[Path, str]): + """Make directories. + + Args: + path (Union[Path, str]): Path to the directory. + """ + if isinstance(path, str): + path = Path(path) + + if path.exists(): + print(f"Output directory already exists: {path}") + + path.mkdir(parents=True, exist_ok=True) + + +def slice_audio_v2_( + input_path: str, + output_dir: str, + num_workers: int, + min_duration: float, + max_duration: float, + min_silence_duration: float, + top_db: int, + hop_length: int, + max_silence_kept: float, + merge_short:bool +): + """(OpenVPI version) Slice audio files into smaller chunks by silence.""" + + input_path_, output_dir_ = Path(input_path), Path(output_dir) + if not input_path_.exists(): + raise RuntimeError("You input a wrong audio path that does not exists, please fix it!") + make_dirs(output_dir_) + if input_path_.is_dir(): + files = list_files(input_path_, extensions=AUDIO_EXTENSIONS) + elif input_path_.is_file() and input_path_.suffix in AUDIO_EXTENSIONS: + files = [input_path_] + input_path_ = input_path_.parent + else: + raise RuntimeError("The input path is not file or dir, please fixes it") + print(f"Found {len(files)} files, processing...") + + + with ProcessPoolExecutor(max_workers=num_workers) as executor: + tasks = [] + + for file in tqdm(files, desc="Preparing tasks"): + # Get relative path to input_dir + relative_path = file.relative_to(input_path_) + save_path = output_dir_ / relative_path.parent / relative_path.stem + + tasks.append( + executor.submit( + slice_audio_file_v2, + input_file=str(file), + output_dir=save_path, + min_duration=min_duration, + max_duration=max_duration, + min_silence_duration=min_silence_duration, + top_db=top_db, + hop_length=hop_length, + max_silence_kept=max_silence_kept, + merge_short=merge_short + ) + ) + + for i in tqdm(as_completed(tasks), total=len(tasks), desc="Processing"): + assert i.exception() is None, i.exception() + + print("Done!") + print(f"Total: {len(files)}") + print(f"Output directory: {output_dir}") + + +def slice_audio_file_v2( + input_file: Union[str, Path], + output_dir: Union[str, Path], + min_duration: float = 4.0, + max_duration: float = 12.0, + min_silence_duration: float = 0.3, + top_db: int = -40, + hop_length: int = 10, + max_silence_kept: float = 0.4, + merge_short: bool = False +) -> None: + """ + Slice audio by silence and save to output folder + + Args: + input_file: input audio file + output_dir: output folder + min_duration: minimum duration of each slice + max_duration: maximum duration of each slice + min_silence_duration: minimum duration of silence + top_db: threshold to detect silence + hop_length: hop length to detect silence + max_silence_kept: maximum duration of silence to be kept + """ + + output_dir = Path(output_dir) + + audio = load_audio(str(input_file),32000) + rate = 32000 + for idx, sliced in enumerate( + slice_audio_v2( + audio, + rate, + min_duration=min_duration, + max_duration=max_duration, + min_silence_duration=min_silence_duration, + top_db=top_db, + hop_length=hop_length, + max_silence_kept=max_silence_kept, + merge_short=merge_short + ) + ): + if len(sliced) <= 3*rate: continue + max_audio=np.abs(sliced).max()#防止爆音,懒得搞混合了,后面有响度匹配 + if max_audio>1: + sliced/=max_audio + sf.write(str(output_dir) + f"_{idx:04d}.wav", sliced, rate) + + +def slice_audio_v2( + audio: np.ndarray, + rate: int, + min_duration: float = 4.0, + max_duration: float = 12.0, + min_silence_duration: float = 0.3, + top_db: int = -40, + hop_length: int = 10, + max_silence_kept: float = 0.5, + merge_short: bool = False +) -> Iterable[np.ndarray]: + """Slice audio by silence + + Args: + audio: audio data, in shape (samples, channels) + rate: sample rate + min_duration: minimum duration of each slice + max_duration: maximum duration of each slice + min_silence_duration: minimum duration of silence + top_db: threshold to detect silence + hop_length: hop length to detect silence + max_silence_kept: maximum duration of silence to be kept + merge_short: merge short slices automatically + + Returns: + Iterable of sliced audio + """ + + if len(audio) / rate < min_duration: + sliced_by_max_duration_chunk = slice_by_max_duration(audio, max_duration, rate) + yield from merge_short_chunks( + sliced_by_max_duration_chunk, max_duration, rate + ) if merge_short else sliced_by_max_duration_chunk + return + + slicer = Slicer( + sr=rate, + threshold=top_db, + min_length=min_duration * 1000, + min_interval=min_silence_duration * 1000, + hop_size=hop_length, + max_sil_kept=max_silence_kept * 1000, + ) + + sliced_audio = slicer.slice(audio) + if merge_short: + sliced_audio = merge_short_chunks(sliced_audio, max_duration, rate) + + for chunk in sliced_audio: + sliced_by_max_duration_chunk = slice_by_max_duration(chunk, max_duration, rate) + yield from sliced_by_max_duration_chunk + + +def slice_by_max_duration( + gen: np.ndarray, slice_max_duration: float, rate: int +) -> Iterable[np.ndarray]: + """Slice audio by max duration + + Args: + gen: audio data, in shape (samples, channels) + slice_max_duration: maximum duration of each slice + rate: sample rate + + Returns: + generator of sliced audio data + """ + + if len(gen) > slice_max_duration * rate: + # Evenly split _gen into multiple slices + n_chunks = math.ceil(len(gen) / (slice_max_duration * rate)) + chunk_size = math.ceil(len(gen) / n_chunks) + + for i in range(0, len(gen), chunk_size): + yield gen[i : i + chunk_size] + else: + yield gen + + +class Slicer: + def __init__( + self, + sr: int, + threshold: float = -40.0, + min_length: int = 4000, + min_interval: int = 300, + hop_size: int = 10, + max_sil_kept: int = 5000, + ): + if not min_length >= min_interval >= hop_size: + raise ValueError( + "The following condition must be satisfied: min_length >= min_interval >= hop_size" + ) + + if not max_sil_kept >= hop_size: + raise ValueError( + "The following condition must be satisfied: max_sil_kept >= hop_size" + ) + + min_interval = sr * min_interval / 1000 + self.threshold = 10 ** (threshold / 20.0) + self.hop_size = round(sr * hop_size / 1000) + self.win_size = min(round(min_interval), 4 * self.hop_size) + self.min_length = round(sr * min_length / 1000 / self.hop_size) + self.min_interval = round(min_interval / self.hop_size) + self.max_sil_kept = round(sr * max_sil_kept / 1000 / self.hop_size) + + def _apply_slice(self, waveform, begin, end): + if len(waveform.shape) > 1: + return waveform[ + :, begin * self.hop_size : min(waveform.shape[1], end * self.hop_size) + ] + else: + return waveform[ + begin * self.hop_size : min(waveform.shape[0], end * self.hop_size) + ] + + def slice(self, waveform): + if len(waveform.shape) > 1: + samples = waveform.mean(axis=0) + else: + samples = waveform + + if samples.shape[0] <= self.min_length: + return [waveform] + + rms_list = librosa.feature.rms( + y=samples, frame_length=self.win_size, hop_length=self.hop_size + ).squeeze(0) + sil_tags = [] + silence_start = None + clip_start = 0 + + for i, rms in enumerate(rms_list): + # Keep looping while frame is silent. + if rms < self.threshold: + # Record start of silent frames. + if silence_start is None: + silence_start = i + continue + + # Keep looping while frame is not silent and silence start has not been recorded. + if silence_start is None: + continue + + # Clear recorded silence start if interval is not enough or clip is too short + is_leading_silence = silence_start == 0 and i > self.max_sil_kept + need_slice_middle = ( + i - silence_start >= self.min_interval + and i - clip_start >= self.min_length + ) + + if not is_leading_silence and not need_slice_middle: + silence_start = None + continue + + # Need slicing. Record the range of silent frames to be removed. + if i - silence_start <= self.max_sil_kept: + pos = rms_list[silence_start : i + 1].argmin() + silence_start + + if silence_start == 0: + sil_tags.append((0, pos)) + else: + sil_tags.append((pos, pos)) + + clip_start = pos + elif i - silence_start <= self.max_sil_kept * 2: + pos = rms_list[ + i - self.max_sil_kept : silence_start + self.max_sil_kept + 1 + ].argmin() + pos += i - self.max_sil_kept + pos_l = ( + rms_list[ + silence_start : silence_start + self.max_sil_kept + 1 + ].argmin() + + silence_start + ) + pos_r = ( + rms_list[i - self.max_sil_kept : i + 1].argmin() + + i + - self.max_sil_kept + ) + + if silence_start == 0: + sil_tags.append((0, pos_r)) + clip_start = pos_r + else: + sil_tags.append((min(pos_l, pos), max(pos_r, pos))) + clip_start = max(pos_r, pos) + else: + pos_l = ( + rms_list[ + silence_start : silence_start + self.max_sil_kept + 1 + ].argmin() + + silence_start + ) + pos_r = ( + rms_list[i - self.max_sil_kept : i + 1].argmin() + + i + - self.max_sil_kept + ) + + if silence_start == 0: + sil_tags.append((0, pos_r)) + else: + sil_tags.append((pos_l, pos_r)) + + clip_start = pos_r + silence_start = None + + # Deal with trailing silence. + total_frames = rms_list.shape[0] + if ( + silence_start is not None + and total_frames - silence_start >= self.min_interval + ): + silence_end = min(total_frames, silence_start + self.max_sil_kept) + pos = rms_list[silence_start : silence_end + 1].argmin() + silence_start + sil_tags.append((pos, total_frames + 1)) + + # Apply and return slices. + if len(sil_tags) == 0: + return [waveform] + else: + chunks = [] + + if sil_tags[0][0] > 0: + chunks.append(self._apply_slice(waveform, 0, sil_tags[0][0])) + + for i in range(len(sil_tags) - 1): + chunks.append( + self._apply_slice(waveform, sil_tags[i][1], sil_tags[i + 1][0]) + ) + + if sil_tags[-1][1] < total_frames: + chunks.append( + self._apply_slice(waveform, sil_tags[-1][1], total_frames) + ) + + return chunks + + +def merge_short_chunks(chunks, max_duration, rate): + merged_chunks = [] + buffer, length = [], 0 + + for chunk in chunks: + if length + len(chunk) > max_duration * rate and len(buffer) > 0: + merged_chunks.append(np.concatenate(buffer)) + buffer, length = [], 0 + else: + buffer.append(chunk) + length += len(chunk) + + if len(buffer) > 0: + merged_chunks.append(np.concatenate(buffer)) + + return merged_chunks + + + + + + + + + +parser = argparse.ArgumentParser() +parser.add_argument("-i","--input_dir",help="切割输入文件夹") +parser.add_argument("-o","--output_dir",help="切割输入文件夹") +parser.add_argument("--threshold",default=-40,help="音量小于这个值视作静音的备选切割点") +parser.add_argument("--min_duration",default=4,help="每段最短多长,如果第一段太短一直和后面段连起来直到超过这个值") +parser.add_argument("--max_duration",default=12,help="每段最长多长") +parser.add_argument("--min_interval",default=0.3,help="最短切割间隔") +parser.add_argument("--hop_size",default=10,help="怎么算音量曲线,越小精度越大计算量越高(不是精度越大效果越好)") +parser.add_argument("--max_sil_kept",default=0.4,help="切完后静音最多留多长") +parser.add_argument("--num_worker",default=os.cpu_count(),help="切使用的进程数") +parser.add_argument("--merge_short",default="False",help="响割使用的进程数") + + +args = parser.parse_args() +input_path = args.input_dir +output_dir = args.output_dir +threshold = float(args.threshold) +min_duration = float(args.min_duration) +max_duration = float(args.max_duration) +min_interval = float(args.min_interval) +hop_size = float(args.hop_size) +max_sil_kept = float(args.max_sil_kept) +num_worker = int(args.num_worker) +merge_short = bool(args.merge_short) + +if __name__ == "__main__": + slice_audio_v2_(input_path, output_dir, num_worker, min_duration, max_duration, min_interval, threshold, hop_size, max_sil_kept,merge_short) diff --git a/tools/slicer2.py b/tools/slicer2.py deleted file mode 100644 index ba6794b6..00000000 --- a/tools/slicer2.py +++ /dev/null @@ -1,261 +0,0 @@ -import numpy as np - - -# This function is obtained from librosa. -def get_rms( - y, - frame_length=2048, - hop_length=512, - pad_mode="constant", -): - padding = (int(frame_length // 2), int(frame_length // 2)) - y = np.pad(y, padding, mode=pad_mode) - - axis = -1 - # put our new within-frame axis at the end for now - out_strides = y.strides + tuple([y.strides[axis]]) - # Reduce the shape on the framing axis - x_shape_trimmed = list(y.shape) - x_shape_trimmed[axis] -= frame_length - 1 - out_shape = tuple(x_shape_trimmed) + tuple([frame_length]) - xw = np.lib.stride_tricks.as_strided(y, shape=out_shape, strides=out_strides) - if axis < 0: - target_axis = axis - 1 - else: - target_axis = axis + 1 - xw = np.moveaxis(xw, -1, target_axis) - # Downsample along the target axis - slices = [slice(None)] * xw.ndim - slices[axis] = slice(0, None, hop_length) - x = xw[tuple(slices)] - - # Calculate power - power = np.mean(np.abs(x) ** 2, axis=-2, keepdims=True) - - return np.sqrt(power) - - -class Slicer: - def __init__( - self, - sr: int, - threshold: float = -40.0, - min_length: int = 5000, - min_interval: int = 300, - hop_size: int = 20, - max_sil_kept: int = 5000, - ): - if not min_length >= min_interval >= hop_size: - raise ValueError( - "The following condition must be satisfied: min_length >= min_interval >= hop_size" - ) - if not max_sil_kept >= hop_size: - raise ValueError( - "The following condition must be satisfied: max_sil_kept >= hop_size" - ) - min_interval = sr * min_interval / 1000 - self.threshold = 10 ** (threshold / 20.0) - self.hop_size = round(sr * hop_size / 1000) - self.win_size = min(round(min_interval), 4 * self.hop_size) - self.min_length = round(sr * min_length / 1000 / self.hop_size) - self.min_interval = round(min_interval / self.hop_size) - self.max_sil_kept = round(sr * max_sil_kept / 1000 / self.hop_size) - - def _apply_slice(self, waveform, begin, end): - if len(waveform.shape) > 1: - return waveform[ - :, begin * self.hop_size : min(waveform.shape[1], end * self.hop_size) - ] - else: - return waveform[ - begin * self.hop_size : min(waveform.shape[0], end * self.hop_size) - ] - - # @timeit - def slice(self, waveform): - if len(waveform.shape) > 1: - samples = waveform.mean(axis=0) - else: - samples = waveform - if samples.shape[0] <= self.min_length: - return [waveform] - rms_list = get_rms( - y=samples, frame_length=self.win_size, hop_length=self.hop_size - ).squeeze(0) - sil_tags = [] - silence_start = None - clip_start = 0 - for i, rms in enumerate(rms_list): - # Keep looping while frame is silent. - if rms < self.threshold: - # Record start of silent frames. - if silence_start is None: - silence_start = i - continue - # Keep looping while frame is not silent and silence start has not been recorded. - if silence_start is None: - continue - # Clear recorded silence start if interval is not enough or clip is too short - is_leading_silence = silence_start == 0 and i > self.max_sil_kept - need_slice_middle = ( - i - silence_start >= self.min_interval - and i - clip_start >= self.min_length - ) - if not is_leading_silence and not need_slice_middle: - silence_start = None - continue - # Need slicing. Record the range of silent frames to be removed. - if i - silence_start <= self.max_sil_kept: - pos = rms_list[silence_start : i + 1].argmin() + silence_start - if silence_start == 0: - sil_tags.append((0, pos)) - else: - sil_tags.append((pos, pos)) - clip_start = pos - elif i - silence_start <= self.max_sil_kept * 2: - pos = rms_list[ - i - self.max_sil_kept : silence_start + self.max_sil_kept + 1 - ].argmin() - pos += i - self.max_sil_kept - pos_l = ( - rms_list[ - silence_start : silence_start + self.max_sil_kept + 1 - ].argmin() - + silence_start - ) - pos_r = ( - rms_list[i - self.max_sil_kept : i + 1].argmin() - + i - - self.max_sil_kept - ) - if silence_start == 0: - sil_tags.append((0, pos_r)) - clip_start = pos_r - else: - sil_tags.append((min(pos_l, pos), max(pos_r, pos))) - clip_start = max(pos_r, pos) - else: - pos_l = ( - rms_list[ - silence_start : silence_start + self.max_sil_kept + 1 - ].argmin() - + silence_start - ) - pos_r = ( - rms_list[i - self.max_sil_kept : i + 1].argmin() - + i - - self.max_sil_kept - ) - if silence_start == 0: - sil_tags.append((0, pos_r)) - else: - sil_tags.append((pos_l, pos_r)) - clip_start = pos_r - silence_start = None - # Deal with trailing silence. - total_frames = rms_list.shape[0] - if ( - silence_start is not None - and total_frames - silence_start >= self.min_interval - ): - silence_end = min(total_frames, silence_start + self.max_sil_kept) - pos = rms_list[silence_start : silence_end + 1].argmin() + silence_start - sil_tags.append((pos, total_frames + 1)) - # Apply and return slices. - ####音频+起始时间+终止时间 - if len(sil_tags) == 0: - return [[waveform,0,int(total_frames*self.hop_size)]] - else: - chunks = [] - if sil_tags[0][0] > 0: - chunks.append([self._apply_slice(waveform, 0, sil_tags[0][0]),0,int(sil_tags[0][0]*self.hop_size)]) - for i in range(len(sil_tags) - 1): - chunks.append( - [self._apply_slice(waveform, sil_tags[i][1], sil_tags[i + 1][0]),int(sil_tags[i][1]*self.hop_size),int(sil_tags[i + 1][0]*self.hop_size)] - ) - if sil_tags[-1][1] < total_frames: - chunks.append( - [self._apply_slice(waveform, sil_tags[-1][1], total_frames),int(sil_tags[-1][1]*self.hop_size),int(total_frames*self.hop_size)] - ) - return chunks - - -def main(): - import os.path - from argparse import ArgumentParser - - import librosa - import soundfile - - parser = ArgumentParser() - parser.add_argument("audio", type=str, help="The audio to be sliced") - parser.add_argument( - "--out", type=str, help="Output directory of the sliced audio clips" - ) - parser.add_argument( - "--db_thresh", - type=float, - required=False, - default=-40, - help="The dB threshold for silence detection", - ) - parser.add_argument( - "--min_length", - type=int, - required=False, - default=5000, - help="The minimum milliseconds required for each sliced audio clip", - ) - parser.add_argument( - "--min_interval", - type=int, - required=False, - default=300, - help="The minimum milliseconds for a silence part to be sliced", - ) - parser.add_argument( - "--hop_size", - type=int, - required=False, - default=10, - help="Frame length in milliseconds", - ) - parser.add_argument( - "--max_sil_kept", - type=int, - required=False, - default=500, - help="The maximum silence length kept around the sliced clip, presented in milliseconds", - ) - args = parser.parse_args() - out = args.out - if out is None: - out = os.path.dirname(os.path.abspath(args.audio)) - audio, sr = librosa.load(args.audio, sr=None, mono=False) - slicer = Slicer( - sr=sr, - threshold=args.db_thresh, - min_length=args.min_length, - min_interval=args.min_interval, - hop_size=args.hop_size, - max_sil_kept=args.max_sil_kept, - ) - chunks = slicer.slice(audio) - if not os.path.exists(out): - os.makedirs(out) - for i, chunk in enumerate(chunks): - if len(chunk.shape) > 1: - chunk = chunk.T - soundfile.write( - os.path.join( - out, - f"%s_%d.wav" - % (os.path.basename(args.audio).rsplit(".", maxsplit=1)[0], i), - ), - chunk, - sr, - ) - - -if __name__ == "__main__": - main() diff --git a/webui.py b/webui.py index e1c36e1e..40cbcf4f 100644 --- a/webui.py +++ b/webui.py @@ -119,6 +119,7 @@ p_label=None p_uvr5=None p_asr=None p_denoise=None +p_loudness_norm=None p_tts_inference=None def kill_proc_tree(pid, including_parent=True): @@ -246,6 +247,31 @@ def close_denoise(): p_denoise=None return "已终止语音降噪进程",{"__type__":"update","visible":True},{"__type__":"update","visible":False} +def open_loudness_norm(loudness_norm_inp_dir,loudness,peak,num_worker): + global p_loudness_norm + if(p_loudness_norm == None): + loudness_norm_inp_dir=my_utils.clean_path(loudness_norm_inp_dir) + cmd = '"%s" tools/loudness_norm.py -i "%s" -l "%s" -p "%s" -n "%s"'%(python_exec,loudness_norm_inp_dir,loudness,peak,num_worker) + + yield "响度匹配任务开启:%s"%cmd,{"__type__":"update","visible":False},{"__type__":"update","visible":True} + print(cmd) + p_loudness_morm = Popen(cmd, shell=True) + p_loudness_morm.wait() + p_loudness_morm=None + yield f"响度匹配任务完成, 查看终端进行下一步",{"__type__":"update","visible":True},{"__type__":"update","visible":False} + else: + yield "已有正在进行的响度匹配任务,需先终止才能开启下一次任务", {"__type__": "update", "visible": False}, {"__type__": "update", "visible": True} + + +def close_loudness_norm(): + global p_loudness_norm + if(p_loudness_norm!=None): + try: + kill_process(p_loudness_norm.pid) + except: + traceback.print_exc() + return "已终止响度匹配进程",{"__type__":"update","visible":True},{"__type__":"update","visible":False} + p_train_SoVITS=None def open1Ba(batch_size,total_epoch,exp_name,text_low_lr_rate,if_save_latest,if_save_every_weights,save_every_epoch,gpu_numbers1Ba,pretrained_s2G,pretrained_s2D): global p_train_SoVITS @@ -337,42 +363,56 @@ def close1Bb(): p_train_GPT=None return "已终止GPT训练",{"__type__":"update","visible":True},{"__type__":"update","visible":False} -ps_slice=[] -def open_slice(inp,opt_root,threshold,min_length,min_interval,hop_size,max_sil_kept,_max,alpha,n_parts): - global ps_slice - inp = my_utils.clean_path(inp) - opt_root = my_utils.clean_path(opt_root) - if(os.path.exists(inp)==False): +p_slice=None +def open_slice(input_path, output_dir, num_worker, min_duration, max_duration, min_interval, threshold, hop_size, max_sil_kept, merge_short, loudness_norm,loudness, peak): + global p_slice + input_path = my_utils.clean_path(input_path) + output_dir = my_utils.clean_path(output_dir) + if(os.path.exists(input_path)==False): yield "输入路径不存在",{"__type__":"update","visible":True},{"__type__":"update","visible":False} return - if os.path.isfile(inp):n_parts=1 - elif os.path.isdir(inp):pass + if os.path.isfile(input_path):num_worker=1 + elif os.path.isdir(input_path):pass else: yield "输入路径存在但既不是文件也不是文件夹",{"__type__":"update","visible":True},{"__type__":"update","visible":False} return - if (ps_slice == []): - for i_part in range(n_parts): - cmd = '"%s" tools/slice_audio.py "%s" "%s" %s %s %s %s %s %s %s %s %s''' % (python_exec,inp, opt_root, threshold, min_length, min_interval, hop_size, max_sil_kept, _max, alpha, i_part, n_parts) - print(cmd) - p = Popen(cmd, shell=True) - ps_slice.append(p) + if (p_slice == None): + cmd = f'"{python_exec}" tools/slice_audio.py -i "{input_path}" -o "{output_dir}" --threshold {threshold} --min_duration {min_duration} --max_duration {max_duration} --min_interval {min_interval} --hop_size {hop_size} --max_sil_kept {max_sil_kept} --num_worker {num_worker} --merge_short {merge_short}''' + print(cmd) + p_slice = Popen(cmd, shell=True) yield "切割执行中", {"__type__": "update", "visible": False}, {"__type__": "update", "visible": True} - for p in ps_slice: - p.wait() - ps_slice=[] + p_slice.wait() + p_slice=None + if loudness_norm: + loudness_norm_inp_dir = output_dir + global p_loudness_norm + if(p_loudness_norm == None): + loudness_norm_inp_dir=my_utils.clean_path(loudness_norm_inp_dir) + cmd = '"%s" tools/loudness_norm.py -i "%s" -l "%s" -p "%s" -n "%s"'%(python_exec,loudness_norm_inp_dir,loudness,peak,num_worker) + print("响度匹配任务开启") + print(cmd) + p_loudness_morm = Popen(cmd, shell=True) + p_loudness_morm.wait() + p_loudness_morm=None + print("响度匹配任务完成, 查看终端进行下一步") + yield "切割结束",{"__type__":"update","visible":True},{"__type__":"update","visible":False} else: yield "已有正在进行的切割任务,需先终止才能开启下一次任务", {"__type__": "update", "visible": False}, {"__type__": "update", "visible": True} def close_slice(): - global ps_slice - if (ps_slice != []): - for p_slice in ps_slice: - try: - kill_process(p_slice.pid) - except: - traceback.print_exc() - ps_slice=[] + global p_slice + if (p_slice != None): + try: + kill_process(p_slice.pid) + except: + traceback.print_exc() + p_slice=None + if (p_loudness_norm != None): + global p_denoise + if(p_denoise!=None): + kill_process(p_denoise.pid) + p_denoise=None return "已终止所有切割进程", {"__type__": "update", "visible": True}, {"__type__": "update", "visible": False} ps1a=[] @@ -692,16 +732,19 @@ with gr.Blocks(title="GPT-SoVITS WebUI") as app: slice_inp_path=gr.Textbox(label=i18n("音频自动切分输入路径,可文件可文件夹"),value="") slice_opt_root=gr.Textbox(label=i18n("切分后的子音频的输出根目录"),value="output/slicer_opt") threshold=gr.Textbox(label=i18n("threshold:音量小于这个值视作静音的备选切割点"),value="-34") - min_length=gr.Textbox(label=i18n("min_length:每段最小多长,如果第一段太短一直和后面段连起来直到超过这个值"),value="4000") - min_interval=gr.Textbox(label=i18n("min_interval:最短切割间隔"),value="300") + min_duration=gr.Textbox(label=i18n("min_duration:每段最短多长,如果第一段太短一直和后面段连起来直到超过这个值"),value="4.0") + max_duration=gr.Textbox(label=i18n("max_duration:每段最长多长"),value="10.0") + min_interval=gr.Textbox(label=i18n("min_interval:最短切割间隔"),value="0.3") + max_sil_kept=gr.Textbox(label=i18n("max_sil_kept:切完后静音最多留多长"),value="0.5") hop_size=gr.Textbox(label=i18n("hop_size:怎么算音量曲线,越小精度越大计算量越高(不是精度越大效果越好)"),value="10") - max_sil_kept=gr.Textbox(label=i18n("max_sil_kept:切完后静音最多留多长"),value="500") + if_merge_short = gr.Checkbox(label=i18n("对于过短音频的处理方法,勾选则合并,不勾选则抛弃"),show_label=True) with gr.Row(): + loudness=gr.Textbox(label=i18n("目标响度"),value="-23") + peak=gr.Textbox(label=i18n("峰值响度"),value="-1") + if_loudness_norm = gr.Checkbox(label=i18n("是否匹配响度"),show_label=True,value=True) + num_worker=gr.Slider(minimum=1,maximum=n_cpu,step=1,label=i18n("切割使用的进程数"),value=4,interactive=True) open_slicer_button=gr.Button(i18n("开启语音切割"), variant="primary",visible=True) close_slicer_button=gr.Button(i18n("终止语音切割"), variant="primary",visible=False) - _max=gr.Slider(minimum=0,maximum=1,step=0.05,label=i18n("max:归一化后最大值多少"),value=0.9,interactive=True) - alpha=gr.Slider(minimum=0,maximum=1,step=0.05,label=i18n("alpha_mix:混多少比例归一化后音频进来"),value=0.25,interactive=True) - n_process=gr.Slider(minimum=1,maximum=n_cpu,step=1,label=i18n("切割使用的进程数"),value=4,interactive=True) slicer_info = gr.Textbox(label=i18n("语音切割进程输出信息")) gr.Markdown(value=i18n("0bb-语音降噪工具")) with gr.Row(): @@ -770,8 +813,7 @@ with gr.Blocks(title="GPT-SoVITS WebUI") as app: if_uvr5.change(change_uvr5, [if_uvr5], [uvr5_info]) open_asr_button.click(open_asr, [asr_inp_dir, asr_opt_dir, asr_model, asr_size, asr_lang], [asr_info,open_asr_button,close_asr_button]) close_asr_button.click(close_asr, [], [asr_info,open_asr_button,close_asr_button]) - open_slicer_button.click(open_slice, [slice_inp_path,slice_opt_root,threshold,min_length,min_interval,hop_size,max_sil_kept,_max,alpha,n_process], [slicer_info,open_slicer_button,close_slicer_button]) - close_slicer_button.click(close_slice, [], [slicer_info,open_slicer_button,close_slicer_button]) + open_slicer_button.click(open_slice, [slice_inp_path, slice_opt_root, num_worker, min_duration, max_duration, min_interval, threshold, hop_size, max_sil_kept,if_merge_short, if_loudness_norm, loudness, peak], [slicer_info,open_slicer_button,close_slicer_button]) open_denoise_button.click(open_denoise, [denoise_input_dir,denoise_output_dir], [denoise_info,open_denoise_button,close_denoise_button]) close_denoise_button.click(close_denoise, [], [denoise_info,open_denoise_button,close_denoise_button]) @@ -785,7 +827,7 @@ with gr.Blocks(title="GPT-SoVITS WebUI") as app: with gr.TabItem(i18n("1A-训练集格式化工具")): gr.Markdown(value=i18n("输出logs/实验名目录下应有23456开头的文件和文件夹")) with gr.Row(): - inp_text = gr.Textbox(label=i18n("*文本标注文件"),value=r"D:\RVC1006\GPT-SoVITS\raw\xxx.list",interactive=True) + inp_text = gr.Textbox(label=i18n("*文本标注文件"),value=r"output/asr_opt/slicer_opt.list",interactive=True) inp_wav_dir = gr.Textbox( label=i18n("*训练集音频文件目录"), # value=r"D:\RVC1006\GPT-SoVITS\raw\xxx",