File size: 4,579 Bytes
7b26bef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89f9b6c
6f1bbcd
 
7b26bef
 
 
6f1bbcd
 
 
7b26bef
6f1bbcd
8140dfa
6f1bbcd
 
 
 
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
import streamlit as st
import os
import tempfile
import subprocess

def separate_audio(audio_path):

    print(f"{audio_path=}")
    head, tail = os.path.split(audio_path)

    gradio_temp_path = head
    audio_filename = tail.split('.')[0]
    print(f"{gradio_temp_path=}")
    print(f"{audio_filename=}")
    
    command = f"spleeter separate -p spleeter:2stems {audio_path}"
    command = command.split()
    print(f"{command=}")

    result = subprocess.run(command)
    print(result)
    
    print("--------")
    accompaniment_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/accompaniment.wav"
    vocals_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/vocals.wav"
    print(f"{accompaniment_path=}")
    print(os.path.exists(accompaniment_path))
    print(f"{vocals_path=}")
    print(os.path.exists(vocals_path))

    return vocals_path, accompaniment_path


def separate_audio_by_stem(audio_path, stem_count):

    print(f"{audio_path=}")
    head, tail = os.path.split(audio_path)

    gradio_temp_path = head
    audio_filename = tail.split('.')[0]
    print(f"{gradio_temp_path=}")
    print(f"{audio_filename=}")
    print(f"{stem_count=}")
    
    command = f"spleeter separate -p spleeter:{stem_count}stems {audio_path}"
    command = command.split()
    print(f"{command=}")

    result = subprocess.run(command)
    print(result)

    if stem_count == 2:
        accompaniment_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/accompaniment.wav"
        vocals_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/vocals.wav"

        print(f"{accompaniment_path=} \t exists: {os.path.exists(accompaniment_path)}")
        print(f"{vocals_path=} \t exists: {os.path.exists(vocals_path)}")
        
        return [
            {'description': 'Accompaniment', 'path':accompaniment_path},
            {'description': 'Vocals', 'path':vocals_path},
        ]
    
    elif stem_count == 4:
        
        vocals_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/vocals.wav"
        drums_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/drums.wav"
        bass_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/bass.wav"
        other_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/other.wav"
        
        print(f"{vocals_path=} \t exists: {os.path.exists(vocals_path)}")
        print(f"{drums_path=} \t exists: {os.path.exists(drums_path)}")
        print(f"{bass_path=} \t exists: {os.path.exists(bass_path)}")
        print(f"{other_path=} \t exists: {os.path.exists(other_path)}")

        return [
            {'description': 'Vocals', 'path':vocals_path},
            {'description': 'Drums', 'path':drums_path},
            {'description': 'Bass', 'path':bass_path},
            {'description': 'Other', 'path':other_path},
        ]
        
    elif stem_count == 5:

        piano_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/piano.wav"
        vocals_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/vocals.wav"
        drums_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/drums.wav"
        bass_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/bass.wav"
        other_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/other.wav"
        
        print(f"{piano_path=} \t exists: {os.path.exists(vocals_path)}")
        print(f"{vocals_path=} \t exists: {os.path.exists(vocals_path)}")
        print(f"{drums_path=} \t exists: {os.path.exists(drums_path)}")
        print(f"{bass_path=} \t exists: {os.path.exists(bass_path)}")
        print(f"{other_path=} \t exists: {os.path.exists(other_path)}")

        return [
            {'description': 'Vocals', 'path':vocals_path},
            {'description': 'Piano', 'path':piano_path},
            {'description': 'Drums', 'path':drums_path},
            {'description': 'Bass', 'path':bass_path},
            {'description': 'Other', 'path':other_path},
        ]
    
uploaded_file = st.file_uploader("", type=["wav","mp3"])
selected_stem_count = st.radio("Stem count", (2,4,5))

if uploaded_file is not None:

    with tempfile.NamedTemporaryFile(delete=False) as temp_file:        
        temp_file.write(uploaded_file.read())
        temp_file_path = temp_file.name
        
    separate_audios = separate_audio_by_stem(temp_file_path, selected_stem_count)
    
    st.write("Output Files:")
    for audio in separate_audios:
        st.write(audio['description'])
        st.audio(audio['path'], format="audio/wav", start_time=0)