IOError输入溢出:使用Tkinter接口录制音频

时间:2017-01-27 23:09:19

标签: python exception tkinter pyaudio ioerror

我正在尝试一个只有一个按钮的简单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.在某些地方,我读到我应该更改块或速率值,试过两个但没人帮忙。另一个地方告诉我要添加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 $

感谢您的帮助,谢谢!

1 个答案:

答案 0 :(得分:1)

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

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

你确定你有最后一个portaudio&amp;你的电脑上安装了tk版本吗?