Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOError Input overflowed: Record audio with Tkinter interface

I'm trying to do a simple GUI application that has only one button: Record.

You press the button and the recording begins. When you release the button the recording is stopped and the recording is saved.

However, I get the following error when I click the button:

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

However I do not have problems with recording a simple audio without the button and Tkinter (the code example they give here).

This is the code:

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()

I tried to change the self.CHUNK and self.RATE. The internal microphone of my iMac says that the rate is 44100. In some places I read that I should change the chunk or rate value, tried both but no one helped. Another place told me to add the except IOError as ex: (...)


PyAudio version: 0.2.10

pyaudio.get_portaudio_version(): 1246720

pyaudio.get_portaudio_version_text(): PortAudio V19.6.0-devel, revision 396fe4b6699ae929d3a685b3ef8a7e97396139a4

Tkinter.__version__: $Revision: 81008 $


I would appreciate your help, thanks!

like image 948
Pichi Wuana Avatar asked Nov 09 '22 02:11

Pichi Wuana


1 Answers

Which python/tk/portaudio/pyaudio version ?

I confirm that your code is good (no issue) under Ubuntu 14.04 LTS x64 (Python 2.7 with portaudio19-dev and PyAudio-0.2.10) so I assume that issue maybe relative to your python, tk, pyaudio or portaudio version...

Are you sure that you have the last portaudio & tk version installed on your computer ?

like image 148
A STEFANI Avatar answered Nov 14 '22 21:11

A STEFANI