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| 1 |
+
import gradio as gr
|
| 2 |
+
import yt_dlp
|
| 3 |
+
import numpy as np
|
| 4 |
+
import librosa
|
| 5 |
+
import soundfile as sf
|
| 6 |
+
|
| 7 |
+
# Function to download audio from YouTube and save it as a WAV file
|
| 8 |
+
def download_youtube_audio(url, audio_name):
|
| 9 |
+
ydl_opts = {
|
| 10 |
+
'format': 'bestaudio/best',
|
| 11 |
+
'postprocessors': [{
|
| 12 |
+
'key': 'FFmpegExtractAudio',
|
| 13 |
+
'preferredcodec': 'wav',
|
| 14 |
+
}],
|
| 15 |
+
"outtmpl": f'youtubeaudio/{audio_name}', # Output template
|
| 16 |
+
}
|
| 17 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 18 |
+
ydl.download([url])
|
| 19 |
+
return f'youtubeaudio/{audio_name}.wav'
|
| 20 |
+
|
| 21 |
+
# Function to calculate RMS
|
| 22 |
+
def get_rms(y, frame_length=2048, hop_length=512, pad_mode="constant"):
|
| 23 |
+
padding = (int(frame_length // 2), int(frame_length // 2))
|
| 24 |
+
y = np.pad(y, padding, mode=pad_mode)
|
| 25 |
+
|
| 26 |
+
axis = -1
|
| 27 |
+
out_strides = y.strides + tuple([y.strides[axis]])
|
| 28 |
+
x_shape_trimmed = list(y.shape)
|
| 29 |
+
x_shape_trimmed[axis] -= frame_length - 1
|
| 30 |
+
out_shape = tuple(x_shape_trimmed) + tuple([frame_length])
|
| 31 |
+
xw = np.lib.stride_tricks.as_strided(
|
| 32 |
+
y, shape=out_shape, strides=out_strides
|
| 33 |
+
)
|
| 34 |
+
if axis < 0:
|
| 35 |
+
target_axis = axis - 1
|
| 36 |
+
else:
|
| 37 |
+
target_axis = axis + 1
|
| 38 |
+
xw = np.moveaxis(xw, -1, target_axis)
|
| 39 |
+
slices = [slice(None)] * xw.ndim
|
| 40 |
+
slices[axis] = slice(0, None, hop_length)
|
| 41 |
+
x = xw[tuple(slices)]
|
| 42 |
+
|
| 43 |
+
power = np.mean(np.abs(x) ** 2, axis=-2, keepdims=True)
|
| 44 |
+
return np.sqrt(power)
|
| 45 |
+
|
| 46 |
+
# Slicer class
|
| 47 |
+
class Slicer:
|
| 48 |
+
def __init__(self, sr, threshold=-40., min_length=5000, min_interval=300, hop_size=20, max_sil_kept=5000):
|
| 49 |
+
if not min_length >= min_interval >= hop_size:
|
| 50 |
+
raise ValueError('The following condition must be satisfied: min_length >= min_interval >= hop_size')
|
| 51 |
+
if not max_sil_kept >= hop_size:
|
| 52 |
+
raise ValueError('The following condition must be satisfied: max_sil_kept >= hop_size')
|
| 53 |
+
min_interval = sr * min_interval / 1000
|
| 54 |
+
self.threshold = 10 ** (threshold / 20.)
|
| 55 |
+
self.hop_size = round(sr * hop_size / 1000)
|
| 56 |
+
self.win_size = min(round(min_interval), 4 * self.hop_size)
|
| 57 |
+
self.min_length = round(sr * min_length / 1000 / self.hop_size)
|
| 58 |
+
self.min_interval = round(min_interval / self.hop_size)
|
| 59 |
+
self.max_sil_kept = round(sr * max_sil_kept / 1000 / self.hop_size)
|
| 60 |
+
|
| 61 |
+
def _apply_slice(self, waveform, begin, end):
|
| 62 |
+
if len(waveform.shape) > 1:
|
| 63 |
+
return waveform[:, begin * self.hop_size: min(waveform.shape[1], end * self.hop_size)]
|
| 64 |
+
else:
|
| 65 |
+
return waveform[begin * self.hop_size: min(waveform.shape[0], end * self.hop_size)]
|
| 66 |
+
|
| 67 |
+
def slice(self, waveform):
|
| 68 |
+
if len(waveform.shape) > 1:
|
| 69 |
+
samples = waveform.mean(axis=0)
|
| 70 |
+
else:
|
| 71 |
+
samples = waveform
|
| 72 |
+
if samples.shape[0] <= self.min_length:
|
| 73 |
+
return [waveform]
|
| 74 |
+
rms_list = get_rms(y=samples, frame_length=self.win_size, hop_length=self.hop_size).squeeze(0)
|
| 75 |
+
sil_tags = []
|
| 76 |
+
silence_start = None
|
| 77 |
+
clip_start = 0
|
| 78 |
+
for i, rms in enumerate(rms_list):
|
| 79 |
+
if rms < self.threshold:
|
| 80 |
+
if silence_start is None:
|
| 81 |
+
silence_start = i
|
| 82 |
+
continue
|
| 83 |
+
if silence_start is None:
|
| 84 |
+
continue
|
| 85 |
+
is_leading_silence = silence_start == 0 and i > self.max_sil_kept
|
| 86 |
+
need_slice_middle = i - silence_start >= self.min_interval and i - clip_start >= self.min_length
|
| 87 |
+
if not is_leading_silence and not need_slice_middle:
|
| 88 |
+
silence_start = None
|
| 89 |
+
continue
|
| 90 |
+
if i - silence_start <= self.max_sil_kept:
|
| 91 |
+
pos = rms_list[silence_start: i + 1].argmin() + silence_start
|
| 92 |
+
if silence_start == 0:
|
| 93 |
+
sil_tags.append((0, pos))
|
| 94 |
+
else:
|
| 95 |
+
sil_tags.append((pos, pos))
|
| 96 |
+
clip_start = pos
|
| 97 |
+
elif i - silence_start <= self.max_sil_kept * 2:
|
| 98 |
+
pos = rms_list[i - self.max_sil_kept: silence_start + self.max_sil_kept + 1].argmin()
|
| 99 |
+
pos += i - self.max_sil_kept
|
| 100 |
+
pos_l = rms_list[silence_start: silence_start + self.max_sil_kept + 1].argmin() + silence_start
|
| 101 |
+
pos_r = rms_list[i - self.max_sil_kept: i + 1].argmin() + i - self.max_sil_kept
|
| 102 |
+
if silence_start == 0:
|
| 103 |
+
sil_tags.append((0, pos_r))
|
| 104 |
+
clip_start = pos_r
|
| 105 |
+
else:
|
| 106 |
+
sil_tags.append((min(pos_l, pos), max(pos_r, pos)))
|
| 107 |
+
clip_start = max(pos_r, pos)
|
| 108 |
+
else:
|
| 109 |
+
pos_l = rms_list[silence_start: silence_start + self.max_sil_kept + 1].argmin() + silence_start
|
| 110 |
+
pos_r = rms_list[i - self.max_sil_kept: i + 1].argmin() + i - self.max_sil_kept
|
| 111 |
+
if silence_start == 0:
|
| 112 |
+
sil_tags.append((0, pos_r))
|
| 113 |
+
else:
|
| 114 |
+
sil_tags.append((pos_l, pos_r))
|
| 115 |
+
clip_start = pos_r
|
| 116 |
+
silence_start = None
|
| 117 |
+
total_frames = rms_list.shape[0]
|
| 118 |
+
if silence_start is not None and total_frames - silence_start >= self.min_interval:
|
| 119 |
+
silence_end = min(total_frames, silence_start + self.max_sil_kept)
|
| 120 |
+
pos = rms_list[silence_start: silence_end + 1].argmin() + silence_start
|
| 121 |
+
sil_tags.append((pos, total_frames + 1))
|
| 122 |
+
if len(sil_tags) == 0:
|
| 123 |
+
return [waveform]
|
| 124 |
+
else:
|
| 125 |
+
chunks = []
|
| 126 |
+
if sil_tags[0][0] > 0:
|
| 127 |
+
chunks.append(self._apply_slice(waveform, 0, sil_tags[0][0]))
|
| 128 |
+
for i in range(len(sil_tags) - 1):
|
| 129 |
+
chunks.append(self._apply_slice(waveform, sil_tags[i][1], sil_tags[i + 1][0]))
|
| 130 |
+
if sil_tags[-1][1] < total_frames:
|
| 131 |
+
chunks.append(self._apply_slice(waveform, sil_tags[-1][1], total_frames))
|
| 132 |
+
return chunks
|
| 133 |
+
|
| 134 |
+
# Function to slice and save audio chunks
|
| 135 |
+
def slice_audio(file_path, audio_name):
|
| 136 |
+
audio, sr = librosa.load(file_path, sr=None, mono=False)
|
| 137 |
+
slicer = Slicer(sr=sr, threshold=-40, min_length=5000, min_interval=500, hop_size=10, max_sil_kept=500)
|
| 138 |
+
chunks = slicer.slice(audio)
|
| 139 |
+
for i, chunk in enumerate(chunks):
|
| 140 |
+
if len(chunk.shape) > 1:
|
| 141 |
+
chunk = chunk.T
|
| 142 |
+
sf.write(f'dataset/{audio_name}/split_{i}.wav', chunk, sr)
|
| 143 |
+
return f"Audio sliced and saved in dataset/{audio_name}/"
|
| 144 |
+
|
| 145 |
+
# Gradio interface
|
| 146 |
+
def process_audio(url, audio_name):
|
| 147 |
+
file_path = download_youtube_audio(url, audio_name)
|
| 148 |
+
result = slice_audio(file_path, audio_name)
|
| 149 |
+
return result
|
| 150 |
+
|
| 151 |
+
with gr.Blocks() as demo:
|
| 152 |
+
gr.Markdown("RVC DATASET MAKER 2.0")
|
| 153 |
+
with gr.Tabs():
|
| 154 |
+
with gr.Row():
|
| 155 |
+
url_input = gr.Textbox(label="YouTube URL")
|
| 156 |
+
audio_name_input = gr.Textbox(label="Audio Name")
|
| 157 |
+
result_output = gr.Textbox(label="Output Directory")
|
| 158 |
+
run_button = gr.Button("Download and Slice Audio")
|
| 159 |
+
run_button.click(fn=process_audio, inputs=[url_input, audio_name_input], outputs=result_output)
|
| 160 |
+
|
| 161 |
+
demo.launch()
|