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 ...
随机推荐
- git分支相关的命令
Git 分支管理及结合gitlab的使用 说明有关gitlab的说明及基本操作,请参考:https://blog.51cto.com/wutengfei/2090253使用git分支的作用,我们先来说 ...
- MongoDB bindIp 与 bindIpAll
Linux服务器上安装MongoDB后,允许其它客户端IP访问MongoDB服务器.阿里云服务器需要设置入站规则,可参见使用外网访问阿里云服务器ZooKeeper 1.允许所有地址访问 NOTE:ne ...
- Cogs 376. [IOI2002]任务安排(后效性DP)
[IOI2002]任务安排 ★☆ 输入文件:batch.in 输出文件:batch.out 简单对比 时间限制:1 s 内存限制:128 MB N个任务排成一个序列在一台机器上等待完成(顺序不得改变) ...
- AtCoder Grand Contest 038 题解
传送门 这场表现的宛如一个\(zz\) \(A\) 先直接把前\(b\)行全写成\(1\),再把前\(a\)列取反就行 const int N=1005; char mp[N][N];int n,m, ...
- ORA-01033:ORACLE initialization or shutdown
ora-01033 错误处理:DOS下依次执行以下命令sqlplus/nologconnect sys/密码@orcl as sysdbashutdown normalstartup mountSQL ...
- Python正则表达式【转载】
原作者:LouieZhang 原文出处:https://www.cnblogs.com/LouieZhang/p/7399836.html 0x00 简介 正则表达式就是描述字符串排列的一套规则.利用 ...
- html转图片/html2canvas的使用/星座测试/类似于损友圈的活动
https://try.fishqc.com/Activity/constellation ---成品 电脑上录的gif 有借鉴的链接,很多,下面这个还不错~先别看,尊重下我先~~~~ https:/ ...
- mysql集群高可用架构
前言 高可用架构对于互联网服务基本是标配,无论是应用服务还是数据库服务都需要做到高可用.对于一个系统而言,可能包含很多模块,比如前端应用,缓存,数据库,搜索,消息队列等,每个模块都需要做到高可用,才能 ...
- OpenFOAM清理计算结果(只保留原始的0,system,constant)
原视频下载地址:https://yunpan.cn/cMpyLZq8sWQgq(提取码:a08b)
- 依赖倒置原则(DIP)
1. 定义 (1)高层模块不应依赖于低层模块,两者都应该依赖于抽象.(2)抽象不应该依赖于细节,细节应该依赖于抽象. 为什么是“倒置”这个词? 这是由于许多传统的软件开发方法,比如结构化分析和设计,总 ...