Skip to content

Commit 28d4061

Browse files
committed
Sound Module
1 parent 7158099 commit 28d4061

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

Sound.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import matplotlib.pyplot as plt
2+
import numpy as np
3+
from scipy.io import wavfile
4+
from scipy.fftpack import fft,fftfreq
5+
6+
import sys
7+
8+
def printCmd(str):
9+
h = (32*'-')+'\n'
10+
print(h+str+'\n'+h)
11+
12+
class Sound:
13+
def __init__(self,**kwargs):
14+
if (kwargs['path'] == ''):
15+
raise AttributeError('please insert a path')
16+
else:
17+
self.path = kwargs['path']
18+
19+
try:
20+
self.name = kwargs['name']
21+
except:
22+
self.name = self.path
23+
24+
self.data = []
25+
self.samplerate = 0
26+
self.fft = []
27+
self.frame = 0
28+
printCmd('\''+self.name + '\' created.')
29+
30+
def readWav(self):
31+
samplerate,data = wavfile.read(self.path,'r')
32+
printCmd('sample rate: '+samplerate)
33+
self.data = data
34+
self.samplerate = samplerate
35+
return self.data
36+
37+
def initFFT(self):
38+
self.fft = fft(self.data)
39+
40+
def getFFT(self):
41+
return self.fft
42+
43+
def draw(self):
44+
plt.figure(self.name)
45+
plt.plot(self.data)
46+
plt.grid(True)
47+
plt.show()
48+
49+
def drawFFT(self):
50+
plt.figure(self.name + ' FFT')
51+
plt.plot(self.fft)
52+
plt.xlim([10, self.samplerate / 2])
53+
plt.xscale('log')
54+
plt.grid(True)
55+
plt.xlabel('Frequency (Hz)')
56+
plt.show()
57+
58+
def drawAll(self):
59+
plt.figure(self.name)
60+
plt.plot(self.data)
61+
plt.grid(True)
62+
plt.figure(self.name + ' FFT')
63+
plt.plot(self.fft)
64+
plt.xlim([10, self.samplerate / 2])
65+
plt.xscale('log')
66+
plt.grid(True)
67+
plt.xlabel('Frequency (Hz)')
68+
plt.show()
69+
70+
def __str__(self):
71+
return self.data

0 commit comments

Comments
 (0)