2021-10-08 03:28:00 +00:00
|
|
|
from typing import Optional
|
|
|
|
|
2021-09-10 05:13:40 +00:00
|
|
|
import torch
|
|
|
|
import torch.nn as nn
|
2021-10-08 03:28:00 +00:00
|
|
|
from scipy.signal.windows import hann
|
2021-09-10 05:13:40 +00:00
|
|
|
from spleeter.audio.adapter import AudioAdapter
|
|
|
|
from torch.utils.data import Dataset
|
2021-10-08 03:28:00 +00:00
|
|
|
import numpy as np
|
|
|
|
import librosa
|
2021-09-10 05:13:40 +00:00
|
|
|
|
|
|
|
from data.util import find_audio_files
|
|
|
|
|
|
|
|
|
2021-10-08 03:28:00 +00:00
|
|
|
def spleeter_stft(
|
|
|
|
data: np.ndarray, inverse: bool = False, length: Optional[int] = None
|
|
|
|
) -> np.ndarray:
|
|
|
|
"""
|
|
|
|
Single entrypoint for both stft and istft. This computes stft and
|
|
|
|
istft with librosa on stereo data. The two channels are processed
|
|
|
|
separately and are concatenated together in the result. The
|
|
|
|
expected input formats are: (n_samples, 2) for stft and (T, F, 2)
|
|
|
|
for istft.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
data (numpy.array):
|
|
|
|
Array with either the waveform or the complex spectrogram
|
|
|
|
depending on the parameter inverse
|
|
|
|
inverse (bool):
|
|
|
|
(Optional) Should a stft or an istft be computed.
|
|
|
|
length (Optional[int]):
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
numpy.ndarray:
|
|
|
|
Stereo data as numpy array for the transform. The channels
|
|
|
|
are stored in the last dimension.
|
|
|
|
"""
|
|
|
|
assert not (inverse and length is None)
|
|
|
|
data = np.asfortranarray(data)
|
|
|
|
N = 4096
|
|
|
|
H = 1024
|
|
|
|
win = hann(N, sym=False)
|
|
|
|
fstft = librosa.core.istft if inverse else librosa.core.stft
|
|
|
|
win_len_arg = {"win_length": None, "length": None} if inverse else {"n_fft": N}
|
|
|
|
n_channels = data.shape[-1]
|
|
|
|
out = []
|
|
|
|
for c in range(n_channels):
|
|
|
|
d = (
|
|
|
|
np.concatenate((np.zeros((N,)), data[:, c], np.zeros((N,))))
|
|
|
|
if not inverse
|
|
|
|
else data[:, :, c].T
|
|
|
|
)
|
|
|
|
s = fstft(d, hop_length=H, window=win, center=False, **win_len_arg)
|
|
|
|
if inverse:
|
|
|
|
s = s[N: N + length]
|
|
|
|
s = np.expand_dims(s.T, 2 - inverse)
|
|
|
|
out.append(s)
|
|
|
|
if len(out) == 1:
|
|
|
|
return out[0]
|
|
|
|
return np.concatenate(out, axis=2 - inverse)
|
|
|
|
|
|
|
|
|
2021-09-10 05:13:40 +00:00
|
|
|
class SpleeterDataset(Dataset):
|
2021-09-14 23:43:40 +00:00
|
|
|
def __init__(self, src_dir, sample_rate=22050, max_duration=20, skip=0):
|
2021-09-10 05:13:40 +00:00
|
|
|
self.files = find_audio_files(src_dir, include_nonwav=True)
|
2021-09-14 23:43:40 +00:00
|
|
|
if skip > 0:
|
|
|
|
self.files = self.files[skip:]
|
2021-09-10 05:13:40 +00:00
|
|
|
self.audio_loader = AudioAdapter.default()
|
|
|
|
self.sample_rate = sample_rate
|
|
|
|
self.max_duration = max_duration
|
|
|
|
|
|
|
|
def __getitem__(self, item):
|
|
|
|
file = self.files[item]
|
2021-10-08 03:28:00 +00:00
|
|
|
wave, sample_rate = self.audio_loader.load(file, sample_rate=self.sample_rate)
|
|
|
|
assert sample_rate == self.sample_rate
|
|
|
|
stft = torch.tensor(spleeter_stft(wave))
|
|
|
|
# TODO: pad this up so it can be batched.
|
2021-09-10 05:13:40 +00:00
|
|
|
return {
|
|
|
|
'path': file,
|
|
|
|
'wave': wave,
|
2021-10-08 03:28:00 +00:00
|
|
|
'stft': stft,
|
|
|
|
#'duration': original_duration,
|
2021-09-10 05:13:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.files)
|