-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsound.py
67 lines (56 loc) · 1.76 KB
/
sound.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
import threading
import os
import queue
import sys
import sounddevice as sd
import soundfile as sf
from pynput import keyboard
flag = True # 停止标志位
class SoundRecThread(threading.Thread):
def __init__(self, audiofile='record.wav'):
threading.Thread.__init__(self)
self.bRecord = True
self.filename = audiofile
self.samplerate = 44100
self.channels = 2
def run(self):
q = queue.Queue()
def callback(indata, frames, time, status):
"""This is called (from a separate thread) for each audio block."""
if status:
print(status, file=sys.stderr)
q.put(indata.copy())
with sf.SoundFile(self.filename,
mode='x',
samplerate=self.samplerate,
channels=self.channels) as file:
with sd.InputStream(samplerate=self.samplerate,
channels=self.channels,
callback=callback):
while self.bRecord:
file.write(q.get())
def stoprecord(self):
self.bRecord = False
def start_sound():
wav_file = "temp.wav"
t2 = SoundRecThread(wav_file)
t2.start()
with keyboard.Listener(on_press=on_press) as listener:
listener.join()
while flag:
pass
t2.stoprecord()
def on_press(key):
"""
键盘监听事件!!!
:param key:
:return:
"""
# print(key)
global flag
if key == keyboard.Key.esc:
flag = False
print("stop monitor!")
return False # 返回False,键盘监听结束!
if __name__ == "__main__":
start_sound()