Weird voice streaming and receiving audio through UDP


Weird voice streaming and receiving audio through UDP



I was implementing two programs in Java, one sends through UDP the output audio and another one receives it and play it.



While executing both programs (they communicate via loopback address), the only thing you can sometimes (every 10 seconds) hear is a voice (apparently of a woman) saying "ciao".



The first time I heard it I was quite scared but now I am really curios to understand when this audio bytes come from.


public static void main(String args) throws Exception {
System.out.println("connect to " + Inet4Address.getLocalHost().toString() + " on port " + port);
DatagramSocket serverSocket = new DatagramSocket(port);
byte receiveData = new byte[1280]; //1280!!!!!!
// ( 1280 for 16 000Hz and 3584 for 44 100Hz (use AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat) to get the correct size)
format = new AudioFormat(sampleRate, 16, 1, true, false);
while (status == true) {
DatagramPacket receivePacket = new DatagramPacket(receiveData,
receiveData.length);
serverSocket.receive(receivePacket);
ByteArrayInputStream baiss = new ByteArrayInputStream(
receivePacket.getData());
ais = new AudioInputStream(baiss, format, receivePacket.getLength());
// A thread solve the problem of chunky audio
new Thread(() -> {
toSpeaker(receivePacket.getData());
}).start();
}
}

public static void toSpeaker(byte soundbytes) {
try {

DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, format);
SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);

sourceDataLine.open(format);

FloatControl volumeControl = (FloatControl) sourceDataLine.getControl(FloatControl.Type.MASTER_GAIN);
volumeControl.setValue(6.0f);

sourceDataLine.start();
sourceDataLine.open(format);

sourceDataLine.start();

System.out.println("format? :" + sourceDataLine.getFormat());

sourceDataLine.write(soundbytes, 0, soundbytes.length);
// System.out.println(soundbytes.toString());
sourceDataLine.drain();
sourceDataLine.close();
} catch (Exception e) {
System.out.println("Not working in speakers...");
e.printStackTrace();
}
}


public static void main(String args) throws IOException {
try {
AudioFormat.Encoding encoding = AudioFormat.Encoding.PCM_SIGNED;
float rate = 16000.0f;
int channels = 1;
int sampleSize = 16;
boolean bigEndian = false;
InetAddress addr;

AudioFormat format = new AudioFormat(rate, sampleSize, channels, true, bigEndian);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);

if (!AudioSystem.isLineSupported(info)) {
System.out.println("Line matching " + info + " not supported.");
return;
}

tdl = (TargetDataLine) AudioSystem.getLine(info);
tdl.open(format);
tdl.start();

socket = new DatagramSocket();

buffer = new byte[tdl.getBufferSize() / 5];

socket = new DatagramSocket();
// hostAddress = InetAddress.getByName("192.168.43.1");
hostAddress = InetAddress.getByName("172.19.0.238");

} catch (SocketException ex) {
Logger.getLogger(AudioSender.class.getName()).log(Level.SEVERE, null, ex);
} catch (LineUnavailableException ex) {
Logger.getLogger(AudioSender.class.getName()).log(Level.SEVERE, null, ex);
}

(new Thread(new Runnable() {
@Override
public void run() {
try {

while (true) {
int s = tdl.read(buffer, 0, buffer.length);
DatagramPacket sendPacket
= new DatagramPacket(buffer, buffer.length, hostAddress, port);
socket.send(sendPacket);
System.out.println(">>>" + sendPacket.getData());
}
} catch (IOException ex) {
Logger.getLogger(AudioSender.class.getName()).log(Level.SEVERE, null, ex);
}

}
})).start();
}





1) For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example. 2) "a voice (apparently of a woman) saying "ciao"." ..have you consumed any mind altering substances recently?
– Andrew Thompson
Jul 1 at 1:43





No I don't use any substances except for coffeine (which could be already enough for allucination). I found the answer. Few months ago i have installer the driver "Virtual Audio Cable" which """apparently""" the FREE version says 'TRIAL' every 15 seconds to get you buy it. The problem is that i am Italian and i have the word 'ciao' inside my heart and second, i don't know english pronuntiation so for me was impossible to understand 'trial'. I am glad i found it out.
– alessandroAmedei
Jul 1 at 13:55





Ah .. the dreaded English. Glad you sorted that out.
– Andrew Thompson
Jul 2 at 2:36




1 Answer
1


DatagramPacket sendPacket
= new DatagramPacket(buffer, buffer.length, hostAddress, port);



The first problem is here. You're ignoring the length. It should be:


DatagramPacket sendPacket
= new DatagramPacket(buffer, s, hostAddress, port);



You're also ignoring end of stream. If s was -1 you should break out of the loop. Then:


s


ByteArrayInputStream baiss = new ByteArrayInputStream(
receivePacket.getData());



The second problem is here. You're ignoring the length. It should be:


ByteArrayInputStream baiss = new ByteArrayInputStream(
receivePacket.getData(), receivePacket.getOffset(), receivePacket.getLength());





Ok but how can i explay the weird voice?
– alessandroAmedei
Jul 1 at 10:36





And then it doesn't work the same..
– alessandroAmedei
Jul 1 at 11:20





The weird voice is because you're ignoring the length variable in all the three ways I specified in my answer, which means you're processing junk data in each datagram. When you fix these errors I should certainly hope it wouldn't work the same.
– EJP
Jul 1 at 17:14







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to input without newline? (Python)

C++ thread error: no type named ‘type’ MINGW

Analog for TagView in flutter