-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplot.py
59 lines (44 loc) · 1.04 KB
/
plot.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
# imports
import matplotlib.pyplot as plt
import numpy as np
import wave, sys
# shows the sound waves
def visualize(path: str):
# reading the audio file
raw = wave.open(path)
# reads all the frames
# -1 indicates all or max frames
signal = raw.readframes(-1)
signal = np.frombuffer(signal, dtype ="int16")
# gets the frame rate
f_rate = raw.getframerate()
# to Plot the x-axis in seconds
# you need get the frame rate
# and divide by size of your signal
# to create a Time Vector
# spaced linearly with the size
# of the audio file
time = np.linspace(
0, # start
len(signal) / f_rate,
num = len(signal)
)
# using matplotlib to plot
# creates a new figure
plt.figure(1)
# title of the plot
plt.title("Sound Wave")
# label of x-axis
plt.xlabel("Time")
# actual plotting
plt.plot(time, signal)
# shows the plot
# in new window
plt.show()
# you can also save
# the plot using
# plt.savefig('filename')
if __name__ == "__main__":
# gets the command line Value
path = './audio@epoch_10.wav'
visualize(path)