-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathhelper.py
75 lines (59 loc) · 1.8 KB
/
helper.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
import matplotlib.pyplot as plt
import librosa
from pathlib import Path
from encoder.inference import plot_embedding_as_heatmap
import sounddevice as sd
import wavio
def draw_embed(embed, name, which):
"""
Draws an embedding.
Parameters:
embed (np.array): array of embedding
name (str): title of plot
Return:
fig: matplotlib figure
"""
fig, embed_ax = plt.subplots()
plot_embedding_as_heatmap(embed)
embed_ax.set_title(name)
embed_ax.set_aspect("equal", "datalim")
embed_ax.set_xticks([])
embed_ax.set_yticks([])
embed_ax.figure.canvas.draw()
return fig
def create_spectrogram(voice_sample):
"""
Creates and saves a spectrogram plot for a sound sample.
Parameters:
voice_sample (str): path to sample of sound
Return:
fig
"""
in_fpath = Path(voice_sample.replace('"', "").replace("'", ""))
original_wav, sampling_rate = librosa.load(str(in_fpath))
# Plot the signal read from wav file
fig = plt.figure()
plt.subplot(211)
plt.title(f"Spectrogram of file {voice_sample}")
plt.plot(original_wav)
plt.xlabel("Sample")
plt.ylabel("Amplitude")
plt.subplot(212)
plt.specgram(original_wav, Fs=sampling_rate)
plt.xlabel("Time")
plt.ylabel("Frequency")
# plt.savefig(voice_sample.split(".")[0] + "_spectogram.png")
return fig
def read_audio(file):
with open(file, "rb") as audio_file:
audio_bytes = audio_file.read()
return audio_bytes
def record(duration=5, fs=48000):
sd.default.samplerate = fs
sd.default.channels = 1
myrecording = sd.rec(int(duration * fs))
sd.wait(duration)
return myrecording
def save_record(path_myrecording, myrecording, fs):
wavio.write(path_myrecording, myrecording, fs, sampwidth=2)
return None