gpt4 book ai didi

java UDP 声音流 : why do I have interferences?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:16:31 25 4
gpt4 key购买 nike

我正在尝试构建一个非常简单的带有源和接收器的音频流。但是当我在“接收器”中接收到声音时,我会受到一些干扰。我正在使用 UDP 协议(protocol)。有没有办法“改进”我的代码以避免这些干扰?

这是我的音频服务器:

import java.io.File;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;

public class AudioPlayerServer implements Runnable {

private SourceDataLine sLine;
private AudioFormat audioFormat;
private AudioInputStream audioInputStream=null;
private String host="127.0.0.1";
private int port=8000;
private DatagramSocket server;
private DatagramPacket packet;
private long startTime;
private long endTime=System.nanoTime();;
private long elapsed=System.nanoTime();;
private double sleepTime;
private long sleepTimeMillis;
private int sleepTimeNanos, epsilon;

AudioPlayerServer(String host, int port) {
this.host=host;
this.port=port;
init();
}

public void init() {
File file = new File("test.wav");
try {
audioInputStream=AudioSystem.getAudioInputStream(file);

} catch (Exception e) {
e.printStackTrace();
}

audioFormat = new AudioFormat(44100, 16, 2, true, false);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
System.out.println(info);

try {
server = new DatagramSocket();
System.out.println("Server started");

} catch (SocketException e) {
e.printStackTrace();
}
}

public void run() {
try {
byte bytes[] = new byte[4096];
byte bytes2[] = new byte[1024];
int bytesRead=0;
//The sending rythm of the data have to be compatible with an audio streaming.
//So, I'll sleep the streaming thread for (1/SampleRate) seconds * (bytes.lenght/4) - epsilon
//=> bytes.lenght/4 because 4 values = 1 frame => For ex, in 1024 bits, there are 1024/4 = 256 frames
//epsilon because the instructions themselves takes time.
//The value have to be convert in milliseconds et nanoseconds.
sleepTime=(1024/audioFormat.getSampleRate());
epsilon=400000;
sleepTimeMillis=(long)(sleepTime*1000);
sleepTimeNanos=(int)((sleepTime*1000-sleepTimeMillis)*1000000);
System.out.println("Sleep time :"+sleepTimeMillis+" ms, "+sleepTimeNanos+" ns");

while ((bytesRead=audioInputStream.read(bytes, 0, bytes.length))!= -1) {
//getSignalLevel(bytes);

try {
//startTime=System.nanoTime();
packet = new DatagramPacket(bytes, bytes.length, InetAddress.getByName(host), port);
packet.setData(bytes);
server.send(packet);
packet.setLength(bytes.length);
//endTime=System.nanoTime();
//System.out.println(endTime-startTime);
Thread.sleep(sleepTimeMillis,sleepTimeNanos);
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("No bytes anymore !");
} catch (Exception e) {
e.printStackTrace();
}
sLine.close();
System.out.println("Line closed");

}

}

这是客户端:

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;

public class AudioReceiver implements Runnable{
private String host;
private int port;
private SourceDataLine sLine;
private AudioFormat audioFormat;
byte[] buffer=new byte[4096];
DatagramPacket packet;

AudioReceiver (String host, int port) {
this.host=host;
this.port=port;
init();
Thread t1=new Thread(new Reader());
t1.start();
}

public void init() {
audioFormat = new AudioFormat(44100, 16, 2, true, false);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);

try {
System.out.println(info);
sLine=(SourceDataLine) AudioSystem.getLine(info);
System.out.println(sLine.getLineInfo() + " - sample rate : "+audioFormat.getSampleRate());
} catch (Exception e) {
e.printStackTrace();
}
}

public void run() {
System.out.println("Client started");
try {
sLine.open(audioFormat);
} catch (Exception e){
e.printStackTrace();
}
sLine.start();
System.out.println("Line started");

try {

DatagramSocket client = new DatagramSocket(port, InetAddress.getByName(host));
while (true) {
try {
packet = new DatagramPacket(buffer, buffer.length);
//System.out.println("Reception beggins for host "+host+" : "+port);
client.receive(packet);
//System.out.println("Reception ends");
buffer=packet.getData();

//sLine.write(packet.getData(), 0, buffer.length);
packet.setLength(buffer.length);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e1) {
e1.printStackTrace();
}

}

public class Reader implements Runnable {
public void run() {
while (true) {
if (packet!=null) {
sLine.write(packet.getData(), 0, buffer.length);
}
}
}
}
}

最佳答案

创建UDP流媒体系统时,经常会用到RTP协议(protocol)。 RTP 使用 UDP,它是一种无连接的不可靠协议(protocol)。在传输层 (UDP),您需要处理丢失和无序到达。此外,网络层是突发的,数据不会以均匀的速率到达。相反,数据包将以不一致的到达间隔率到达。因此,您必须在本地缓冲数据以应对这种网络抖动。
This post answers and explains关于java、UDP、RTP、网络抖动、缓冲、丢包。处理损失也有不同的策略。您可以用沉默填充它或估计丢失的数据。此外,您的客户端播放样本的速度可能比您的服务器快,并最终耗尽数据。这是由于没有公共(public)总线的两个系统之间时钟晶体的变化。 This post answers and explains处理数据包丢失和时钟漂移。

关于java UDP 声音流 : why do I have interferences?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34582301/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com