简体   繁体   English

在Raspberry Pi上使用PyAudio抑制音频

[英]Stuttering audio with PyAudio on Raspberry Pi

I'm trying to make a simplex communication (that will become half-duplex as soon as I solve this issue) between two Raspberry Pi. 我正在尝试在两个Raspberry Pi之间进行单工通信(一旦解决此问题,它将变为半双工)。

One sends audio packets through UDP protocol and the other receives this packets and plays them through it's default audio output device. 一个通过UDP协议发送音频数据包,另一个接收此数据包并通过其默认音频输出设备播放它们。

I'm using the PyAudio library. 我正在使用PyAudio库。 I managed to make the communication work, but the sound that comes out is stuttered. 我设法使通讯正常进行,但是发出的声音却卡住了。 The code I used is the following: 我使用的代码如下:

Client (IP address: 192.168.1.40) 客户端(IP地址:192.168.1.40)

#!/usr/bin/env python

import pyaudio
import wave
import socket

p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16,
            channels=1,
            input_device_index=2,
            rate=44100,
            input=True,
            frames_per_buffer=8192)

print("Stream on.")
while True:
try:
    data = stream.read(8192)
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.connect(("192.168.1.30", 1000))
    sock.send(data)
    sock.close()

except KeyboardInterrupt:   
    print("Stream off.")
    stream.stop_stream()
    stream.close()
    p.terminate()

Server (IP address: 192.168.1.30) 服务器(IP地址:192.168.1.30)

#!/usr/bin/env python

import pyaudio
import wave
import socket

p = pyaudio.PyAudio()

stream = p.open(format=pyaudio.paInt16,
            channels=1,
            input_device_index=2,
            rate=44100,
            output=True,
            frames_per_buffer=8192)

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 
s.bind(('0.0.0.0', 1000)) 

while True:
try:
    data = s.recv(8192)
    if data:
        stream.write(data)

except KeyboardInterrupt:
    s.close()
    print "Exiting code."
    stream.stop_stream()
    stream.close()
    p.terminate()

Any help will be appreciated! 任何帮助将不胜感激!

I too faced similar issues of stuttering. 我也面临着类似的口吃问题。 This was because of the input overflowed error which came up during the streaming. 这是因为流期间出现输入溢出错误。 If the chunk size of every communication is reduced to 512bytes the communication is coming out well as per my experience. 如果每次通信的块大小都减小到512字节,那么按照我的经验,通信就会顺利进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM