ZeroTwo3 commited on
Commit
383ae4b
·
1 Parent(s): 068c34c

Upload APIs.py

Browse files
Files changed (1) hide show
  1. APIs.py +202 -0
APIs.py ADDED
@@ -0,0 +1,202 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import numpy as np
3
+ import requests
4
+ import yaml
5
+ import pyloudnorm as pyln
6
+ from scipy.io.wavfile import write
7
+ import torchaudio
8
+ from retrying import retry
9
+
10
+ os.environ['OPENBLAS_NUM_THREADS'] = '1'
11
+ SAMPLE_RATE = 32000
12
+
13
+ def get_service_url():
14
+ service_url = os.environ.get('WAVJOURNEY_SERVICE_URL')
15
+ return service_url
16
+
17
+ def get_service_port():
18
+ service_port = os.environ.get('WAVJOURNEY_SERVICE_PORT')
19
+ return service_port
20
+
21
+ with open('config.yaml', 'r') as file:
22
+ config = yaml.safe_load(file)
23
+ service_port = get_service_port()
24
+ localhost_addr = get_service_url()
25
+ enable_sr = config['Speech-Restoration']['Enable']
26
+
27
+ def LOUDNESS_NORM(audio, sr=32000, volumn=-25):
28
+ # peak normalize audio to -1 dB
29
+ peak_normalized_audio = pyln.normalize.peak(audio, -10.0)
30
+ # measure the loudness first
31
+ meter = pyln.Meter(sr) # create BS.1770 meter
32
+ loudness = meter.integrated_loudness(peak_normalized_audio)
33
+ # loudness normalize audio to -12 dB LUFS
34
+ normalized_audio = pyln.normalize.loudness(peak_normalized_audio, loudness, volumn)
35
+ return normalized_audio
36
+
37
+ def WRITE_AUDIO(wav, name=None, sr=SAMPLE_RATE):
38
+ """
39
+ function: write audio numpy to .wav file
40
+ @params:
41
+ wav: np.array [samples]
42
+ """
43
+ if name is None:
44
+ name = 'output.wav'
45
+
46
+ if len(wav.shape) > 1:
47
+ wav = wav[0]
48
+
49
+ # declipping
50
+
51
+ max_value = np.max(np.abs(wav))
52
+ if max_value > 1:
53
+ wav *= 0.9 / max_value
54
+
55
+ # write audio
56
+ write(name, sr, np.round(wav*32767).astype(np.int16))
57
+
58
+ def READ_AUDIO_NUMPY(wav, sr=SAMPLE_RATE):
59
+ """
60
+ function: read audio numpy
61
+ return: np.array [samples]
62
+ """
63
+ waveform, sample_rate = torchaudio.load(wav)
64
+
65
+ if sample_rate != sr:
66
+ waveform = torchaudio.functional.resample(waveform, orig_freq=sample_rate, new_freq=sr)
67
+
68
+ wav_numpy = waveform[0].numpy()
69
+
70
+ return wav_numpy
71
+
72
+ def MIX(wavs=[['1.wav', 0.], ['2.wav', 10.]], out_wav='out.wav', sr=SAMPLE_RATE):
73
+ """
74
+ wavs:[[wav_name, absolute_offset], ...]
75
+ """
76
+
77
+ max_length = max([int(wav[1]*sr + len(READ_AUDIO_NUMPY(wav[0]))) for wav in wavs])
78
+ template_wav = np.zeros(max_length)
79
+
80
+ for wav in wavs:
81
+ cur_name, cur_offset = wav
82
+ cur_wav = READ_AUDIO_NUMPY(cur_name)
83
+ cur_len = len(cur_wav)
84
+ cur_offset = int(cur_offset * sr)
85
+
86
+ # mix
87
+ template_wav[cur_offset:cur_offset+cur_len] += cur_wav
88
+
89
+ WRITE_AUDIO(template_wav, name=out_wav)
90
+
91
+ def CAT(wavs, out_wav='out.wav'):
92
+ """
93
+ wavs: List of wav file ['1.wav', '2.wav', ...]
94
+ """
95
+ wav_num = len(wavs)
96
+
97
+ segment0 = READ_AUDIO_NUMPY(wavs[0])
98
+
99
+ cat_wav = segment0
100
+
101
+ if wav_num > 1:
102
+ for i in range(1, wav_num):
103
+ next_wav = READ_AUDIO_NUMPY(wavs[i])
104
+ cat_wav = np.concatenate((cat_wav, next_wav), axis=-1)
105
+
106
+ WRITE_AUDIO(cat_wav, name=out_wav)
107
+
108
+ def COMPUTE_LEN(wav):
109
+ wav= READ_AUDIO_NUMPY(wav)
110
+ return len(wav) / 32000
111
+
112
+ @retry(stop_max_attempt_number=5, wait_fixed=2000)
113
+ def TTM(text, length=10, volume=-28, out_wav='out.wav'):
114
+ url = f'http://{localhost_addr}:{service_port}/generate_music'
115
+ url = "https://zerotwo3-wavjourney.hf.space/generate_music"
116
+ data = {
117
+ 'text': f'{text}',
118
+ 'length': f'{length}',
119
+ 'volume': f'{volume}',
120
+ 'output_wav': f'{out_wav}',
121
+ }
122
+
123
+ response = requests.post(url, json=data)
124
+
125
+ if response.status_code == 200:
126
+ print('Success:', response.json()['message'])
127
+ else:
128
+ print('Error:', response.json()['API error'])
129
+ raise RuntimeError(response.json()['API error'])
130
+
131
+ @retry(stop_max_attempt_number=5, wait_fixed=2000)
132
+ def TTA(text, length=5, volume=-35, out_wav='out.wav'):
133
+ url = f'http://{localhost_addr}:{service_port}/generate_audio'
134
+ url = "https://zerotwo3-wavjourney.hf.space/generate_audio"
135
+ data = {
136
+ 'text': f'{text}',
137
+ 'length': f'{length}',
138
+ 'volume': f'{volume}',
139
+ 'output_wav': f'{out_wav}',
140
+ }
141
+
142
+ response = requests.post(url, json=data)
143
+
144
+ if response.status_code == 200:
145
+ print('Success:', response.json()['message'])
146
+ else:
147
+ print('Error:', response.json()['API error'])
148
+ raise RuntimeError(response.json()['API error'])
149
+
150
+ @retry(stop_max_attempt_number=5, wait_fixed=2000)
151
+ def TTS(text, volume=-20, out_wav='out.wav', enhanced=enable_sr, speaker_id='', speaker_npz=''):
152
+ url = f'http://{localhost_addr}:{service_port}/generate_speech'
153
+ url = "https://zerotwo3-wavjourney.hf.space/generate_speech"
154
+ data = {
155
+ 'text': f'{text}',
156
+ 'speaker_id': f'{speaker_id}',
157
+ 'speaker_npz': f'{speaker_npz}',
158
+ 'volume': f'{volume}',
159
+ 'output_wav': f'{out_wav}',
160
+ }
161
+
162
+ response = requests.post(url, json=data)
163
+ print(response.json())
164
+ if response.status_code == 200:
165
+ print('Success:', response.json()['message'])
166
+ else:
167
+ print('Error:', response.json()['API error'])
168
+ raise RuntimeError(response.json()['API error'])
169
+
170
+ if enhanced:
171
+ SR(processfile=out_wav)
172
+
173
+ @retry(stop_max_attempt_number=5, wait_fixed=2000)
174
+ def SR(processfile):
175
+ url = f'http://{localhost_addr}:{service_port}/fix_audio'
176
+ url = "https://zerotwo3-wavjourney.hf.space/fix_audio"
177
+ data = {'processfile': f'{processfile}'}
178
+
179
+ response = requests.post(url, json=data)
180
+
181
+ if response.status_code == 200:
182
+ print('Success:', response.json()['message'])
183
+ else:
184
+ print('Error:', response.json()['API error'])
185
+ raise RuntimeError(response.json()['API error'])
186
+
187
+ @retry(stop_max_attempt_number=5, wait_fixed=2000)
188
+ def VP(wav_path, out_dir):
189
+ url = f'http://{localhost_addr}:{service_port}/parse_voice'
190
+ url = "https://zerotwo3-wavjourney.hf.space/parse_voice"
191
+ data = {
192
+ 'wav_path': f'{wav_path}',
193
+ 'out_dir':f'{out_dir}'
194
+ }
195
+
196
+ response = requests.post(url, json=data)
197
+
198
+ if response.status_code == 200:
199
+ print('Success:', response.json()['message'])
200
+ else:
201
+ print('Error:', response.json()['API error'])
202
+ raise RuntimeError(response.json()['API error'])