2013-10-14 20:08:15 +02:00
|
|
|
package mekanism.client.voice;
|
|
|
|
|
|
|
|
import javax.sound.sampled.AudioInputStream;
|
|
|
|
import javax.sound.sampled.AudioSystem;
|
|
|
|
import javax.sound.sampled.DataLine;
|
|
|
|
import javax.sound.sampled.TargetDataLine;
|
|
|
|
|
|
|
|
import mekanism.client.MekanismKeyHandler;
|
2013-10-14 20:25:04 +02:00
|
|
|
import cpw.mods.fml.relauncher.Side;
|
|
|
|
import cpw.mods.fml.relauncher.SideOnly;
|
2013-10-14 20:08:15 +02:00
|
|
|
|
2013-10-14 20:25:04 +02:00
|
|
|
@SideOnly(Side.CLIENT)
|
2013-10-14 20:08:15 +02:00
|
|
|
public class VoiceInput extends Thread
|
|
|
|
{
|
|
|
|
public VoiceClient voiceClient;
|
|
|
|
|
2013-10-14 20:25:04 +02:00
|
|
|
public DataLine.Info microphone;
|
2013-10-14 20:08:15 +02:00
|
|
|
|
|
|
|
public TargetDataLine targetLine;
|
|
|
|
|
|
|
|
public VoiceInput(VoiceClient client)
|
|
|
|
{
|
|
|
|
voiceClient = client;
|
2013-10-14 20:25:04 +02:00
|
|
|
microphone = new DataLine.Info(TargetDataLine.class, voiceClient.format, 2200);
|
2013-10-14 20:08:15 +02:00
|
|
|
|
|
|
|
setDaemon(true);
|
|
|
|
setName("VoiceServer Client Input Thread");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
targetLine = ((TargetDataLine)AudioSystem.getLine(microphone));
|
|
|
|
targetLine.open(voiceClient.format, 2200);
|
|
|
|
targetLine.start();
|
|
|
|
AudioInputStream audioInput = new AudioInputStream(targetLine);
|
|
|
|
|
|
|
|
boolean doFlush = false;
|
|
|
|
|
|
|
|
while(voiceClient.running)
|
|
|
|
{
|
2013-10-15 05:36:07 +02:00
|
|
|
if(MekanismKeyHandler.voice.isPressed())
|
2013-10-14 20:08:15 +02:00
|
|
|
{
|
|
|
|
targetLine.flush();
|
|
|
|
|
2013-10-15 05:36:07 +02:00
|
|
|
while(voiceClient.running && MekanismKeyHandler.voice.isPressed())
|
2013-10-14 20:08:15 +02:00
|
|
|
{
|
|
|
|
int availableBytes = audioInput.available();
|
|
|
|
byte[] audioData = new byte[availableBytes > 2200 ? 2200 : availableBytes];
|
|
|
|
int bytesRead = audioInput.read(audioData, 0, audioData.length);
|
|
|
|
|
|
|
|
if(bytesRead > 0)
|
|
|
|
{
|
|
|
|
voiceClient.output.writeShort(audioData.length);
|
|
|
|
voiceClient.output.write(audioData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
Thread.sleep(200L);
|
|
|
|
} catch(Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
doFlush = true;
|
|
|
|
}
|
|
|
|
else if(doFlush)
|
|
|
|
{
|
|
|
|
voiceClient.output.flush();
|
|
|
|
doFlush = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
Thread.sleep(20L);
|
|
|
|
} catch(Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
audioInput.close();
|
|
|
|
} catch(Exception e) {
|
2013-10-14 20:25:04 +02:00
|
|
|
System.err.println("[Mekanism] VoiceServer: Error while running client input thread.");
|
2013-10-14 20:08:15 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void close()
|
|
|
|
{
|
|
|
|
targetLine.flush();
|
|
|
|
targetLine.close();
|
|
|
|
}
|
|
|
|
}
|