使用UDP与Python服务器从Android到PC的音频流噪声

时间:2015-11-29 03:58:01

标签: android python udp android-sensors pyaudio

我正在使用带有Android App作为客户端和Python服务器的UDP将我手机的麦克风输入数据传输到PC。它工作正常,没有错误。

但即使经过大量调整,我的服务器端也会产生很多噪音。 我想知道我的代码是否有问题或者它是否正常?

客户:

    public class MainActivity extends Activity {
    private Button startButton,stopButton;

    public byte[] buffer;
    public static DatagramSocket socket;
    private int port=8080;

    AudioRecord recorder;

    private int sampleRate = 44100 ; // 44100 for music
    private int channelConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO;
    private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
    int minBufSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
    private boolean status = true;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startButton = (Button) findViewById (R.id.start_button);
        stopButton = (Button) findViewById (R.id.stop_button);

        startButton.setOnClickListener (startListener);
        stopButton.setOnClickListener (stopListener);

    }

    private final OnClickListener stopListener = new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            status = false;
            recorder.release();
            Log.d("VS","Recorder released");
        }

    };

    private final OnClickListener startListener = new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            status = true;
            startStreaming();
        }

    };

    public void startStreaming() {


        Thread streamThread = new Thread(new Runnable() {

            @Override
            public void run() {
                try {

                    DatagramSocket socket = new DatagramSocket();
                    Log.d("VS", "Socket Created");

                    byte[] buffer = new byte[minBufSize];

                    Log.d("VS","Buffer created of size " + minBufSize);
                    DatagramPacket packet;


                    Log.d("VS", "Address retrieved");

                    final InetAddress destination = InetAddress.getByName("10.0.0.2");
                    Log.d("VS", "Address retrieved");


                    recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,sampleRate,channelConfig,audioFormat,minBufSize*10);
                    Log.d("VS", "Recorder initialized");

                    recorder.startRecording();


                    while(status == true) {


                        //reading data from MIC into buffer
                        minBufSize = recorder.read(buffer, 0, buffer.length);

                        //putting buffer in the packet
                        packet = new DatagramPacket (buffer,buffer.length,destination,port);

                        socket.send(packet);

                        System.out.println("MinBufferSize: " +minBufSize);


                    }


                } catch(UnknownHostException e) {
                    Log.e("VS", "UnknownHostException",e);


                } catch (IOException e) {
                    e.printStackTrace();
                    Log.e("VS", ""+ e);
                }
            }

        });
        streamThread.start();
    }
}

服务器:

import pyaudio
import socket
from threading import Thread
import numpy as np
from matplotlib import pyplot as plt

frames = []

def udpStream(CHUNK):

    udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    udp.bind(("0.0.0.0", 8080))

    while True:
       # soundData, addr = udp.recvfrom(CHUNK)
        soundData, addr = udp.recvfrom(CHUNK * CHANNELS * 2)
        frames.append(soundData)
        print numpydata
        plt.plot(numpydata)
        plt.show()

    udp.close()

def play(stream, CHUNK):
    BUFFER = 10
    while True:
            if len(frames) == BUFFER:
                while True:
                    try:
                        stream.write(frames.pop(0), CHUNK)
                    except: 
                        pass

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,
                    input=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()

2 个答案:

答案 0 :(得分:0)

您的代码不包含音频安全缓冲区来处理UDP网络传输速率中的抖动。 UDP over WiFi既不是媒体同步也不是可靠的传输,因此可能需要预先填充的安全缓冲区的一小部分,以及平滑处理丢失的内容。

答案 1 :(得分:0)

我能够通过简单得多的服务器设置使用您的Android应用代码,并且效果很好。

import pyaudio
import socket

FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
CHUNK = 4096

udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp.bind(("0.0.0.0", 5001))

audio = pyaudio.PyAudio()
stream = audio.open(format=FORMAT, channels=CHANNELS, rate=RATE, output=True, frames_per_buffer=CHUNK)

try:
    while True:
        data, addr = udp.recvfrom(CHUNK)
        stream.write(data)
except KeyboardInterrupt:
    pass

print('Shutting down')
udp.close()
stream.close()
audio.terminate()