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 ...
随机推荐
- javascript单一复制粘贴
<div id="copy-txt">内容内容</div> <button type="submit" onclick=" ...
- 项目架构&架构部署&网站分析&网站优化
一.架构演变 一个项目至少由三层内容组成:web访问层.数据库层.存储层 初级阶段 单体阶段 常见场景:项目初期 部署特点:所有应用服务都在一台主机 应用特点:开发简单 应用/数据分离阶段 常见场景: ...
- [React] Write a Custom React Effect Hook
Similar to writing a custom State Hook, we’ll write our own Effect Hook called useStarWarsQuote, whi ...
- ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/usr/local/mysql/mysql.sock' (2)
这种情况一般是mysql被杀掉了. 要重新启动. ps -A | grep -i mysql kill 列出来的进程 service mysql start 我的问题就解决了
- [SCOI2016]妖怪
嘟嘟嘟 离NOI最后一周,把自己容易忘的知识点和板子复习一下. (刚答完loj的笔试模拟,感觉上不了90--) update:哦,我89-- 先把式子写出来,每一个妖怪的战斗力\(S(i) = A + ...
- yii2.0简单使用elasticsearch
1.安装扩展 /c/phpStudy/PHPTutorial/php/php-5.5.38/php /c/ProgramData/ComposerSetup/bin/composer.phar req ...
- [golang]写了一个可以用 go 来写脚本的工具:gosl
转自:https://golangtc.com/t/53cca103320b52060a000030 写了一个可以用 go 来写脚本的工具:gosl 代码和使用说明可以看这里: http://gith ...
- YII框架的行为
一.什么是行为 行为,也称为 mixins,可以无须改变类继承关系即可增强一个已有的类的功能. 当一个对象或类被注入某些行为后,这个对象可以像访问自己定义的方法和属性一样访问注入进来的方法和属性. 二 ...
- ICEM-管肋
原视频下载地址:https://yunpan.cn/cMgkmd7u9ZPdC 访问密码 8a73
- ICEM-轴
https://yunpan.cn/cuy98EwwXvYkb 访问密码 0897