5

Right, well. First time I'm actually using Java to fix a problem. I bought a new headphone set called Sennheiser 120 HD; but there's an issue. If there isn't a constant emission of audio then the base for the headphones will eventually time out and turn off. The headphones are spammed with static, which is horrible on the ears. The solution to this for me currently is playing music 24/7 to prevent the static of death. Maybe I'm weird, but I don't want to listen to music 24/7.

I believe a workable solution for this would be to constantly emit a sound that the base can detect but I can't hear. The application would need to be efficient since it's running 24/7.

I've been doing some research, but I'm not that experienced with Java. I'm unable to find any library for emitting a certain frequency. Does anyone know of any?

It would be best to get the solution for this within 4 days, before my return policy at the store is no longer valid. Incase if this doesn't work.

Email from Sennheiser

4

1 回答 1

3

我想你会发现听一个恒定频率的声音会让耳朵很痛苦。但是,您可以这样做,只需使用标准 Java 库:

AudioFormat format = new AudioFormat(44000f, 16, 1, true, false);
SourceDataLine line = (SourceDataLine)AudioSystem.getLine(new DataLine.Info(SourceDataLine.class, format));

line.open(format);
line.start();

double f = 440; // Hz
double t = 3; // seconds

byte[] buffer = new byte[(int)(format.getSampleRate() * t * 2 + .5)];

f *= Math.PI / format.getSampleRate();

for(int i = 0; i < buffer.length; i += 2) {
    int value = (int)(32767 * Math.sin(i * f));
    buffer[i + 1] = (byte)((value >> 8) & 0xFF);
    buffer[i] = (byte)(value & 0xFF);
}

line.write(buffer, 0, buffer.length);

line.drain();
于 2012-07-04T15:13:41.057 回答