gpt4 book ai didi

python - 如何记录特定时间的音频,直到在Python sounddevice中进行某些操作(如按下键盘)为止?

转载 作者:行者123 更新时间:2023-12-02 23:27:24 25 4
gpt4 key购买 nike

我是Python的新手,所以我有一个小的projet可以用来了解有关Python的更多信息,我的项目很简单,仅使用sounddevice记录音频,但问题是,在记录片中,它仅记录是否有静态秒,例如代码示例代码在下面,我不知道如何停止或暂停录制,在我的情况下是按一个键,例如“输入停止,切换到暂停”

import sounddevice as sd
import soundfile as sf
import time
def sync_record(filename, duration, fs, channels):
print('recording')
myrecording = sd.rec(int(duration * fs), samplerate=fs, channels=channels)
sd.wait()
sf.write(filename, myrecording, fs)
print('done recording')
# playback file
sync_record('sync_record.wav', 10, 16000, 1)

所以我的问题是,我该如何在不需要持续时间的情况下暂停和录制音频,以及如何使用 sounddevice停止和暂停音频,非常感谢大家

最佳答案

print "* record until keypress"
from openexp.keyboard import keyboard
# Short timeout so get_key() doesn't block
my_keyboard = keyboard(exp, timeout=2)
all = []
while my_keyboard.get_key()[0] == None: # Loop until a key has been pressed
data = stream.read(chunk) # Record data
all.append(data) # Add the data to a buffer (a list of chunks)
print "* done recording"

这说明了如何在按键之前流数据。
import keyboard
k = keyboard.read_key() # in my python interpreter, this captures "enter up"
k = keyboard.read_key() # so repeat the line if in the python interpreter

对您的代码使用相同的逻辑。因此,一旦用户按下一个键,录制就会停止。

关于python - 如何记录特定时间的音频,直到在Python sounddevice中进行某些操作(如按下键盘)为止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60709944/

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