CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
y33-j3T

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: y33-j3T/Coursera-Deep-Learning
Path: blob/master/Sequence Models/Week 3/Trigger word detection/td_utils.py
Views: 13377
1
import matplotlib.pyplot as plt
2
from scipy.io import wavfile
3
import os
4
from pydub import AudioSegment
5
6
# Calculate and plot spectrogram for a wav audio file
7
def graph_spectrogram(wav_file):
8
rate, data = get_wav_info(wav_file)
9
nfft = 200 # Length of each window segment
10
fs = 8000 # Sampling frequencies
11
noverlap = 120 # Overlap between windows
12
nchannels = data.ndim
13
if nchannels == 1:
14
pxx, freqs, bins, im = plt.specgram(data, nfft, fs, noverlap = noverlap)
15
elif nchannels == 2:
16
pxx, freqs, bins, im = plt.specgram(data[:,0], nfft, fs, noverlap = noverlap)
17
return pxx
18
19
# Load a wav file
20
def get_wav_info(wav_file):
21
rate, data = wavfile.read(wav_file)
22
return rate, data
23
24
# Used to standardize volume of audio clip
25
def match_target_amplitude(sound, target_dBFS):
26
change_in_dBFS = target_dBFS - sound.dBFS
27
return sound.apply_gain(change_in_dBFS)
28
29
# Load raw audio files for speech synthesis
30
def load_raw_audio():
31
activates = []
32
backgrounds = []
33
negatives = []
34
for filename in os.listdir("./raw_data/activates"):
35
if filename.endswith("wav"):
36
activate = AudioSegment.from_wav("./raw_data/activates/"+filename)
37
activates.append(activate)
38
for filename in os.listdir("./raw_data/backgrounds"):
39
if filename.endswith("wav"):
40
background = AudioSegment.from_wav("./raw_data/backgrounds/"+filename)
41
backgrounds.append(background)
42
for filename in os.listdir("./raw_data/negatives"):
43
if filename.endswith("wav"):
44
negative = AudioSegment.from_wav("./raw_data/negatives/"+filename)
45
negatives.append(negative)
46
return activates, negatives, backgrounds
47