我想使用 Java 创建一个彩色噪声发生器,它将能够生成本文中定义的所有颜色:http://en.wikipedia.org/wiki/Colors_of_noise
我既对如何产生噪音本身感到困惑,也对产生噪音后如何通过扬声器输出感到困惑。
任何链接或提示将不胜感激!
我还看过另一个问题:Java generating sound
但是我不完全理解其中一条评论中给出的代码中发生了什么。它也没有告诉我该代码会产生什么噪音,所以我不知道如何修改它以使其产生白噪音。
最佳答案
这是一个用纯 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;
}
}
}
关于java - 在 Java 中生成噪声颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26963342/
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
如何使用Ruby的默认Curses库获取颜色?所以像这样:puts"\e[0m\e[30;47mtest\e[0m"效果很好。在浅灰色背景上呈现漂亮的黑色。但是这个:#!/usr/bin/envrubyrequire'curses'Curses.noecho#donotshowtypedkeysCurses.init_screenCurses.stdscr.keypad(true)#enablearrowkeys(forpageup/down)Curses.stdscr.nodelay=1Curses.clearCurses.setpos(0,0)Curses.addstr"Hello
状态:我正在构建一个应用程序,其中需要一个可供用户选择颜色的字段,该字段将包含RGB颜色代码字符串。我已经测试了一个看起来很漂亮但效果不佳的。它是“挑剔的颜色”,并托管在此存储库中:https://github.com/Astorsoft/picky-color.在这里我打开一个关于它的一些问题的问题。问题:请建议我在Rails3应用程序中使用一些颜色选择器。 最佳答案 也许页面上的列表jQueryUIDevelopment:ColorPicker为您提供开箱即用的产品。原因是jQuery现在包含在Rails3应用程序中,因此使用基
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht
这篇文章是继上一篇文章“Observability:从零开始创建Java微服务并监控它(一)”的续篇。在上一篇文章中,我们讲述了如何创建一个Javaweb应用,并使用Filebeat来收集应用所生成的日志。在今天的文章中,我来详述如何收集应用的指标,使用APM来监控应用并监督web服务的在线情况。源码可以在地址 https://github.com/liu-xiao-guo/java_observability 进行下载。摄入指标指标被视为可以随时更改的时间点值。当前请求的数量可以改变任何毫秒。你可能有1000个请求的峰值,然后一切都回到一个请求。这也意味着这些指标可能不准确,你还想提取最小/
HashMap中为什么引入红黑树,而不是AVL树呢1.概述开始学习这个知识点之前我们需要知道,在JDK1.8以及之前,针对HashMap有什么不同。JDK1.7的时候,HashMap的底层实现是数组+链表JDK1.8的时候,HashMap的底层实现是数组+链表+红黑树我们要思考一个问题,为什么要从链表转为红黑树呢。首先先让我们了解下链表有什么不好???2.链表上述的截图其实就是链表的结构,我们来看下链表的增删改查的时间复杂度增:因为链表不是线性结构,所以每次添加的时候,只需要移动一个节点,所以可以理解为复杂度是N(1)删:算法时间复杂度跟增保持一致查:既然是非线性结构,所以查询某一个节点的时候