Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 PyAudio recorder脚本IOError:[Errno输入溢出]-9981_Python 2.7_Exception_Io_Audio Recording_Pyaudio - Fatal编程技术网

Python 2.7 PyAudio recorder脚本IOError:[Errno输入溢出]-9981

Python 2.7 PyAudio recorder脚本IOError:[Errno输入溢出]-9981,python-2.7,exception,io,audio-recording,pyaudio,Python 2.7,Exception,Io,Audio Recording,Pyaudio,下面的代码是我用来录制音频的,直到按下“回车”键,它返回一个异常 import pyaudio import wave import curses from time import gmtime, strftime import sys, select, os # Name of sub-directory where WAVE files are placed current_experiment_path = os.path.dirname(os.path.realpath(__file_

下面的代码是我用来录制音频的,直到按下“回车”键,它返回一个异常

import pyaudio
import wave
import curses
from time import gmtime, strftime
import sys, select, os

# Name of sub-directory where WAVE files are placed
current_experiment_path = os.path.dirname(os.path.realpath(__file__))
subdir_recording = '/recording/'

# print current_experiment_path + subdir_recording

# Variables for Pyaudio
chunk = 1024
format = pyaudio.paInt16
channels = 2
rate = 48000

# Set variable for the labelling of the recorded WAVE file.
timestamp = strftime("%Y-%m-%d-%H:%M:%S", gmtime())
#wave_output_filename = '%s.wav' % self.get('subject_nr')
wave_output_filename = '%s.wav' % timestamp

print current_experiment_path + subdir_recording + wave_output_filename

# pyaudio recording stuff
p = pyaudio.PyAudio()

stream = p.open(format = format,
                channels = channels,
                rate = rate,
                input = True,
                frames_per_buffer = chunk)

print "* recording"

# Create an empty list for audio recording
frames = []

# Record audio until Enter is pressed
i = 0
while True:
    os.system('cls' if os.name == 'nt' else 'clear')
    print "Recording Audio. Press Enter to stop recording and save file in " + wave_output_filename
    print i
    if sys.stdin in select.select([sys.stdin], [], [], 0)[0]:
        line = raw_input()
        # Record data audio data
        data = stream.read(chunk)
        # Add the data to a buffer (a list of chunks)
        frames.append(data)
        break
    i += 1

print("* done recording")

# Close the audio recording stream
stream.stop_stream()
stream.close()
p.terminate()

# write data to WAVE file
wf = wave.open(current_experiment_path + subdir_recording + wave_output_filename, 'wb')
wf.setnchannels(channels)
wf.setsampwidth(p.get_sample_size(format))
wf.setframerate(rate)
wf.writeframes(''.join(frames))
wf.close()
产生的例外是

Recording Audio. Press Enter to stop recording and save file in 2015-11-20-22:15:38.wav
925
Traceback (most recent call last):
  File "audio-record-timestamp.py", line 51, in <module>
    data = stream.read(chunk)
  File "/Library/Python/2.7/site-packages/pyaudio.py", line 605, in read
    return pa.read_stream(self._stream, num_frames)
IOError: [Errno Input overflowed] -9981
录制音频。按Enter键停止录制并将文件保存在2015-11-20-22:15:38.wav中
925
回溯(最近一次呼叫最后一次):
文件“audio record timestamp.py”,第51行,在
data=stream.read(块)
文件“/Library/Python/2.7/site packages/pyaudio.py”,第605行,已读
返回pa.read\u流(self.\u流,num\u帧)
IOError:[Errno输入溢出]-9981

产生异常的原因是什么?我尝试更改块大小(5122568192),但它不起作用。更改了while循环条件,但它不起作用。

我遇到了类似的问题;有三种解决方法(我可以找到)

  • 设定费率=24000
  • 在“read()”调用中添加选项“exception\u on\u overflow=False”,即使其成为“stream.read(chunk,exception\u on\u overflow=False)”
  • 使用回调
  • 为了方便起见,这里有一个“使用回调”的示例

    #!/usr/bin/python
    
    import sys, os, math, time,  pyaudio
    
    try:
        import numpy
    except:
        numpy = None
    
    rate=48000
    chan=2
    
    sofar=0
    
    p = pyaudio.PyAudio()
    
    def callback(in_data, frame_count, time_info, status):
        global sofar
        sofar += len(in_data)
        if numpy:
            f = numpy.fromstring(in_data, dtype=numpy.int16)
            sys.stderr.write('length %6d sofar %6d std %4.1f  \r' % \
                             (len(in_data), sofar, numpy.std(f)))
        else:
            sys.stderr.write('length %6d sofar %6d  \r' % \
                             (len(in_data), sofar))
        data = None
        return (data, pyaudio.paContinue)
    
    stream = p.open(format=pyaudio.paInt16, channels=chan, rate=rate,
                    input=True, stream_callback=callback)
    
    while True:
        time.sleep(1)