這是一個在純Java中產生白噪聲的程序。它可以很容易地改變,以產生其他顏色的噪音。
import javax.sound.sampled.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.nio.ByteBuffer;
import java.util.Random;
public class WhiteNoise extends JFrame {
private GeneratorThread generatorThread;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
WhiteNoise frame = new WhiteNoise();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public WhiteNoise() {
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
generatorThread.exit();
System.exit(0);
}
});
setTitle("White Noise Generator");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 200, 50);
setLocationRelativeTo(null);
getContentPane().setLayout(new BorderLayout(0, 0));
generatorThread = new GeneratorThread();
generatorThread.start();
}
class GeneratorThread extends Thread {
final static public int SAMPLE_SIZE = 2;
final static public int PACKET_SIZE = 5000;
SourceDataLine line;
public boolean exitExecution = false;
public void run() {
try {
AudioFormat format = new AudioFormat(44100, 16, 1, true, true);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format, PACKET_SIZE * 2);
if (!AudioSystem.isLineSupported(info)) {
throw new LineUnavailableException();
}
line = (SourceDataLine)AudioSystem.getLine(info);
line.open(format);
line.start();
} catch (LineUnavailableException e) {
e.printStackTrace();
System.exit(-1);
}
ByteBuffer buffer = ByteBuffer.allocate(PACKET_SIZE);
Random random = new Random();
while (exitExecution == false) {
buffer.clear();
for (int i=0; i < PACKET_SIZE /SAMPLE_SIZE; i++) {
buffer.putShort((short) (random.nextGaussian() * Short.MAX_VALUE));
}
line.write(buffer.array(), 0, buffer.position());
}
line.drain();
line.close();
}
public void exit() {
exitExecution =true;
}
}
}
您鏈接的問題涉及生成「嗶嗶聲」並播放它們。 「嘟嘟」只是你的簡單週期波,其頻率與你所演奏的波的頻率成正比(在這個例子中,正弦波)。 –