5

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!

2
  • When run recording disable some element/function for re-recall. __init__ call device and another function call to. self.stream = None later(under a function) self.stream = self.p.open(format=self.FORMAT,.............., You got single pipe on every process. Commented Feb 1, 2017 at 8:09
  • @dsgdfg Can you explain better? I don't get how can I fix this. Commented Feb 2, 2017 at 6:54

1 Answer 1

1

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 ?

Sign up to request clarification or add additional context in comments.

5 Comments

portaudio version upgrade required (seen here: trac.macports.org/ticket/39150 => "I upgraded this morning my portaudio installation to the new version 19.20140130 and it fixed the issue on my system")
I edited my question adding the versions of everything you wondered. Moreover, I did update the portaudio to V19.6.0-devel and still the same problem.
@ASTEFANI which app manage your sound stream pipe ? On Ubuntu reqired proccess enumarator for creating a device stream. This question mean opening device twist or where my device id's ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.