크 랜 베 리 파이 3B + 가이드 (13) pyaudio 녹음 및 재생

pyaudio 녹음 및 재생
인터넷 에는 많은 범례 가 있 지만 wav 파일 을 재생 할 때 순환 에 걸 려 서 나 오지 못 하고 마지막 으로 공식 문서 에서 정확 한 사용 방법 을 찾 았 습 니 다.공식 문서:http://people.csail.mit.edu/hubert/pyaudio/docs/#indices-and-tables
다음은 인터넷 의 범례 와 공식 문 서 를 결합 하여 크 랜 베 리 파 에서 사용 할 수 있 는 방법 이다.
import wave
from pyaudio import PyAudio,paInt16

framerate=16000
NUM_SAMPLES=2000
chunk=2014
channels=1
sampwidth=2
TIME=20 #ms

#  
def save_wave_file(filename,data):

    '''save the date to the wavfile'''
    wf=wave.open(filename,'wb')
    wf.setnchannels(channels)
    wf.setsampwidth(sampwidth)
    wf.setframerate(framerate)
    wf.writeframes(b"".join(data))
    wf.close()

#  
def my_record():

    pa=PyAudio()
    stream=pa.open(format = paInt16,channels=1,
                   rate=framerate,input=True,
                   frames_per_buffer=NUM_SAMPLES)
    my_buf=[]
    count=0
    
    while count 0:
	    stream.write(data)
	    data = wf.readframes(CHUNK)
	    
	stream.stop_stream()
    stream.close()
    
    p.terminate()

if __name__ == '__main__':
    my_record()
    print('Over!') 
    play()

여러 프로그램 에서 pyaudio 를 사용 하면 오류 가 발생 할 수 있 습 니 다.
IOError: [Errno -9985] Device unavailable

또는 다른 장치 에서 사용 할 수 없 는 오류 와 유사 하 므 로 다음 코드 를 시도 해 보 세 요.
sudo apt-get install pulseaudio
pulseaudio --start

이것으로 끝 났 습 니 다. 여러분 을 도 울 수 있 기 를 바 랍 니 다!

좋은 웹페이지 즐겨찾기