IOError input overflow: sound recording using Tkinter interface

I am trying to make a simple graphical application that has only one button: Record.

You press the button and recording starts. When you release the button, the recording stops 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 have no problem recording simple sound without a button and Tkinter (they give sample code 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 self.CHUNKand self.RATE. The internal microphone of my iMac says that the speed is 44100. In some places I read that I need to change the piece or the cost of the course, I tried both, but no one helped. Elsewhere I was told to addexcept IOError as ex: (...)


PyAudio Version: 0.2.10

pyaudio.get_portaudio_version(): 1246720

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

Tkinter.__version__: $: 81008 $


, !

+6
1

python/tk/portaudio/pyaudio?

, ( ) Ubuntu 14.04 LTS x64 (Python 2.7 portaudio19-dev PyAudio-0.2.10), , python, tk, pyaudio portaudio...

, ​​ portaudio tk?

+1

Source: https://habr.com/ru/post/1014505/


All Articles