记录未知长度的数据时如何修复错误?
how to fix Error while recording data of unknown length?
我正在尝试用我的麦克风录制所需持续时间的音频。我想通过让用户 input.Based 决定我必须停止录制并在该持续时间内保存文件来控制文件的持续时间。
import pyaudio
import wave
from multiprocessing import Process
global flag
global frames
def try_recording(stream,RATE, CHUNK):
while flag != 1:
#for i in range(0,int(RATE/CHUNK*DURATION):
for i in range(0,int(RATE/CHUNK)):
data = stream.read(CHUNK)
frames.append(data)
def scan_input():
num = input('Enter input number: ')
if int(num) == 1:
flag = 1
else:
scan_input()
if __name__=='__main__':
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
CHUNK = 1024
WAVE_OUTPUT_FILENAME = "file.wav"
flag = 0
audio = pyaudio.PyAudio()
# start Recording
stream = audio.open(format=FORMAT, channels=CHANNELS,
rate=RATE, input=True,
frames_per_buffer=CHUNK)
print("recording...")
frames = []
#sys.setrecursionlimit(1500)
p1 = Process(target=try_recording, args = (stream,RATE,CHUNK))
p1.start()
p2 = Process(target=scan_input)
p2.start()
#frames = try_recording(stream, RATE, CHUNK)
print('finished recording')
# stop Recording
stream.stop_stream()
stream.close()
audio.terminate()
waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
waveFile.setnchannels(CHANNELS)
waveFile.setsampwidth(audio.get_sample_size(FORMAT))
waveFile.setframerate(RATE)
waveFile.writeframes(b''.join(frames))
waveFile.close()
我可以通过添加 sys.setrecursionlimit(1500)
来 record files when I define the time in seconds. Now I want to scan the input from user end when to stop the recording. So I modified the program by adding so that two functions run simultaneously. When I got a recursion error, I tried to resolve 它,但结果没有任何改变。
如何解决问题,以便我可以录制所需时长的音频。
更新:
错误和回溯
recording...
Traceback (most recent call last):
File "D:/Projects/Trails/try.py", line 14, in try_recording
try_recording(stream,RATE,CHUNK)
File "D:/Projects/Trails/try.py", line 14, in try_recording
try_recording(stream,RATE,CHUNK)
File "D:/Projects/Trails/try.py", line 14, in try_recording
try_recording(stream,RATE,CHUNK)
[Previous line repeated 995 more times]
File "D:/Projects/Trails/try.py", line 9, in try_recording
if flag == 1:
RecursionError: maximum recursion depth exceeded in comparison
如果我将限制增加到 44000 sys.setrecursionlimit(44000)
代码运行大约 1 秒并以错误结束
Process finished with exit code -1073741571 (0xC00000FD)
更新 2:
按照@quamrana 的建议更新了代码。
这两个进程没有同时工作。 Loop 不在 process 2 函数之外。使用 print 语句进行测试我知道,在我按下 1 之前,它只在进程 1 中循环,后来它移动到第二个进程,但无法扫描来自第一个函数的输入以结束记录。
更新 3:
更新流程参数
Traceback (most recent call last):
File "D:/Projects/Trails/try.py", line 42, in <module>
p1.start()
File "C:\Program Files\Python36\lib\multiprocessing\process.py", line 105, in start
self._popen = self._Popen(self)
File "C:\Program Files\Python36\lib\multiprocessing\context.py", line 223, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
File "C:\Program Files\Python36\lib\multiprocessing\context.py", line 322, in _Popen
return Popen(process_obj)
File "C:\Program Files\Python36\lib\multiprocessing\popen_spawn_win32.py", line 65, in __init__
reduction.dump(process_obj, to_child)
File "C:\Program Files\Python36\lib\multiprocessing\reduction.py", line 60, in dump
ForkingPickler(file, protocol).dump(obj)
TypeError: can't pickle _portaudio.Stream objects
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Program Files\Python36\lib\multiprocessing\spawn.py", line 105, in spawn_main
exitcode = _main(fd)
File "C:\Program Files\Python36\lib\multiprocessing\spawn.py", line 115, in _main
self = reduction.pickle.load(from_parent)
EOFError: Ran out of input
如何解决多处理 pickle 错误?
一旦您 try_recording
进入 else
,您希望如何更新 flag
。你在这里实现了任何 IPC 机制。导致它进入无限循环
我假设你实际上并不需要 try_recording
是递归的,所以它可以这样重写:
def try_recording(stream,RATE, CHUNK):
while flag != 1:
for i in range(0,int(RATE/CHUNK)):
data = stream.read(CHUNK)
frames.append(data)
此外,您没有正确启动流程。他们应该是:
p1 = Process(target=try_recording, args=(stream,RATE,CHUNK))
p2 = Process(target=scan_input)
你的标志应该是一个共享变量:
# flag = 0
flag = multiprocessing.Value('i', 0)
我正在尝试用我的麦克风录制所需持续时间的音频。我想通过让用户 input.Based 决定我必须停止录制并在该持续时间内保存文件来控制文件的持续时间。
import pyaudio
import wave
from multiprocessing import Process
global flag
global frames
def try_recording(stream,RATE, CHUNK):
while flag != 1:
#for i in range(0,int(RATE/CHUNK*DURATION):
for i in range(0,int(RATE/CHUNK)):
data = stream.read(CHUNK)
frames.append(data)
def scan_input():
num = input('Enter input number: ')
if int(num) == 1:
flag = 1
else:
scan_input()
if __name__=='__main__':
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
CHUNK = 1024
WAVE_OUTPUT_FILENAME = "file.wav"
flag = 0
audio = pyaudio.PyAudio()
# start Recording
stream = audio.open(format=FORMAT, channels=CHANNELS,
rate=RATE, input=True,
frames_per_buffer=CHUNK)
print("recording...")
frames = []
#sys.setrecursionlimit(1500)
p1 = Process(target=try_recording, args = (stream,RATE,CHUNK))
p1.start()
p2 = Process(target=scan_input)
p2.start()
#frames = try_recording(stream, RATE, CHUNK)
print('finished recording')
# stop Recording
stream.stop_stream()
stream.close()
audio.terminate()
waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
waveFile.setnchannels(CHANNELS)
waveFile.setsampwidth(audio.get_sample_size(FORMAT))
waveFile.setframerate(RATE)
waveFile.writeframes(b''.join(frames))
waveFile.close()
我可以通过添加 sys.setrecursionlimit(1500)
来 record files when I define the time in seconds. Now I want to scan the input from user end when to stop the recording. So I modified the program by adding
如何解决问题,以便我可以录制所需时长的音频。
更新: 错误和回溯
recording...
Traceback (most recent call last):
File "D:/Projects/Trails/try.py", line 14, in try_recording
try_recording(stream,RATE,CHUNK)
File "D:/Projects/Trails/try.py", line 14, in try_recording
try_recording(stream,RATE,CHUNK)
File "D:/Projects/Trails/try.py", line 14, in try_recording
try_recording(stream,RATE,CHUNK)
[Previous line repeated 995 more times]
File "D:/Projects/Trails/try.py", line 9, in try_recording
if flag == 1:
RecursionError: maximum recursion depth exceeded in comparison
如果我将限制增加到 44000 sys.setrecursionlimit(44000)
代码运行大约 1 秒并以错误结束
Process finished with exit code -1073741571 (0xC00000FD)
更新 2: 按照@quamrana 的建议更新了代码。
这两个进程没有同时工作。 Loop 不在 process 2 函数之外。使用 print 语句进行测试我知道,在我按下 1 之前,它只在进程 1 中循环,后来它移动到第二个进程,但无法扫描来自第一个函数的输入以结束记录。
更新 3: 更新流程参数
Traceback (most recent call last):
File "D:/Projects/Trails/try.py", line 42, in <module>
p1.start()
File "C:\Program Files\Python36\lib\multiprocessing\process.py", line 105, in start
self._popen = self._Popen(self)
File "C:\Program Files\Python36\lib\multiprocessing\context.py", line 223, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
File "C:\Program Files\Python36\lib\multiprocessing\context.py", line 322, in _Popen
return Popen(process_obj)
File "C:\Program Files\Python36\lib\multiprocessing\popen_spawn_win32.py", line 65, in __init__
reduction.dump(process_obj, to_child)
File "C:\Program Files\Python36\lib\multiprocessing\reduction.py", line 60, in dump
ForkingPickler(file, protocol).dump(obj)
TypeError: can't pickle _portaudio.Stream objects
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Program Files\Python36\lib\multiprocessing\spawn.py", line 105, in spawn_main
exitcode = _main(fd)
File "C:\Program Files\Python36\lib\multiprocessing\spawn.py", line 115, in _main
self = reduction.pickle.load(from_parent)
EOFError: Ran out of input
如何解决多处理 pickle 错误?
一旦您 try_recording
进入 else
,您希望如何更新 flag
。你在这里实现了任何 IPC 机制。导致它进入无限循环
我假设你实际上并不需要 try_recording
是递归的,所以它可以这样重写:
def try_recording(stream,RATE, CHUNK):
while flag != 1:
for i in range(0,int(RATE/CHUNK)):
data = stream.read(CHUNK)
frames.append(data)
此外,您没有正确启动流程。他们应该是:
p1 = Process(target=try_recording, args=(stream,RATE,CHUNK))
p2 = Process(target=scan_input)
你的标志应该是一个共享变量:
# flag = 0
flag = multiprocessing.Value('i', 0)