-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoice.py
71 lines (57 loc) · 1.95 KB
/
voice.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
69
70
71
# https://stackoverflow.com/questions/18406570/python-record-audio-on-detected-sound?newreg=ce474bdf18e043e0911502ec2587e97c
import pyaudio
import math
import struct
import wave
import time
from ayarlar import *
class Recorder:
@staticmethod
def rms(frame):
count = len(frame) / Genişlik
format = "%dh" % (count)
shorts = struct.unpack(format, frame)
sum_squares = 0.0
for sample in shorts:
n = sample * Normalleştirme
sum_squares += n * n
rms = math.pow(sum_squares / count, 0.5)
return rms * 1000
def __init__(self):
self.p = pyaudio.PyAudio()
self.stream = self.p.open(format=Biçim,
channels=KanalSayısı,
rate=Frekans,
input=True,
output=True,
frames_per_buffer=ParçaDeğeri)
self.done = False
def record(self):
print('Kayıt başladı')
rec = []
current = time.time()
end = time.time() + Normalleştirme
while current <= end:
data = self.stream.read(ParçaDeğeri)
if self.rms(data) >= Hassasiyet: end = time.time() + SessizlikSüresi
current = time.time()
rec.append(data)
self.write(b''.join(rec))
def write(self, recording):
filename = "sounds/recording.wav"
wf = wave.open(filename, 'wb')
wf.setnchannels(KanalSayısı)
wf.setsampwidth(self.p.get_sample_size(Biçim))
wf.setframerate(Frekans)
wf.writeframes(recording)
wf.close()
self.done = True
def listen(self):
while not self.done:
input = self.stream.read(ParçaDeğeri)
rms_val = self.rms(input)
if rms_val > Hassasiyet:
self.record()
if __name__ == "__main__":
a = Recorder()
a.listen()