-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
27 lines (22 loc) · 998 Bytes
/
utils.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
# Copyright (C) 2022. Huawei Technologies Co., Ltd. All rights reserved.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the MIT License.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# MIT License for more details.
import numpy as np
import matplotlib.pyplot as plt
from scipy.io import wavfile
def save_plot(tensor, savepath):
plt.style.use('default')
fig, ax = plt.subplots(figsize=(12, 3))
im = ax.imshow(tensor, aspect="auto", origin="lower", interpolation='none')
plt.colorbar(im, ax=ax)
plt.tight_layout()
fig.canvas.draw()
plt.savefig(savepath)
plt.close()
def save_audio(file_path, sampling_rate, audio):
audio = np.clip(audio.detach().cpu().squeeze().numpy(), -0.999, 0.999)
wavfile.write(file_path, sampling_rate, (audio * 32767).astype("int16"))