'Generate a seed for random generator with audio in java? [closed]
I try to write my own random generator in java and it should be a true random generator. So my plan is to use SecureRandom which use a true random seed. This random seed is generated by the audio input from the pc in the following code:
private boolean stopped = false;
private AudioFormat format = new AudioFormat(48000.0f, 16, 2, true, true);
TargetDataLine line = null;
FloatControl control = null;
int numBytesRead;
byte[] data;
long seed;
public void listenAudio() {
new Thread(new Runnable() {
@Override
public void run() {
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
if (!AudioSystem.isLineSupported(info)) {
System.err.println("Line is not supported");
} else {
try {
line = (TargetDataLine) AudioSystem.getLine(info);
data = new byte[line.getBufferSize() / 5];
line.open(format);
line.start();
if(line.isOpen()) {
System.out.println("Line is open");
}
while(!stopped) {
numBytesRead = line.read(data, 0, data.length);
for(int i=0; i<data.length; i++) {
if(i%2 == 0)
seed -= Math.abs(data[i]);
else
seed += Math.abs(data[i]);
}
System.out.println(seed);
Thread.sleep(500);
}
} catch (LineUnavailableException e) {
System.err.println("LineUnavailableException"+e.getMessage());
} catch (InterruptedException e) {
System.err.println("InterruptedException"+e.getMessage());
} finally {
if(line.isRunning()) {
line.stop();
System.out.println("Line stoped");
}
if(line.isOpen()) {
line.close();
System.out.println("Line closed");
}
}
}
}
}).start();
}
Connected to the audio input of the pc is a audio mixer. Connected to the audio mixer:
- a piano
- old smartphone which plays random titles
- some microphones which are placed in the room
The pc system settings is line input as default and the format is as the format in the code
So my questions are:
- Cause I want to learn how to code, what do you think about this code?
- How to make this code more robust/ how to improve the code?
- Does this work to generate true randomness? I think it does, because the audio is random.
Edit: i tried the following in the code above:
if(line.isControlSupported(FloatControl.Type.VOLUME)){
FloatControl control = (FloatControl) line.getControl(FloatControl.Type.VOLUME);
System.out.println(control.getValue());
} else if(line.isControlSupported(FloatControl.Type.MASTER_GAIN)){
FloatControl control = (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN);
System.out.println(control.getValue());
}
it seems the line has no VOLUME or MASTER_GAIN value, because it prints no value. So i tried this:
for(Control c : line.getControls())
System.out.println("Control: "+c);
But this does not print anything. How can i access the available controls?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
