2

我对 Python 很陌生,所以我有一个小项目可以用来了解更多关于 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,非常感谢你们

4

1 回答 1

0
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

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

于 2020-03-16T16:54:14.620 回答