forked from AlexandaJerry/whisper-vits-japanese
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchange_sample_rate.py
62 lines (51 loc) · 2.19 KB
/
change_sample_rate.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
import librosa
import soundfile
import os
import time
import sys
import shutil
def pre_process_audio(audio_path):
path_audio_processed = './ready_for_slice/'
if not os.path.exists(path_audio_processed):
try:
os.mkdir(path_audio_processed)
except OSError:
print('Creation of directory %s failed' %path_audio_processed)
else:
print('Successfully created the directory %s' %path_audio_processed)
start_sub = time.time()
n = 0
print('Downsampling wav files...')
for file in os.listdir(audio_path):
if(file.endswith('.wav')):
try:
nameSolo_1 = file.rsplit('.', 1)[0]
data, samplerate = librosa.load(audio_path + file, sr=22050) # Downsample 44.1kHz to 24kHz
soundfile.write(path_audio_processed + nameSolo_1 + '.wav', data, samplerate, subtype='PCM_16')
n = n+1
print('File ', n , ' completed:', nameSolo_1)
except EOFError as error:
next
print('Downsampling complete')
print('---------------------------------------------------------------------')
# s = 0
# print('Changing bit pro sample...')
# for file in os.listdir(path_audio_processed):
# if(file.endswith('.wav')):
# try:
# nameSolo_2 = file.rsplit('.', 1)[0]
# #nameSolo_2 = nameSolo_2.replace('')
# data, samplerate = soundfile.read(path_audio_processed + file)
# soundfile.write(path_audio_processed + nameSolo_2 + '.wav', data, samplerate, subtype='PCM_16')
# s = s + 1
# print('File ' , s , ' completed')
# except EOFError as error:
# next
# print('Bit pro sample changed')
# print('---------------------------------------------------------------------')
#shutil.rmtree('./audio', ignore_errors=True)
end_sub = time.time()
print('The script took ', end_sub-start_sub, ' seconds to run')
#Source:
#https://stackoverflow.com/questions/30619740/python-downsampling-wav-audio-file
#https://stackoverflow.com/questions/44812553/how-to-convert-a-24-bit-wav-file-to-16-or-32-bit-files-in-python3