Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Sequence Models/Week 3/Trigger word detection/td_utils.py
Views: 13377
import matplotlib.pyplot as plt1from scipy.io import wavfile2import os3from pydub import AudioSegment45# Calculate and plot spectrogram for a wav audio file6def graph_spectrogram(wav_file):7rate, data = get_wav_info(wav_file)8nfft = 200 # Length of each window segment9fs = 8000 # Sampling frequencies10noverlap = 120 # Overlap between windows11nchannels = data.ndim12if nchannels == 1:13pxx, freqs, bins, im = plt.specgram(data, nfft, fs, noverlap = noverlap)14elif nchannels == 2:15pxx, freqs, bins, im = plt.specgram(data[:,0], nfft, fs, noverlap = noverlap)16return pxx1718# Load a wav file19def get_wav_info(wav_file):20rate, data = wavfile.read(wav_file)21return rate, data2223# Used to standardize volume of audio clip24def match_target_amplitude(sound, target_dBFS):25change_in_dBFS = target_dBFS - sound.dBFS26return sound.apply_gain(change_in_dBFS)2728# Load raw audio files for speech synthesis29def load_raw_audio():30activates = []31backgrounds = []32negatives = []33for filename in os.listdir("./raw_data/activates"):34if filename.endswith("wav"):35activate = AudioSegment.from_wav("./raw_data/activates/"+filename)36activates.append(activate)37for filename in os.listdir("./raw_data/backgrounds"):38if filename.endswith("wav"):39background = AudioSegment.from_wav("./raw_data/backgrounds/"+filename)40backgrounds.append(background)41for filename in os.listdir("./raw_data/negatives"):42if filename.endswith("wav"):43negative = AudioSegment.from_wav("./raw_data/negatives/"+filename)44negatives.append(negative)45return activates, negatives, backgrounds4647