Skip to content

Commit 7861a2d

Browse files
committed
Add audio visualization logic
1 parent d7b361c commit 7861a2d

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

pyAudioProcessing/plot.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
"""
4+
Created on Mon Jul 19 18:22:15 2021
5+
6+
@author: jsingh
7+
"""
8+
# Imports
9+
import os
10+
import matplotlib.pyplot as plot
11+
from pyAudioProcessing.utils import read_audio
12+
13+
# Functions
14+
15+
def spectrogram(
16+
input_file, show=True, save_to_disk=True, output_file="spectogram.png"
17+
):
18+
"""
19+
Plot spctrogram of input audio signal and save the output to disk.
20+
"""
21+
sampling_rate, signal = read_audio(input_file)
22+
# Prepare the signal for spectrogram computation
23+
if len(signal) > 0:
24+
if len(list(signal[0])) == 1:
25+
signal = [i for i in signal]
26+
elif len(list(signal[0])) > 1:
27+
signal = [list(i)[0] for i in signal]
28+
# Plot the signal's spectrogram
29+
plot.title('Spectrogram')
30+
plot.specgram(signal, Fs=sampling_rate)
31+
plot.xlabel('Time')
32+
plot.ylabel('Frequency')
33+
if save_to_disk:
34+
plot.savefig(output_file)
35+
if show:
36+
plot.show()
37+
38+
def time(input_file, show=True, save_to_disk=True, output_file="time.png"):
39+
"""
40+
Plot time series audio amplitude and save the output to disk.
41+
"""
42+
sampling_rate, signal = read_audio(input_file)
43+
plot.title('Time series plot')
44+
plot.plot(signal)
45+
46+
plot.xlabel('Sample')
47+
48+
plot.ylabel('Amplitude')
49+
if save_to_disk:
50+
plot.savefig(output_file)
51+
if show:
52+
plot.show()
53+
54+

0 commit comments

Comments
 (0)