import pyaudioimport waveimport cursesfrom time import gmtime,strftimeimport sys,select,os# name of sub-directory where WAVE files are placedcurrent_experiment_path = os.path.dirname(os.path.realpath(__file__))subdir_recording = '/recording/'# print current_experiment_path + subdir_recording# Variables for Pyaudiochunk = 1024format = pyaudio.paInt16channels = 2rate = 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' % timestampprint current_experiment_path + subdir_recording + wave_output_filename# pyaudio recording stuffp = 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 recordingframes = []# Record audio until Enter is pressedi = 0while 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 += 1print("* done recording")# Close the audio recording streamstream.stop_stream()stream.close()p.terminate()# write data to WAVE filewf = 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.wav925Traceback (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
什么是产生例外?我尝试更改块大小(512,256,8192)它不起作用.更改了while循环条件,但它不起作用.
解决方法 我有类似的问题;有3种方法可以解决它(我能找到)>设定率= 24000
>将选项“exception_on_overflow = False”添加到“read()”调用,即使其成为“stream.read(chunk,exception_on_overflow = False)”
>使用回调
为方便起见,这是“使用回调”的示例
#!/usr/bin/pythonimport sys,os,math,time,pyaudiotry: import numpyexcept: numpy = Nonerate=48000chan=2sofar=0p = 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)总结
以上是内存溢出为你收集整理的python-2.7 – PyAudio recorder脚本IOError:[Errno输入溢出] -9981全部内容,希望文章能够帮你解决python-2.7 – PyAudio recorder脚本IOError:[Errno输入溢出] -9981所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)