-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathaudio.py
231 lines (162 loc) · 7.12 KB
/
audio.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# Audio-related helper functions
import fnmatch
import glob
import os
import subprocess
import pydub
import tmpfile
import helper
import ifs
import wavbintool
helper.check_ffmpeg()
def get_audio_file(filename):
filename = helper.getCaseInsensitivePath(filename)
if not filename or not os.path.exists(filename):
return None
if filename.lower().endswith('.xa'):
wav_filename = helper.getCaseInsensitivePath(filename.lower().replace('.xa', '.wav'))
if not os.path.exists(wav_filename):
filename = get_wav_from_xa(filename)
else:
filename = wav_filename
return pydub.AudioSegment.from_file(filename, "wav")
def get_duration(filename):
filename = helper.getCaseInsensitivePath(filename)
sound_file = get_audio_file(filename)
if not sound_file:
return 0
return len(sound_file) / 1000
def clip_audio(input_filename, output_filename, duration, loop_duration=0.370):
filename = helper.getCaseInsensitivePath(input_filename)
sound_file = get_audio_file(filename)
while duration * 1000 > len(sound_file):
if len(sound_file) < loop_duration * 1000:
tail_loop = sound_file[::]
else:
tail_loop = sound_file[-loop_duration * 1000:]
sound_file = sound_file.append(tail_loop, crossfade=30 if len(tail_loop) > 30 else len(tail_loop) - 1)
sound_file = sound_file[:duration * 1000]
sound_file.export(output_filename, format="wav")
# print("Generated", output_filename, len(sound_file) / 1000, duration)
def merge_bgm(bgm_info, input_foldername, output_filename=None):
longest_duration = bgm_info['end']
# Find maximum duration of BGM
channels = 1
for bgm in bgm_info['data']:
filename = helper.getCaseInsensitivePath(os.path.join(input_foldername, bgm['filename']))
print(filename)
bgm['file'] = pydub.AudioSegment.from_file(filename)
duration = bgm['timestamp'] + len(bgm['file']) / 1000
if bgm['file'].channels > channels:
channels = bgm['file'].channels
if duration > longest_duration:
longest_duration = duration
output = pydub.AudioSegment.silent(duration=longest_duration * 1000, frame_rate=48000)
output.set_channels(channels)
for bgm in bgm_info['data']:
output = output.overlay(bgm['file'], position=bgm['timestamp'] * 1000)
if output_filename:
temp_filename = output_filename
else:
temp_filename = tmpfile.mkstemp(suffix=".wav")
output.export(temp_filename, format="wav")
return temp_filename
def get_wav_from_xa(input_filename):
input_filename = helper.getCaseInsensitivePath(input_filename)
prefix = ""
if helper.is_wsl():
prefix = "./"
elif os.name != "nt":
prefix = "wine "
cmd = "{}xa.exe -d \"{}\"".format(prefix, helper.get_windows_path(input_filename))
subprocess.call(cmd, shell=True)
temp_filename = os.path.splitext(input_filename)[0] + ".wav"
tmpfile.add_temp_file(temp_filename)
return temp_filename
def get_wav_from_pcm(input_filename):
input_filename = helper.getCaseInsensitivePath(input_filename)
prefix = ""
if helper.is_wsl():
prefix = "./"
elif os.name != "nt":
prefix = "wine "
wav_filename = os.path.splitext(input_filename)[0] + ".wav"
cmd = "{}vgmstream_cli.exe -q -o \"{}\" \"{}\"".format(prefix, helper.get_windows_path(wav_filename), helper.get_windows_path(input_filename))
subprocess.call(cmd, shell=True)
return wav_filename
def get_processed_wav(input_filename, output_filename=None, channels=1, bits=16, rate=48000):
input_filename = helper.getCaseInsensitivePath(input_filename)
output = get_audio_file(input_filename)
if not output:
return None
made_change = False
if output.sample_width != bits // 8:
made_change = True
output = output.set_sample_width(bits // 8)
if output.channels != channels:
made_change = True
output = output.set_channels(channels)
if output.frame_rate != rate:
made_change = True
output = output.set_frame_rate(rate)
if not made_change and input_filename.lower().endswith('.wav'):
# This file is already the exact requirements, just return the original
return input_filename
if output_filename == None:
output_filename = tmpfile.mkstemp(suffix=".wav")
#print("Converted {} to {}".format(input_filename, output_filename))
output.export(output_filename, format="wav")
return output_filename
def get_isolated_bgm(input, instrument):
if os.path.isfile(input):
filenames_bgm, ifs_path = ifs.extract(input)
tmpfile.add_temp_folder(ifs_path)
input = ifs_path
else:
filenames_bgm = glob.glob(input + "/bgm*.bin") + glob.glob(input + "/bgm*.wav")
ifs_path = input
# Find matching BGMs for each isolation type
isolation_bgms_bin = {
"drum": [("bgm*___k.bin", "bgm*d__k.bin"), ("bgm*___k_xg.bin", "bgm*d__k_xg.bin")],
"guitar": [("bgm*__bk.bin", "bgm*_gbk.bin"), ("bgm*__bk_xg.bin", "bgm*_gbk_xg.bin")],
"bass": [("bgm*___k.bin", "bgm*__bk.bin"), ("bgm*___k_xg.bin", "bgm*__bk_xg.bin")],
}
isolation_bgms_wav = {
"drum": [("bgm*___k.wav", "bgm*d__k.wav"), ("bgm*___k_xg.wav", "bgm*d__k_xg.wav")],
"guitar": [("bgm*__bk.wav", "bgm*_gbk.wav"), ("bgm*__bk_xg.wav", "bgm*_gbk_xg.wav")],
"bass": [("bgm*___k.wav", "bgm*__bk.wav"), ("bgm*___k_xg.wav", "bgm*__bk_xg.wav")],
}
filenames_bgm_basename = [os.path.basename(x) for x in filenames_bgm]
base_bgm = None
instrument_bgm = None
for isolated_bgms in [isolation_bgms_bin, isolation_bgms_wav]:
isolated_bgm_set = isolated_bgms[instrument]
for (base_bgm_pattern, instrument_bgm_pattern) in isolated_bgm_set:
if not base_bgm:
results = fnmatch.filter(filenames_bgm_basename, base_bgm_pattern)
if len(results) > 0:
base_bgm = results[0]
if not instrument_bgm:
results = fnmatch.filter(filenames_bgm_basename, instrument_bgm_pattern)
if len(results) > 0:
instrument_bgm = results[0]
if base_bgm and instrument_bgm:
break
if base_bgm and instrument_bgm:
break
base_bgm = os.path.join(input, base_bgm)
instrument_bgm = os.path.join(input, instrument_bgm)
if base_bgm.endswith(".bin"):
base_bgm_out = base_bgm.replace(".bin", ".wav")
wavbintool.parse_bin(base_bgm, base_bgm_out)
tmpfile.add_temp_file(base_bgm_out)
base_bgm = base_bgm_out
if instrument_bgm.endswith(".bin"):
instrument_bgm_out = instrument_bgm.replace(".bin", ".wav")
wavbintool.parse_bin(instrument_bgm, instrument_bgm_out)
tmpfile.add_temp_file(instrument_bgm_out)
instrument_bgm = instrument_bgm_out
base_audio = pydub.AudioSegment.from_file(base_bgm).invert_phase()
instrument_audio = pydub.AudioSegment.from_file(instrument_bgm)
instrument_phased = instrument_audio.overlay(base_audio)
return instrument_phased, base_bgm