-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudioListener.py
More file actions
34 lines (28 loc) · 890 Bytes
/
audioListener.py
File metadata and controls
34 lines (28 loc) · 890 Bytes
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
import soundcard as sc
import numpy as np
import asyncio
class AudioListener:
def __init__(self):
# Gets default speaker to use as name match for loopback
self.speaker = sc.default_speaker()
# Sets input as loopback for default speaker
self.input = sc.get_microphone(self.speaker.name, include_loopback=True)
self.currentFreq = 0
self.previousFreq = 0
async def record(self):
with self.input.recorder(samplerate=48000, channels=2) as recorder:
data = recorder.record(numframes=1024)
data = np.average(data, axis=1)
fft = np.fft.fft(data)
freqs = np.fft.fftfreq(len(fft))
l = len(data)
imax = np.argmax(np.abs(fft))
fs = freqs[imax]
freq = abs(fs*20480)
print(freq)
return freq
async def startListen(self):
while(True):
self.previousFreq = self.currentFreq
self.currentFreq = await self.record()
await asyncio.sleep(0.005)