树莓派录制的音频文件上的音频卡顿问题

时间:2018-12-16 18:44:50

标签: python raspberry-pi pyaudio

我在Raspberry Pi 3B + Python 3 IDLE上使用了以下代码来录制声音文件,然后播放它,但是录制的音频文件断断续续* 。我认为代码没有问题。你们怎么看?你们当中有人在Raspberry Pi上遇到过这个问题吗?

import pyaudio
import wave
import sys

form_1 = pyaudio.paInt16 #16-bit resolution
chans = 1 #1 channel
samp_rate = 44100 #44.1kHz sampling rate
chunk = 4096 #2^12 samples for buffer
record_secs = 5 #seconds to record
dev_index = 2 #device index found by p.get_device_info_by_index(ii)
wav_output_filename = 'test3.wav' #name of .wav file

audio = pyaudio.PyAudio() #create pyaudio instantiation

#create pyaudio stream
stream = audio.open(format = form_1, rate = samp_rate, channels = chans, \
                input_device_index = dev_index, input = True, \
                frames_per_buffer=chunk)
print("recording")
frames = []

#loop through stream and append audio chunks to frame array
for ii in range(0,int((samp_rate/chunk)*record_secs)):
data = stream.read(chunk)
frames.append(data)

print("finished recording")

#stop the stream, close it, and terminate the pyaudio instantiation
stream.stop_stream()
stream.close()
audio.terminate()

#save the audio frames as .wav file
wavefile = wave.open(wav_output_filename,'wb')
wavefile.setnchannels(chans)
wavefile.setsampwidth(audio.get_sample_size(form_1))
wavefile.setframerate(samp_rate)
wavefile.writeframes(b' '.join(frames))
wavefile.close()

#play audio

sound = wave.open('test2.wav')
p = pyaudio.PyAudio()
chunk = 4096
stream = p.open(format =
            p.get_format_from_width(sound.getsampwidth()),
            channels = sound.getnchannels(),
            rate = sound.getframerate(),
            output = True)

data = sound.readframes(chunk)
while data != ' ':
    stream.write(data)
    data = sound.readframes(chunk)`

0 个答案:

没有答案