Java Sound : generate play sine wave - source code
转载自:http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/java-sound-generate-play-sine-wave.html
Working source code example on how to generate and play sine wave in Java :
View my previous post for playing any PCM amplitude array.
Generate Sine wave of a particular frequency :
private static byte[] generateSineWavefreq(int frequencyOfSignal, int seconds) {
// total samples = (duration in second) * (samples per second)
byte[] sin = new byte[seconds * sampleRate];
double samplingInterval = (double) (sampleRate / frequencyOfSignal);
System.out.println("Sampling Frequency : "+sampleRate);
System.out.println("Frequency of Signal : "+frequencyOfSignal);
System.out.println("Sampling Interval : "+samplingInterval);
for (int i = 0; i < sin.length; i++) {
double angle = (2.0 * Math.PI * i) / samplingInterval;
sin[i] = (byte) (Math.sin(angle) * 127);
System.out.println("" + sin[i]);
}
return sin;
}
Generated Sequence Plot

Generated Sine wave plot
CODE For Playing The Generated Sequence:
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.SourceDataLine; public class PlayAnArray {
private static int sampleRate = 8000;
public static void main(String[] args) {
final AudioFormat af = new AudioFormat(sampleRate, 16, 1, true, true);
try {
SourceDataLine line = AudioSystem.getSourceDataLine(af);
line.open(af);
line.start();
//play Frequency = 200 Hz for 1 seconds
play(line, generateSineWavefreq(200,1));
line.drain();
line.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static byte[] generateSineWavefreq(int frequencyOfSignal, int seconds) {
// total samples = (duration in second) * (samples per second)
byte[] sin = new byte[seconds * sampleRate];
double samplingInterval = (double) (sampleRate / frequencyOfSignal);
System.out.println("Sampling Frequency : "+sampleRate);
System.out.println("Frequency of Signal : "+frequencyOfSignal);
System.out.println("Sampling Interval : "+samplingInterval);
for (int i = 0; i < sin.length; i++) {
double angle = (2.0 * Math.PI * i) / samplingInterval;
sin[i] = (byte) (Math.sin(angle) * 127);
//System.out.println("" + sin[i]);
}
return sin;
}
private static void play(SourceDataLine line, byte[] array) {
int length = sampleRate * array.length / 1000;
line.write(array, 0, array.length);
}
}
Java Sound : generate play sine wave - source code的更多相关文章
- Sound (audio file) player in java - working source code example
转载自:http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/sound-audio-file-player-in-java-working.html ...
- Java Sound Capture from Microphone working code
转载自:http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/java-sound-capture-from-microphone.html Soun ...
- [转]Native Java Bytecode Debugging without Source Code
link from:http://www.crowdstrike.com/blog/native-java-bytecode-debugging-without-source-code/index.h ...
- Artistic Style 3.1 A Free, Fast, and Small Automatic Formatter for C, C++, C++/CLI, Objective‑C, C#, and Java Source Code
Artistic Style - Index http://astyle.sourceforge.net/ Artistic Style 3.1 A Free, Fast, and Small Aut ...
- 转: Source Code Lookup in Eclipse(主要讲的是java的)
Source Code Lookup in Eclipse https://www.intertech.com/Blog/source-code-lookup-in-eclipse/
- java sound初探
网上关于java sound的正规资源讲解的非常好,本文不再给出示例,主要提供一些好的资源,并说说我的一些理解,用于形成对java sound的整体认识. 一.几个词汇 TTS:text-to-spe ...
- How to build the Robotics Library from source code on Windows
The Robotics Library is an open source C++ library for robot kinematics, motion planning and control ...
- view class source code with JAD plugin in Eclipse
The default class viewer doesn't decompile the class file so you cannot open and check the source co ...
- Attach source code to a Netbeans Library Wrapper Module
http://rubenlaguna.com/wp/2008/02/22/attach-source-code-to-a-netbeans-library-wrapper-module/ Attach ...
随机推荐
- stm32自带的flash分布图
缘由是要用到flash来保存数据,因此查阅了数据手册与参考手册,一般情况下,将要保存的数据存放到比较靠后的地方,page254,page255,4k字节,已经相当多的了,
- java之比较器
java中的比较器有两种: 1.实体类实现Comparable接口,并实现其中的compareTo方法 2.在外部定义实现Comparator接口的比较器类,并实现其中的compare方法 Compa ...
- 【DP入门到入土】
DP例题较多,可以根据自己需求食用~ update:下翻有状压DP入门讲解,也只有讲解了(逃~ DP的实质,就是状态的枚举. 一般用DP解决的问题,都是求计数或最优问题,所以这类问题,我们也可以用搜索 ...
- 010——MATLAB运行错误跳到下一个循环
(一)MATLAB运行错误跳到下一个循环 :%文件的个数 try %运行的程序放到这里 catch continue%假如上面的没法执行则执行continue,到下个循环 end
- Joomla 3.4.6 远程代码执行 漏洞复现
使用利用工具 https://github.com/YangSirrr/Joomla-3.4.6-RCE http://192.168.43.230/Joomla//configuration.php ...
- Hadoop2.8 安装
一.下载Hadoop与java jdk-8u221-linux-x64.tar.gz Oracle官网下载 hadoop-2.8.5.tar.gz Hadoop官网下载 二.配置服务期间ssh免 ...
- CSS3新增选择器:伪元素选择器
一. ::first-letter 第一个字 二. ::first-line 第一行(以浏览器为准的第一行) 三. ::selection 被选中的字行(鼠标选中的字段)只能向 ::selectio ...
- pt
https://www.hdarea.co/torrents.php http://hdhome.org/torrents.php https://ourbits.club/torrents.php ...
- npm 删除指定的某个包以及再次安装
npm uninstall xxxx --save-dev //删除包及删除配置项 npm install xxx@version //安装指定版本 npm install //覆盖
- linux安装sox,踩过坑的方法
参考文章 : https://blog.csdn.net/e_zhiwen/article/details/80037476 重新在源码中 执行一遍 ./configure --prefix=$HOM ...