Created
December 15, 2017 05:41
-
-
Save StanSilas/eb76e5843753780b0165bcf49eccaac5 to your computer and use it in GitHub Desktop.
printing audio segments on a plot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import wave | |
import pickle | |
import numpy as np | |
from sklearn.mixture import GMM | |
import librosa | |
#from auditok import DataValidator, ADSFactory, DataSource, StreamTokenizer, BufferAudioSource, player_for | |
import warnings | |
warnings.filterwarnings('ignore') | |
SAMPLING_RATE =16000 | |
wfp = wave.open("/home/vivek/Music/test_hold.wav") | |
audio_data = wfp.readframes(-1) | |
width = wfp.getsampwidth() | |
wfp.close() | |
# data as numpy array will be used to plot signal | |
fmt = {1: np.int8 , 2: np.int16, 4: np.int32} | |
signal = np.array(np.frombuffer(audio_data, dtype=fmt[width]), dtype=np.float64) | |
%matplotlib inline | |
import matplotlib.pyplot as plt | |
import matplotlib.pylab as pylab | |
pylab.rcParams['figure.figsize'] = 24, 18 | |
def plot_signal_and_segmentation(signal, sampling_rate, segments=[]): | |
_time = np.arange(0., np.ceil(float(len(signal))) / sampling_rate, 1./sampling_rate ) | |
if len(_time) > len(signal): | |
_time = _time[: len(signal) - len(_time)] | |
pylab.subplot(211) | |
for seg in segments: | |
fc = seg.get("fc", "g") | |
ec = seg.get("ec", "b") | |
lw = seg.get("lw", 2) | |
alpha = seg.get("alpha", 0.4) | |
ts = seg["timestamps"] | |
# plot first segmentation outside loop to show one single legend for this class | |
p = pylab.axvspan(ts[0][0], ts[0][1], fc=fc, ec=ec, lw=lw, alpha=alpha, label = seg.get("title", "")) | |
for start, end in ts[1:]: | |
p = pylab.axvspan(start, end, fc=fc, ec=ec, lw=lw, alpha=alpha) | |
pylab.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, | |
borderaxespad=0., fontsize=22, ncol=2) | |
pylab.plot(_time, signal) | |
pylab.xlabel("Time (s)", fontsize=22) | |
pylab.ylabel("Signal Amplitude", fontsize=22) | |
pylab.show() | |
annotations = {} | |
ts = [line.rstrip("\r\n\t ").split(" ") for line in open("/home/vivek/Music/hold.lst").readlines()] | |
ts = [(float(t[0]), float(t[1])) for t in ts] | |
annotations["hold"] = {"fc" : "y", "ec" : "y", "lw" : 0, "alpha" : 0.4, "title" : "Hold", "timestamps" : ts} | |
ts = [line.rstrip("\r\n\t ").split(" ") for line in open("/home/vivek/Music/tring.lst").readlines()] | |
ts = [(float(t[0]), float(t[1])) for t in ts] | |
annotations["tring"] = {"fc" : "r", "ec" : "r", "lw" : 0, "alpha" : 0.9, "title" : "Tring", "timestamps" : ts} | |
def plot_annot(): | |
plot_signal_and_segmentation(signal, SAMPLING_RATE, | |
[annotations["tring"], | |
annotations["hold"]]) | |
plot_annot() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment