File size: 6,063 Bytes
cb092c7
 
 
 
 
 
 
 
 
 
490fae8
 
 
 
e66c674
 
 
 
 
 
cb092c7
 
 
 
 
 
 
 
 
 
 
 
 
 
490fae8
cb092c7
 
 
 
 
 
 
 
 
490fae8
cb092c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
490fae8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb092c7
 
 
490fae8
cb092c7
 
 
 
 
490fae8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# separation_utils.py
import numpy as np
import os
import librosa
import soundfile as sf
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.special import rel_entr
import nussl
import types
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
import collections
import collections.abc
collections.MutableMapping = collections.abc.MutableMapping
collections.Sequence = collections.abc.Sequence
collections.Mapping = collections.abc.Mapping



def _validate_mask_patched(self, mask_):
    assert isinstance(mask_, np.ndarray), 'Mask must be a numpy array!'
    if mask_.dtype == bool:
        return mask_
    mask_ = mask_ > 0.5
    if not np.all(np.logical_or(mask_, np.logical_not(mask_))):
        raise ValueError('All mask entries must be 0 or 1.')
    return mask_

nussl.core.masks.binary_mask.BinaryMask._validate_mask = types.MethodType(
    _validate_mask_patched, nussl.core.masks.binary_mask.BinaryMask)


def Repet(mix):
    return nussl.separation.primitive.Repet(mix)( )

def Repet_Sim(mix):
    return nussl.separation.primitive.RepetSim(mix)( )

def Two_DFT(mix):
    return nussl.separation.primitive.FT2D(mix)( )


def calculate_psnr(clean_signal, separated_signal):
    min_length = min(len(clean_signal), len(separated_signal))
    clean_signal = clean_signal[:min_length]
    separated_signal = separated_signal[:min_length]
    mse = np.mean((clean_signal - separated_signal) ** 2)
    if mse == 0:
        return float('inf')
    max_val = np.max(np.abs(clean_signal))
    return 10 * np.log10((max_val ** 2) / mse)

def calculate_melspectrogram_kl_divergence(clean_signal, separated_signal, sr):
    clean_mel = compute_mel_spectrogram(clean_signal, sr)
    separated_mel = compute_mel_spectrogram(separated_signal, sr)
    clean_mel_norm = clean_mel / np.sum(clean_mel)
    separated_mel_norm = separated_mel / np.sum(separated_mel)
    return np.sum(rel_entr(np.clip(clean_mel_norm, 1e-10, None), np.clip(separated_mel_norm, 1e-10, None)))

def compute_mel_spectrogram(signal, sr, n_fft=2048, hop_length=512, n_mels=128):
    return librosa.feature.melspectrogram(
        y=signal, sr=sr, n_fft=n_fft, hop_length=hop_length, n_mels=n_mels, power=2.0
    )

def extract_features(audio, sr, frame_size=5046, hop_length=2048):
    zcr = librosa.feature.zero_crossing_rate(audio, frame_length=frame_size, hop_length=hop_length)
    rms = librosa.feature.rms(y=audio, frame_length=frame_size, hop_length=hop_length)
    spectral_centroid = librosa.feature.spectral_centroid(y=audio, sr=sr, hop_length=hop_length)
    features = np.vstack((zcr, rms, spectral_centroid)).T
    return features

def process_pipeline(fg_path, bg_path, sr):
    fg_audio, _ = librosa.load(fg_path, sr=sr)
    bg_audio, _ = librosa.load(bg_path, sr=sr)
    fg_features = extract_features(fg_audio, sr)
    bg_features = extract_features(bg_audio, sr)
    fg_labels = np.ones(fg_features.shape[0])
    bg_labels = np.zeros(bg_features.shape[0])
    features = np.vstack((fg_features, bg_features))
    labels = np.hstack((fg_labels, bg_labels))
    return features, labels

def train_rf_model(X, y):
    X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)
    clf = RandomForestClassifier(n_estimators=100, random_state=42)
    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_val)
    print(classification_report(y_val, y_pred))
    return clf

def reconstruct_audio(mixed_audio, labels, sr, frame_size=2048, hop_length=512):
    frames = librosa.util.frame(mixed_audio, frame_length=frame_size, hop_length=hop_length).T
    labels = labels[:frames.shape[0]]
    fg_frames = frames[labels == 1.0] if np.any(labels == 1.0) else np.zeros_like(frames[:1])
    bg_frames = frames[labels == 0.0] if np.any(labels == 0.0) else np.zeros_like(frames[:1])
    fg_audio = librosa.istft(fg_frames.T, hop_length=hop_length) if fg_frames.shape[0] > 0 else np.zeros_like(mixed_audio)
    bg_audio = librosa.istft(bg_frames.T, hop_length=hop_length) if bg_frames.shape[0] > 0 else np.zeros_like(mixed_audio)
    return fg_audio, bg_audio

def process_audio(file_path):
    signal = nussl.AudioSignal(file_path)
    mix_signal, sr = librosa.load(file_path, sr=None)

    ft2d_bg, ft2d_fg = Two_DFT(signal)
    repet_bg, repet_fg = Repet(signal)
    rsim_bg, rsim_fg = Repet_Sim(signal)

    # Save the 3 outputs
    fg_paths = {
        "2dft": "output_foreground_2dft.wav",
        "repet": "output_foreground_repet.wav",
        "rsim": "output_foreground_rsim.wav"
    }
    ft2d_fg.write_audio_to_file(fg_paths["2dft"])
    repet_fg.write_audio_to_file(fg_paths["repet"])
    rsim_fg.write_audio_to_file(fg_paths["rsim"])

    # Select best for training
    fg_path, bg_path = fg_paths["rsim"], fg_paths["repet"]  # Use RepetSim FG and Repet BG

    features, labels = process_pipeline(fg_path, bg_path, sr)
    clf = train_rf_model(features, labels)

    test_features = extract_features(mix_signal, sr)
    predicted_labels = clf.predict(test_features)
    fg_rec, bg_rec = reconstruct_audio(mix_signal, predicted_labels, sr)

    fg_rf_path = "output_foreground_rf.wav"
    bg_rf_path = "output_background_rf.wav"
    sf.write(fg_rf_path, fg_rec, sr)
    sf.write(bg_rf_path, bg_rec, sr)

    psnr_rf = calculate_psnr(signal.audio_data, fg_rec)
    kl_rf = calculate_melspectrogram_kl_divergence(signal.audio_data, fg_rec, sr)

    return (
        fg_paths["2dft"], calculate_psnr(signal.audio_data, ft2d_fg.audio_data), calculate_melspectrogram_kl_divergence(signal.audio_data, ft2d_fg.audio_data, sr),
        fg_paths["repet"], calculate_psnr(signal.audio_data, repet_fg.audio_data), calculate_melspectrogram_kl_divergence(signal.audio_data, repet_fg.audio_data, sr),
        fg_paths["rsim"], calculate_psnr(signal.audio_data, rsim_fg.audio_data), calculate_melspectrogram_kl_divergence(signal.audio_data, rsim_fg.audio_data, sr),
        fg_rf_path, psnr_rf, kl_rf,
        bg_rf_path
    )