-
Notifications
You must be signed in to change notification settings - Fork 0
/
__microphone__.py
68 lines (58 loc) · 1.73 KB
/
__microphone__.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
60
61
62
63
64
65
66
67
68
import pydub
import math
import wave
import struct
class Microphone(object):
CHUNK_MILLES = 50
'''
'''
def __init__(self, x, y, fileName):
self.fileName = fileName
self.mx = x
self.my = y
self.signal = pydub.AudioSegment.from_wav(fileName)
self.amplitudeList = []
waveFile = wave.open(self.fileName, 'r')
length = waveFile.getnframes()
for i in range(0, length):
waveData = waveFile.readframes(i - i + 1)
data = struct.unpack("<h", waveData)
self.amplitudeList.append(int(data[0]))
'''
Breaks wav file into chunks and gets abs(dBFS) of each of them to get the mean
'''
def getNormalizedDBFS(self):
sounds = list(self.signal[::self.CHUNK_MILLES])
normalizedMeans = []
for i in range(len(sounds)):
normalizedMeans.append(abs(sounds[i].dBFS))
if(len(normalizedMeans) == 0):
return 0
return sum(normalizedMeans)/len(normalizedMeans)
def getAmplitudeList(self):
return self.amplitudeList
@property
def weight(self):
return self.weight
@property
def x(self):
return self.mx
@property
def y(self):
return self.my
def adjustWeighting(self, dB):
self.signal = self.signal - dB
@property
def angle(self):
a = self.x
b = self.y
degrees = math.degrees(math.atan(abs(b)/abs(a)))
if a > 0 and b > 0:
return degrees
elif a < 0 and b > 0:
return 180 - degrees
elif a < 0 and b < 0:
return 180 + degrees
elif a > 0 and b < 0:
return 360 - degrees
return degrees