gpt4 book ai didi

python - 每次用户按键时如何录制音频?

转载 作者:行者123 更新时间:2023-12-04 01:21:50 25 4
gpt4 key购买 nike

当且仅当用户按下 ctrl 键并在用户按下 ctrl+c 键时关闭录音循环,如何不确定地录制用户的音频? 目前基于一些在线示例构建这个脚本:

from pynput import keyboard
import time, os
import pyaudio
import wave
import sched
import sys
from playsound import playsound


CHUNK = 8192
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
WAVE_OUTPUT_FILENAME = "mic.wav"

p = pyaudio.PyAudio()
frames = []

def callback(in_data, frame_count, time_info, status):
frames.append(in_data)
return (in_data, pyaudio.paContinue)

class MyListener(keyboard.Listener):
def __init__(self):
super(MyListener, self).__init__(self.on_press, self.on_release)
self.key_pressed = None
self.wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
self.wf.setnchannels(CHANNELS)
self.wf.setsampwidth(p.get_sample_size(FORMAT))
self.wf.setframerate(RATE)
def on_press(self, key):

try:
if key.ctrl:
self.key_pressed = True
return True
except AttributeError:
sys.exit()





def on_release(self, key):

if key.ctrl:
self.key_pressed = False
return True


listener = MyListener()


listener.start()
started = False
stream = None

def recorder():
global started, p, stream, frames

while True:

try:
if listener.key_pressed and not started:
# Start the recording
try:
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK,
stream_callback = callback)
print("Stream active:", stream.is_active())
started = True
print("start Stream")
except KeyboardInterrupt:
print('\nRecording finished: ' + repr(WAVE_OUTPUT_FILENAME))
quit()

elif not listener.key_pressed and started:

print("Stop recording")
listener.wf.writeframes(b''.join(frames))
listener.wf.close()
print("You should have a wav file in the current directory")
print('-> Playing recorded sound...')
playsound(str(os.getcwd())+'/mic.wav')
os.system('python "/Users/user/rec.py"')

except KeyboardInterrupt:
print('\nRecording finished: ' + repr(WAVE_OUTPUT_FILENAME))
quit()
except AttributeError:
quit()





print ("-> Press and hold the 'ctrl' key to record your audio")
print ("-> Release the 'ctrl' key to end recording")


recorder()

问题在于它确实效率低下,例如计算机开始发热。我发现让程序继续运行并录制不同音频样本的唯一方法是使用:os.system('python "/Users/user/rec.py"')。为了完成程序,我尝试使用以下方法捕获异常:

except AttributeError:
sys.exit()

或使用用户输入:

if key.ctrl_c:
sys.exit()

基于 pyinput docs ,我试图有效地利用听众。但是,对于这种特定情况,推荐使用这些监听器的方式是什么?

最佳答案

至于您的计算机似乎工作异常困难的主要问题,那是因为您使用了一个 while 循环来不断检查记录 key 何时被释放。在这个循环中,计算机将尽可能快地循环,而不会中断。

更好的解决方案是使用事件驱动编程,让操作系统定期通知您事件,并在事件发生时检查您是否想做任何事情。这听起来可能很复杂,但幸运的是pynput为您完成大部分艰苦的工作。

如果您跟踪记录或回放的状态,那么在下一次控制键按下事件发生时开始新的记录也相当简单,而不需要为每个递归调用整个新过程的“黑客攻击”新录音。键盘监听器内的事件循环将继续进行,直到其中一个回调函数返回 False。或加注 self.stopException() .

我创建了一个简单的 listener类似于您最初尝试调用记录器或播放器实例(我稍后会谈到)来启动和停止的类。我也必须同意 Anwarvic 的观点 <ctl-c>应该保留作为停止脚本的紧急方式,所以我将停止命令更改为字母 q .

class listener(keyboard.Listener):
def __init__(self, recorder, player):
super().__init__(on_press = self.on_press, on_release = self.on_release)
self.recorder = recorder
self.player = player

def on_press(self, key):
if key is None: #unknown event
pass
elif isinstance(key, keyboard.Key): #special key event
if key.ctrl and self.player.playing == 0:
self.recorder.start()
elif isinstance(key, keyboard.KeyCode): #alphanumeric key event
if key.char == 'q': #press q to quit
if self.recorder.recording:
self.recorder.stop()
return False #this is how you stop the listener thread
if key.char == 'p' and not self.recorder.recording:
self.player.start()

def on_release(self, key):
if key is None: #unknown event
pass
elif isinstance(key, keyboard.Key): #special key event
if key.ctrl:
self.recorder.stop()
elif isinstance(key, keyboard.KeyCode): #alphanumeric key event
pass

if __name__ == '__main__':
r = recorder("mic.wav")
p = player("mic.wav")
l = listener(r, p)
print('hold ctrl to record, press p to playback, press q to quit')
l.start() #keyboard listener is a thread so we start it here
l.join() #wait for the tread to terminate so the program doesn't instantly close

有了这个结构,我们就需要一个带有启动和停止功能的记录器类,它不会阻止(异步)监听器线程继续接收关键事件。 PyAudio 的文档为异步输出提供了一个很好的示例,因此我只是将其应用于输入。稍微重新安排一下,还有一个标志让我们的听众知道我们稍后录制的时间,我们有一个录音机类:

class recorder:
def __init__(self,
wavfile,
chunksize=8192,
dataformat=pyaudio.paInt16,
channels=2,
rate=44100):
self.filename = wavfile
self.chunksize = chunksize
self.dataformat = dataformat
self.channels = channels
self.rate = rate
self.recording = False
self.pa = pyaudio.PyAudio()

def start(self):
#we call start and stop from the keyboard listener, so we use the asynchronous
# version of pyaudio streaming. The keyboard listener must regain control to
# begin listening again for the key release.
if not self.recording:
self.wf = wave.open(self.filename, 'wb')
self.wf.setnchannels(self.channels)
self.wf.setsampwidth(self.pa.get_sample_size(self.dataformat))
self.wf.setframerate(self.rate)

def callback(in_data, frame_count, time_info, status):
#file write should be able to keep up with audio data stream (about 1378 Kbps)
self.wf.writeframes(in_data)
return (in_data, pyaudio.paContinue)

self.stream = self.pa.open(format = self.dataformat,
channels = self.channels,
rate = self.rate,
input = True,
stream_callback = callback)
self.stream.start_stream()
self.recording = True
print('recording started')

def stop(self):
if self.recording:
self.stream.stop_stream()
self.stream.close()
self.wf.close()

self.recording = False
print('recording finished')

最后,我们创建一个音频播放器,当您按 p 时播放音频。 .我将 PyAudio 示例放入一个线程中,该线程在您每次按下按钮时都会创建,以便可以创建多个彼此重叠的播放器。我们还会跟踪有多少玩家在玩,所以我们不会在文件已被玩家使用时尝试录制。 (我还在顶部包含了我的导入)

from threading import Thread, Lock
from pynput import keyboard
import pyaudio
import wave

class player:
def __init__(self, wavfile):
self.wavfile = wavfile
self.playing = 0 #flag so we don't try to record while the wav file is in use
self.lock = Lock() #muutex so incrementing and decrementing self.playing is safe

#contents of the run function are processed in another thread so we use the blocking
# version of pyaudio play file example: http://people.csail.mit.edu/hubert/pyaudio/#play-wave-example
def run(self):
with self.lock:
self.playing += 1
with wave.open(self.wavfile, 'rb') as wf:
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)
data = wf.readframes(8192)
while data != b'':
stream.write(data)
data = wf.readframes(8192)

stream.stop_stream()
stream.close()
p.terminate()
wf.close()
with self.lock:
self.playing -= 1

def start(self):
Thread(target=self.run).start()

我不能保证它完全没有错误,但如果您对它的工作原理/如何让它工作有任何疑问,请随时发表评论。

关于python - 每次用户按键时如何录制音频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62520952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com