Skip to content

Commit 1a91387

Browse files
authored
Merge pull request #3 from nimaiji/sound-class
Sound class
2 parents 7158099 + 4d7170d commit 1a91387

File tree

2 files changed

+100
-1
lines changed

2 files changed

+100
-1
lines changed

README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,29 @@
1-
# python-soundrecognition
1+
# Sound Recognition with Python
2+
3+
## First Of All
4+
Clone or Download project and ...
5+
```python
6+
import Sound
7+
```
8+
## Sound simulation
9+
* Initialize a Sound module with
10+
```python
11+
s = Sound.Sound(path='path.wav')
12+
```
13+
or
14+
```python
15+
s = Sound.Sound(path='path.wav',name='mySound')
16+
```
17+
18+
* Initialize FFT(Fast Fourier transform) on our Sound module
19+
```python
20+
s.initFFT()
21+
print(s.getFFT())
22+
s.drawFFT()
23+
```
24+
* Sound schematics
25+
```python
26+
s.draw() #Sound Digital Schematic
27+
s.drawFFT() #Sound FFT Schematic
28+
```
29+

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)