Is there an library in Java for emitting a certain frequency constantly?

纵饮孤独 提交于 2019-12-22 07:28:11

问题


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.


回答1:


I think you'll find that listening to a constant-frequency sound is painful on the ears. However, you could do it something like this, just using standard Java libraries:

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();


来源:https://stackoverflow.com/questions/11331485/is-there-an-library-in-java-for-emitting-a-certain-frequency-constantly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!