-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.py
97 lines (73 loc) · 2.81 KB
/
index.py
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
import json
import pyaudacity as pa
import uuid
import os
import utils
# python = "/home/erwan/anaconda3/envs/ai_perso/bin/python"
python = "python"
questions = utils.get_answers()
def import_audio(input_path, name=None):
pa.do("Select: Track=0")
pa.do(f"Import2: Filename={input_path}")
if name:
pa.do(f"SetTrackStatus: Name={name}")
def get_tracks():
tracks = pa.do("GetInfo: Type=Tracks Format=JSON").replace("BatchCommand finished: OK", "")
return json.loads(tracks)
def get_track_by_name(name):
tracks = get_tracks()
for i, track in enumerate(tracks):
if track.get("name") == name:
return str(i)
return False
def select_track(track):
pa.do(f"Select: Track={track}")
id = str(uuid.uuid4())[:5]
utils.mkdir(os.path.join(utils.TEMP_FOLDER, id))
filename = os.path.join(utils.TEMP_FOLDER, id, f"{id}.mp3")
# export the selected part to mp3
pa.export(filename)
if questions.get("separate") is True:
# separate vocals from song using demucs
utils.separate_tracks(filename, id)
separated_song_folder = os.path.join(utils.TEMP_FOLDER, id)
# importe voice
voice_label = f"voix-{id}"
import_audio(os.path.join(separated_song_folder, "vocals.wav"), voice_label)
pa.do("Align_StartToSelStart:")
# import other
other_label = f"autres-{id}"
import_audio(os.path.join(separated_song_folder, "other.wav"), other_label)
pa.do("Align_StartToSelStart:")
# import drums
drums_label = f"batterie-{id}"
import_audio(os.path.join(separated_song_folder, "drums.wav"), drums_label)
pa.do("Align_StartToSelStart:")
# import bass
bass_label = f"bass-{id}"
import_audio(os.path.join(separated_song_folder, "bass.wav"), bass_label)
pa.do("Align_StartToSelStart:")
# remove files after import
# os.remove(os.path.join(separated_song_folder, "vocals.wav"))
# os.remove(os.path.join(separated_song_folder, "other.wav"))
# os.remove(os.path.join(separated_song_folder, "drums.wav"))
# os.remove(os.path.join(separated_song_folder, "bass.wav"))
# os.removedirs(separated_song_folder)
# fit tracks vertically
pa.do("FitV:")
if questions.get("model"):
model_name = questions.get("model")
if questions.get("separate", False) is True:
# Mute original track
select_track("0")
pa.mute_tracks()
utils.change_voice(os.path.join(separated_song_folder, "vocals.wav"), id, str(questions.get("pitch", 0)), questions["model"])
import_audio(os.path.join(utils.TEMP_FOLDER, id, "aivoice.wav"), f"voix_ai_{model_name}-{id}")
pa.do("Align_StartToSelStart:")
# fit tracks vertically
pa.do("FitV:")
# mute separated audio track
select_track(get_track_by_name(voice_label))
pa.mute_tracks()
# else:
# pa.export(filename)