在Java中生成噪声颜色
我想创建一个使用Java的彩色噪声生成器,它将能够生成本文中定义的所有颜色:http://en.wikipedia.org/wiki/Colors_of_noise
我对如何产生噪声本身感到困惑,并且对于一旦产生了声音就可以通过扬声器输出来感到困惑。
任何链接或提示将非常感激!
我还看了另一个问题:Java生成声音
但我不完全理解其中一条评论中给出的代码。 它也不会告诉我该代码会产生什么噪音,所以我不知道如何修改它以便产生白噪声。
这是一个在纯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;
        }
    }
}
实际上,我正在研究一个白噪声和抽样产生随机数的项目。 你需要的是相反的!
声音是压力与时间的关系。 基本上从0压力开始,并从 - (最大振幅)到(最大振幅)增加一个随机数量的压力。 白噪声的幅度是随机的并且是正态分布的,因此您可以使用Random.nextGaussian()来生成随机z分数。 将z分数乘以标准偏差(您可能需要做一些测试以找到您喜欢的幅度的标准偏差),然后将其作为音频文件中每个样本的幅度。
至于生成声音文件本身,如果你还没有,你应该看看Java Sound API。 它为创建声音文件和播放提供了许多不错的方法。
你的问题的下一部分,非白色噪声,恐怕我不确定波形是什么样子。 它可能遵循类似的生成随机z分数,并将它们乘以某个幅度标准偏差(或者更可能由某个随时间变化的幅度函数)。
链接地址: http://www.djcxy.com/p/82441.html上一篇: Generating colors of noise in Java
下一篇: How can I rethrow an exception in Javascript, but preserve the stack?
