File size: 1,717 Bytes
a361db3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Advanced source separation using neural networks.
Requires: pip install asteroid-bin pyannote.audio
"""

import soundfile as sf
import os


def separate_with_neural(audio_path: str, output_dir: str, n_sources: int = 6):
    """
    Use asteroid or pyannote for better separation.
    """

    try:
        from asteroid.models import ConvTasNet

        print("Using Conv-TasNet for separation...")

        model = ConvTasNet.from_pretrained("mpariente/ConvTasNet_WHAM!_sepclean")

        audio, sr = sf.read(audio_path)
        if audio.ndim > 1:
            audio = audio.mean(axis=1)

        import torch

        audio_tensor = torch.from_numpy(audio).float().unsqueeze(0)

        with torch.no_grad():
            estimates = model(audio_tensor)

        os.makedirs(output_dir, exist_ok=True)

        for i in range(min(n_sources, estimates.shape[1])):
            source = estimates[0, i].numpy()
            sf.write(os.path.join(output_dir, f"neural_source_{i + 1}.wav"), source, sr)

        print(f"Saved {min(n_sources, estimates.shape[1])} neural-separated sources")

    except ImportError:
        print("Neural libraries not available. Install with:")
        print("  pip install asteroid-bin pyannote.audio")

        audio, sr = sf.read(audio_path)

        print(f"\nFallback: Using current ICA separation")
        print(f"  Audio: {audio_path}")
        print(f"  Duration: {len(audio) / sr:.1f}s")

        return False

    return True


if __name__ == "__main__":
    import sys

    audio_file = sys.argv[1] if len(sys.argv) > 1 else "../data/mixture.wav"
    output = sys.argv[2] if len(sys.argv) > 2 else "output_neural"

    separate_with_neural(audio_file, output, n_sources=6)