转载自: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的更多相关文章

  1. Sound (audio file) player in java - working source code example

    转载自:http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/sound-audio-file-player-in-java-working.html ...

  2. Java Sound Capture from Microphone working code

    转载自:http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/java-sound-capture-from-microphone.html Soun ...

  3. [转]Native Java Bytecode Debugging without Source Code

    link from:http://www.crowdstrike.com/blog/native-java-bytecode-debugging-without-source-code/index.h ...

  4. 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 ...

  5. 转: Source Code Lookup in Eclipse(主要讲的是java的)

    Source Code Lookup in Eclipse https://www.intertech.com/Blog/source-code-lookup-in-eclipse/

  6. java sound初探

    网上关于java sound的正规资源讲解的非常好,本文不再给出示例,主要提供一些好的资源,并说说我的一些理解,用于形成对java sound的整体认识. 一.几个词汇 TTS:text-to-spe ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. C++创建对象的3种方式(转载)

    #include <iostream> using namespace std; class A { private: int n; public: A(int m):n(m) { } ~ ...

  2. 浮点数转成字符串函数gcvt()

    头文件:#include <stdlib.h> 这三个函数都是将数字转化为字符串,简单区别如下: 1.gcvt()所转换后的字符串包含小数点或正负符号 2.ecvt()的转换结果中不包括十 ...

  3. python - django (视图)

    # """ 一:# 视图 (接收请求返回响应的那部分) FBV版:基于 函数的 请求 CBV版:基于 类的 请求 注册方法: url(r'^add_publisher/' ...

  4. Java【基础学习】向下转型和上转型例子

    Java小白应付期末考试QWQ class Animal{ public void move() { System.); } } class Dog extends Animal{ public vo ...

  5. MongoDB 复制集监控

    1.复制集状态查询:rs.status() 2.查看当前副本集oplog状态:rs.printReplicationInfo() 3.查看复制延迟:rs.printSlaveReplicationIn ...

  6. 1.6synchronized代码块

    1.synchronized可以使用任意的Object进行加锁,用法比较灵活 ============================================================= ...

  7. zabbix的历史数据存储到elasticsearch中

    基本配置项 https://www.jianshu.com/p/bffca8128e8f 官方说这个实验性的功能支持es的版本是5.0.x - > 6.1.x,如果使用早期或更高版本的Elast ...

  8. test命令用法。功能:检查文件和比较值

    test命令用法.功能:检查文件和比较值 1)判断表达式 if test  (表达式为真) if test !表达式为假 test 表达式1 –a 表达式2                  两个表达 ...

  9. P4555 【[国家集训队]最长双回文串】

    不知道有没有人跟我一样数据结构学傻了 首先这道题是要求回文串,那么我们可以想到manacher算法 但由于\(manacher\)不能求出双回文子串,我们要考虑一些性质 首先对于一个回文串,删掉两边的 ...

  10. 第12组 Alpha冲刺(1/6)

    Header 队名:To Be Done 组长博客 作业博客 团队项目进行情况 燃尽图(组内共享) 展示Git当日代码/文档签入记录(组内共享) 注: 由于GitHub的免费范围内对多人开发存在较多限 ...