package voxo.client.threads;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.EnumSet;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;
import voxo.common.Verbose;
import voxo.common.enums.EnumVerbose;
public class VoiceReceiver implements Runnable {
public static Verbose verbose;
boolean go = true;
ServerSocket MyService;
Socket clientSocket = null;
BufferedInputStream input;
TargetDataLine targetDataLine;
AudioFormat audioFormat;
SourceDataLine sourceDataLine;
byte tempBuffer[] = new byte[10000];
// Determine audio format
private AudioFormat getAudioFormat() {
float sampleRate = 8000.0F;
int sampleSizeInBits = 16;
int channels = 1;
boolean signed = true;
boolean bigEndian = false;
return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian);
}
@Override
public void run() {
try {
audioFormat = getAudioFormat();
// Specify a type of dataline to get your hands on
DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat);
// Get the audio line
sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
// Make the line ready to receive a specific audio format
sourceDataLine.open(audioFormat);
// Start capturing the input
sourceDataLine.start();
// Prepare the socket to start receiving the data
MyService = new ServerSocket(5005);
clientSocket = MyService.accept();
input = new BufferedInputStream(clientSocket.getInputStream());
// Listen to your socket and shoot it through the AudioLine using a
// byte array
while (go && input != null && input.read(tempBuffer) != -1) {
sourceDataLine.write(tempBuffer, 0, 10000);
}
// Etre gentil sur la fermeture du thread
sourceDataLine.stop();
sourceDataLine.close();
try {
if (input != null)
input.close();
} catch (IOException e1) {
verbose.addConsoleMsg(e1, EnumSet.of(EnumVerbose.ToConsole, EnumVerbose.ToLog));
}
try {
if (clientSocket != null)
clientSocket.close();
} catch (IOException e) {
verbose.addConsoleMsg(e, EnumSet.of(EnumVerbose.ToConsole, EnumVerbose.ToLog));
}
try {
if (MyService != null)
MyService.close();
} catch (IOException e) {
verbose.addConsoleMsg(e, EnumSet.of(EnumVerbose.ToConsole, EnumVerbose.ToLog));
}
} catch (IOException | LineUnavailableException e) {
verbose.addConsoleMsg(e, EnumSet.of(EnumVerbose.ToConsole, EnumVerbose.ToLog));
killSocket();
}
}
public boolean isGo() {
return go;
}
public void setGo(boolean go) {
this.go = go;
}
public void killSocket() {
go = false;
}
}