NeoPy commited on
Commit
a946e1d
·
verified ·
1 Parent(s): 248d53b

Delete utils.py

Browse files
Files changed (1) hide show
  1. utils.py +0 -151
utils.py DELETED
@@ -1,151 +0,0 @@
1
- import ffmpeg
2
- import numpy as np
3
-
4
- # import praatio
5
- # import praatio.praat_scripts
6
- import os
7
- import sys
8
-
9
- import random
10
-
11
- import csv
12
-
13
- platform_stft_mapping = {
14
- "linux": "stftpitchshift",
15
- "darwin": "stftpitchshift",
16
- "win32": "stftpitchshift.exe",
17
- }
18
-
19
- stft = platform_stft_mapping.get(sys.platform)
20
- # praatEXE = join('.',os.path.abspath(os.getcwd()) + r"\Praat.exe")
21
-
22
-
23
- def CSVutil(file, rw, type, *args):
24
- if type == "formanting":
25
- if rw == "r":
26
- with open(file) as fileCSVread:
27
- csv_reader = list(csv.reader(fileCSVread))
28
- return (
29
- (csv_reader[0][0], csv_reader[0][1], csv_reader[0][2])
30
- if csv_reader is not None
31
- else (lambda: exec('raise ValueError("No data")'))()
32
- )
33
- else:
34
- if args:
35
- doformnt = args[0]
36
- else:
37
- doformnt = False
38
- qfr = args[1] if len(args) > 1 else 1.0
39
- tmb = args[2] if len(args) > 2 else 1.0
40
- with open(file, rw, newline="") as fileCSVwrite:
41
- csv_writer = csv.writer(fileCSVwrite, delimiter=",")
42
- csv_writer.writerow([doformnt, qfr, tmb])
43
- elif type == "stop":
44
- stop = args[0] if args else False
45
- with open(file, rw, newline="") as fileCSVwrite:
46
- csv_writer = csv.writer(fileCSVwrite, delimiter=",")
47
- csv_writer.writerow([stop])
48
-
49
-
50
- def load_audio(file, sr, DoFormant, Quefrency, Timbre):
51
- converted = False
52
- DoFormant, Quefrency, Timbre = CSVutil("csvdb/formanting.csv", "r", "formanting")
53
- try:
54
- # https://github.com/openai/whisper/blob/main/whisper/audio.py#L26
55
- # This launches a subprocess to decode audio while down-mixing and resampling as necessary.
56
- # Requires the ffmpeg CLI and `ffmpeg-python` package to be installed.
57
- file = (
58
- file.strip(" ").strip('"').strip("\n").strip('"').strip(" ")
59
- ) # 防止小白拷路径头尾带了空格和"和回车
60
- file_formanted = file.strip(" ").strip('"').strip("\n").strip('"').strip(" ")
61
-
62
- # print(f"dofor={bool(DoFormant)} timbr={Timbre} quef={Quefrency}\n")
63
-
64
- if (
65
- lambda DoFormant: True
66
- if DoFormant.lower() == "true"
67
- else (False if DoFormant.lower() == "false" else DoFormant)
68
- )(DoFormant):
69
- numerator = round(random.uniform(1, 4), 4)
70
- # os.system(f"stftpitchshift -i {file} -q {Quefrency} -t {Timbre} -o {file_formanted}")
71
- # print('stftpitchshift -i "%s" -p 1.0 --rms -w 128 -v 8 -q %s -t %s -o "%s"' % (file, Quefrency, Timbre, file_formanted))
72
-
73
- if not file.endswith(".wav"):
74
- if not os.path.isfile(f"{file_formanted}.wav"):
75
- converted = True
76
- # print(f"\nfile = {file}\n")
77
- # print(f"\nfile_formanted = {file_formanted}\n")
78
- converting = (
79
- ffmpeg.input(file_formanted, threads=0)
80
- .output(f"{file_formanted}.wav")
81
- .run(
82
- cmd=["ffmpeg", "-nostdin"],
83
- capture_stdout=True,
84
- capture_stderr=True,
85
- )
86
- )
87
- else:
88
- pass
89
-
90
- file_formanted = (
91
- f"{file_formanted}.wav"
92
- if not file_formanted.endswith(".wav")
93
- else file_formanted
94
- )
95
-
96
- print(f" · Formanting {file_formanted}...\n")
97
-
98
- os.system(
99
- '%s -i "%s" -q "%s" -t "%s" -o "%sFORMANTED_%s.wav"'
100
- % (
101
- stft,
102
- file_formanted,
103
- Quefrency,
104
- Timbre,
105
- file_formanted,
106
- str(numerator),
107
- )
108
- )
109
-
110
- print(f" · Formanted {file_formanted}!\n")
111
-
112
- # filepraat = (os.path.abspath(os.getcwd()) + '\\' + file).replace('/','\\')
113
- # file_formantedpraat = ('"' + os.path.abspath(os.getcwd()) + '/' + 'formanted'.join(file_formanted) + '"').replace('/','\\')
114
- # print("%sFORMANTED_%s.wav" % (file_formanted, str(numerator)))
115
-
116
- out, _ = (
117
- ffmpeg.input(
118
- "%sFORMANTED_%s.wav" % (file_formanted, str(numerator)), threads=0
119
- )
120
- .output("-", format="f32le", acodec="pcm_f32le", ac=1, ar=sr)
121
- .run(
122
- cmd=["ffmpeg", "-nostdin"], capture_stdout=True, capture_stderr=True
123
- )
124
- )
125
-
126
- try:
127
- os.remove("%sFORMANTED_%s.wav" % (file_formanted, str(numerator)))
128
- except Exception:
129
- pass
130
- print("couldn't remove formanted type of file")
131
-
132
- else:
133
- out, _ = (
134
- ffmpeg.input(file, threads=0)
135
- .output("-", format="f32le", acodec="pcm_f32le", ac=1, ar=sr)
136
- .run(
137
- cmd=["ffmpeg", "-nostdin"], capture_stdout=True, capture_stderr=True
138
- )
139
- )
140
- except Exception as e:
141
- raise RuntimeError(f"Failed to load audio: {e}")
142
-
143
- if converted:
144
- try:
145
- os.remove(file_formanted)
146
- except Exception:
147
- pass
148
- print("couldn't remove converted type of file")
149
- converted = False
150
-
151
- return np.frombuffer(out, np.float32).flatten()