gpt4 book ai didi

python - IOError 输入溢出 : Record audio with Tkinter interface

转载 作者:太空狗 更新时间:2023-10-30 01:32:24 28 4
gpt4 key购买 nike

我正在尝试做一个只有一个按钮的简单 GUI 应用程序:记录

您按下按钮,录音开始。当您松开按钮时,录音将停止并保存录音。

但是,当我点击按钮时出现以下错误:

Traceback (most recent call last):
...
data = self.stream.read(self.CHUNK)
File (...), line 608, in read
return pa.read_stream(self._stream, num_frames, exception_on_overflow)
IOError: [Errno -9981] Input overflowed
Exception in Tkinter callback

但是我没有在没有按钮和 Tkinter 的情况下录制简单音频的问题(他们提供的代码示例 here)。

这是代码:

import Tkinter as tk
import pyaudio, wave

class AppRecording:
def __init__(self, root):
self.root = root
self.mouse_pressed = False
recordingButton = tk.Button(root, text = "Record")
recordingButton.pack()
recordingButton.bind("<ButtonPress-1>", self.OnMouseDown)
recordingButton.bind("<ButtonRelease-1>", self.OnMouseUp)
self.CHUNK = 1024
self.FORMAT = pyaudio.paInt16
self.CHANNELS = 2
self.RATE = 44100
self.WAVE_OUTPUT_FILENAME = "output.wav"

self.p = pyaudio.PyAudio()

try: self.stream = self.p.open(format=self.FORMAT,
channels=self.CHANNELS,
rate=self.RATE,
input=True,
frames_per_buffer=self.CHUNK)
except:
raise Exception("There is no connected microphone. Check that you connect to the left hole if you have a PC.")
return None

self.frames = []

def recordFrame(self):
try:
data = self.stream.read(self.CHUNK)
print "after try"
except IOError as ex:
print "inside except"
if ex[1] != pyaudio.paInputOverflowed:
print "before raise"
raise
print "after raise"

data = '\x00' * self.CHUNK # or however you choose to handle it, e.g. return None

self.frames.append(data)

def finishRecording(self):

self.stream.stop_stream()
self.stream.close()
self.p.terminate()

wf = wave.open(self.WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(self.CHANNELS)
wf.setsampwidth(self.p.get_sample_size(self.FORMAT))
wf.setframerate(self.RATE)
wf.writeframes(b''.join(self.frames))
wf.close()

def OnMouseDown(self, event):
self.mouse_pressed = True
self.poll()

def OnMouseUp(self, event):
self.root.after_cancel(self.after_id)
print "Finished recording!"
self.finishRecording()

def poll(self):
if self.mouse_pressed:
self.recordFrame()
self.after_id = self.root.after(1, self.poll)

root=tk.Tk()
app = AppRecording(root)
root.mainloop()

我试图改变 self.CHUNKself.RATE。我的 iMac 的内部麦克风显示速率为 44100。在某些地方我读到我应该更改 block 或速率值,两者都尝试过但没有人帮助。另一个地方告诉我添加 except IOError as ex: (...)


PyAudio 版本:0.2.10

pyaudio.get_portaudio_version():1246720

pyaudio.get_portaudio_version_text():PortAudio V19.6.0-devel,修订版 396fe4b6699ae929d3a685b3ef8a7e97396139a4

Tkinter.__version__:$Revision:81008 $


非常感谢您的帮助,谢谢!

最佳答案

哪个 python/tk/portaudio/pyaudio 版本?

我确认您的代码在 Ubuntu 14.04 LTS x64(带有 portaudio19-dev 和 PyAudio-0.2.10 的 Python 2.7)下是好的(没有问题)所以我认为这个问题可能与您的 python、tk、pyaudio 或 portaudio 版本有关...

您确定您的计算机上安装了最新的 portaudio & tk 版本吗?

关于python - IOError 输入溢出 : Record audio with Tkinter interface,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41904501/

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