• <bdo id='Ugk95'></bdo><ul id='Ugk95'></ul>
      1. <tfoot id='Ugk95'></tfoot>
        <i id='Ugk95'><tr id='Ugk95'><dt id='Ugk95'><q id='Ugk95'><span id='Ugk95'><b id='Ugk95'><form id='Ugk95'><ins id='Ugk95'></ins><ul id='Ugk95'></ul><sub id='Ugk95'></sub></form><legend id='Ugk95'></legend><bdo id='Ugk95'><pre id='Ugk95'><center id='Ugk95'></center></pre></bdo></b><th id='Ugk95'></th></span></q></dt></tr></i><div id='Ugk95'><tfoot id='Ugk95'></tfoot><dl id='Ugk95'><fieldset id='Ugk95'></fieldset></dl></div>

      2. <small id='Ugk95'></small><noframes id='Ugk95'>

        <legend id='Ugk95'><style id='Ugk95'><dir id='Ugk95'><q id='Ugk95'></q></dir></style></legend>

        UDP声音传输:播放的声音有很大的噪音

        时间:2023-09-28

        <i id='pPw4t'><tr id='pPw4t'><dt id='pPw4t'><q id='pPw4t'><span id='pPw4t'><b id='pPw4t'><form id='pPw4t'><ins id='pPw4t'></ins><ul id='pPw4t'></ul><sub id='pPw4t'></sub></form><legend id='pPw4t'></legend><bdo id='pPw4t'><pre id='pPw4t'><center id='pPw4t'></center></pre></bdo></b><th id='pPw4t'></th></span></q></dt></tr></i><div id='pPw4t'><tfoot id='pPw4t'></tfoot><dl id='pPw4t'><fieldset id='pPw4t'></fieldset></dl></div>

          <legend id='pPw4t'><style id='pPw4t'><dir id='pPw4t'><q id='pPw4t'></q></dir></style></legend>
            <bdo id='pPw4t'></bdo><ul id='pPw4t'></ul>

          • <tfoot id='pPw4t'></tfoot>

            <small id='pPw4t'></small><noframes id='pPw4t'>

                  <tbody id='pPw4t'></tbody>
                  本文介绍了UDP声音传输:播放的声音有很大的噪音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我不知道如何解决这个问题.请帮帮我:)

                  I have no idea how to solve this problem. Please help me :)

                  我想将一台 PC 录制的声音数据发送到另一台 PC 并播放.(通过 UDP)

                  I would like to send sound data, recorded by one PC, to the other PC and play it. (by UDP)

                  程序可能正常运行,但声音包含(?)不舒服的噪音.当我试图记录 &在一个程序序列中播放声音,它工作正常.没有噪音.如果即使在一台 PC 上使用 UDP,使用 IP 127.0.0.1,就会出现噪音.起初,我认为这是因为播放的声音在另一台 PC 中出现了,我通过制作缓冲区来修复它.它解决了一点噪音,但几乎所有的噪音仍然存在.

                  The program might work correctly, but the sound contain(?) uncomfortable noise. when I tried to record & play sound in one program sequence, it worked correctly. There was no noise. In case of using UDP even in one PC, use IP 127.0.0.1, the noise appeared. At first, I thought the factor is because played sound is out in the other PC and I fixed it by making buffer. It solved little noise, but almost all the noise is still remaining.

                  就是下面的代码

                  客户

                  import pyaudio
                  import socket
                  from threading import Thread
                  
                  frames = []
                  
                  def udpStream():
                      udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)    
                  
                      while True:
                          if len(frames) > 0:
                              udp.sendto(frames.pop(0), ("127.0.0.1", 12345))
                  
                      udp.close()
                  
                  def record(stream, CHUNK):    
                      while True:
                          frames.append(stream.read(CHUNK))
                  
                  if __name__ == "__main__":
                      CHUNK = 1024
                      FORMAT = pyaudio.paInt16
                      CHANNELS = 2
                      RATE = 44100
                  
                      p = pyaudio.PyAudio()
                  
                      stream = p.open(format = FORMAT,
                                      channels = CHANNELS,
                                      rate = RATE,
                                      input = True,
                                      frames_per_buffer = CHUNK,
                                      )
                  
                      Tr = Thread(target = record, args = (stream, CHUNK,))
                      Ts = Thread(target = udpStream)
                      Tr.setDaemon(True)
                      Ts.setDaemon(True)
                      Tr.start()
                      Ts.start()
                      Tr.join()
                      Ts.join()
                  

                  服务器

                  import pyaudio
                  import socket
                  from threading import Thread
                  
                  frames = []
                  
                  def udpStream(CHUNK):
                  
                      udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
                      udp.bind(("127.0.0.1", 12345))
                  
                      while True:
                          soundData, addr = udp.recvfrom(CHUNK)
                          frames.append(soundData)
                  
                      udp.close()
                  
                  def play(stream, CHUNK):
                      BUFFER = 10
                      while True:
                              if len(frames) == BUFFER:
                                  while True:
                                      stream.write(frames.pop(0), CHUNK)
                  
                  if __name__ == "__main__":
                      FORMAT = pyaudio.paInt16
                      CHUNK = 1024
                      CHANNELS = 2
                      RATE = 44100
                  
                      p = pyaudio.PyAudio()
                  
                      stream = p.open(format=FORMAT,
                                      channels = CHANNELS,
                                      rate = RATE,
                                      output = True,
                                      frames_per_buffer = CHUNK,
                                      )
                  
                      Ts = Thread(target = udpStream, args=(CHUNK,))
                      Tp = Thread(target = play, args=(stream, CHUNK,))
                      Ts.setDaemon(True)
                      Tp.setDaemon(True)
                      Ts.start()
                      Tp.start()
                      Ts.join()
                      Tp.join()
                  

                  抱歉,源代码太长了.随意玩这个程序.

                  sorry for long source code. Feel free to play this program.

                  推荐答案

                  我已经搜索了这个噪音的原因.终于我知道为什么会这样了.

                  I have searched for the reason of this noise. Finally I could detect why this happened.

                  其实这个程序的UDP传输并没有造成丢包.

                  Actually, This program UDP transfer did not cause packet loss.

                  即使有,声音也没有那么大的噪音.

                  Even if it did, the sound do not have such a serious noise.


                  本程序正确发送数据,几乎没有丢包,但receive"方法无法正确接收数据.


                  This program sent data correctly, and there are almost no packet loss, but the "receive" method could not receive data correctly.


                  在服务器程序中


                  In server program

                  def udpStream(CHUNK):
                  
                      udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
                      udp.bind(("127.0.0.1", 12345))
                  
                      while True:
                          soundData, addr = udp.recvfrom(CHUNK)
                          frames.append(soundData)
                  
                      udp.close()
                  

                  这个程序只能提供25%"的数据.(我检查了数据量)

                  This program could data only "25%". (I checked the amount of data)

                  所以,我尝试接收数据乘法(CHANNELS * 2)

                  So, I tried to receive the data multiply (CHANNELS * 2)

                          soundData, addr = udp.recvfrom(CHUNK * CHANNELS * 2)
                  

                  这导致声音数据可以100%完全接收.

                  This results in the sound data can be received 100% completely.

                  最后,一台电脑录制的声音在另一台电脑上播放,没有噪音.

                  Finally, the sound recorded by one PC is played in the other PC without noise.

                  这篇关于UDP声音传输:播放的声音有很大的噪音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:看不到来自另一台设备的 UDP 多播消息 下一篇:通过 Socket 发送和接收数组

                  相关文章

                  <i id='H6aOy'><tr id='H6aOy'><dt id='H6aOy'><q id='H6aOy'><span id='H6aOy'><b id='H6aOy'><form id='H6aOy'><ins id='H6aOy'></ins><ul id='H6aOy'></ul><sub id='H6aOy'></sub></form><legend id='H6aOy'></legend><bdo id='H6aOy'><pre id='H6aOy'><center id='H6aOy'></center></pre></bdo></b><th id='H6aOy'></th></span></q></dt></tr></i><div id='H6aOy'><tfoot id='H6aOy'></tfoot><dl id='H6aOy'><fieldset id='H6aOy'></fieldset></dl></div>
                    <bdo id='H6aOy'></bdo><ul id='H6aOy'></ul>

                  <small id='H6aOy'></small><noframes id='H6aOy'>

                  1. <tfoot id='H6aOy'></tfoot>

                    1. <legend id='H6aOy'><style id='H6aOy'><dir id='H6aOy'><q id='H6aOy'></q></dir></style></legend>